// File: /workspace/github_cpp_makefile_star2_O1/kztomita[P]http-dump/./http2_socket.h
// Function: void send_http2_headers<boost::asio::ssl::stream<boost::asio::basic_stream_socket<boost::asio::ip::tcp, boost::asio::execution::any_executor<boost::asio::execution::context_as_t<boost::asio::execution_context&>, boost::asio::execution::detail::blocking::never_t<0>, boost::asio::execution::prefer_only<boost::asio::execution::detail::blocking::possibly_t<0>>, boost::asio::execution::prefer_only<boost::asio::execution::detail::outstanding_work::tracked_t<0>>, boost::asio::execution::prefer_only<boost::asio::execution::detail::outstanding_work::untracked_t<0>>, boost::asio::execution::prefer_only<boost::asio::execution::detail::relationship::fork_t<0>>, boost::asio::execution::prefer_only<boost::asio::execution::detail::relationship::continuation_t<0>>>>>>(boost::asio::ssl::stream<boost::asio::basic_stream_socket<boost::asio::ip::tcp, boost::asio::execution::any_executor<boost::asio::execution::context_as_t<boost::asio::execution_context&>, boost::asio::execution::detail::blocking::never_t<0>, boost::asio::execution::prefer_only<boost::asio::execution::detail::blocking::possibly_t<0>>, boost::asio::execution::prefer_only<boost::asio::execution::detail::outstanding_work::tracked_t<0>>, boost::asio::execution::prefer_only<boost::asio::execution::detail::outstanding_work::untracked_t<0>>, boost::asio::execution::prefer_only<boost::asio::execution::detail::relationship::fork_t<0>>, boost::asio::execution::prefer_only<boost::asio::execution::detail::relationship::continuation_t<0>>>>>&, unsigned int, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, std::__cxx11::list<std::unique_ptr<http_header, std::default_delete<http_header>>, std::allocator<std::unique_ptr<http_header, std::default_delete<http_header>>>> const&, unsigned long)
void send_http2_headers(SyncWriteStream& stream,
                        uint32_t stream_id,
                        const std::string& host,
                        const std::string& path,
                        const http_header_list& req_headers,
                        std::size_t max_payload_len = 16384) {
  std::vector<hpack::header_type> header_array{
    {":method", "GET"},
    {":scheme", "https"},
    {":path", path},
    {":authority", host},
  };
  // XXX ヘッダーが多くても現状フラグメントは行っていない
  for (const auto& h : req_headers) {
    header_array.emplace_back(hpack::header_type{
        string_util::tolower(h->key),
        h->value});
  }

  // HPACK(rfc7541)形式でヘッダーを作成
  hpack::hpack_encoder encoder;
  auto header_block = encoder.encode<http2_frame::payload_type>(header_array);

  if (header_block.size() <= max_payload_len) {
    http2_frame headers;
    auto& header = headers.header();
    header.type = http2_frame_header::TYPE_HEADERS;
    header.flags = 0x05;         // END_HEADERS | END_STREAM
    header.set_stream_id(stream_id);

    headers.header().set_length(header_block.size());
    using std::swap;
    swap(headers.payload(), header_block);

    write_http2_frame(stream, headers);
  } else {
    // HEADERS AND CONTINUATION
    // nginx相手だとENHANCE YOUR CALMが返される
    {
      http2_frame headers;
      auto& header = headers.header();
      header.type = http2_frame_header::TYPE_HEADERS;
      header.flags = 0x01;         // END_STREAM
      header.set_stream_id(stream_id);

      headers.header().set_length(max_payload_len);
      std::copy(header_block.begin(),
                header_block.begin() + max_payload_len,
                std::back_inserter(headers.payload()));

      write_http2_frame(stream, headers);
    }

    std::size_t offset = max_payload_len;
    while (offset < header_block.size()) {
      auto left_bytes = header_block.size() - offset;
      auto len = std::min(left_bytes, max_payload_len);

      http2_frame continuation;
      auto& header = continuation.header();
      header.type = http2_frame_header::TYPE_CONTINUATION;
      header.flags = 0x00;
      if (len <= max_payload_len) {
        header.flags |= 0x04;       // END_HEADERS
      }
      header.set_stream_id(stream_id);

      continuation.header().set_length(len);
      auto start = header_block.begin() + offset;
      std::copy(start,
                start + len,
                std::back_inserter(continuation.payload()));

      write_http2_frame(stream, continuation);

      offset += len;
    }
  }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x158, %rsp            # imm = 0x158
movq %r9, 0x28(%rsp)
movq %r8, %r15
movq %rcx, %r13
movq %rdx, %rbx
movl %esi, 0x34(%rsp)
movq %rdi, %rbp
leaq 0x58(%rsp), %r14
leaq 0xe4aa(%rip), %rsi      # 0x34476
leaq 0xe4ab(%rip), %rdx      # 0x3447e
movq %r14, %r12
movq %r14, %rdi
callq 0x27daa
leaq 0x98(%rsp), %r12
leaq 0xe495(%rip), %rsi      # 0x34482
leaq 0xc16d(%rip), %rdx      # 0x32161
movq %r12, %rdi
callq 0x27e28
leaq 0xd8(%rsp), %r12
leaq 0xe47f(%rip), %rsi      # 0x3448a
movq %r12, %rdi
movq %r13, %rdx
callq 0x27ea6
leaq 0x118(%rsp), %r12
leaq 0xe46b(%rip), %rsi      # 0x34490
movq %r12, %rdi
movq %rbx, %rdx
callq 0x27f1c
leaq 0x40(%rsp), %rdi
leaq 0x58(%rsp), %rsi
leaq 0x8(%rsp), %rcx
movl $0x4, %edx
callq 0x27f92
movq $-0x100, %rbx
leaq 0x148(%rsp), %r14
movq -0x10(%r14), %rdi
cmpq %rdi, %r14
je 0x2606c
movq (%r14), %rsi
incq %rsi
callq 0xb600
movq -0x30(%r14), %rdi
leaq -0x20(%r14), %rax
cmpq %rdi, %rax
je 0x26084
movq (%rax), %rsi
incq %rsi
callq 0xb600
addq $-0x40, %r14
addq $0x40, %rbx
jne 0x26058
movq %rbp, 0x38(%rsp)
movq (%r15), %r14
cmpq %r15, %r14
je 0x2615b
leaq 0x18(%rsp), %rbp
leaq 0x8(%rsp), %r12
leaq 0x58(%rsp), %r13
leaq 0x40(%rsp), %rbx
movq 0x10(%r14), %rax
movq %rbp, 0x8(%rsp)
movq $0x0, 0x10(%rsp)
movb $0x0, 0x18(%rsp)
movq (%rax), %rdi
movq 0x8(%rax), %rsi
addq %rdi, %rsi
movq %r12, %rdx
callq 0x2474a
movq 0x10(%r14), %rdx
addq $0x20, %rdx
movq %r13, %rdi
movq %r12, %rsi
callq 0x27fda
movq %rbx, %rdi
movq %r13, %rsi
callq 0x1cedc
movq 0x78(%rsp), %rdi
leaq 0x88(%rsp), %rax
cmpq %rax, %rdi
je 0x2611c
movq 0x88(%rsp), %rsi
incq %rsi
callq 0xb600
movq 0x58(%rsp), %rdi
leaq 0x68(%rsp), %rax
cmpq %rax, %rdi
je 0x26138
movq 0x68(%rsp), %rsi
incq %rsi
callq 0xb600
movq 0x8(%rsp), %rdi
cmpq %rbp, %rdi
je 0x2614f
movq 0x18(%rsp), %rsi
incq %rsi
callq 0xb600
movq (%r14), %r14
cmpq %r15, %r14
jne 0x260b3
leaq 0x8(%rsp), %rdi
leaq 0x7(%rsp), %rsi
leaq 0x40(%rsp), %rdx
callq 0x2805e
movq 0x10(%rsp), %rax
subq 0x8(%rsp), %rax
cmpq 0x28(%rsp), %rax
movq 0x38(%rsp), %r13
movl 0x34(%rsp), %ebp
xorps %xmm0, %xmm0
leaq 0x58(%rsp), %rdi
jbe 0x262a2
movups %xmm0, 0x10(%rdi)
movq $0x0, 0x20(%rdi)
movw $0x101, 0x3(%rdi)       # imm = 0x101
movl %ebp, %esi
callq 0x273a8
leaq 0x58(%rsp), %rdi
movq 0x28(%rsp), %rsi
callq 0x27402
leaq 0x68(%rsp), %r15
movq 0x8(%rsp), %rdi
movq 0x28(%rsp), %rax
leaq (%rdi,%rax), %rsi
movq %r15, %rdx
callq 0x28410
leaq 0x58(%rsp), %rsi
movq %r13, %rdi
callq 0x2746c
movq 0x68(%rsp), %rdi
testq %rdi, %rdi
je 0x261fc
movq 0x78(%rsp), %rsi
subq %rdi, %rsi
callq 0xb600
movq 0x10(%rsp), %rbx
subq 0x8(%rsp), %rbx
cmpq 0x28(%rsp), %rbx
jbe 0x26315
leaq 0x58(%rsp), %r12
movq 0x28(%rsp), %r14
subq %r14, %rbx
movq 0x28(%rsp), %rax
cmpq %rax, %rbx
cmovaeq %rax, %rbx
xorps %xmm0, %xmm0
movups %xmm0, (%r15)
movq $0x0, 0x10(%r15)
movw $0x409, 0x5b(%rsp)      # imm = 0x409
movq %r12, %rdi
movl %ebp, %esi
callq 0x273a8
movq %r12, %rdi
movl %ebx, %esi
callq 0x27402
movq 0x8(%rsp), %rdi
addq %r14, %rdi
leaq (%rdi,%rbx), %rsi
movq %r15, %rdx
callq 0x28410
movq %r13, %rdi
movq %r12, %rsi
callq 0x2746c
movq 0x68(%rsp), %rdi
testq %rdi, %rdi
je 0x2628a
movq 0x78(%rsp), %rsi
subq %rdi, %rsi
callq 0xb600
addq %rbx, %r14
movq 0x10(%rsp), %rbx
subq 0x8(%rsp), %rbx
cmpq %rbx, %r14
jb 0x2621b
jmp 0x26315
movups %xmm0, 0x10(%rdi)
movq $0x0, 0x20(%rdi)
movw $0x501, 0x3(%rdi)       # imm = 0x501
movl %ebp, %esi
callq 0x273a8
movl 0x10(%rsp), %esi
subl 0x8(%rsp), %esi
leaq 0x58(%rsp), %rdi
callq 0x27402
leaq 0x58(%rsp), %rsi
movups 0x10(%rsi), %xmm0
movq 0x20(%rsi), %rax
movq 0x8(%rsp), %rcx
movq %rcx, 0x10(%rsi)
movups 0x10(%rsp), %xmm1
movups %xmm1, 0x18(%rsi)
movups %xmm0, 0x8(%rsp)
movq %rax, 0x18(%rsp)
movq %r13, %rdi
callq 0x2746c
movq 0x68(%rsp), %rdi
testq %rdi, %rdi
je 0x26315
movq 0x78(%rsp), %rsi
subq %rdi, %rsi
callq 0xb600
movq 0x8(%rsp), %rdi
testq %rdi, %rdi
je 0x2632c
movq 0x18(%rsp), %rsi
subq %rdi, %rsi
callq 0xb600
leaq 0x40(%rsp), %rdi
callq 0x19bbc
addq $0x158, %rsp            # imm = 0x158
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x2639d
movq %rax, %rbx
jmp 0x263f6
movq %rax, %rbx
movl $0xc0, %r14d
leaq (%rsp,%r14), %rdi
addq $0x58, %rdi
callq 0x19c44
addq $-0x40, %r14
cmpq $-0x40, %r14
jne 0x2635b
jmp 0x26400
jmp 0x2639d
jmp 0x2639d
movq %rax, %rbx
cmpq %r12, %r14
je 0x26400
leaq 0x58(%rsp), %r14
addq $-0x40, %r12
movq %r12, %rdi
callq 0x19c44
cmpq %r14, %r12
jne 0x26388
jmp 0x26400
jmp 0x2639d
movq %rax, %rbx
movq 0x68(%rsp), %rdi
testq %rdi, %rdi
je 0x263b7
movq 0x78(%rsp), %rsi
subq %rdi, %rsi
callq 0xb600
movq 0x8(%rsp), %rdi
testq %rdi, %rdi
je 0x263f6
movq 0x18(%rsp), %rsi
subq %rdi, %rsi
jmp 0x263f1
jmp 0x263cd
movq %rax, %rbx
jmp 0x263df
movq %rax, %rbx
leaq 0x58(%rsp), %rdi
callq 0x19c44
movq 0x8(%rsp), %rdi
cmpq %rbp, %rdi
je 0x263f6
movq 0x18(%rsp), %rsi
incq %rsi
callq 0xb600
leaq 0x40(%rsp), %rdi
callq 0x19bbc
movq %rbx, %rdi
callq 0xbbb0