// File: 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
movq %rsp, %rbp
subq $0x450, %rsp            # imm = 0x450
movl $0x0, -0x4(%rbp)
movb $0x1, -0x5(%rbp)
leaq -0x5(%rbp), %rax
movq %rax, -0x10(%rbp)
leaq -0x38(%rbp), %rdi
movq %rdi, -0x320(%rbp)
leaq -0x11(%rbp), %rsi
callq 0x4b10
movq -0x320(%rbp), %rax
movq %rax, -0x20(%rbp)
movq 0xc034(%rip), %rdi      # 0xffd8
leaq 0x7148(%rip), %rsi      # 0xb0f3
callq 0x3230
movq %rax, -0x318(%rbp)
jmp 0x3fb9
movq -0x20(%rbp), %rdi
callq 0x58f0
movq -0x318(%rbp), %rdi
movq %rax, %rsi
callq 0x3070
movq %rax, -0x328(%rbp)
jmp 0x3fda
movq -0x328(%rbp), %rdi
leaq 0x70c9(%rip), %rsi      # 0xb0b1
callq 0x3230
jmp 0x3fef
movb $0x1, -0x126(%rbp)
leaq -0x118(%rbp), %rax
movq %rax, -0x338(%rbp)
movq %rax, -0x120(%rbp)
leaq -0x121(%rbp), %rdi
movq %rdi, -0x330(%rbp)
callq 0x33f0
movq -0x338(%rbp), %rdi
movq -0x330(%rbp), %rdx
leaq 0x708a(%rip), %rsi      # 0xb0bd
callq 0x5310
jmp 0x403a
leaq -0xf8(%rbp), %rax
movq %rax, -0x348(%rbp)
movq %rax, -0x120(%rbp)
leaq -0x122(%rbp), %rdi
movq %rdi, -0x340(%rbp)
callq 0x33f0
movq -0x348(%rbp), %rdi
movq -0x340(%rbp), %rdx
leaq 0x704a(%rip), %rsi      # 0xb0c1
callq 0x5310
jmp 0x407e
leaq -0xd8(%rbp), %rax
movq %rax, -0x358(%rbp)
movq %rax, -0x120(%rbp)
leaq -0x123(%rbp), %rdi
movq %rdi, -0x350(%rbp)
callq 0x33f0
movq -0x358(%rbp), %rdi
movq -0x350(%rbp), %rdx
leaq 0x700a(%rip), %rsi      # 0xb0c5
callq 0x5310
jmp 0x40c2
leaq -0xb8(%rbp), %rax
movq %rax, -0x368(%rbp)
movq %rax, -0x120(%rbp)
leaq -0x124(%rbp), %rdi
movq %rdi, -0x360(%rbp)
callq 0x33f0
movq -0x368(%rbp), %rdi
movq -0x360(%rbp), %rdx
leaq 0x6fca(%rip), %rsi      # 0xb0c9
callq 0x5310
jmp 0x4106
leaq -0x98(%rbp), %rax
movq %rax, -0x378(%rbp)
movq %rax, -0x120(%rbp)
leaq -0x125(%rbp), %rdi
movq %rdi, -0x370(%rbp)
callq 0x33f0
movq -0x378(%rbp), %rdi
movq -0x370(%rbp), %rdx
leaq 0x6f8a(%rip), %rsi      # 0xb0cd
callq 0x5310
jmp 0x414a
movb $0x0, -0x126(%rbp)
leaq -0x118(%rbp), %rax
movq %rax, -0x78(%rbp)
movq $0x5, -0x70(%rbp)
leaq -0x127(%rbp), %rdi
movq %rdi, -0x380(%rbp)
callq 0x5910
movq -0x380(%rbp), %rcx
movq -0x78(%rbp), %rsi
movq -0x70(%rbp), %rdx
leaq -0x68(%rbp), %rdi
callq 0x5930
jmp 0x4191
leaq -0x127(%rbp), %rdi
callq 0x59b0
leaq -0x118(%rbp), %rax
movq %rax, -0x390(%rbp)
addq $0xa0, %rax
movq %rax, -0x388(%rbp)
movq -0x388(%rbp), %rdi
addq $-0x20, %rdi
movq %rdi, -0x398(%rbp)
callq 0x3150
movq -0x390(%rbp), %rcx
movq -0x398(%rbp), %rax
cmpq %rcx, %rax
movq %rax, -0x388(%rbp)
jne 0x41b8
leaq -0x125(%rbp), %rdi
callq 0x3290
leaq -0x124(%rbp), %rdi
callq 0x3290
leaq -0x123(%rbp), %rdi
callq 0x3290
leaq -0x122(%rbp), %rdi
callq 0x3290
leaq -0x121(%rbp), %rdi
callq 0x3290
leaq -0x68(%rbp), %rax
movq %rax, -0x50(%rbp)
movq -0x50(%rbp), %rdi
callq 0x59d0
movq %rax, -0x130(%rbp)
movq -0x50(%rbp), %rdi
callq 0x5a00
movq %rax, -0x138(%rbp)
leaq -0x130(%rbp), %rdi
leaq -0x138(%rbp), %rsi
callq 0x5a30
testb $0x1, %al
jne 0x43d8
leaq -0x68(%rbp), %rdi
callq 0x5f20
jmp 0x4ae9
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x4afe
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x436f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x4363
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x4357
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x434b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x433f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0x127(%rbp), %rdi
callq 0x59b0
leaq -0x118(%rbp), %rax
movq %rax, -0x3a8(%rbp)
addq $0xa0, %rax
movq %rax, -0x3a0(%rbp)
movq -0x3a0(%rbp), %rdi
addq $-0x20, %rdi
movq %rdi, -0x3b0(%rbp)
callq 0x3150
movq -0x3a8(%rbp), %rcx
movq -0x3b0(%rbp), %rax
cmpq %rcx, %rax
movq %rax, -0x3a0(%rbp)
jne 0x430c
jmp 0x433f
leaq -0x125(%rbp), %rdi
callq 0x3290
leaq -0x124(%rbp), %rdi
callq 0x3290
leaq -0x123(%rbp), %rdi
callq 0x3290
leaq -0x122(%rbp), %rdi
callq 0x3290
leaq -0x121(%rbp), %rdi
callq 0x3290
testb $0x1, -0x126(%rbp)
jne 0x4386
jmp 0x43d3
movq -0x120(%rbp), %rax
leaq -0x118(%rbp), %rcx
cmpq %rax, %rcx
movq %rax, -0x3b8(%rbp)
je 0x43d1
movq -0x3b8(%rbp), %rdi
addq $-0x20, %rdi
movq %rdi, -0x3c0(%rbp)
callq 0x3150
movq -0x3c0(%rbp), %rax
leaq -0x118(%rbp), %rcx
cmpq %rcx, %rax
movq %rax, -0x3b8(%rbp)
jne 0x43a0
jmp 0x43d3
jmp 0x4afe
leaq -0x130(%rbp), %rdi
callq 0x5a70
movq %rax, -0x140(%rbp)
leaq -0x161(%rbp), %rdi
movq %rdi, -0x3c8(%rbp)
callq 0x33f0
movq -0x3c8(%rbp), %rdx
leaq 0x6cc5(%rip), %rsi      # 0xb0d1
leaq -0x160(%rbp), %rdi
callq 0x5310
jmp 0x441a
leaq -0x161(%rbp), %rdi
callq 0x3290
leaq -0x180(%rbp), %rdi
callq 0x5a90
movq -0x20(%rbp), %rax
movq %rax, -0x188(%rbp)
movq -0x188(%rbp), %rdi
callq 0x5ab0
movq %rax, -0x190(%rbp)
movq -0x188(%rbp), %rdi
callq 0x5ae0
movq %rax, -0x198(%rbp)
leaq -0x190(%rbp), %rdi
leaq -0x198(%rbp), %rsi
callq 0x5b10
testb $0x1, %al
jne 0x447f
jmp 0x4729
leaq -0x190(%rbp), %rdi
callq 0x5b50
movq %rax, -0x1a0(%rbp)
movq -0x140(%rbp), %rdx
leaq 0x6c35(%rip), %rsi      # 0xb0d5
leaq -0x260(%rbp), %rdi
callq 0x5460
jmp 0x44ae
leaq 0x6c31(%rip), %rdx      # 0xb0e6
leaq -0x240(%rbp), %rdi
leaq -0x260(%rbp), %rsi
callq 0x5410
jmp 0x44ca
leaq -0x220(%rbp), %rdi
leaq -0x240(%rbp), %rsi
leaq -0x160(%rbp), %rdx
callq 0x53c0
jmp 0x44e6
leaq 0x6c01(%rip), %rdx      # 0xb0ee
leaq -0x200(%rbp), %rdi
leaq -0x220(%rbp), %rsi
callq 0x5410
jmp 0x4502
movq -0x1a0(%rbp), %rdx
leaq -0x1e0(%rbp), %rdi
leaq -0x200(%rbp), %rsi
callq 0x53c0
jmp 0x451e
leaq -0x1c0(%rbp), %rdi
leaq -0x10(%rbp), %rsi
leaq -0x1e0(%rbp), %rdx
callq 0x4c50
jmp 0x4537
leaq -0x1e0(%rbp), %rdi
callq 0x3150
leaq -0x200(%rbp), %rdi
callq 0x3150
leaq -0x220(%rbp), %rdi
callq 0x3150
leaq -0x240(%rbp), %rdi
callq 0x3150
leaq -0x260(%rbp), %rdi
callq 0x3150
leaq -0x288(%rbp), %rdi
leaq -0x160(%rbp), %rsi
leaq -0x1c0(%rbp), %rdx
callq 0x3d80
leaq -0x288(%rbp), %rdi
callq 0x30e0
movq %rax, %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x31b0
movsd %xmm0, -0x3d0(%rbp)
leaq -0x288(%rbp), %rdi
callq 0x3150
movsd -0x3d0(%rbp), %xmm0
movsd %xmm0, -0x268(%rbp)
movsd -0x268(%rbp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jbe 0x46fb
movq -0x1a0(%rbp), %rdx
leaq -0x2b0(%rbp), %rdi
leaq -0x268(%rbp), %rsi
callq 0x5ba0
jmp 0x45fa
leaq -0x180(%rbp), %rdi
leaq -0x2b0(%rbp), %rsi
callq 0x5b70
jmp 0x460f
leaq -0x2b0(%rbp), %rdi
callq 0x5be0
jmp 0x46fb
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0x161(%rbp), %rdi
callq 0x3290
jmp 0x4ade
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x4ac6
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x46c2
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x46b6
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x46aa
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x469e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0x1e0(%rbp), %rdi
callq 0x3150
leaq -0x200(%rbp), %rdi
callq 0x3150
leaq -0x220(%rbp), %rdi
callq 0x3150
leaq -0x240(%rbp), %rdi
callq 0x3150
leaq -0x260(%rbp), %rdi
callq 0x3150
jmp 0x4ac6
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x4718
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0x2b0(%rbp), %rdi
callq 0x5be0
jmp 0x4718
leaq -0x1c0(%rbp), %rdi
callq 0x3150
leaq -0x190(%rbp), %rdi
callq 0x5c00
jmp 0x4463
leaq -0x1c0(%rbp), %rdi
callq 0x3150
jmp 0x4ac6
leaq -0x180(%rbp), %rdi
movq %rdi, -0x3d8(%rbp)
callq 0x5c60
movq -0x3d8(%rbp), %rdi
movq %rax, -0x2b8(%rbp)
callq 0x5c90
movq %rax, -0x2c0(%rbp)
movq -0x2b8(%rbp), %rdi
movq -0x2c0(%rbp), %rsi
callq 0x5c20
jmp 0x476b
leaq -0x180(%rbp), %rdi
movq %rdi, -0x3e0(%rbp)
callq 0x5c60
movq -0x3e0(%rbp), %rdi
movq %rax, -0x2c8(%rbp)
callq 0x5c90
movq %rax, -0x2d0(%rbp)
movq -0x2c8(%rbp), %rdi
movq -0x2d0(%rbp), %rsi
callq 0x5cc0
jmp 0x47ad
leaq -0x180(%rbp), %rdi
callq 0x5d00
testb $0x1, %al
jne 0x47bf
jmp 0x47cf
xorps %xmm0, %xmm0
movsd %xmm0, -0x3e8(%rbp)
jmp 0x4863
leaq -0x180(%rbp), %rdi
movq %rdi, -0x3f8(%rbp)
callq 0x5d40
movq -0x3f8(%rbp), %rdi
movq %rax, -0x2e0(%rbp)
callq 0x5d70
movq %rax, -0x2e8(%rbp)
movq -0x2e0(%rbp), %rdi
movq -0x2e8(%rbp), %rsi
xorps %xmm0, %xmm0
callq 0x4d10
movsd %xmm0, -0x3f0(%rbp)
jmp 0x481c
leaq -0x180(%rbp), %rdi
callq 0x5da0
movsd -0x3f0(%rbp), %xmm0
movq %rax, %xmm1
movaps 0x67d4(%rip), %xmm2     # 0xb010
punpckldq %xmm2, %xmm1    # xmm1 = xmm1[0],xmm2[0],xmm1[1],xmm2[1]
movapd 0x67d8(%rip), %xmm2     # 0xb020
subpd %xmm2, %xmm1
movaps %xmm1, %xmm2
unpckhpd %xmm1, %xmm1            # xmm1 = xmm1[1,1]
addsd %xmm2, %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, -0x3e8(%rbp)
movsd -0x3e8(%rbp), %xmm0
movsd %xmm0, -0x2d8(%rbp)
leaq -0x180(%rbp), %rdi
callq 0x5d00
testb $0x1, %al
jne 0x4a9d
movq 0xb74a(%rip), %rdi      # 0xffd8
leaq 0x153b(%rip), %rsi      # 0x5dd0
callq 0x30a0
movq %rax, -0x400(%rbp)
jmp 0x48a3
xorl %edi, %edi
callq 0x5e00
movl %eax, -0x404(%rbp)
jmp 0x48b2
movq -0x400(%rbp), %rdi
movl -0x404(%rbp), %eax
movl %eax, -0x2f0(%rbp)
movl -0x2f0(%rbp), %esi
callq 0x3380
jmp 0x48d2
movq 0xb6ff(%rip), %rdi      # 0xffd8
leaq 0x6812(%rip), %rsi      # 0xb0f2
callq 0x3230
movq %rax, -0x410(%rbp)
jmp 0x48ee
movq -0x410(%rbp), %rdi
movq -0x140(%rbp), %rsi
callq 0x3210
movq %rax, -0x418(%rbp)
jmp 0x490a
movq -0x418(%rbp), %rdi
leaq 0x67de(%rip), %rsi      # 0xb0f6
callq 0x3230
movq %rax, -0x420(%rbp)
jmp 0x4926
movq -0x420(%rbp), %rdi
leaq -0x160(%rbp), %rsi
callq 0x3210
movq %rax, -0x428(%rbp)
jmp 0x4942
movq -0x428(%rbp), %rdi
leaq 0x67a4(%rip), %rsi      # 0xb0f4
callq 0x3230
movq %rax, -0x430(%rbp)
jmp 0x495e
movq -0x430(%rbp), %rdi
movsd -0x2d8(%rbp), %xmm0
callq 0x3420
movq %rax, -0x438(%rbp)
jmp 0x497b
movq -0x438(%rbp), %rdi
movl $0xa, %esi
callq 0x32e0
jmp 0x498e
movq 0xb643(%rip), %rdi      # 0xffd8
leaq 0x675c(%rip), %rsi      # 0xb0f8
callq 0x3230
jmp 0x49a3
leaq -0x180(%rbp), %rax
movq %rax, -0x2f8(%rbp)
movq -0x2f8(%rbp), %rdi
callq 0x5c60
movq %rax, -0x300(%rbp)
movq -0x2f8(%rbp), %rdi
callq 0x5c90
movq %rax, -0x308(%rbp)
leaq -0x300(%rbp), %rdi
leaq -0x308(%rbp), %rsi
callq 0x5e20
testb $0x1, %al
jne 0x49f3
jmp 0x4a86
leaq -0x300(%rbp), %rdi
callq 0x5e60
movq %rax, -0x310(%rbp)
movq -0x310(%rbp), %rax
movsd (%rax), %xmm0
movq 0xb5c0(%rip), %rdi      # 0xffd8
callq 0x3420
movq %rax, -0x440(%rbp)
jmp 0x4a26
movq -0x440(%rbp), %rdi
movl $0x9, %esi
callq 0x32e0
movq %rax, -0x448(%rbp)
jmp 0x4a40
movq -0x448(%rbp), %rdi
movq -0x310(%rbp), %rsi
addq $0x8, %rsi
callq 0x3210
movq %rax, -0x450(%rbp)
jmp 0x4a60
movq -0x450(%rbp), %rdi
movl $0xa, %esi
callq 0x32e0
jmp 0x4a73
jmp 0x4a75
leaq -0x300(%rbp), %rdi
callq 0x5e80
jmp 0x49d7
movq 0xb54b(%rip), %rdi      # 0xffd8
leaq 0x6664(%rip), %rsi      # 0xb0f8
callq 0x3230
jmp 0x4a9b
jmp 0x4a9d
leaq -0x180(%rbp), %rdi
callq 0x5ea0
leaq -0x160(%rbp), %rdi
callq 0x3150
leaq -0x130(%rbp), %rdi
callq 0x5f00
jmp 0x424d
leaq -0x180(%rbp), %rdi
callq 0x5ea0
leaq -0x160(%rbp), %rdi
callq 0x3150
leaq -0x68(%rbp), %rdi
callq 0x5f20
jmp 0x4afe
leaq -0x38(%rbp), %rdi
callq 0x5f20
movl -0x4(%rbp), %eax
addq $0x450, %rsp            # imm = 0x450
popq %rbp
retq
leaq -0x38(%rbp), %rdi
callq 0x5f20
movq -0x40(%rbp), %rdi
callq 0x33e0