// File: kztomita[P]http-dump/hpack_decoder.cc
// Function: hpack::hpack_decoder::decode_representation(hpack::decoding_context&)
hpack_decoder::decoded_result hpack_decoder::decode_representation(decoding_context& context) {

  if (!buffer_view_.test_range(decode_pos_, 1)) {
    return decoded_result(std::nullopt, true);
  }

  int top_byte = buffer_view_.at(decode_pos_);

  if (top_byte & 0x80) {
    // 0b1...
    // Indexed Header Field Representation
    auto index = fetch_integer(7);
    return decoded_result(hpack_table_at(*context.dynamic_table, index),
                          false);
  } else if ((top_byte & 0xc0) == 0x40) {
    // 0b01...
    // Literal Header Field with Incremental Indexing
    auto index = fetch_integer(6);
    header_type header = fetch_header(context, index);
    context.dynamic_table->add(header);
    return decoded_result(std::move(header),
                          false);
  } else if ((top_byte & 0xf0) == 0x00) {
    // 0b0000...
    // Literal Header Field without Indexing
    auto index = fetch_integer(4);
    return decoded_result(fetch_header(context, index),
                          false);
  } else if ((top_byte & 0xf0) == 0x10) {
    // 0b0001...
    // Literal Header Field Never Indexed
    auto index = fetch_integer(4);
    return decoded_result(fetch_header(context, index),
                          false);
  } else if ((top_byte & 0xe0) == 0x20) {
    // 0b001...
    // Dynamic Table Size Update
    // このエントリはコマンドなのでヘッダはない
    auto max_size = fetch_integer(5);
    context.dynamic_table->set_maximum_table_size(max_size);
    return decoded_result(std::nullopt, false);
  }

  throw std::runtime_error("Unknown representation.");
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x190, %rsp            # imm = 0x190
movq %rdi, -0x168(%rbp)
movq %rdi, %rax
movq %rax, -0x160(%rbp)
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq -0x10(%rbp), %rdi
movq %rdi, -0x158(%rbp)
movq 0x10(%rdi), %rsi
movl $0x1, %edx
callq 0x61440
testb $0x1, %al
jne 0x62212
movq -0x168(%rbp), %rdi
leaq 0x137e4(%rip), %rsi     # 0x759e7
movl $0x1, %edx
callq 0x62e40
jmp 0x62526
movq -0x158(%rbp), %rdi
movq 0x10(%rdi), %rsi
callq 0x612f0
movzbl %al, %eax
movl %eax, -0x1c(%rbp)
movl -0x1c(%rbp), %eax
andl $0x80, %eax
cmpl $0x0, %eax
je 0x622a8
movq -0x158(%rbp), %rdi
movl $0x7, %esi
callq 0x62540
movl %eax, -0x20(%rbp)
movq -0x18(%rbp), %rdi
callq 0x5a5d0
movq %rax, %rsi
movslq -0x20(%rbp), %rdx
leaq -0x68(%rbp), %rdi
movq %rdi, -0x170(%rbp)
callq 0x64250
movq -0x168(%rbp), %rdi
movq -0x170(%rbp), %rsi
xorl %edx, %edx
callq 0x62e80
jmp 0x62280
leaq -0x68(%rbp), %rdi
callq 0x62e20
jmp 0x62526
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x70(%rbp)
movl %eax, -0x74(%rbp)
leaq -0x68(%rbp), %rdi
callq 0x62e20
jmp 0x62536
movl -0x1c(%rbp), %eax
andl $0xc0, %eax
cmpl $0x40, %eax
jne 0x6234e
movq -0x158(%rbp), %rdi
movl $0x6, %esi
callq 0x62540
movq -0x158(%rbp), %rsi
movl %eax, -0x78(%rbp)
movq -0x18(%rbp), %rdx
movslq -0x78(%rbp), %rcx
leaq -0xb8(%rbp), %rdi
movq %rdi, -0x178(%rbp)
callq 0x629e0
movq -0x18(%rbp), %rdi
callq 0x62ec0
movq -0x178(%rbp), %rsi
movq %rax, %rdi
callq 0x63fb0
jmp 0x62309
movq -0x168(%rbp), %rdi
leaq -0xb8(%rbp), %rsi
xorl %edx, %edx
callq 0x62ee0
jmp 0x62320
leaq -0xb8(%rbp), %rdi
callq 0x38f50
jmp 0x62526
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x70(%rbp)
movl %eax, -0x74(%rbp)
leaq -0xb8(%rbp), %rdi
callq 0x38f50
jmp 0x62536
movl -0x1c(%rbp), %eax
andl $0xf0, %eax
cmpl $0x0, %eax
jne 0x623e0
movq -0x158(%rbp), %rdi
movl $0x4, %esi
callq 0x62540
movq -0x158(%rbp), %rsi
movl %eax, -0xbc(%rbp)
movq -0x18(%rbp), %rdx
movslq -0xbc(%rbp), %rcx
leaq -0x100(%rbp), %rdi
movq %rdi, -0x180(%rbp)
callq 0x629e0
movq -0x168(%rbp), %rdi
movq -0x180(%rbp), %rsi
xorl %edx, %edx
callq 0x62ee0
jmp 0x623b2
leaq -0x100(%rbp), %rdi
callq 0x38f50
jmp 0x62526
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x70(%rbp)
movl %eax, -0x74(%rbp)
leaq -0x100(%rbp), %rdi
callq 0x38f50
jmp 0x62536
movl -0x1c(%rbp), %eax
andl $0xf0, %eax
cmpl $0x10, %eax
jne 0x62472
movq -0x158(%rbp), %rdi
movl $0x4, %esi
callq 0x62540
movq -0x158(%rbp), %rsi
movl %eax, -0x104(%rbp)
movq -0x18(%rbp), %rdx
movslq -0x104(%rbp), %rcx
leaq -0x148(%rbp), %rdi
movq %rdi, -0x188(%rbp)
callq 0x629e0
movq -0x168(%rbp), %rdi
movq -0x188(%rbp), %rsi
xorl %edx, %edx
callq 0x62ee0
jmp 0x62444
leaq -0x148(%rbp), %rdi
callq 0x38f50
jmp 0x62526
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x70(%rbp)
movl %eax, -0x74(%rbp)
leaq -0x148(%rbp), %rdi
callq 0x38f50
jmp 0x62536
movl -0x1c(%rbp), %eax
andl $0xe0, %eax
cmpl $0x20, %eax
jne 0x624c5
movq -0x158(%rbp), %rdi
movl $0x5, %esi
callq 0x62540
movl %eax, -0x14c(%rbp)
movq -0x18(%rbp), %rdi
callq 0x62ec0
movq %rax, %rdi
movslq -0x14c(%rbp), %rsi
callq 0x63e40
movq -0x168(%rbp), %rdi
leaq 0x1352b(%rip), %rsi     # 0x759e7
xorl %edx, %edx
callq 0x62e40
jmp 0x62526
jmp 0x624c7
jmp 0x624c9
jmp 0x624cb
jmp 0x624cd
movl $0x10, %edi
callq 0xd580
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0x190(%rbp)
leaq 0x134cb(%rip), %rsi     # 0x759b6
callq 0xd3b0
jmp 0x624f2
movq -0x190(%rbp), %rdi
movq 0x4aa90(%rip), %rsi     # 0xacf90
movq 0x4aa59(%rip), %rdx     # 0xacf60
callq 0xd970
movq -0x190(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x70(%rbp)
movl %eax, -0x74(%rbp)
callq 0xdc70
jmp 0x62536
movq -0x160(%rbp), %rax
addq $0x190, %rsp            # imm = 0x190
popq %rbp
retq
movq -0x70(%rbp), %rdi
callq 0xd990
nop