      function eqlbrm_f_g_h(b, astar, bigb)
      external bigb
      eqlbrm_f_g_h = bigb(astar, b)
      end

      function eqlbrm_f_g(a, astar, biga, bigb, bstar, n)
      external biga
      bstar = argmax_2(eqlbrm_f_g_h, astar, bigb, bstar, n)
      eqlbrm_f_g = biga(a, bstar)
      end

      function eqlbrm_f(astar, biga, bigb, bstar, n)
      eqlbrm_f = argmax_1(astar, biga, bigb, bstar, astar, n) - astar
      end

C     astar & bstar: guesses in, optimized values out
      subroutine eqlbrm(biga, bigb, astar, bstar, n)
      astar = root_2(biga, bigb, bstar, astar, n)
      end

      function root(f, x0, n)
      x = x0
      do 1669 i = 1, n
      call deriv2(f, x, y, yprime)
 1669 x = x - y / yprime
      root = x
      end

      function root_1(f1, x0, n)
      x = x0
      do 1669 i = 1, n
      call deriv2_1(f1, x, y, yprime)
 1669 x = x - y / yprime
      root_1 = x
      end

      function root_1_1(astar, biga, bigb, bstar, x0, n)
      x = x0
      do 1669 i = 1, n
      call deriv2_1_1(astar, biga, bigb, bstar, n, x, y, yprime)
 1669 x = x - y / yprime
      root_1_1 = x
      end

      function root_1_2(f1, astar, bigb, x0, n)
      x = x0
      do 1669 i = 1, n
      call deriv2_1_2(f1, astar, bigb, x, y, yprime)
 1669 x = x - y / yprime
      root_1_2 = x
      end

      function root_2(biga, bigb, bstar, x0, n)
      x = x0
      do 1669 i = 1, n
      call deriv2_2(biga, bigb, bstar, n, x, y, yprime)
 1669 x = x - y / yprime
      root_2 = x
      end

      subroutine deriv2(f, x, y, yprime)
      external f
      adf(x)
      y = f(x)
      end adf(yprime = tangent(y))
      end

      subroutine deriv2_1(f1, x, y, yprime)
      adf(x)
      y = argmax_fprime(x, f1)
      end adf(yprime = tangent(y))
      end

      subroutine deriv2_1_1(astar, biga, bigb, bstar, n, x, y, yprime)
      adf(x)
      y = argmax_fprime_1(x, astar, biga, bigb, bstar, n)
      end adf(yprime = tangent(y))
      end

      subroutine deriv2_1_2(f1, astar, bigb, x, y, yprime)
      adf(x)
      y = argmax_fprime_2(x, f1, astar, bigb)
      end adf(yprime = tangent(y))
      end

      subroutine deriv2_2(biga, bigb, bstar, n, x, y, yprime)
      adf(x)
      y = eqlbrm_f(x, biga, bigb, bstar, n)
      end adf(yprime = tangent(y))
      end

      function argmax_fprime(x, f)
      argmax_fprime = deriv1(f, x)
      end

      function argmax_fprime_1(x, astar, biga, bigb, bstar, n)
      argmax_fprime_1 = deriv1_1(astar, biga, bigb, bstar, n, x)
      end

      function argmax_fprime_2(x, f, astar, bigb)
      argmax_fprime_2 = deriv1_2(f, astar, bigb, x)
      end

      function argmax(f, x0, n)
      argmax = root_1(f, x0, n)
      end

      function argmax_1(astar, biga, bigb, bstar, x0, n)
      argmax_1 = root_1_1(astar, biga, bigb, bstar, x0, n)
      end

      function argmax_2(f, astar, bigb, x0, n)
      argmax_2 = root_1_2(f, astar, bigb, x0, n)
      end

      function deriv1(f, x)
      external f
      adf(x)
      y = f(x)
      end adf(deriv1 = tangent(y))
      end

      function deriv1_1(astar, biga, bigb, bstar, n, x)
      adf(x)
      y = eqlbrm_f_g(x, astar, biga, bigb, bstar, n)
      end adf(deriv1_1 = tangent(y))
      end

      function deriv1_2(f, astar, bigb, x)
      external f
      adf(x)
      y = f(x, astar, bigb)
      end adf(deriv1_2 = tangent(y))
      end

      function gmbiga(a, b)
      price = 20 - 0.1*a - 0.1*b
      costs = a*(10 - 0.05*a)
      gmbiga = a*price - costs
      end

      function gmbigb(a, b)
      price = 20 - 0.1*b - 0.0999*a
      costs = b*(10.005 - 0.05*b)
      gmbigb = b*price - costs
      end

      program main
      read *, astar
      read *, bstar
      read *, n
      call eqlbrm(gmbiga, gmbigb, astar, bstar, n)
      print *, astar, bstar
      end
