// File: /workspace/github_cpp_makefile_star2_O2/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 $0x208, %rsp            # imm = 0x208
leaq 0x1d8(%rsp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
xorl %ecx, %ecx
movb %cl, (%rax)
leaq 0x1f8(%rsp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movb %cl, (%rax)
leaq 0xa8(%rsp), %rax
movq %rax, 0x8(%rax)
movq %rax, (%rax)
andq $0x0, 0x10(%rax)
cmpl $0x1, %edi
jle 0xd0f7
movq %rsi, %r14
movl %edi, %ebp
leaq 0xe8(%rsp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movb $0x0, (%rax)
movl %edi, %eax
movq -0x8(%rsi,%rax,8), %rsi
cmpb $0x2d, (%rsi)
je 0xcc7a
leaq 0xd8(%rsp), %rdi
callq 0xbcf0
decl %ebp
movq $0x0, 0x38(%rsp)
leaq 0x1e4cc(%rip), %r15     # 0x2b156
leaq 0x39a5f(%rip), %r12     # 0x466f0
leaq 0xf8(%rsp), %r13
leaq 0x180(%rsp), %rbx
movl %ebp, %edi
movq %r14, %rsi
movq %r15, %rdx
movq %r12, %rcx
xorl %r8d, %r8d
callq 0xbb10
cmpl $0x2, %eax
je 0xcd4b
cmpl $0x48, %eax
je 0xccf9
cmpl $0x6b, %eax
je 0xcd61
cmpl $0x76, %eax
je 0xcd6d
cmpl $-0x1, %eax
je 0xcd85
cmpl $0x1, %eax
je 0xcd79
cmpl $0x3f, %eax
je 0xd0f0
cmpl $0x68, %eax
jne 0xcca1
jmp 0xd0f0
movq 0x39220(%rip), %rax     # 0x45f20
movq (%rax), %rsi
movq %r13, %rdi
leaq 0x40(%rsp), %rdx
callq 0xd416
movq %rbx, %rdi
movq %r13, %rsi
callq 0x1dd42
movq %r13, %rdi
callq 0xb2f0
cmpq $0x0, 0x180(%rsp)
je 0xcd3e
leaq 0xa8(%rsp), %rdi
movq %rbx, %rsi
callq 0xd452
movq %rbx, %rdi
callq 0xd46c
jmp 0xcca1
movq 0x391ce(%rip), %rax     # 0x45f20
movq (%rax), %rax
movq %rax, 0x39e74(%rip)     # 0x46bd0
jmp 0xcca1
movb $0x0, 0x399f0(%rip)     # 0x46758
jmp 0xcca1
movb $0x1, 0x39e64(%rip)     # 0x46bd8
jmp 0xcca1
movb $0x1, %al
movq %rax, 0x38(%rsp)
jmp 0xcca1
leaq 0xf8(%rsp), %rdi
leaq 0xd8(%rsp), %rsi
callq 0x11fdc
leaq 0x1e3bb(%rip), %rsi     # 0x2b15c
leaq 0xf8(%rsp), %rdi
callq 0xd486
movw $0x50, %bp
testb %al, %al
jne 0xcdd6
leaq 0x1e3a4(%rip), %rsi     # 0x2b161
leaq 0xf8(%rsp), %rdi
callq 0xd486
movw $0x1bb, %bp             # imm = 0x1BB
testb %al, %al
je 0xcf06
cmpb $0x0, 0x13c(%rsp)
je 0xcde8
movzwl 0x138(%rsp), %ebp
andl $0x0, 0x28(%rsp)
movb $0x0, 0x2c(%rsp)
leaq 0x37507(%rip), %rax     # 0x44300
movq %rax, 0x30(%rsp)
leaq 0x98(%rsp), %rdi
callq 0xd494
leaq 0x180(%rsp), %rdi
leaq 0x98(%rsp), %rsi
xorl %edx, %edx
callq 0xf816
leaq 0x78(%rsp), %rdx
movl $0x2, (%rdx)
andq $0x0, 0x40(%rsp)
movq 0x118(%rsp), %r8
movq 0x120(%rsp), %rcx
leaq 0x20466(%rip), %rax     # 0x2d2b0
movq %rax, 0x48(%rsp)
movups 0x40(%rsp), %xmm0
movups %xmm0, (%rsp)
leaq 0x28(%rsp), %rax
movq %rax, 0x10(%rsp)
leaq 0xc0(%rsp), %rdi
leaq 0x180(%rsp), %rsi
xorl %r9d, %r9d
callq 0x114e4
cmpb $0x1, 0x2c(%rsp)
jne 0xcec0
movq 0x30(%rsp), %rsi
movl 0x28(%rsp), %edx
movq (%rsi), %rax
leaq 0x40(%rsp), %rdi
callq *0x20(%rax)
movq 0x39144(%rip), %rdi     # 0x45fe0
leaq 0x40(%rsp), %rsi
callq 0xb570
movq %rax, %rdi
callq 0xb320
leaq 0x40(%rsp), %rdi
callq 0xb2f0
pushq $0x1
popq %rbx
jmp 0xd081
leaq 0x40(%rsp), %rdi
leaq 0xc0(%rsp), %rsi
callq 0xd4f8
movq 0x40(%rsp), %rax
imulq $0x60, 0x50(%rsp), %rcx
movq (%rax), %rdx
leaq (%rdx,%rcx), %rax
cmpw $0x2, (%rdx,%rcx)
jne 0xcf29
movl 0x4(%rax), %eax
xorps %xmm0, %xmm0
movups %xmm0, 0x80(%rsp)
andq $0x0, 0x90(%rsp)
xorl %ecx, %ecx
jmp 0xcf45
movq 0x390d3(%rip), %rdi     # 0x45fe0
leaq 0x1e253(%rip), %rsi     # 0x2b167
callq 0xb5e0
movq %rax, %rdi
callq 0xb320
pushq $0x1
popq %rbx
jmp 0xd0a8
movl 0x18(%rax), %ecx
movups 0x8(%rax), %xmm0
movups %xmm0, 0x80(%rsp)
movq %rcx, 0x90(%rsp)
xorl %eax, %eax
pushq $0x1
popq %rcx
movl %ecx, 0x78(%rsp)
movl %eax, 0x7c(%rsp)
leaq 0x48(%rsp), %rdi
callq 0xf37e
leaq 0x1e1fe(%rip), %rsi     # 0x2b15c
leaq 0xf8(%rsp), %rdi
callq 0xd486
testb %al, %al
je 0xcf79
andq $0x0, 0x20(%rsp)
xorl %ebx, %ebx
jmp 0xcfc0
leaq 0x1e1e1(%rip), %rsi     # 0x2b161
leaq 0xf8(%rsp), %rdi
callq 0xd486
testb %al, %al
je 0xd0fe
andq $0x0, 0x20(%rsp)
testb $0x1, 0x38(%rsp)
je 0xcfbd
pushq $0x8
popq %rdi
callq 0xb5f0
leaq 0x38397(%rip), %rcx     # 0x45348
addq $0x10, %rcx
movq %rcx, (%rax)
pushq $0x1
popq %rbx
jmp 0xcfd6
pushq $0x1
popq %rbx
pushq $0x8
popq %rdi
callq 0xb5f0
leaq 0x38121(%rip), %rcx     # 0x450f0
addq $0x10, %rcx
movq %rcx, (%rax)
leaq 0x118(%rsp), %r14
leaq 0x20(%rsp), %r15
movq %r15, %rdi
movq %rax, %rsi
callq 0x11fc4
movq (%r15), %rsi
leaq 0x140(%rsp), %rax
movq (%rsi), %rcx
movq (%rcx), %r10
leaq 0xa8(%rsp), %rcx
movq %rcx, 0x8(%rsp)
movq %rax, (%rsp)
movzwl %bp, %ecx
leaq 0x40(%rsp), %rdi
leaq 0x78(%rsp), %rdx
movl %ebx, %r8d
movq %r14, %r9
callq *%r10
leaq 0x40(%rsp), %rdi
callq 0xd535
leaq 0x160(%rsp), %rdi
leaq 0x40(%rsp), %rsi
callq 0x1e37c
movq 0x38f2f(%rip), %rdi     # 0x45f78
leaq 0x160(%rsp), %rsi
callq 0xb570
movq %rax, %rdi
callq 0xb320
leaq 0x160(%rsp), %rdi
callq 0xb2f0
leaq 0x40(%rsp), %rdi
callq 0xd544
leaq 0x20(%rsp), %rdi
callq 0xd55e
xorl %ebx, %ebx
leaq 0xc8(%rsp), %rdi
callq 0xf37e
leaq 0x180(%rsp), %rdi
callq 0x1149c
leaq 0x98(%rsp), %rdi
callq 0xd578
leaq 0xf8(%rsp), %rdi
callq 0xd5ae
leaq 0xd8(%rsp), %rdi
callq 0xb2f0
leaq 0xa8(%rsp), %rdi
callq 0xf4a2
leaq 0x1c8(%rsp), %rdi
callq 0xd5ce
movl %ebx, %eax
addq $0x208, %rsp            # imm = 0x208
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %edi, %edi
callq 0xcafc
xorl %edi, %edi
callq 0xcafc
pushq $0x10
popq %rdi
callq 0xb330
movq %rax, %r15
leaq 0x1e06b(%rip), %rsi     # 0x2b17b
movq %rax, %rdi
callq 0xbcc0
movq 0x38e99(%rip), %rsi     # 0x45fb8
movq 0x38e6a(%rip), %rdx     # 0x45f90
movq %r15, %rdi
callq 0xbb50
jmp 0xd156
movq %rdx, %r14
movq %rax, %rbx
movq %r15, %rdi
callq 0xb4d0
jmp 0xd1a1
jmp 0xd142
movq %rdx, %r14
movq %rax, %rbx
jmp 0xd17b
jmp 0xd14c
movq %rdx, %r14
movq %rax, %rbx
jmp 0xd185
jmp 0xd156
movq %rdx, %r14
movq %rax, %rbx
jmp 0xd1a1
jmp 0xd23b
jmp 0xd276
movq %rdx, %r14
movq %rax, %rbx
leaq 0x160(%rsp), %rdi
callq 0xb2f0
leaq 0x40(%rsp), %rdi
callq 0xd544
leaq 0x20(%rsp), %rdi
callq 0xd55e
jmp 0xd1a1
movq %rdx, %r14
movq %rax, %rbx
leaq 0x40(%rsp), %rdi
callq 0xb2f0
leaq 0xc8(%rsp), %rdi
callq 0xf37e
leaq 0x180(%rsp), %rdi
callq 0x1149c
leaq 0x98(%rsp), %rdi
callq 0xd578
leaq 0xf8(%rsp), %rdi
callq 0xd5ae
cmpl $0x1, %r14d
jne 0xd279
movq %rbx, %rdi
callq 0xb290
movq (%rax), %rcx
movq %rax, %rdi
callq *0x10(%rcx)
movq 0x38de9(%rip), %rdi     # 0x45fe0
movq %rax, %rsi
callq 0xb5e0
movq %rax, %rdi
callq 0xb320
callq 0xbb00
pushq $0x1
popq %rbx
jmp 0xd0b5
jmp 0xd276
movq %rax, %rbx
callq 0xbb00
jmp 0xd279
movq %rax, %rdi
callq 0xd363
movq %rdx, %r14
movq %rax, %rbx
jmp 0xd1ae
movq %rdx, %r14
movq %rax, %rbx
jmp 0xd1bb
movq %rdx, %r14
movq %rax, %rbx
jmp 0xd1c8
movq %rdx, %r14
movq %rax, %rbx
jmp 0xd1d5
movq %rax, %rbx
jmp 0xd286
movq %rax, %rbx
leaq 0x180(%rsp), %rdi
callq 0xd46c
jmp 0xd279
jmp 0xd276
movq %rax, %rbx
leaq 0xf8(%rsp), %rdi
callq 0xb2f0
jmp 0xd279
movq %rax, %rbx
leaq 0xd8(%rsp), %rdi
callq 0xb2f0
leaq 0xa8(%rsp), %rdi
callq 0xf4a2
leaq 0x1c8(%rsp), %rdi
callq 0xd5ce
movq %rbx, %rdi
callq 0xbba0