// File: /workspace/github_cpp_makefile_star2_O3/kztomita[P]http-dump/main.cc
// Function: main
int main(int argc, char *argv[]) {
  namespace asio = boost::asio;

  bool use_http2 = false;
  http_header h;
  http_header_list headers;

  if (argc < 2) {
    usage();
  }

  int opt_argc = argc;
  std::string url;
  if (argv[argc - 1][0] != '-') {
    url = argv[argc - 1];
    opt_argc--;
  }

  static struct option long_options[] = {
    {"http2",     no_argument,       0,  1 },
    {"keylog",    required_argument, 0,  2 },
    {0,           0,                 0,  0 },
  };
  while (true) {
    int opt = getopt_long(opt_argc, argv, "hH:kv",
                          long_options, NULL);
    if (opt == -1) {
      break;
    }
    switch (opt) {
    case 1:     // --http2
      use_http2 = true;
      break;
    case 2:     // --keylog
      g_keylog_file = optarg;
      break;
    case 'H':
      {
        auto h = create_http_header_from_header_line_or_null(optarg);
        if (h) {
          headers.emplace_back(std::move(h));
        }
      }
      break;
    case 'k':
      g_verify_cert = false;
      break;
    case 'v':
      g_verbose = true;
      break;
    case 'h':
    case '?':
      usage();
    }
  }

  try {
    url_components c = parse_url(url);

    uint16_t port = 0;
    if (c.scheme == "http") {
      port = 80;
    } else if (c.scheme == "https") {
      port = 443;
    } else {
      std::cerr << "Unsupported scheme." << std::endl;
      return 1;
    }
    if (c.port) {
      port = c.port.value();
    }

    boost::system::error_code error;
    asio::io_context context;
    asio::ip::tcp::resolver resolver(context);
    auto endpoints = resolver.resolve(asio::ip::tcp::v4(), c.host, "", error);
    if (error) {
      std::cerr << error.message() << std::endl;
      return 1;
    }
    boost::asio::ip::address remote_address(endpoints.begin()->endpoint().address());

    http_scheme scheme;
    if (c.scheme == "http") {
      scheme = http_scheme::http;
    } else if (c.scheme == "https") {
      scheme = http_scheme::https;
    } else {
      throw std::invalid_argument("unsupported scheme.");
    }

    std::unique_ptr<http_protocol> protocol;
    if (use_http2 && scheme == http_scheme::https) {
      protocol.reset(new httpv2);
    } else {
      protocol.reset(new httpv1);
    }
    http_response r = protocol->get(remote_address, port, scheme, c.host, c.path_and_query, headers);
    debug_dump(r);
    std::cout << r.stringify_payload() << std::endl;
  } catch (const std::exception& e) {
    std::cerr << e.what() << std::endl;
    return 1;
  }

  return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1f8, %rsp            # imm = 0x1F8
leaq 0x1c8(%rsp), %rcx
movq %rcx, -0x10(%rcx)
xorl %eax, %eax
movq %rax, -0x8(%rcx)
movb %al, (%rcx)
leaq 0x1e8(%rsp), %rcx
movq %rcx, -0x10(%rcx)
movq %rax, -0x8(%rcx)
movb %al, (%rcx)
leaq 0x120(%rsp), %rcx
movq %rcx, 0x8(%rcx)
movq %rcx, (%rcx)
movq %rax, 0x10(%rcx)
cmpl $0x1, %edi
jle 0xd5cc
movq %rsi, %r14
movl %edi, %ebp
leaq 0x88(%rsp), %rax
movq %rax, -0x10(%rax)
movq $0x0, -0x8(%rax)
movb $0x0, (%rax)
movl %edi, %eax
movq -0x8(%rsi,%rax,8), %rbx
cmpb $0x2d, (%rbx)
je 0xcf1d
movq %rbx, %rdi
callq 0xb2c0
leaq 0x78(%rsp), %rdi
xorl %esi, %esi
xorl %edx, %edx
movq %rbx, %rcx
movq %rax, %r8
callq 0xba50
decl %ebp
movq $0x0, 0x28(%rsp)
leaq 0x24229(%rip), %r15     # 0x31156
leaq 0x3a7ac(%rip), %r12     # 0x476e0
movq 0x39fd5(%rip), %rbx     # 0x46f10
leaq 0xb8(%rsp), %r13
movl %ebp, %edi
movq %r14, %rsi
movq %r15, %rdx
movq %r12, %rcx
xorl %r8d, %r8d
callq 0xbb00
cmpl $0x67, %eax
jg 0xcf75
cmpl $0x3e, %eax
jg 0xcf90
cmpl $0x2, %eax
jne 0xd061
movq (%rbx), %rax
movq %rax, 0x3ac4d(%rip)     # 0x47bc0
jmp 0xcf43
cmpl $0x6b, %eax
je 0xd04a
cmpl $0x76, %eax
jne 0xd056
movb $0x1, 0x3ac3a(%rip)     # 0x47bc8
jmp 0xcf43
cmpl $0x48, %eax
jne 0xd07b
movq (%rbx), %rsi
movq %r13, %rdi
leaq 0x40(%rsp), %rdx
callq 0xd9d2
leaq 0x170(%rsp), %rdi
movq %r13, %rsi
callq 0x20dfb
movq 0xb8(%rsp), %rdi
leaq 0xc8(%rsp), %rax
cmpq %rax, %rdi
je 0xcfde
movq 0xc8(%rsp), %rsi
incq %rsi
callq 0xb600
cmpq $0x0, 0x170(%rsp)
je 0xcf43
movl $0x18, %edi
callq 0xb5e0
movq 0x170(%rsp), %rcx
movq %rcx, 0x10(%rax)
movq $0x0, 0x170(%rsp)
movq %rax, %rdi
leaq 0x120(%rsp), %rsi
callq 0xb0b0
incq 0x130(%rsp)
movq 0x170(%rsp), %rsi
testq %rsi, %rsi
je 0xcf43
leaq 0x170(%rsp), %rdi
callq 0x103c6
jmp 0xcf43
movb $0x0, 0x3a6f7(%rip)     # 0x47748
jmp 0xcf43
cmpl $0x68, %eax
jne 0xcf43
jmp 0xd084
cmpl $-0x1, %eax
je 0xd08b
cmpl $0x1, %eax
jne 0xcf43
movb $0x1, %al
movq %rax, 0x28(%rsp)
jmp 0xcf43
cmpl $0x3f, %eax
jne 0xcf43
xorl %edi, %edi
callq 0xcd3c
leaq 0xb8(%rsp), %rdi
leaq 0x78(%rsp), %rsi
callq 0x1350c
leaq 0x240b8(%rip), %rsi     # 0x3115c
leaq 0xb8(%rsp), %rdi
callq 0xb190
testl %eax, %eax
je 0xd11d
leaq 0x240a5(%rip), %rsi     # 0x31161
leaq 0xb8(%rsp), %rdi
callq 0xb190
testl %eax, %eax
je 0xd123
movq 0x39f0c(%rip), %rbx     # 0x46fe0
leaq 0x2408c(%rip), %rsi     # 0x31167
movl $0x13, %edx
movq %rbx, %rdi
callq 0xb730
movq (%rbx), %rax
addq -0x18(%rax), %rbx
movq %rbx, %rdi
movl $0xa, %esi
callq 0xb5b0
movsbl %al, %esi
movq 0x39eda(%rip), %rdi     # 0x46fe0
callq 0xb050
movl $0x1, %ebx
movq %rax, %rdi
callq 0xb450
jmp 0xd4d0
movw $0x50, %bp
jmp 0xd127
movw $0x1bb, %bp             # imm = 0x1BB
cmpb $0x0, 0xfc(%rsp)
je 0xd139
movzwl 0xf8(%rsp), %ebp
movl $0x0, 0x18(%rsp)
movb $0x0, 0x1c(%rsp)
leaq 0x381c3(%rip), %rax     # 0x45310
movq %rax, 0x20(%rsp)
leaq 0x30(%rsp), %rdi
callq 0xda10
leaq 0x170(%rsp), %rdi
leaq 0x30(%rsp), %rsi
xorl %edx, %edx
callq 0x1040c
leaq 0x98(%rsp), %rdx
movl $0x2, (%rdx)
movq 0xd8(%rsp), %r8
movq 0xe0(%rsp), %rcx
movq $0x0, 0x40(%rsp)
leaq 0x26112(%rip), %rax     # 0x332b0
movq %rax, 0x48(%rsp)
movups 0x40(%rsp), %xmm0
movups %xmm0, (%rsp)
leaq 0x18(%rsp), %rax
movq %rax, 0x10(%rsp)
leaq 0x158(%rsp), %rdi
leaq 0x170(%rsp), %rsi
xorl %r9d, %r9d
callq 0x126e0
cmpb $0x1, 0x1c(%rsp)
jne 0xd24e
movq 0x20(%rsp), %rsi
movl 0x18(%rsp), %edx
movq (%rsi), %rax
leaq 0x40(%rsp), %rdi
callq *0x20(%rax)
movq 0x40(%rsp), %rsi
movq 0x48(%rsp), %rdx
movq 0x39de6(%rip), %rdi     # 0x46fe0
callq 0xb730
movq %rax, %rbx
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0xb5b0
movsbl %al, %esi
movq %rbx, %rdi
callq 0xb050
movq %rax, %rdi
callq 0xb450
leaq 0x50(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0xd244
movq 0x50(%rsp), %rsi
incq %rsi
callq 0xb600
movl $0x1, %ebx
jmp 0xd449
movq 0x158(%rsp), %rax
movq 0x160(%rsp), %rdi
testq %rdi, %rdi
je 0xd278
movq 0x39d0e(%rip), %rcx     # 0x46f78
cmpb $0x0, (%rcx)
je 0xd274
incl 0x8(%rdi)
jmp 0xd278
lock
incl 0x8(%rdi)
movq (%rax), %rax
cmpw $0x2, (%rax)
jne 0xd29f
movl 0x4(%rax), %eax
xorps %xmm0, %xmm0
movups %xmm0, 0xa0(%rsp)
movq $0x0, 0xb0(%rsp)
xorl %ecx, %ecx
jmp 0xd2bd
movl 0x18(%rax), %ecx
movups 0x8(%rax), %xmm0
movups %xmm0, 0xa0(%rsp)
movq %rcx, 0xb0(%rsp)
xorl %eax, %eax
movl $0x1, %ecx
movl %ecx, 0x98(%rsp)
movl %eax, 0x9c(%rsp)
testq %rdi, %rdi
je 0xd2d5
callq 0x1014c
leaq 0x23e80(%rip), %rsi     # 0x3115c
leaq 0xb8(%rsp), %rdi
callq 0xb190
testl %eax, %eax
je 0xd337
leaq 0x23e6d(%rip), %rsi     # 0x31161
leaq 0xb8(%rsp), %rdi
callq 0xb190
testl %eax, %eax
jne 0xd5d3
movl $0x1, %ebx
testb $0x1, 0x28(%rsp)
je 0xd339
movl $0x8, %edi
callq 0xb5e0
movq %rax, %r14
leaq 0x3900f(%rip), %rax     # 0x46338
addq $0x10, %rax
movq %rax, (%r14)
movl $0x1, %ebx
jmp 0xd354
xorl %ebx, %ebx
movl $0x8, %edi
callq 0xb5e0
movq %rax, %r14
leaq 0x38d93(%rip), %rax     # 0x460e0
addq $0x10, %rax
movq %rax, (%r14)
leaq 0xd8(%rsp), %r9
leaq 0x100(%rsp), %rcx
leaq 0x120(%rsp), %rdx
movq %rdx, 0x8(%rsp)
movq %rcx, (%rsp)
movzwl %bp, %ecx
leaq 0x40(%rsp), %rdi
leaq 0x98(%rsp), %rdx
movq %r14, %rsi
movl %ebx, %r8d
callq *(%rax)
cmpb $0x1, 0x3a834(%rip)     # 0x47bc8
jne 0xd3a0
leaq 0x40(%rsp), %rdi
callq 0x214fc
leaq 0x138(%rsp), %rdi
leaq 0x40(%rsp), %rsi
callq 0x214dc
movq 0x138(%rsp), %rsi
movq 0x140(%rsp), %rdx
movq 0x39bb7(%rip), %rdi     # 0x46f80
callq 0xb730
movq %rax, %rbx
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0xb5b0
movsbl %al, %esi
movq %rbx, %rdi
callq 0xb050
movq %rax, %rdi
callq 0xb450
leaq 0x148(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0xd419
movq 0x148(%rsp), %rsi
incq %rsi
callq 0xb600
movq 0x60(%rsp), %rdi
testq %rdi, %rdi
je 0xd430
movq 0x70(%rsp), %rsi
subq %rdi, %rsi
callq 0xb600
leaq 0x48(%rsp), %rdi
callq 0x101bc
movl $0x8, %esi
movq %r14, %rdi
callq 0xb600
xorl %ebx, %ebx
movq 0x160(%rsp), %rdi
testq %rdi, %rdi
je 0xd45b
callq 0x1014c
leaq 0x170(%rsp), %rdi
callq 0x126a4
movq 0x30(%rsp), %r14
movq 0x38(%r14), %r15
testq %r15, %r15
je 0xd4ba
movq (%r15), %rax
movq %r15, %rdi
callq *0x10(%rax)
movq 0x20(%r15), %r15
testq %r15, %r15
jne 0xd476
movq 0x30(%rsp), %r14
movq 0x38(%r14), %rdi
testq %rdi, %rdi
je 0xd4ba
movq (%rdi), %rax
movq 0x20(%rdi), %r15
callq *0x8(%rax)
movq %r15, 0x38(%r14)
movq %r15, %rdi
testq %r15, %r15
jne 0xd496
movq 0x30(%rsp), %r14
testq %r14, %r14
je 0xd5c4
leaq 0x8(%r14), %rdi
callq 0xb790
movl $0x40, %esi
movq %r14, %rdi
callq 0xb600
leaq 0x110(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0xd4f1
movq 0x110(%rsp), %rsi
incq %rsi
callq 0xb600
leaq 0xe8(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0xd512
movq 0xe8(%rsp), %rsi
incq %rsi
callq 0xb600
movq 0xb8(%rsp), %rdi
leaq 0xc8(%rsp), %rax
cmpq %rax, %rdi
je 0xd537
movq 0xc8(%rsp), %rsi
incq %rsi
callq 0xb600
movq 0x78(%rsp), %rdi
leaq 0x88(%rsp), %rax
cmpq %rax, %rdi
je 0xd559
movq 0x88(%rsp), %rsi
incq %rsi
callq 0xb600
leaq 0x120(%rsp), %rdi
callq 0x10212
movq 0x1d8(%rsp), %rdi
leaq 0x1e8(%rsp), %rax
cmpq %rax, %rdi
je 0xd58b
movq 0x1e8(%rsp), %rsi
incq %rsi
callq 0xb600
movq 0x1b8(%rsp), %rdi
leaq 0x1c8(%rsp), %rax
cmpq %rax, %rdi
je 0xd5b0
movq 0x1c8(%rsp), %rsi
incq %rsi
callq 0xb600
movl %ebx, %eax
addq $0x1f8, %rsp            # imm = 0x1F8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %r14d, %r14d
jmp 0xd4c3
xorl %edi, %edi
callq 0xcd3c
movl $0x10, %edi
callq 0xb300
movq %rax, %r14
leaq 0x23b94(%rip), %rsi     # 0x3117b
movq %rax, %rdi
callq 0xbcd0
movq 0x399c2(%rip), %rsi     # 0x46fb8
movq 0x39993(%rip), %rdx     # 0x46f90
movq %r14, %rdi
callq 0xbb60
jmp 0xd62e
movq %rdx, %r15
movq %rax, %rbx
movq %r14, %rdi
callq 0xb4b0
jmp 0xd6c8
jmp 0xd61c
movq %rdx, %r15
movq %rax, %rbx
jmp 0xd693
movq %rdx, %r15
movq %rax, %rbx
jmp 0xd69d
jmp 0xd62e
movq %rdx, %r15
movq %rax, %rbx
jmp 0xd6c8
jmp 0xd7a8
movq %rdx, %r15
movq %rax, %rbx
jmp 0xd6da
movq %rdx, %r15
movq %rax, %rbx
jmp 0xd6e7
jmp 0xd6f3
movq %rdx, %r15
movq %rax, %rbx
jmp 0xd706
movq %rax, %rbx
jmp 0xd7cd
movq %rdx, %r15
movq %rax, %rbx
leaq 0x148(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0xd693
movq 0x148(%rsp), %rsi
incq %rsi
callq 0xb600
leaq 0x40(%rsp), %rdi
callq 0xdbb6
movl $0x8, %esi
movq %r14, %rdi
jmp 0xd6c3
movq %rdx, %r15
movq %rax, %rbx
leaq 0x50(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0xd6c8
movq 0x50(%rsp), %rsi
incq %rsi
callq 0xb600
movq 0x160(%rsp), %rdi
testq %rdi, %rdi
je 0xd6da
callq 0x1014c
leaq 0x170(%rsp), %rdi
callq 0x126a4
leaq 0x30(%rsp), %rdi
callq 0xdbdc
jmp 0xd6f9
movq %rdx, %r15
movq %rax, %rbx
leaq 0xb8(%rsp), %rdi
callq 0xdc54
cmpl $0x1, %r15d
jne 0xd7ab
movq %rbx, %rdi
callq 0xb290
movq (%rax), %rcx
movq %rax, %rdi
callq *0x10(%rcx)
movq 0x398b8(%rip), %rdi     # 0x46fe0
movq %rax, %rsi
callq 0xb5d0
movq %rax, %rdi
callq 0xb2f0
movl $0x1, %ebx
callq 0xbaf0
jmp 0xd537
jmp 0xd7a8
movq %rax, %rbx
callq 0xbaf0
jmp 0xd7ab
jmp 0xd755
movq %rax, %rdi
callq 0xd8d9
movq %rax, %rbx
movq 0x170(%rsp), %rsi
testq %rsi, %rsi
je 0xd7ab
leaq 0x170(%rsp), %rdi
callq 0x103c6
jmp 0xd7ab
jmp 0xd7a8
movq %rax, %rbx
movq 0xb8(%rsp), %rdi
leaq 0xc8(%rsp), %rax
cmpq %rax, %rdi
je 0xd7ab
movq 0xc8(%rsp), %rsi
incq %rsi
callq 0xb600
jmp 0xd7ab
movq %rax, %rbx
movq 0x78(%rsp), %rdi
leaq 0x88(%rsp), %rax
cmpq %rax, %rdi
je 0xd7cd
movq 0x88(%rsp), %rsi
incq %rsi
callq 0xb600
leaq 0x120(%rsp), %rdi
callq 0x10212
leaq 0x1b8(%rsp), %rdi
callq 0xdca2
movq %rbx, %rdi
callq 0xbbb0
nop