// File: yangwanyuan[P]HLKVDS/src/Migrate.cc
// Function: hlkvds::Migrate::loadSegKV(std::__cxx11::list<hlkvds::KVSlice*, std::allocator<hlkvds::KVSlice*>>&, unsigned int, unsigned long)
void Migrate::loadSegKV(list<KVSlice*> &slice_list, uint32_t num_keys,
                          uint64_t phy_offset) {
    uint32_t head_offset = SegBase::SizeOfSegOnDisk();

    Volume *ft_vol = ft_->GetVolume();
    int vol_id = ft_vol->GetId();

    for (uint32_t index = 0; index < num_keys; index++) {
        DataHeader header;
        memcpy(&header, &ftDataBuf_[head_offset],
               IndexManager::SizeOfDataHeader());

        DataHeaderAddress addrs(vol_id, phy_offset + (uint64_t)head_offset);
        HashEntry hash_entry(header, addrs, NULL);

        __DEBUG("load hash_entry from seg_offset = %ld, header_offset = %d", phy_offset, head_offset );

        if (idxMgr_->IsSameInMem(hash_entry)) {

            Kvdb_Digest digest = header.GetDigest();
            uint16_t data_len = header.GetDataSize();
            if (data_len != 0) {
                uint32_t data_offset = header.GetDataOffset();
                char* data = new char[data_len];
                memcpy(data, &ftDataBuf_[data_offset], data_len);

                //memcpy key to KVSlice.GetNextHeadOffsetGetNextHeadOffset
                uint16_t key_len =header.GetKeySize();
                uint32_t next_head_offset = header.GetNextHeadOffset();
                uint32_t key_offset;
                if (ALIGNED_SIZE == data_len) {
                    key_offset = next_head_offset - key_len;
                } else {
                    key_offset = next_head_offset - data_len - key_len;
                }
                char* key = new char[key_len+1];
                memcpy(key, &ftDataBuf_[key_offset], key_len);
                key[key_len] = '\0';
                KVSlice *slice = new KVSlice(&digest, key, key_len, data, data_len);
                slice->SetHashEntryBeforeGC(&hash_entry);

                slice_list.push_back(slice);
                __DEBUG("the slice key_digest = %s, value = %s, seg_offset = %ld, head_offset = %d is valid, need to write", digest.GetDigest(), data, phy_offset, head_offset);
            }
        }

        head_offset = header.GetNextHeadOffset();
    }

}

