// File: /workspace/github_cpp_makefile_star2_O2/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, %r14
movq %r8, %rbp
movq %rcx, %rbx
movq %rdx, %r15
movl %esi, 0x8(%rsp)
movq %rdi, 0x38(%rsp)
leaq 0xb6ed(%rip), %rsi      # 0x2d476
leaq 0xb6ee(%rip), %rdx      # 0x2d47e
leaq 0x58(%rsp), %r12
movq %r12, %rdi
callq 0x233b2
leaq 0x98(%rsp), %r13
leaq 0xb6d6(%rip), %rsi      # 0x2d482
leaq 0x93ae(%rip), %rdx      # 0x2b161
movq %r13, %rdi
callq 0x233f2
leaq 0xd8(%rsp), %r13
leaq 0xb6c0(%rip), %rsi      # 0x2d48a
movq %r13, %rdi
movq %rbx, %rdx
callq 0x23432
leaq 0x118(%rsp), %r13
leaq 0xb6ac(%rip), %rsi      # 0x2d490
movq %r13, %rdi
movq %r15, %rdx
callq 0x2346e
leaq 0x40(%rsp), %rdi
leaq 0x58(%rsp), %rsi
pushq $0x4
popq %rdx
leaq 0x10(%rsp), %rcx
callq 0x234aa
movl $0xc0, %ebx
leaq (%rsp,%rbx), %rdi
addq $0x58, %rdi
callq 0x17960
addq $-0x40, %rbx
cmpq $-0x40, %rbx
jne 0x21e0b
leaq 0x10(%rsp), %r12
leaq 0x58(%rsp), %r13
leaq 0x40(%rsp), %rbx
movq %rbp, %r15
movq (%r15), %r15
cmpq %rbp, %r15
je 0x21e78
movq 0x10(%r15), %rsi
movq %r12, %rdi
callq 0x1fe7b
movq 0x10(%r15), %rdx
addq $0x20, %rdx
movq %r13, %rdi
movq %r12, %rsi
callq 0x234e4
movq %rbx, %rdi
movq %r13, %rsi
callq 0x1a164
movq %r13, %rdi
callq 0x17960
movq %r12, %rdi
callq 0xb2f0
jmp 0x21e34
leaq 0x10(%rsp), %rdi
leaq 0xf(%rsp), %rsi
leaq 0x40(%rsp), %rdx
callq 0x2351a
movq 0x18(%rsp), %rax
subq 0x10(%rsp), %rax
cmpq %r14, %rax
movq 0x38(%rsp), %rbp
jbe 0x21f76
leaq 0x68(%rsp), %r15
xorps %xmm0, %xmm0
movups %xmm0, (%r15)
andq $0x0, 0x10(%r15)
movw $0x101, -0xd(%r15)      # imm = 0x101
leaq 0x58(%rsp), %rdi
movl 0x8(%rsp), %esi
callq 0x22e38
leaq 0x58(%rsp), %rdi
movl %r14d, %esi
callq 0x22e90
movq 0x10(%rsp), %rdi
leaq (%rdi,%r14), %rsi
movq %r15, %rdx
callq 0x2386c
leaq 0x58(%rsp), %rsi
movq %rbp, %rdi
callq 0x22ef8
movq %r15, %rdi
callq 0xf41c
leaq 0x58(%rsp), %r12
movq %r14, %rbx
movq 0x18(%rsp), %r13
subq 0x10(%rsp), %r13
subq %rbx, %r13
jbe 0x21fe7
cmpq %r14, %r13
jb 0x21f20
movq %r14, %r13
xorps %xmm0, %xmm0
movups %xmm0, (%r15)
andq $0x0, 0x10(%r15)
movw $0x409, 0x5b(%rsp)      # imm = 0x409
movq %r12, %rdi
movl 0x8(%rsp), %esi
callq 0x22e38
movq %r12, %rdi
movl %r13d, %esi
callq 0x22e90
movq 0x10(%rsp), %rdi
addq %rbx, %rdi
leaq (%rdi,%r13), %rsi
movq %r15, %rdx
callq 0x2386c
movq %rbp, %rdi
movq %r12, %rsi
callq 0x22ef8
addq %r13, %rbx
movq %r15, %rdi
callq 0xf41c
jmp 0x21f05
leaq 0x68(%rsp), %r14
xorps %xmm0, %xmm0
movups %xmm0, (%r14)
andq $0x0, 0x10(%r14)
movw $0x501, -0xd(%r14)      # imm = 0x501
leaq 0x58(%rsp), %rdi
movl 0x8(%rsp), %esi
callq 0x22e38
movl 0x18(%rsp), %esi
subl 0x10(%rsp), %esi
leaq 0x58(%rsp), %rdi
callq 0x22e90
leaq 0x58(%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 %rbp, %rdi
callq 0x22ef8
movq %r14, %rdi
callq 0xf41c
leaq 0x10(%rsp), %rdi
callq 0xf41c
leaq 0x40(%rsp), %rdi
callq 0x178fa
addq $0x158, %rsp            # imm = 0x158
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x2205a
jmp 0x22071
movq %rax, %rbx
movl $0xc0, %r14d
leaq (%rsp,%r14), %rdi
addq $0x58, %rdi
callq 0x17960
addq $-0x40, %r14
cmpq $-0x40, %r14
jne 0x2201a
jmp 0x2209c
movq %rax, %rbx
jmp 0x2209c
jmp 0x2205a
movq %rax, %rbx
movq %r14, %rdi
jmp 0x22060
movq %rax, %rbx
addq $-0x40, %r13
movq %r13, %rdi
callq 0x17960
cmpq %r12, %r13
jne 0x22045
jmp 0x2209c
jmp 0x2205a
movq %rax, %rbx
movq %r15, %rdi
callq 0xf41c
leaq 0x10(%rsp), %rdi
callq 0xf41c
jmp 0x22092
movq %rax, %rbx
jmp 0x22092
movq %rax, %rbx
jmp 0x22088
movq %rax, %rbx
leaq 0x58(%rsp), %rdi
callq 0x17960
leaq 0x10(%rsp), %rdi
callq 0xb2f0
leaq 0x40(%rsp), %rdi
callq 0x178fa
movq %rbx, %rdi
callq 0xbba0