// File: /workspace/github_cpp_makefile_star2_O1/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 $0x378, %rsp            # imm = 0x378
leaq 0x32ec(%rip), %rsi      # 0x6105
leaq 0x170(%rsp), %rbx
movq %rbx, %rdi
movl $0x8, %edx
callq 0x22b0
xorl %eax, %eax
leaq 0xf0(%rsp), %r14
movq %rax, 0x10(%r14)
xorpd %xmm0, %xmm0
movapd %xmm0, (%r14)
leaq 0x40(%rsp), %rcx
movq %rcx, -0x10(%rcx)
movq %rax, -0x8(%rcx)
movb $0x0, (%rcx)
leaq 0x30(%rsp), %r15
movq %rbx, %rdi
movq %r15, %rsi
callq 0x21d0
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
jne 0x2e80
movq %r14, %rdi
movq %r15, %rsi
callq 0x3cec
jmp 0x2e5a
movq 0x30(%rsp), %rdi
leaq 0x40(%rsp), %rax
cmpq %rax, %rdi
je 0x2e9c
movq 0x40(%rsp), %rsi
incq %rsi
callq 0x2180
leaq 0x170(%rsp), %rdi
callq 0x2060
movq 0x6128(%rip), %rdi      # 0x8fd8
leaq 0x3244(%rip), %rsi      # 0x60fb
movl $0x2, %edx
callq 0x21a0
movq 0xf8(%rsp), %rsi
subq 0xf0(%rsp), %rsi
sarq $0x5, %rsi
movq 0x60fc(%rip), %rdi      # 0x8fd8
callq 0x20e0
leaq 0x31d1(%rip), %rsi      # 0x60b9
movl $0xb, %edx
movq %rax, %rdi
callq 0x21a0
leaq 0x180(%rsp), %rbx
movq %rbx, -0x10(%rbx)
leaq 0x31bd(%rip), %rsi      # 0x60c5
leaq 0x31b9(%rip), %rdx      # 0x60c8
leaq 0x170(%rsp), %r15
movq %r15, %rdi
callq 0x403c
leaq 0x190(%rsp), %r14
leaq 0x1a0(%rsp), %rax
movq %rax, -0x10(%rax)
leaq 0x318f(%rip), %rsi      # 0x60c9
leaq 0x318b(%rip), %rdx      # 0x60cc
movq %r14, %rdi
callq 0x403c
leaq 0x1b0(%rsp), %r14
leaq 0x1c0(%rsp), %rax
movq %rax, -0x10(%rax)
leaq 0x3169(%rip), %rsi      # 0x60cd
leaq 0x3165(%rip), %rdx      # 0x60d0
movq %r14, %rdi
callq 0x403c
leaq 0x1d0(%rsp), %r14
leaq 0x1e0(%rsp), %rax
movq %rax, -0x10(%rax)
leaq 0x3143(%rip), %rsi      # 0x60d1
leaq 0x313f(%rip), %rdx      # 0x60d4
movq %r14, %rdi
callq 0x403c
leaq 0x1f0(%rsp), %r14
leaq 0x200(%rsp), %r12
movq %r12, -0x10(%r12)
leaq 0x311c(%rip), %rsi      # 0x60d5
leaq 0x3118(%rip), %rdx      # 0x60d8
movq %r14, %rdi
callq 0x403c
leaq 0x158(%rsp), %rdi
leaq 0x170(%rsp), %rsi
leaq 0x30(%rsp), %rcx
movl $0x5, %edx
callq 0x3b80
movq $-0xa0, %r14
movq -0x10(%r12), %rdi
cmpq %rdi, %r12
je 0x3004
movq (%r12), %rsi
incq %rsi
callq 0x2180
addq $-0x20, %r12
addq $0x20, %r14
jne 0x2fee
movq 0x158(%rsp), %r14
movq 0x160(%rsp), %rax
movq %rax, 0x110(%rsp)
cmpq %rax, %r14
je 0x3708
leaq 0x170(%rsp), %r13
leaq 0xc0(%rsp), %rax
movq %rax, 0xb0(%rsp)
leaq 0xb0(%rsp), %rdi
leaq 0x3083(%rip), %rsi      # 0x60d9
leaq 0x307f(%rip), %rdx      # 0x60dc
callq 0x403c
xorpd %xmm0, %xmm0
movapd %xmm0, 0x10(%rsp)
movq $0x0, 0x20(%rsp)
movq 0xf0(%rsp), %r12
movq 0xf8(%rsp), %rbp
cmpq %rbp, %r12
je 0x349d
leaq 0x138(%rsp), %r15
movq %r15, %rdi
leaq 0x303d(%rip), %rsi      # 0x60dd
movq %r14, %rdx
callq 0x3a11
movq %r15, %rdi
leaq 0x303c(%rip), %rsi      # 0x60ee
callq 0x22d0
leaq 0x60(%rsp), %rsi
movq %rsi, 0x50(%rsp)
movq (%rax), %rdx
movq %rax, %rcx
addq $0x10, %rcx
cmpq %rcx, %rdx
je 0x30df
movq %rdx, 0x50(%rsp)
movq (%rcx), %rdx
movq %rdx, 0x60(%rsp)
jmp 0x30e7
movupd (%rcx), %xmm0
movupd %xmm0, (%rsi)
movq 0x8(%rax), %rdx
movq %rdx, 0x58(%rsp)
movq %rcx, (%rax)
movq $0x0, 0x8(%rax)
movb $0x0, 0x10(%rax)
movq 0xb0(%rsp), %rsi
movq 0xb8(%rsp), %rdx
leaq 0x50(%rsp), %rdi
callq 0x2080
leaq 0x80(%rsp), %rsi
movq %rsi, 0x70(%rsp)
movq (%rax), %rdx
movq %rax, %rcx
addq $0x10, %rcx
cmpq %rcx, %rdx
je 0x3147
movq %rdx, 0x70(%rsp)
movq (%rcx), %rdx
movq %rdx, 0x80(%rsp)
jmp 0x314f
movupd (%rcx), %xmm0
movupd %xmm0, (%rsi)
movq 0x8(%rax), %rdx
movq %rdx, 0x78(%rsp)
movq %rcx, (%rax)
movq $0x0, 0x8(%rax)
movb $0x0, 0x10(%rax)
leaq 0x70(%rsp), %rdi
leaq 0x2f83(%rip), %rsi      # 0x60f6
callq 0x22d0
leaq 0xa0(%rsp), %rsi
movq %rsi, 0x90(%rsp)
movq (%rax), %rdx
movq %rax, %rcx
addq $0x10, %rcx
cmpq %rcx, %rdx
je 0x31ac
movq %rdx, 0x90(%rsp)
movq (%rcx), %rdx
movq %rdx, 0xa0(%rsp)
jmp 0x31b4
movupd (%rcx), %xmm0
movupd %xmm0, (%rsi)
movq 0x8(%rax), %rdx
movq %rdx, 0x98(%rsp)
movq %rcx, (%rax)
movq $0x0, 0x8(%rax)
movb $0x0, 0x10(%rax)
movq (%r12), %rsi
movq 0x8(%r12), %rdx
leaq 0x90(%rsp), %rdi
callq 0x2080
leaq 0xe0(%rsp), %rsi
movq %rsi, 0xd0(%rsp)
movq (%rax), %rdx
movq %rax, %rcx
addq $0x10, %rcx
cmpq %rcx, %rdx
je 0x3219
movq %rdx, 0xd0(%rsp)
movq (%rcx), %rdx
movq %rdx, 0xe0(%rsp)
jmp 0x3221
movupd (%rcx), %xmm0
movupd %xmm0, (%rsi)
leaq 0x30(%rsp), %r15
movq 0x8(%rax), %rdx
movq %rdx, 0xd8(%rsp)
movq %rcx, (%rax)
movq $0x0, 0x8(%rax)
movb $0x0, 0x10(%rax)
movq %r15, %rdi
leaq 0x2ee2(%rip), %rsi      # 0x612d
leaq 0xd0(%rsp), %rdx
callq 0x3a11
movq %r15, %rdi
leaq 0x2eee(%rip), %rsi      # 0x6150
callq 0x22d0
movq %rbx, 0x170(%rsp)
movq (%rax), %rdx
movq %rax, %rcx
addq $0x10, %rcx
cmpq %rcx, %rdx
je 0x3293
movq %rdx, 0x170(%rsp)
movq (%rcx), %rdx
movq %rdx, 0x180(%rsp)
jmp 0x329b
movupd (%rcx), %xmm0
movupd %xmm0, (%rbx)
movq 0x8(%rax), %rdx
movq %rdx, 0x178(%rsp)
movq %rcx, (%rax)
movq $0x0, 0x8(%rax)
movb $0x0, 0x10(%rax)
leaq 0x118(%rsp), %rdi
movq %r13, %rsi
movl $0x1, %edx
callq 0x24fc
movq 0x170(%rsp), %rdi
cmpq %rbx, %rdi
je 0x32e8
movq 0x180(%rsp), %rsi
incq %rsi
callq 0x2180
movq 0x30(%rsp), %rdi
leaq 0x40(%rsp), %rax
cmpq %rax, %rdi
je 0x3304
movq 0x40(%rsp), %rsi
incq %rsi
callq 0x2180
movq 0xd0(%rsp), %rdi
leaq 0xe0(%rsp), %rax
cmpq %rax, %rdi
je 0x3329
movq 0xe0(%rsp), %rsi
incq %rsi
callq 0x2180
movq 0x90(%rsp), %rdi
leaq 0xa0(%rsp), %rax
cmpq %rax, %rdi
je 0x334e
movq 0xa0(%rsp), %rsi
incq %rsi
callq 0x2180
movq 0x70(%rsp), %rdi
leaq 0x80(%rsp), %rax
cmpq %rax, %rdi
je 0x3370
movq 0x80(%rsp), %rsi
incq %rsi
callq 0x2180
movq 0x50(%rsp), %rdi
leaq 0x60(%rsp), %rax
cmpq %rax, %rdi
je 0x338c
movq 0x60(%rsp), %rsi
incq %rsi
callq 0x2180
movq 0x138(%rsp), %rdi
leaq 0x148(%rsp), %rax
cmpq %rax, %rdi
je 0x33b1
movq 0x148(%rsp), %rsi
incq %rsi
callq 0x2180
movq %r13, %rdi
leaq 0xb0(%rsp), %rsi
leaq 0x118(%rsp), %rdx
callq 0x2c68
movq 0x170(%rsp), %rdi
xorl %esi, %esi
callq 0x2120
movq 0x170(%rsp), %rdi
cmpq %rbx, %rdi
je 0x3401
movq 0x180(%rsp), %rsi
incq %rsi
movsd %xmm0, 0x8(%rsp)
callq 0x2180
movsd 0x8(%rsp), %xmm0
ucomisd 0x2c37(%rip), %xmm0     # 0x6040
jbe 0x346f
movsd %xmm0, 0x170(%rsp)
leaq 0x188(%rsp), %rax
movq %rax, 0x178(%rsp)
movq (%r12), %rsi
movq 0x8(%r12), %rdx
addq %rsi, %rdx
leaq 0x178(%rsp), %rdi
callq 0x3fac
leaq 0x10(%rsp), %rdi
movq %r13, %rsi
callq 0x4340
movq 0x178(%rsp), %rdi
leaq 0x188(%rsp), %rax
cmpq %rax, %rdi
je 0x346f
movq 0x188(%rsp), %rsi
incq %rsi
callq 0x2180
movq 0x118(%rsp), %rdi
leaq 0x128(%rsp), %rax
cmpq %rax, %rdi
je 0x3494
movq 0x128(%rsp), %rsi
incq %rsi
callq 0x2180
addq $0x20, %r12
jmp 0x3085
movq 0x10(%rsp), %rbp
movq 0x18(%rsp), %r12
cmpq %r12, %rbp
je 0x34e7
movq %r12, %rax
subq %rbp, %rax
sarq $0x3, %rax
movabsq $-0x3333333333333333, %rcx # imm = 0xCCCCCCCCCCCCCCCD
imulq %rcx, %rax
bsrq %rax, %rdx
xorl $0x3f, %edx
addl %edx, %edx
xorq $0x7e, %rdx
movq %rbp, %rdi
movq %r12, %rsi
callq 0x4615
movq %rbp, %rdi
movq %r12, %rsi
callq 0x46f2
movq 0x10(%rsp), %rdi
movq 0x18(%rsp), %rsi
callq 0x50f3
movq 0x10(%rsp), %rax
movq 0x18(%rsp), %rcx
cmpq %rcx, %rax
je 0x355e
xorpd %xmm2, %xmm2
movq %rax, %rdx
addsd (%rdx), %xmm2
addq $0x28, %rdx
cmpq %rcx, %rdx
jne 0x350c
movq %rcx, %rdx
subq %rax, %rdx
sarq $0x3, %rdx
movabsq $-0x3333333333333333, %rsi # imm = 0xCCCCCCCCCCCCCCCD
imulq %rsi, %rdx
movq %rdx, %xmm0
punpckldq 0x2ad2(%rip), %xmm0 # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
subpd 0x2ada(%rip), %xmm0     # 0x6020
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
divsd %xmm1, %xmm2
movsd %xmm2, 0x8(%rsp)
jmp 0x3568
xorpd %xmm0, %xmm0
movsd %xmm0, 0x8(%rsp)
cmpq %rcx, %rax
je 0x36c7
movq 0x5a60(%rip), %rdi      # 0x8fd8
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
movq $0x0, 0x8(%rdi,%rax)
movl $0x3, %edx
leaq 0x2b50(%rip), %rsi      # 0x60fa
callq 0x21a0
movq (%r14), %rsi
movq 0x8(%r14), %rdx
movq 0x5a1b(%rip), %rdi      # 0x8fd8
callq 0x21a0
movq %rax, %r12
movl $0x1, %edx
movq %rax, %rdi
leaq 0x2b2a(%rip), %rsi      # 0x60fe
callq 0x21a0
movq 0xb0(%rsp), %rsi
movq 0xb8(%rsp), %rdx
movq %r12, %rdi
callq 0x21a0
movq %rax, %r12
movl $0x1, %edx
movq %rax, %rdi
leaq 0x2af9(%rip), %rsi      # 0x60fc
callq 0x21a0
movq %r12, %rdi
movsd 0x8(%rsp), %xmm0
callq 0x2250
movb $0xa, 0x170(%rsp)
movl $0x1, %edx
movq %rax, %rdi
movq %r13, %rsi
callq 0x21a0
movl $0x4, %edx
movq 0x599e(%rip), %rdi      # 0x8fd8
leaq 0x2abf(%rip), %rsi      # 0x6100
callq 0x21a0
movq 0x10(%rsp), %r12
movq 0x18(%rsp), %rbp
cmpq %rbp, %r12
je 0x36af
movsd (%r12), %xmm0
movq 0x5976(%rip), %rdi      # 0x8fd8
callq 0x2250
movb $0x9, 0x170(%rsp)
movl $0x1, %edx
movq %rax, %rdi
movq %r13, %rsi
callq 0x21a0
movq 0x8(%r12), %rsi
movq 0x10(%r12), %rdx
movq %rax, %rdi
callq 0x21a0
movb $0xa, 0x170(%rsp)
movl $0x1, %edx
movq %rax, %rdi
movq %r13, %rsi
callq 0x21a0
addq $0x28, %r12
jmp 0x3650
movl $0x4, %edx
movq 0x591d(%rip), %rdi      # 0x8fd8
leaq 0x2a3e(%rip), %rsi      # 0x6100
callq 0x21a0
leaq 0x10(%rsp), %rdi
callq 0x3bc8
movq 0xb0(%rsp), %rdi
leaq 0xc0(%rsp), %rax
cmpq %rax, %rdi
je 0x36f6
movq 0xc0(%rsp), %rsi
incq %rsi
callq 0x2180
addq $0x20, %r14
cmpq 0x110(%rsp), %r14
jne 0x3037
leaq 0x158(%rsp), %rdi
callq 0x3c24
leaq 0xf0(%rsp), %rdi
callq 0x3c24
xorl %eax, %eax
addq $0x378, %rsp            # imm = 0x378
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %r12
leaq 0x210(%rsp), %rdi
leaq 0x170(%rsp), %rsi
callq 0x232a
movb $0x1, %al
jmp 0x3767
jmp 0x3758
jmp 0x3758
jmp 0x3758
movq %rax, %r12
xorl %eax, %eax
jmp 0x3767
movq %rax, %r12
xorl %eax, %eax
movq %r15, %r14
cmpq %r14, %r15
sete %cl
orb %al, %cl
jne 0x39b2
addq $-0x10, %r14
leaq 0x170(%rsp), %rbx
movq -0x10(%r14), %rdi
cmpq %rdi, %r14
je 0x3795
movq (%r14), %rsi
incq %rsi
callq 0x2180
leaq -0x20(%r14), %rax
addq $-0x10, %r14
cmpq %rbx, %r14
movq %rax, %r14
jne 0x3781
jmp 0x39b2
movq %rax, %r12
jmp 0x39b2
movq %rax, %r12
jmp 0x39a5
jmp 0x3973
movq %rax, %r12
movq 0x30(%rsp), %rdi
leaq 0x40(%rsp), %rax
cmpq %rax, %rdi
je 0x37de
movq 0x40(%rsp), %rsi
incq %rsi
callq 0x2180
leaq 0xf0(%rsp), %rdi
callq 0x3c24
leaq 0x170(%rsp), %rdi
callq 0x2060
jmp 0x39bf
jmp 0x3973
movq %rax, %r12
movq 0x178(%rsp), %rdi
leaq 0x188(%rsp), %rax
cmpq %rax, %rdi
je 0x382f
movq 0x188(%rsp), %rsi
incq %rsi
callq 0x2180
jmp 0x382f
movq %rax, %r12
movq 0x118(%rsp), %rdi
leaq 0x128(%rsp), %rax
cmpq %rax, %rdi
je 0x3976
movq 0x128(%rsp), %rsi
jmp 0x3969
jmp 0x3973
movq %rax, %r12
jmp 0x38b5
movq %rax, %r12
jmp 0x394c
movq %rax, %r12
jmp 0x3899
movq %rax, %r12
leaq 0x60(%rsp), %rbx
jmp 0x3935
movq %rax, %r12
movq 0x170(%rsp), %rdi
cmpq %rbx, %rdi
je 0x3899
movq 0x180(%rsp), %rsi
incq %rsi
callq 0x2180
movq 0x30(%rsp), %rdi
leaq 0x40(%rsp), %rax
cmpq %rax, %rdi
je 0x38b5
movq 0x40(%rsp), %rsi
incq %rsi
callq 0x2180
movq 0xd0(%rsp), %rdi
leaq 0xe0(%rsp), %rax
cmpq %rax, %rdi
je 0x38e9
movq 0xe0(%rsp), %rsi
incq %rsi
callq 0x2180
jmp 0x38e9
movq %rax, %r12
leaq 0x60(%rsp), %rbx
jmp 0x3913
movq %rax, %r12
leaq 0x60(%rsp), %rbx
leaq 0xa0(%rsp), %rax
movq 0x90(%rsp), %rdi
cmpq %rax, %rdi
je 0x3913
movq 0xa0(%rsp), %rsi
incq %rsi
callq 0x2180
leaq 0x80(%rsp), %rax
movq 0x70(%rsp), %rdi
cmpq %rax, %rdi
je 0x3935
movq 0x80(%rsp), %rsi
incq %rsi
callq 0x2180
movq 0x50(%rsp), %rdi
cmpq %rbx, %rdi
je 0x394c
movq 0x60(%rsp), %rsi
incq %rsi
callq 0x2180
movq 0x138(%rsp), %rdi
leaq 0x148(%rsp), %rax
cmpq %rax, %rdi
je 0x3976
movq 0x148(%rsp), %rsi
incq %rsi
callq 0x2180
jmp 0x3976
movq %rax, %r12
leaq 0x10(%rsp), %rdi
callq 0x3bc8
movq 0xb0(%rsp), %rdi
leaq 0xc0(%rsp), %rax
cmpq %rax, %rdi
je 0x39a5
movq 0xc0(%rsp), %rsi
incq %rsi
callq 0x2180
leaq 0x158(%rsp), %rdi
callq 0x3c24
leaq 0xf0(%rsp), %rdi
callq 0x3c24
movq %r12, %rdi
callq 0x2290
nop