// File: /workspace/github_cpp_makefile_star2_O1/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 0xd624
movq %rsi, %r14
movl %edi, %ebp
leaq 0xa8(%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 0xcf40
movq %rbx, %rdi
callq 0xb2c0
leaq 0x98(%rsp), %rdi
xorl %esi, %esi
xorl %edx, %edx
movq %rbx, %rcx
movq %rax, %r8
callq 0xba50
decl %ebp
movl $0x0, 0x1c(%rsp)
leaq 0x25207(%rip), %r15     # 0x32156
leaq 0x3b78a(%rip), %r12     # 0x486e0
leaq 0xb8(%rsp), %r13
xorl %ebx, %ebx
movl %ebp, %edi
movq %r14, %rsi
movq %r15, %rdx
movq %r12, %rcx
xorl %r8d, %r8d
callq 0xbb00
cmpl $0x47, %eax
jg 0xcf9c
cmpl $0x1, %eax
jg 0xcfbf
cmpl $-0x1, %eax
je 0xd09d
cmpl $0x1, %eax
jne 0xd09b
movb $0x1, %bl
movb $0x1, %al
movl %eax, 0x1c(%rsp)
jmp 0xd09d
cmpl $0x6a, %eax
jle 0xcfde
cmpl $0x6b, %eax
je 0xd094
cmpl $0x76, %eax
jne 0xd09b
movb $0x1, 0x3bc0e(%rip)     # 0x48bc8
jmp 0xd09b
cmpl $0x2, %eax
jne 0xd0a7
movq 0x3af49(%rip), %rax     # 0x47f18
movq (%rax), %rax
movq %rax, 0x3bbe7(%rip)     # 0x48bc0
jmp 0xd09b
cmpl $0x48, %eax
jne 0xd0ae
movq 0x3af2a(%rip), %rax     # 0x47f18
movq (%rax), %rsi
movq %r13, %rdi
leaq 0x40(%rsp), %rdx
callq 0xda18
leaq 0x170(%rsp), %rdi
movq %r13, %rsi
callq 0x20f2f
movq 0xb8(%rsp), %rdi
leaq 0xc8(%rsp), %rax
cmpq %rax, %rdi
je 0xd033
movq 0xc8(%rsp), %rsi
incq %rsi
callq 0xb600
cmpq $0x0, 0x170(%rsp)
je 0xd078
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 0xd09b
leaq 0x170(%rsp), %rdi
callq 0x10490
jmp 0xd09b
movb $0x0, 0x3b6ad(%rip)     # 0x48748
movb $0x1, %bl
testb %bl, %bl
jne 0xcf5e
jmp 0xd0ba
cmpl $0x3f, %eax
jne 0xd09b
jmp 0xd0b3
cmpl $0x68, %eax
jne 0xd09b
xorl %edi, %edi
callq 0xcd5c
leaq 0xb8(%rsp), %rdi
leaq 0x98(%rsp), %rsi
callq 0x13650
leaq 0x25086(%rip), %rsi     # 0x3215c
leaq 0xb8(%rsp), %rdi
callq 0xb190
testl %eax, %eax
je 0xd14f
leaq 0x25073(%rip), %rsi     # 0x32161
leaq 0xb8(%rsp), %rdi
callq 0xb190
testl %eax, %eax
je 0xd156
movq 0x3aee2(%rip), %rbx     # 0x47fe8
leaq 0x2505a(%rip), %rsi     # 0x32167
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 0x3aeb0(%rip), %rdi     # 0x47fe8
callq 0xb050
movl $0x1, %ebx
movq %rax, %rdi
callq 0xb450
jmp 0xd52d
movw $0x50, %r15w
jmp 0xd15b
movw $0x1bb, %r15w           # imm = 0x1BB
cmpb $0x0, 0xfc(%rsp)
je 0xd16e
movzwl 0xf8(%rsp), %r15d
movl $0x0, 0x20(%rsp)
movb $0x0, 0x24(%rsp)
leaq 0x390ee(%rip), %rax     # 0x46270
movq %rax, 0x28(%rsp)
leaq 0x30(%rsp), %rdi
callq 0xda56
leaq 0x170(%rsp), %rdi
leaq 0x30(%rsp), %rsi
xorl %edx, %edx
callq 0x104d8
leaq 0x78(%rsp), %rdx
movl $0x2, (%rdx)
movq 0xd8(%rsp), %r8
movq 0xe0(%rsp), %rcx
movq $0x0, 0x40(%rsp)
leaq 0x270e0(%rip), %rax     # 0x342b0
movq %rax, 0x48(%rsp)
movups 0x40(%rsp), %xmm0
movups %xmm0, (%rsp)
leaq 0x20(%rsp), %rax
movq %rax, 0x10(%rsp)
leaq 0x158(%rsp), %rdi
leaq 0x170(%rsp), %rsi
xorl %r9d, %r9d
callq 0x12860
cmpb $0x1, 0x24(%rsp)
jne 0xd280
movq 0x28(%rsp), %rsi
movl 0x20(%rsp), %edx
movq (%rsi), %rax
leaq 0x40(%rsp), %rdi
callq *0x20(%rax)
movq 0x40(%rsp), %rsi
movq 0x48(%rsp), %rdx
movq 0x3adbc(%rip), %rdi     # 0x47fe8
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 0xd276
movq 0x50(%rsp), %rsi
incq %rsi
callq 0xb600
movl $0x1, %ebx
jmp 0xd4ad
movq 0x158(%rsp), %rax
movq 0x160(%rsp), %rdi
testq %rdi, %rdi
je 0xd2aa
movq 0x3ace4(%rip), %rcx     # 0x47f80
cmpb $0x0, (%rcx)
je 0xd2a6
incl 0x8(%rdi)
jmp 0xd2aa
lock
incl 0x8(%rdi)
movq (%rax), %rax
cmpw $0x2, (%rax)
jne 0xd2db
movl 0x4(%rax), %eax
movl $0x0, 0x78(%rsp)
movl %eax, 0x7c(%rsp)
xorps %xmm0, %xmm0
movups %xmm0, 0x80(%rsp)
movq $0x0, 0x90(%rsp)
jmp 0xd2fb
movl 0x18(%rax), %ecx
movups 0x8(%rax), %xmm0
movups %xmm0, 0x80(%rsp)
movq $0x1, 0x78(%rsp)
movq %rcx, 0x90(%rsp)
testq %rdi, %rdi
je 0xd305
callq 0x1021a
leaq 0x24e50(%rip), %rsi     # 0x3215c
leaq 0xb8(%rsp), %rdi
callq 0xb190
movl %eax, %ebp
testl %eax, %eax
je 0xd36e
leaq 0x24e3b(%rip), %rsi     # 0x32161
leaq 0xb8(%rsp), %rdi
callq 0xb190
movl $0x1, %ebx
testl %eax, %eax
je 0xd370
movl $0x10, %edi
callq 0xb300
movq %rax, %r14
leaq 0x24e2b(%rip), %rsi     # 0x3217b
movq %rax, %rdi
callq 0xbcd0
movq 0x3ac61(%rip), %rsi     # 0x47fc0
movq 0x3ac32(%rip), %rdx     # 0x47f98
movq %r14, %rdi
callq 0xbb60
xorl %ebx, %ebx
testl %ebp, %ebp
sete %al
movl 0x1c(%rsp), %ecx
notb %cl
orb %al, %cl
testb $0x1, %cl
jne 0xd398
movl $0x8, %edi
callq 0xb5e0
movq %rax, %r14
leaq 0x39f62(%rip), %rax     # 0x472f8
jmp 0xd3ac
movl $0x8, %edi
callq 0xb5e0
movq %rax, %r14
leaq 0x39cf4(%rip), %rax     # 0x470a0
addq $0x10, %rax
movq %rax, (%r14)
leaq 0xd8(%rsp), %r9
leaq 0x100(%rsp), %rax
movq (%r14), %rcx
movq (%rcx), %r10
leaq 0x120(%rsp), %rcx
movq %rcx, 0x8(%rsp)
movq %rax, (%rsp)
movzwl %r15w, %ecx
leaq 0x40(%rsp), %rdi
leaq 0x78(%rsp), %rdx
movq %r14, %rsi
movl %ebx, %r8d
callq *%r10
cmpb $0x1, 0x3b7d0(%rip)     # 0x48bc8
jne 0xd404
leaq 0x40(%rsp), %rdi
callq 0x21644
leaq 0x138(%rsp), %rdi
leaq 0x40(%rsp), %rsi
callq 0x21624
movq 0x138(%rsp), %rsi
movq 0x140(%rsp), %rdx
movq 0x3ab5b(%rip), %rdi     # 0x47f88
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 0xd47d
movq 0x148(%rsp), %rsi
incq %rsi
callq 0xb600
movq 0x60(%rsp), %rdi
testq %rdi, %rdi
je 0xd494
movq 0x70(%rsp), %rsi
subq %rdi, %rsi
callq 0xb600
leaq 0x48(%rsp), %rdi
callq 0x1028a
movl $0x8, %esi
movq %r14, %rdi
callq 0xb600
xorl %ebx, %ebx
movq 0x160(%rsp), %rdi
testq %rdi, %rdi
je 0xd4bf
callq 0x1021a
leaq 0x170(%rsp), %rdi
callq 0x12824
movq 0x30(%rsp), %rax
movq 0x38(%rax), %r14
testq %r14, %r14
je 0xd4e9
movq (%r14), %rax
movq %r14, %rdi
callq *0x10(%rax)
movq 0x20(%r14), %r14
jmp 0xd4d5
movq 0x30(%rsp), %r14
movq 0x38(%r14), %rdi
testq %rdi, %rdi
je 0xd50d
movq (%rdi), %rax
movq 0x20(%rdi), %r15
callq *0x8(%rax)
movq %r15, 0x38(%r14)
movq %r15, %rdi
testq %r15, %r15
jne 0xd4f7
movq 0x30(%rsp), %r14
testq %r14, %r14
je 0xd52d
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 0xd54e
movq 0x110(%rsp), %rsi
incq %rsi
callq 0xb600
leaq 0xe8(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0xd56f
movq 0xe8(%rsp), %rsi
incq %rsi
callq 0xb600
movq 0xb8(%rsp), %rdi
leaq 0xc8(%rsp), %rax
cmpq %rax, %rdi
je 0xd594
movq 0xc8(%rsp), %rsi
incq %rsi
callq 0xb600
movq 0x98(%rsp), %rdi
leaq 0xa8(%rsp), %rax
cmpq %rax, %rdi
je 0xd5b9
movq 0xa8(%rsp), %rsi
incq %rsi
callq 0xb600
leaq 0x120(%rsp), %rdi
callq 0x102da
movq 0x1d8(%rsp), %rdi
leaq 0x1e8(%rsp), %rax
cmpq %rax, %rdi
je 0xd5eb
movq 0x1e8(%rsp), %rsi
incq %rsi
callq 0xb600
movq 0x1b8(%rsp), %rdi
leaq 0x1c8(%rsp), %rax
cmpq %rax, %rdi
je 0xd610
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 %edi, %edi
callq 0xcd5c
jmp 0xd65d
movq %rdx, %r15
movq %rax, %rbx
movq %r14, %rdi
callq 0xb4b0
jmp 0xd6fc
jmp 0xd642
movq %rdx, %r15
movq %rax, %rbx
jmp 0xd6c2
movq %rdx, %r15
movq %rax, %rbx
jmp 0xd6cc
movq %rdx, %r15
movq %rax, %rbx
xorl %r14d, %r14d
jmp 0xd6cc
movq %rdx, %r15
movq %rax, %rbx
jmp 0xd6fc
jmp 0xd7e3
movq %rdx, %r15
movq %rax, %rbx
jmp 0xd70e
movq %rdx, %r15
movq %rax, %rbx
jmp 0xd71b
jmp 0xd727
movq %rdx, %r15
movq %rax, %rbx
jmp 0xd73a
movq %rax, %rbx
jmp 0xd80b
movq %rdx, %r15
movq %rax, %rbx
leaq 0x148(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0xd6c2
movq 0x148(%rsp), %rsi
incq %rsi
callq 0xb600
leaq 0x40(%rsp), %rdi
callq 0xdbf6
testq %r14, %r14
je 0xd6fc
movl $0x8, %esi
movq %r14, %rdi
jmp 0xd6f7
movq %rdx, %r15
movq %rax, %rbx
leaq 0x50(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0xd6fc
movq 0x50(%rsp), %rsi
incq %rsi
callq 0xb600
movq 0x160(%rsp), %rdi
testq %rdi, %rdi
je 0xd70e
callq 0x1021a
leaq 0x170(%rsp), %rdi
callq 0x12824
leaq 0x30(%rsp), %rdi
callq 0xdc1c
jmp 0xd72d
movq %rdx, %r15
movq %rax, %rbx
leaq 0xb8(%rsp), %rdi
callq 0xdc92
cmpl $0x1, %r15d
jne 0xd7e6
movq %rbx, %rdi
callq 0xb290
movq (%rax), %rcx
movq %rax, %rdi
callq *0x10(%rcx)
movq 0x3a88c(%rip), %rdi     # 0x47fe8
movq %rax, %rsi
callq 0xb5d0
movq %rax, %rdi
callq 0xb2f0
movl $0x1, %ebx
callq 0xbaf0
jmp 0xd594
jmp 0xd7e3
movq %rax, %rbx
callq 0xbaf0
jmp 0xd7e6
jmp 0xd82d
movq %rax, %rbx
movq 0x170(%rsp), %rsi
testq %rsi, %rsi
je 0xd7a9
leaq 0x170(%rsp), %rdi
callq 0x10490
movq $0x0, 0x170(%rsp)
jmp 0xd7e6
movq %rax, %rbx
movq 0xb8(%rsp), %rdi
leaq 0xc8(%rsp), %rax
cmpq %rax, %rdi
je 0xd7e6
movq 0xc8(%rsp), %rsi
incq %rsi
callq 0xb600
jmp 0xd7e6
jmp 0xd7e3
movq %rax, %rbx
movq 0x98(%rsp), %rdi
leaq 0xa8(%rsp), %rax
cmpq %rax, %rdi
je 0xd80b
movq 0xa8(%rsp), %rsi
incq %rsi
callq 0xb600
leaq 0x120(%rsp), %rdi
callq 0x102da
leaq 0x1b8(%rsp), %rdi
callq 0xdce0
movq %rbx, %rdi
callq 0xbbb0
movq %rax, %rdi
callq 0xd91f
nop