// File: /workspace/github_cpp_makefile_star2_O2/kztomita[P]http-dump/./socket.h
// Function: http_response read_response<sync_stream_wrapper>(sync_stream_wrapper&)
http_response read_response(SyncReadStream& socket) {
  namespace asio = boost::asio;

  std::string line;
  asio::streambuf receive_buffer;

  http_response r;

  // Status Line読み込み
  read_line(socket, receive_buffer, line);
  auto elements = string_util::split(line, " ");
  r.code_ = std::atoi(elements[1].c_str());

  // ヘッダー読み込み
  std::optional<std::string> transfer_encoding;
  std::optional<std::size_t> content_length;

  while (true) {
    if (!read_line(socket, receive_buffer, line)) {
      break;
    }
    if (line.size() == 0) {
      // ヘッダー終了
      break;
    }
    auto h = create_http_header_from_header_line(line);

    if (string_util::tolower(h->key) == "transfer-encoding") {
      transfer_encoding = h->value;
    } else if (string_util::tolower(h->key) == "content-length") {
      content_length = std::atoi(h->value.c_str());
    }

    r.headers_.push_back(std::move(h));
  }

  // payload読み込み

  if (!transfer_encoding ||
      transfer_encoding == "identity") {
    if (!content_length) {
      throw std::runtime_error("Content-Length not found.");
    }
    std::size_t length = content_length.value();

    fill_streambuf(socket, receive_buffer, length);
    const char* data = asio::buffer_cast<const char*>(receive_buffer.data());
    http_response::payload_type payload(data, data + length);
    receive_buffer.consume(length);

    using std::swap;
    swap(r.payload_, payload);
  } else if (transfer_encoding == "chunked") {
    http_response::payload_type payload;
    std::string line;
    while (read_line(socket, receive_buffer, line)) {
      if (line.size() == 0) {
        break;
      }
      std::size_t pos;
      std::size_t chunk_size = std::stoi(line.c_str(), &pos, 16);
      if (pos != line.size()) {
        throw std::runtime_error("Can't parse chunk size.");
      }
      if (chunk_size == 0) {
        // \r\n読み捨て
        std::string l;
        read_line(socket, receive_buffer, l);
        break;
      }

      fill_streambuf(socket, receive_buffer, chunk_size);
      const char* data = asio::buffer_cast<const char*>(receive_buffer.data());
      payload.resize(payload.size() + chunk_size);
      std::copy(data, data + chunk_size, payload.end() - chunk_size);
      receive_buffer.consume(chunk_size);

      // \r\n読み捨て
      fill_streambuf(socket, receive_buffer, 2);
      receive_buffer.consume(2);
    }

    using std::swap;
    swap(r.payload_, payload);
  } else {
    std::ostringstream msg;
    msg << "Unsupported Transfer-Encoding type ("
        << transfer_encoding.value()
        << ").";
    throw std::runtime_error(msg.str());
  }

  return r;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x2a8, %rsp            # imm = 0x2A8
movq %rsi, %r14
movq %rdi, %rbx
leaq 0x108(%rsp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movb $0x0, (%rax)
leaq 0x40(%rsp), %rdi
pushq $-0x1
popq %rsi
leaq 0x130(%rsp), %rdx
callq 0x1fb5e
xorps %xmm0, %xmm0
movups %xmm0, 0x28(%rbx)
movups %xmm0, 0x18(%rbx)
movq %rbx, 0x10(%rsp)
movups %xmm0, 0x8(%rbx)
leaq 0x40(%rsp), %rsi
leaq 0xf8(%rsp), %rdx
movq %r14, %rdi
callq 0x1fbf9
movq %r14, 0x18(%rsp)
leaq 0xf00e(%rip), %rdx      # 0x2d792
leaq 0x118(%rsp), %rdi
leaq 0xf8(%rsp), %rsi
callq 0x1fd32
movq 0x10(%rsp), %r14
leaq 0x8(%r14), %rbx
movq 0x118(%rsp), %rax
movq 0x20(%rax), %rdi
callq 0xba70
movl %eax, (%r14)
leaq 0xd0(%rsp), %rax
movb $0x0, 0x20(%rax)
xorl %r14d, %r14d
leaq 0xf8(%rsp), %rbp
leaq 0x20(%rsp), %r15
leaq 0x130(%rsp), %r12
movq 0x18(%rsp), %rdi
leaq 0x40(%rsp), %rsi
movq %rbp, %rdx
callq 0x1fbf9
cmpq $0x0, 0x100(%rsp)
setne %cl
testb %cl, %al
je 0x1e8ba
movq %r15, %rdi
movq %rbp, %rsi
callq 0x1df25
movq 0x20(%rsp), %rsi
movq %r12, %rdi
callq 0x1fe7b
movq %r12, %rdi
leaq 0xf320(%rip), %rsi      # 0x2db42
callq 0xd486
movl %eax, %r13d
movq %r12, %rdi
callq 0xb2f0
testb %r13b, %r13b
je 0x1e84f
movq 0x20(%rsp), %rsi
addq $0x20, %rsi
leaq 0xd0(%rsp), %rdi
callq 0x1fec2
jmp 0x1e8a2
movq 0x20(%rsp), %rsi
movq %r12, %rdi
callq 0x1fe7b
movq %r12, %rdi
leaq 0xf176(%rip), %rsi      # 0x2d9dc
callq 0xd486
movl %eax, %r13d
movq %r12, %rdi
callq 0xb2f0
testb %r13b, %r13b
je 0x1e8a2
movq 0x20(%rsp), %rax
movq 0x20(%rax), %rdi
callq 0xba70
testb $0x1, %r14b
movzbl %r14b, %r14d
pushq $0x1
popq %rcx
cmovel %ecx, %r14d
cltq
movq %rax, 0xc0(%rsp)
movq %rbx, %rdi
movq %r15, %rsi
callq 0x20b7a
movq %r15, %rdi
callq 0xd46c
jmp 0x1e7da
cmpb $0x1, 0xf0(%rsp)
jne 0x1e8e0
leaq 0xe646(%rip), %rsi      # 0x2cf11
leaq 0xd0(%rsp), %rdi
callq 0x1fee3
testb %al, %al
je 0x1e98e
testb $0x1, %r14b
je 0x1ec20
leaq 0x40(%rsp), %rsi
movq 0x18(%rsp), %rdi
movq 0xc0(%rsp), %rbx
movq %rbx, %rdx
callq 0x1fef0
movq 0x50(%rsp), %rsi
leaq (%rsi,%rbx), %rdx
leaq 0x130(%rsp), %rdi
leaq 0x20(%rsp), %rcx
callq 0x1ff02
movq 0x68(%rsp), %rax
cmpq %rax, 0x58(%rsp)
jae 0x1e93d
movq 0x88(%rsp), %rcx
movq %rcx, 0x48(%rsp)
movq %rax, 0x58(%rsp)
movq 0x50(%rsp), %rcx
leaq (%rcx,%rbx), %rdx
movl %eax, %esi
subl %ecx, %esi
cmpq %rax, %rdx
cmovbeq %rbx, %rsi
movslq %esi, %rax
addq %rcx, %rax
movq %rax, 0x50(%rsp)
movq 0x10(%rsp), %r14
movq 0x30(%r14), %rax
leaq 0x130(%rsp), %rdi
movaps (%rdi), %xmm0
movq 0x10(%rdi), %rcx
movq %rcx, 0x30(%r14)
movups 0x20(%r14), %xmm1
movups %xmm0, 0x20(%r14)
movaps %xmm1, (%rdi)
movq %rax, 0x10(%rdi)
jmp 0x1eba0
leaq 0xe59f(%rip), %rsi      # 0x2cf34
leaq 0xd0(%rsp), %rdi
callq 0x1ff6a
testb %al, %al
je 0x1ec55
xorps %xmm0, %xmm0
leaq 0xa0(%rsp), %rax
movaps %xmm0, (%rax)
andq $0x0, 0x10(%rax)
leaq 0x140(%rsp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movb $0x0, (%rax)
leaq 0x40(%rsp), %r12
leaq 0x20(%rsp), %rbp
pushq $0x2
popq %rbx
movq 0x18(%rsp), %r14
movq %r14, %rdi
movq %r12, %rsi
leaq 0x130(%rsp), %rdx
callq 0x1fbf9
cmpq $0x0, 0x138(%rsp)
setne %cl
testb %cl, %al
je 0x1eb63
movq 0x130(%rsp), %rsi
movq %rbp, %rdi
leaq 0xf(%rsp), %rdx
callq 0xd416
movq %rbp, %rdi
leaq 0xc8(%rsp), %rsi
pushq $0x10
popq %rdx
callq 0x12364
movl %eax, %r15d
movq %rbp, %rdi
callq 0xb2f0
movq 0xc8(%rsp), %rax
cmpq 0x138(%rsp), %rax
jne 0x1ebeb
testl %r15d, %r15d
je 0x1eb36
movslq %r15d, %r13
movq %r14, %rdi
movq %r12, %rsi
movq %r13, %rdx
callq 0x1fef0
movq 0x50(%rsp), %r15
movq 0xa8(%rsp), %rsi
addq %r13, %rsi
subq 0xa0(%rsp), %rsi
leaq 0xa0(%rsp), %rdi
callq 0x1ff78
leaq (%r15,%r13), %rsi
movq 0xa8(%rsp), %rdx
subq %r13, %rdx
movq %r15, %rdi
callq 0x210eb
movq 0x68(%rsp), %rax
cmpq %rax, 0x58(%rsp)
jae 0x1eac7
movq 0x88(%rsp), %rcx
movq %rcx, 0x48(%rsp)
movq %rax, 0x58(%rsp)
movq 0x50(%rsp), %rcx
leaq (%rcx,%r13), %rdx
movl %eax, %esi
subl %ecx, %esi
cmpq %rax, %rdx
cmovbeq %r13, %rsi
movslq %esi, %rax
addq %rcx, %rax
movq %rax, 0x50(%rsp)
movq %r14, %rdi
movq %r12, %rsi
movq %rbx, %rdx
callq 0x1fef0
movq 0x68(%rsp), %rax
cmpq %rax, 0x58(%rsp)
jae 0x1eb12
movq 0x88(%rsp), %rcx
movq %rcx, 0x48(%rsp)
movq %rax, 0x58(%rsp)
movq 0x50(%rsp), %rcx
leaq 0x2(%rcx), %rdx
movl %eax, %esi
subl %ecx, %esi
cmpq %rax, %rdx
movslq %esi, %rax
cmovbeq %rbx, %rax
addq %rcx, %rax
movq %rax, 0x50(%rsp)
jmp 0x1e9e3
leaq 0x30(%rsp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movb $0x0, (%rax)
leaq 0x40(%rsp), %rsi
leaq 0x20(%rsp), %rdx
movq %r14, %rdi
callq 0x1fbf9
leaq 0x20(%rsp), %rdi
callq 0xb2f0
movq 0x10(%rsp), %r14
movq 0x30(%r14), %rax
leaq 0xa0(%rsp), %rbx
movaps (%rbx), %xmm0
movq 0x10(%rbx), %rcx
movq %rcx, 0x30(%r14)
movups 0x20(%r14), %xmm1
movups %xmm0, 0x20(%r14)
movaps %xmm1, (%rbx)
movq %rax, 0x10(%rbx)
leaq 0x130(%rsp), %rdi
callq 0xb2f0
movq %rbx, %rdi
callq 0xf41c
leaq 0xd0(%rsp), %rdi
callq 0x210f4
leaq 0x118(%rsp), %rdi
callq 0x1643c
leaq 0x40(%rsp), %rdi
callq 0x1ffa4
leaq 0xf8(%rsp), %rdi
callq 0xb2f0
movq %r14, %rax
addq $0x2a8, %rsp            # imm = 0x2A8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
pushq $0x10
popq %rdi
callq 0xb330
movq %rax, %rbx
leaq 0xe33f(%rip), %rsi      # 0x2cf3c
movq %rax, %rdi
callq 0xb270
movq 0x273dc(%rip), %rsi     # 0x45fe8
movq 0x27335(%rip), %rdx     # 0x45f48
movq %rbx, %rdi
callq 0xbb50
jmp 0x1ece4
pushq $0x10
popq %rdi
callq 0xb330
movq %rax, %rbx
leaq 0xe2e8(%rip), %rsi      # 0x2cf1a
movq %rax, %rdi
callq 0xb270
movq 0x273a7(%rip), %rsi     # 0x45fe8
movq 0x27300(%rip), %rdx     # 0x45f48
movq %rbx, %rdi
callq 0xbb50
jmp 0x1ece4
leaq 0x130(%rsp), %rdi
callq 0xb8f0
leaq 0xe2eb(%rip), %rsi      # 0x2cf54
leaq 0x130(%rsp), %rdi
callq 0xb5e0
cmpb $0x0, 0xf0(%rsp)
je 0x1ece4
leaq 0xd0(%rsp), %rsi
movq %rax, %rdi
callq 0xb570
leaq 0xe2e2(%rip), %rsi      # 0x2cf79
movq %rax, %rdi
callq 0xb5e0
pushq $0x10
popq %rdi
callq 0xb330
movq %rax, %rbx
leaq 0x138(%rsp), %rsi
leaq 0x20(%rsp), %rdi
callq 0xbaa0
movb $0x1, %bpl
leaq 0x20(%rsp), %rsi
movq %rbx, %rdi
callq 0xbac0
xorl %ebp, %ebp
movq 0x27313(%rip), %rsi     # 0x45fe8
movq 0x2726c(%rip), %rdx     # 0x45f48
movq %rbx, %rdi
callq 0xbb50
callq 0x20d49
movq %rax, %r14
leaq 0x20(%rsp), %rdi
callq 0xb2f0
testb %bpl, %bpl
jne 0x1ed00
jmp 0x1ed0d
movq %rax, %r14
movq %rbx, %rdi
callq 0xb4d0
jmp 0x1ed0d
movq %rax, %r14
leaq 0x130(%rsp), %rdi
callq 0xb120
jmp 0x1eda2
jmp 0x1ed58
jmp 0x1ed90
jmp 0x1ed90
movq %rax, %r14
movq %rbx, %rdi
callq 0xb4d0
jmp 0x1eda2
jmp 0x1ed90
jmp 0x1ed90
jmp 0x1ed38
movq %rax, %r14
jmp 0x1edbc
movq %rax, %r14
jmp 0x1edd0
jmp 0x1ed6b
movq %rax, %r14
movq %rbx, %rdi
callq 0xb4d0
jmp 0x1ed6e
jmp 0x1ed6b
jmp 0x1ed6b
movq %rax, %r14
leaq 0x20(%rsp), %rdi
callq 0xb2f0
jmp 0x1ed6e
jmp 0x1ed6b
jmp 0x1ed6b
movq %rax, %r14
leaq 0x130(%rsp), %rdi
callq 0xb2f0
leaq 0xa0(%rsp), %rdi
callq 0xf41c
jmp 0x1eda2
jmp 0x1ed95
jmp 0x1ed90
jmp 0x1ed95
movq %rax, %r14
jmp 0x1eda2
movq %rax, %r14
leaq 0x20(%rsp), %rdi
callq 0xd46c
leaq 0xd0(%rsp), %rdi
callq 0x210f4
leaq 0x118(%rsp), %rdi
callq 0x1643c
movq 0x10(%rsp), %rdi
callq 0xd544
leaq 0x40(%rsp), %rdi
callq 0x1ffa4
leaq 0xf8(%rsp), %rdi
callq 0xb2f0
movq %r14, %rdi
callq 0xbba0
nop