// File: /workspace/github_cpp_makefile_star2_O2/deanturpin[P]curly/curly.cpp
// Function: main
int main() {
  // Construct a CryptoCompare URL: prepend the domain and append the
  // application ID
  const auto application_id = true;
  const auto cc = [&application_id](const std::string &url) {
    return curly("https://min-api.cryptocompare.com/" + url +
                 (application_id
                      ? "&extraParams=https://github.com/deanturpin/curly"
                      : ""));
  };

  // Read all exchange names from a text file
  const auto get_exchanges = []() {
    std::ifstream in("exchanges.txt");
    std::vector<std::string> e;
    std::string name;
    while (in >> name)
      e.push_back(name);

    return e;
  };

  // Report exchange count
  const std::vector<std::string> &stage1_exchanges = get_exchanges();
  std::cout << "# " << stage1_exchanges.size() << " exchanges\n";

  // Fetch prices for each currency
  for (const auto &from_symbol :
       std::vector<std::string>({"BTC", "ETH", "BCH", "LTC", "ETC"})) {
    const std::string to_symbol = "USD";

    // Fetch prices for each exchange
    std::vector<std::pair<double, std::string>> stage1_prices;
    for (const std::string &name : stage1_exchanges) {
      const std::string response = cc("data/price?fsym=" + from_symbol +
                                      "&tsyms=" + to_symbol + "&e=" + name);

      const double price =
          std::strtod(find_token(to_symbol, response).c_str(), NULL);

      // Only store non-zero prices
      if (price > 0.0)
        stage1_prices.push_back({price, name});
    }

    // Sort the prices before calculating the combinations
    std::sort(stage1_prices.begin(), stage1_prices.end());
    std::reverse(stage1_prices.begin(), stage1_prices.end());

    // Calculate average over all exchanges
    const auto average =
        (stage1_prices.empty()
             ? 0.0
             : std::accumulate(
                   stage1_prices.cbegin(), stage1_prices.cend(), 0.0,
                   [](double sum, const auto &p) { return sum += p.first; }) /
                   stage1_prices.size());

    // Print the combined prices
    if (!stage1_prices.empty()) {
      std::cout << std::fixed << std::setprecision(0);
      std::cout << "## " << from_symbol << "-" << to_symbol << " " << average
                << '\n';
      std::cout << "```\n";
      for (const auto &p : stage1_prices)
        std::cout << p.first << '\t' << p.second << '\n';
      std::cout << "```\n";
    }
  }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x388, %rsp            # imm = 0x388
leaq 0x2750(%rip), %rsi      # 0x5105
leaq 0x180(%rsp), %rbx
pushq $0x8
popq %rdx
movq %rbx, %rdi
callq 0x22e0
leaq 0x30(%rsp), %r14
andq $0x0, 0x10(%r14)
leaq 0x68(%rsp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
xorpd %xmm0, %xmm0
movapd %xmm0, (%r14)
movb $0x0, (%rax)
leaq 0x58(%rsp), %r15
movq %rbx, %rdi
movq %r15, %rsi
callq 0x2200
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
jne 0x2a17
movq %r14, %rdi
movq %r15, %rsi
callq 0x33e6
jmp 0x29f1
leaq 0x58(%rsp), %rdi
callq 0x20b0
leaq 0x180(%rsp), %rdi
callq 0x2060
movq 0x55a3(%rip), %rdi      # 0x7fd8
leaq 0x26bf(%rip), %rsi      # 0x50fb
callq 0x2180
movq 0x38(%rsp), %rsi
subq 0x30(%rsp), %rsi
sarq $0x5, %rsi
movq %rax, %rdi
callq 0x20e0
leaq 0x265b(%rip), %rsi      # 0x50b9
movq %rax, %rdi
callq 0x2180
leaq 0x2658(%rip), %rsi      # 0x50c5
leaq 0x180(%rsp), %r14
leaq 0x58(%rsp), %rdx
movq %r14, %rdi
callq 0x30f6
leaq 0x1a0(%rsp), %r15
leaq 0x2638(%rip), %rsi      # 0x50c9
leaq 0x78(%rsp), %rdx
movq %r15, %rdi
callq 0x30f6
leaq 0x1c0(%rsp), %r15
leaq 0x2620(%rip), %rsi      # 0x50cd
leaq 0x120(%rsp), %rdx
movq %r15, %rdi
callq 0x30f6
leaq 0x1e0(%rsp), %r15
leaq 0x2605(%rip), %rsi      # 0x50d1
leaq 0x100(%rsp), %rdx
movq %r15, %rdi
callq 0x30f6
leaq 0x200(%rsp), %r15
leaq 0x25ea(%rip), %rsi      # 0x50d5
leaq 0xe0(%rsp), %rdx
movq %r15, %rdi
callq 0x30f6
leaq 0x98(%rsp), %rdi
leaq 0x180(%rsp), %rsi
pushq $0x5
popq %rdx
leaq 0xc0(%rsp), %rcx
callq 0x32d0
movl $0x80, %ebx
leaq (%rsp,%rbx), %rdi
addq $0x180, %rdi            # imm = 0x180
callq 0x20b0
addq $-0x20, %rbx
cmpq $-0x20, %rbx
jne 0x2b20
movq 0x98(%rsp), %r14
movq 0xa0(%rsp), %rax
movq %rax, 0x50(%rsp)
leaq 0x180(%rsp), %rbp
leaq 0x160(%rsp), %r15
cmpq 0x50(%rsp), %r14
je 0x2ee1
movq %r15, %r12
movq %r14, 0x20(%rsp)
leaq 0x78(%rsp), %rdi
leaq 0x255b(%rip), %rsi      # 0x50d9
movq %rbp, %rdx
callq 0x30f6
andq $0x0, 0x10(%rsp)
xorpd %xmm0, %xmm0
movapd %xmm0, (%rsp)
movq 0x30(%rsp), %r14
movq 0x38(%rsp), %r13
cmpq %r13, %r14
je 0x2d35
movq %r12, %r15
movq %r12, %rdi
leaq 0x2528(%rip), %rsi      # 0x50dd
movq 0x20(%rsp), %rdx
callq 0x316f
leaq 0x140(%rsp), %rbx
movq %rbx, %rdi
movq %r15, %rsi
leaq 0x251a(%rip), %rdx      # 0x50ee
callq 0x3150
leaq 0xc0(%rsp), %r15
movq %r15, %rdi
movq %rbx, %rsi
leaq 0x78(%rsp), %rdx
callq 0x3131
leaq 0xe0(%rsp), %rbx
movq %rbx, %rdi
movq %r15, %rsi
leaq 0x24f0(%rip), %rdx      # 0x50f6
callq 0x3150
leaq 0x100(%rsp), %r15
movq %r15, %rdi
movq %rbx, %rsi
movq %r14, %rdx
callq 0x3131
leaq 0x58(%rsp), %rbx
movq %rbx, %rdi
leaq 0x24fd(%rip), %rsi      # 0x512d
movq %r15, %rdx
callq 0x316f
movq %rbp, %rdi
movq %rbx, %rsi
leaq 0x250b(%rip), %rdx      # 0x5150
callq 0x3150
leaq 0x120(%rsp), %rbx
movq %rbx, %rdi
movq %rbp, %rsi
pushq $0x1
popq %rdx
callq 0x244c
movq %rbp, %rdi
callq 0x20b0
leaq 0x58(%rsp), %rdi
callq 0x20b0
leaq 0x100(%rsp), %rdi
callq 0x20b0
leaq 0xe0(%rsp), %rdi
callq 0x20b0
leaq 0xc0(%rsp), %rdi
callq 0x20b0
leaq 0x140(%rsp), %rdi
callq 0x20b0
movq %r12, %rdi
callq 0x20b0
movq %rbp, %rdi
leaq 0x78(%rsp), %rsi
movq %rbx, %rdx
callq 0x28a7
movq 0x180(%rsp), %rdi
xorl %esi, %esi
callq 0x2120
movsd %xmm0, 0x28(%rsp)
movq %rbp, %rdi
callq 0x20b0
movsd 0x28(%rsp), %xmm0
ucomisd 0x2357(%rip), %xmm0     # 0x5040
jbe 0x2d24
movsd %xmm0, 0x180(%rsp)
leaq 0x188(%rsp), %rdi
movq %r14, %rsi
callq 0x2220
movq %rsp, %rdi
movq %rbp, %rsi
callq 0x39ca
leaq 0x188(%rsp), %rdi
callq 0x20b0
leaq 0x120(%rsp), %rbx
movq %rbx, %rdi
callq 0x20b0
addq $0x20, %r14
jmp 0x2b9f
movq (%rsp), %rdi
movq 0x8(%rsp), %rsi
callq 0x3bc9
movq (%rsp), %rdi
movq 0x8(%rsp), %rsi
callq 0x4346
movq (%rsp), %rcx
movq 0x8(%rsp), %rax
cmpq %rax, %rcx
movq 0x20(%rsp), %r14
movq %r12, %r15
je 0x2ec6
xorpd %xmm0, %xmm0
movq %rcx, %rdx
cmpq %rax, %rdx
je 0x2d81
addsd (%rdx), %xmm0
addq $0x28, %rdx
jmp 0x2d72
movsd %xmm0, 0x28(%rsp)
subq %rcx, %rax
cqto
pushq $0x28
popq %rcx
idivq %rcx
movq %rax, %xmm0
punpckldq 0x2271(%rip), %xmm0 # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
subpd 0x2279(%rip), %xmm0     # 0x5020
movapd %xmm0, 0xb0(%rsp)
movq 0x5221(%rip), %rdi      # 0x7fd8
movq (%rdi), %rax
movq -0x18(%rax), %rcx
movl 0x18(%rdi,%rcx), %edx
movl $0xfffffefb, %esi       # imm = 0xFFFFFEFB
andl %esi, %edx
orl $0x4, %edx
movl %edx, 0x18(%rdi,%rcx)
movq -0x18(%rax), %rax
andq $0x0, 0x8(%rdi,%rax)
leaq 0x2319(%rip), %rsi      # 0x50fa
callq 0x2180
movq %rax, %rdi
movq %r14, %rsi
callq 0x2170
movq %rax, %rdi
leaq 0x2303(%rip), %rsi      # 0x50fe
callq 0x2180
movq %rax, %rdi
leaq 0x78(%rsp), %rsi
callq 0x2170
movq %rax, %rdi
leaq 0x22e5(%rip), %rsi      # 0x50fc
callq 0x2180
movapd 0xb0(%rsp), %xmm0
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
movsd 0x28(%rsp), %xmm0
divsd %xmm1, %xmm0
movq %rax, %rdi
callq 0x2290
movq %rax, %rdi
pushq $0xa
popq %rsi
callq 0x21e0
movq 0x5183(%rip), %rdi      # 0x7fd8
leaq 0x22a4(%rip), %rsi      # 0x5100
callq 0x2180
movq (%rsp), %r14
movq 0x8(%rsp), %r13
cmpq %r13, %r14
je 0x2eab
movsd (%r14), %xmm0
movq 0x515d(%rip), %rdi      # 0x7fd8
callq 0x2290
movq %rax, %rdi
pushq $0x9
popq %rsi
callq 0x21e0
addq $0x8, %r14
movq %rax, %rdi
movq %r14, %rsi
callq 0x2170
movq %rax, %rdi
pushq $0xa
popq %rsi
callq 0x21e0
addq $0x20, %r14
jmp 0x2e6a
movq 0x5126(%rip), %rdi      # 0x7fd8
leaq 0x2247(%rip), %rsi      # 0x5100
callq 0x2180
movq 0x20(%rsp), %r14
movq %r12, %r15
movq %rsp, %rdi
callq 0x330a
leaq 0x78(%rsp), %rdi
callq 0x20b0
addq $0x20, %r14
jmp 0x2b5f
leaq 0x98(%rsp), %rdi
callq 0x332c
leaq 0x30(%rsp), %rdi
callq 0x332c
xorl %eax, %eax
addq $0x388, %rsp            # imm = 0x388
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
movl $0x80, %r14d
leaq (%rsp,%r14), %rdi
addq $0x180, %rdi            # imm = 0x180
callq 0x20b0
addq $-0x20, %r14
cmpq $-0x20, %r14
jne 0x2f15
jmp 0x3056
jmp 0x2f3a
jmp 0x2f3a
jmp 0x2f3a
movq %rax, %rbx
addq $-0x20, %r15
movq %r15, %rdi
callq 0x20b0
cmpq %r14, %r15
jne 0x2f3d
jmp 0x3056
jmp 0x2f55
movq %rax, %rbx
jmp 0x3056
jmp 0x3034
movq %rax, %rbx
jmp 0x3049
movq %rax, %rbx
leaq 0x58(%rsp), %rdi
callq 0x20b0
leaq 0x30(%rsp), %rdi
callq 0x332c
leaq 0x180(%rsp), %rdi
callq 0x2060
jmp 0x3060
jmp 0x3034
movq %rax, %rbx
jmp 0x2fad
movq %rax, %rbx
leaq 0x188(%rsp), %rdi
callq 0x20b0
leaq 0x120(%rsp), %rdi
jmp 0x302d
movq %rax, %rbx
jmp 0x2fdb
movq %rax, %rbx
jmp 0x2fd1
movq %rax, %rbx
leaq 0x180(%rsp), %rdi
callq 0x20b0
leaq 0x58(%rsp), %rdi
callq 0x20b0
leaq 0x100(%rsp), %rdi
callq 0x20b0
jmp 0x2ffe
jmp 0x3034
movq %rax, %rbx
jmp 0x3025
movq %rax, %rbx
jmp 0x3018
movq %rax, %rbx
jmp 0x300b
movq %rax, %rbx
leaq 0xe0(%rsp), %rdi
callq 0x20b0
leaq 0xc0(%rsp), %rdi
callq 0x20b0
leaq 0x140(%rsp), %rdi
callq 0x20b0
leaq 0x160(%rsp), %rdi
callq 0x20b0
jmp 0x3037
movq %rax, %rbx
movq %rsp, %rdi
callq 0x330a
leaq 0x78(%rsp), %rdi
callq 0x20b0
leaq 0x98(%rsp), %rdi
callq 0x332c
leaq 0x30(%rsp), %rdi
callq 0x332c
movq %rbx, %rdi
callq 0x22c0