// Assembly:
subq $0x118, %rsp            # imm = 0x118
movq %rdi, 0x110(%rsp)
movq %rsi, 0x108(%rsp)
movl %edx, 0x104(%rsp)
movq %rcx, 0xf8(%rsp)
movq 0x110(%rsp), %rax
movq %rax, 0x40(%rsp)
callq 0x166a0
movq %rax, %rcx
movq 0x40(%rsp), %rax
movl %ecx, 0xf4(%rsp)
movq (%rax), %rdi
callq 0x37250
movq %rax, 0xe8(%rsp)
movq 0xe8(%rsp), %rdi
callq 0x238c0
movl %eax, 0xe4(%rsp)
movl $0x0, 0xe0(%rsp)
movl 0xe0(%rsp), %eax
cmpl 0x104(%rsp), %eax
jae 0x37185
leaq 0xc0(%rsp), %rdi
callq 0x24d50
movq 0x40(%rsp), %rax
movq 0x48(%rax), %rax
movl 0xf4(%rsp), %ecx
addq %rcx, %rax
movq %rax, 0x30(%rsp)
callq 0x238d0
movq %rax, 0x38(%rsp)
jmp 0x36e0b
movq 0x38(%rsp), %rdx
movq 0x30(%rsp), %rsi
leaq 0xc0(%rsp), %rdi
callq 0x8410
movzwl 0xe4(%rsp), %esi
movq 0xf8(%rsp), %rdx
movl 0xf4(%rsp), %eax
addq %rax, %rdx
leaq 0xaa(%rsp), %rdi
callq 0x238e0
jmp 0x36e4b
xorl %eax, %eax
movl %eax, %ecx
leaq 0x90(%rsp), %rdi
leaq 0xc0(%rsp), %rsi
leaq 0xaa(%rsp), %rdx
callq 0x25210
jmp 0x36e6e
movq 0x40(%rsp), %rax
movq 0x10(%rax), %rdi
leaq 0x90(%rsp), %rsi
callq 0x26610
movb %al, 0x2f(%rsp)
jmp 0x36e8a
movb 0x2f(%rsp), %al
testb $0x1, %al
jne 0x36e97
jmp 0x37101
leaq 0x7c(%rsp), %rdi
leaq 0xc0(%rsp), %rsi
callq 0x23910
jmp 0x36eab
leaq 0xc0(%rsp), %rdi
callq 0x195b0
movw %ax, 0x2c(%rsp)
jmp 0x36ebf
movw 0x2c(%rsp), %ax
movw %ax, 0x7a(%rsp)
movzwl 0x7a(%rsp), %eax
cmpl $0x0, %eax
je 0x370e9
leaq 0xc0(%rsp), %rdi
callq 0x23940
movl %eax, 0x28(%rsp)
jmp 0x36eea
movl 0x28(%rsp), %eax
movl %eax, 0x74(%rsp)
movzwl 0x7a(%rsp), %eax
movl %eax, %edi
callq 0x8040
movq %rax, 0x20(%rsp)
jmp 0x36f05
movq 0x40(%rsp), %rax
movq 0x20(%rsp), %rcx
movq %rcx, 0x68(%rsp)
movq 0x68(%rsp), %rdi
movq 0x48(%rax), %rsi
movl 0x74(%rsp), %eax
addq %rax, %rsi
movzwl 0x7a(%rsp), %eax
movl %eax, %edx
callq 0x8410
leaq 0xc0(%rsp), %rdi
callq 0x195d0
movw %ax, 0x1e(%rsp)
jmp 0x36f44
movw 0x1e(%rsp), %ax
movw %ax, 0x66(%rsp)
leaq 0xc0(%rsp), %rdi
callq 0x23950
movl %eax, 0x18(%rsp)
jmp 0x36f61
movl 0x18(%rsp), %eax
movl %eax, 0x60(%rsp)
movzwl 0x7a(%rsp), %ecx
movl $0x1000, %eax           # imm = 0x1000
cmpl %ecx, %eax
jne 0x36fec
movl 0x60(%rsp), %eax
movzwl 0x66(%rsp), %ecx
subl %ecx, %eax
movl %eax, 0x5c(%rsp)
jmp 0x37002
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xb8(%rsp)
movl %eax, 0xb4(%rsp)
jmp 0x37176
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xb8(%rsp)
movl %eax, 0xb4(%rsp)
jmp 0x37169
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xb8(%rsp)
movl %eax, 0xb4(%rsp)
jmp 0x3715c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xb8(%rsp)
movl %eax, 0xb4(%rsp)
jmp 0x370f5
movl 0x60(%rsp), %eax
movzwl 0x7a(%rsp), %ecx
subl %ecx, %eax
movzwl 0x66(%rsp), %ecx
subl %ecx, %eax
movl %eax, 0x5c(%rsp)
movzwl 0x66(%rsp), %eax
incl %eax
movl %eax, %edi
testq %rdi, %rdi
movq $-0x1, %rax
cmovsq %rax, %rdi
callq 0x8040
movq %rax, 0x10(%rsp)
jmp 0x37025
movq 0x40(%rsp), %rax
movq 0x10(%rsp), %rcx
movq %rcx, 0x50(%rsp)
movq 0x50(%rsp), %rdi
movq 0x48(%rax), %rsi
movl 0x5c(%rsp), %eax
addq %rax, %rsi
movzwl 0x66(%rsp), %eax
movl %eax, %edx
callq 0x8410
movq 0x50(%rsp), %rax
movzwl 0x66(%rsp), %ecx
movb $0x0, (%rax,%rcx)
movl $0x40, %edi
callq 0x84f0
movq %rax, 0x8(%rsp)
jmp 0x3706f
movq 0x8(%rsp), %rdi
movq 0x50(%rsp), %rdx
movzwl 0x66(%rsp), %ecx
movq 0x68(%rsp), %r8
movzwl 0x7a(%rsp), %r9d
leaq 0x7c(%rsp), %rsi
callq 0x410c0
jmp 0x37095
movq 0x8(%rsp), %rax
movq %rax, 0x48(%rsp)
movq 0x48(%rsp), %rdi
leaq 0x90(%rsp), %rsi
callq 0x41380
jmp 0x370b3
movq 0x108(%rsp), %rdi
leaq 0x48(%rsp), %rsi
callq 0x235f0
jmp 0x370c7
jmp 0x370e9
movq 0x8(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xb8(%rsp)
movl %eax, 0xb4(%rsp)
callq 0x84b0
jmp 0x370f5
leaq 0x7c(%rsp), %rdi
callq 0x29d50
jmp 0x37101
leaq 0x7c(%rsp), %rdi
callq 0x29d50
jmp 0x3715c
leaq 0xc0(%rsp), %rdi
callq 0x23950
movl %eax, 0x4(%rsp)
jmp 0x37114
movl 0x4(%rsp), %eax
movl %eax, 0xf4(%rsp)
leaq 0x90(%rsp), %rdi
callq 0x253b0
leaq 0xaa(%rsp), %rdi
callq 0x24e40
leaq 0xc0(%rsp), %rdi
callq 0x24e00
movl 0xe0(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xe0(%rsp)
jmp 0x36dc6
leaq 0x90(%rsp), %rdi
callq 0x253b0
leaq 0xaa(%rsp), %rdi
callq 0x24e40
leaq 0xc0(%rsp), %rdi
callq 0x24e00
jmp 0x3718d
addq $0x118, %rsp            # imm = 0x118
retq
movq 0xb8(%rsp), %rdi
callq 0x8820
nopw (%rax,%rax)