// File: /workspace/github_cpp_makefile_star2_O3/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 $0x168, %rsp            # imm = 0x168
movq %r9, 0x38(%rsp)
movq %r8, %r15
movq %rcx, %rbp
movq %rdx, %rbx
movl %esi, 0x44(%rsp)
movq %rdi, 0x48(%rsp)
leaq 0xd73a(%rip), %rsi      # 0x33476
leaq 0xd73b(%rip), %rdx      # 0x3347e
leaq 0x68(%rsp), %r12
movq %r12, %rdi
callq 0x279da
leaq 0xa8(%rsp), %r13
leaq 0xd723(%rip), %rsi      # 0x33482
leaq 0xb3fb(%rip), %rdx      # 0x31161
movq %r13, %rdi
callq 0x27a58
leaq 0xe8(%rsp), %r13
leaq 0xd70d(%rip), %rsi      # 0x3348a
movq %r13, %rdi
movq %rbp, %rdx
callq 0x27ad6
leaq 0x128(%rsp), %r13
leaq 0xd6f9(%rip), %rsi      # 0x33490
movq %r13, %rdi
movq %rbx, %rdx
callq 0x27b4c
leaq 0x50(%rsp), %rdi
leaq 0x68(%rsp), %rsi
leaq 0x10(%rsp), %rcx
movl $0x4, %edx
callq 0x27bc2
movq $-0x100, %rbx
leaq 0x158(%rsp), %r14
movq -0x10(%r14), %rdi
cmpq %rdi, %r14
je 0x25dde
movq (%r14), %rsi
incq %rsi
callq 0xb600
movq -0x30(%r14), %rdi
leaq -0x20(%r14), %rax
cmpq %rdi, %rax
je 0x25df6
movq (%rax), %rsi
incq %rsi
callq 0xb600
addq $-0x40, %r14
addq $0x40, %rbx
jne 0x25dca
movq (%r15), %r14
cmpq %r15, %r14
je 0x25ecb
leaq 0x20(%rsp), %rbp
leaq 0x10(%rsp), %r12
leaq 0x68(%rsp), %r13
leaq 0x50(%rsp), %rbx
movq 0x10(%r14), %rax
movq %rbp, 0x10(%rsp)
movq $0x0, 0x18(%rsp)
movb $0x0, 0x20(%rsp)
movq (%rax), %rdi
movq 0x8(%rax), %rsi
addq %rdi, %rsi
movq %r12, %rdx
callq 0x244fa
movq 0x10(%r14), %rdx
addq $0x20, %rdx
movq %r13, %rdi
movq %r12, %rsi
callq 0x27c0a
movq %rbx, %rdi
movq %r13, %rsi
callq 0x1cd52
movq 0x88(%rsp), %rdi
leaq 0x98(%rsp), %rax
cmpq %rax, %rdi
je 0x25e8c
movq 0x98(%rsp), %rsi
incq %rsi
callq 0xb600
movq 0x68(%rsp), %rdi
leaq 0x78(%rsp), %rax
cmpq %rax, %rdi
je 0x25ea8
movq 0x78(%rsp), %rsi
incq %rsi
callq 0xb600
movq 0x10(%rsp), %rdi
cmpq %rbp, %rdi
je 0x25ebf
movq 0x20(%rsp), %rsi
incq %rsi
callq 0xb600
movq (%r14), %r14
cmpq %r15, %r14
jne 0x25e20
leaq 0x10(%rsp), %rdi
leaq 0xf(%rsp), %rsi
leaq 0x50(%rsp), %rdx
callq 0x27c8e
movq 0x18(%rsp), %rax
subq 0x10(%rsp), %rax
cmpq 0x38(%rsp), %rax
movq 0x48(%rsp), %r13
movl 0x44(%rsp), %ebp
xorps %xmm0, %xmm0
leaq 0x68(%rsp), %rdi
jbe 0x2601e
movups %xmm0, 0x10(%rdi)
movq $0x0, 0x20(%rdi)
movw $0x101, 0x3(%rdi)       # imm = 0x101
movl %ebp, %esi
callq 0x27028
leaq 0x68(%rsp), %rdi
movq 0x38(%rsp), %rsi
callq 0x27082
leaq 0x78(%rsp), %r15
movq 0x10(%rsp), %rdi
movq 0x38(%rsp), %rax
leaq (%rdi,%rax), %rsi
movq %r15, %rdx
callq 0x28048
leaq 0x68(%rsp), %rsi
movq %r13, %rdi
callq 0x270ec
movq 0x78(%rsp), %rdi
testq %rdi, %rdi
je 0x25f6f
movq 0x88(%rsp), %rsi
subq %rdi, %rsi
callq 0xb600
movq 0x10(%rsp), %rdi
movq 0x18(%rsp), %rbx
subq %rdi, %rbx
cmpq 0x38(%rsp), %rbx
jbe 0x26099
leaq 0x68(%rsp), %r12
movq 0x38(%rsp), %r14
subq %r14, %rbx
movq 0x38(%rsp), %rax
cmpq %rax, %rbx
cmovaeq %rax, %rbx
xorps %xmm0, %xmm0
movups %xmm0, (%r15)
movq $0x0, 0x10(%r15)
movw $0x409, 0x6b(%rsp)      # imm = 0x409
movq %r12, %rdi
movl %ebp, %esi
callq 0x27028
movq %r12, %rdi
movl %ebx, %esi
callq 0x27082
movq 0x10(%rsp), %rdi
addq %r14, %rdi
leaq (%rdi,%rbx), %rsi
movq %r15, %rdx
callq 0x28048
movq %r13, %rdi
movq %r12, %rsi
callq 0x270ec
movq 0x78(%rsp), %rdi
testq %rdi, %rdi
je 0x26003
movq 0x88(%rsp), %rsi
subq %rdi, %rsi
callq 0xb600
addq %rbx, %r14
movq 0x10(%rsp), %rdi
movq 0x18(%rsp), %rbx
subq %rdi, %rbx
cmpq %rbx, %r14
jb 0x25f91
jmp 0x26099
movups %xmm0, 0x10(%rdi)
movq $0x0, 0x20(%rdi)
movw $0x501, 0x3(%rdi)       # imm = 0x501
movl %ebp, %esi
callq 0x27028
movl 0x18(%rsp), %esi
subl 0x10(%rsp), %esi
leaq 0x68(%rsp), %rdi
callq 0x27082
leaq 0x68(%rsp), %rsi
movq 0x20(%rsi), %rax
movaps 0x10(%rsp), %xmm0
movq 0x20(%rsp), %rcx
movq %rcx, 0x20(%rsi)
movups 0x10(%rsi), %xmm1
movups %xmm0, 0x10(%rsi)
movaps %xmm1, 0x10(%rsp)
movq %rax, 0x20(%rsp)
movq %r13, %rdi
callq 0x270ec
movq 0x78(%rsp), %rdi
testq %rdi, %rdi
je 0x26094
movq 0x88(%rsp), %rsi
subq %rdi, %rsi
callq 0xb600
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x260ab
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
callq 0xb600
leaq 0x50(%rsp), %rdi
callq 0x19a48
addq $0x168, %rsp            # imm = 0x168
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x2611a
movq %rax, %rbx
jmp 0x26176
movq %rax, %rbx
movl $0xc0, %r14d
leaq (%rsp,%r14), %rdi
addq $0x68, %rdi
callq 0x19ad0
addq $-0x40, %r14
cmpq $-0x40, %r14
jne 0x260da
jmp 0x26180
movq %rax, %rbx
jmp 0x26180
jmp 0x2611a
jmp 0x2611a
movq %rax, %rbx
addq $-0x40, %r13
movq %r13, %rdi
callq 0x19ad0
cmpq %r12, %r13
jne 0x26105
jmp 0x26180
jmp 0x2611a
movq %rax, %rbx
movq 0x78(%rsp), %rdi
testq %rdi, %rdi
je 0x26137
movq 0x88(%rsp), %rsi
subq %rdi, %rsi
callq 0xb600
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x26176
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
jmp 0x26171
jmp 0x2614d
movq %rax, %rbx
jmp 0x2615f
movq %rax, %rbx
leaq 0x68(%rsp), %rdi
callq 0x19ad0
movq 0x10(%rsp), %rdi
cmpq %rbp, %rdi
je 0x26176
movq 0x20(%rsp), %rsi
incq %rsi
callq 0xb600
leaq 0x50(%rsp), %rdi
callq 0x19a48
movq %rbx, %rdi
callq 0xbbb0