// File: /workspace/github_cpp_makefile_star2_O1/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:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x98, %rsp
movq %rsi, 0x58(%rsp)
testl %edx, %edx
je 0x17285
movq %rcx, %r14
movq %rdi, %r12
movq (%rdi), %rax
movq 0x1d8(%rax), %rax
movzwl 0x28(%rax), %eax
movw %ax, 0xe(%rsp)
movl $0x20, %ebx
leaq 0x20(%rsp), %r15
leaq 0x80(%rsp), %r13
leaq 0x16(%rsp), %rbp
movq %rcx, 0x50(%rsp)
movq %rdi, 0x48(%rsp)
movl %edx, 0x10(%rsp)
movq %r15, %rdi
callq 0x10d0c
movq 0x48(%r12), %rax
movl %ebx, %ecx
movups (%rax,%rcx), %xmm0
movups 0x10(%rax,%rcx), %xmm1
movaps %xmm1, 0x30(%rsp)
movaps %xmm0, 0x20(%rsp)
addq %r14, %rcx
movzwl 0xe(%rsp), %eax
movw %ax, 0x16(%rsp)
movq %rcx, 0x18(%rsp)
movq %r13, %rdi
movq %r15, %rsi
movq %rbp, %rdx
xorl %ecx, %ecx
callq 0x10f8c
movq 0x10(%r12), %rdi
movq %r13, %rbp
movq %r13, %rsi
callq 0x11916
testb %al, %al
je 0x17250
leaq 0x6c(%rsp), %rdi
leaq 0x20(%rsp), %rsi
callq 0x12bce
movzwl 0x36(%rsp), %ebx
testl %ebx, %ebx
je 0x17246
movl 0x38(%rsp), %r13d
movl %ebx, %r15d
movq %r15, %rdi
callq 0x6040
movq 0x48(%r12), %r12
addq %r12, %r13
movq %rax, 0x60(%rsp)
movq %rax, %rdi
movq %r13, %rsi
movq %r15, %rdx
callq 0x6390
movzwl 0x34(%rsp), %r13d
movl 0x3c(%rsp), %r14d
leaq 0x1(%r13), %rdi
callq 0x6040
movq %rax, %r15
cmpl $0x1000, %ebx           # imm = 0x1000
movl %ebx, %eax
movl $0x0, %ecx
cmovel %ecx, %eax
addl %r13d, %eax
subl %eax, %r14d
addq %r14, %r12
movl %r13d, %r14d
movq %r15, %rdi
movq %r12, %rsi
movq %r14, %rdx
callq 0x6390
movb $0x0, (%r15,%r14)
movl $0x40, %edi
callq 0x6420
movq %rax, %r12
movq %rax, %rdi
leaq 0x6c(%rsp), %rsi
movq %r15, %rdx
movl %r13d, %ecx
movq 0x60(%rsp), %r8
movl %ebx, %r9d
callq 0x1b9f4
movq %rbp, %rsi
movq %r12, %rdi
callq 0x1baf2
movl $0x18, %edi
callq 0x6420
movq %r12, 0x10(%rax)
movq %rax, %rdi
movq 0x58(%rsp), %rbx
movq %rbx, %rsi
callq 0x6080
incq 0x10(%rbx)
movq 0x50(%rsp), %r14
movq 0x48(%rsp), %r12
leaq 0x6c(%rsp), %rdi
callq 0x12bdc
movl 0x3c(%rsp), %ebx
movq %rbp, %r13
movq %rbp, %rdi
callq 0x11066
leaq 0x16(%rsp), %rbp
movq %rbp, %rdi
callq 0x10d6e
leaq 0x20(%rsp), %r15
movq %r15, %rdi
callq 0x10d62
movl 0x10(%rsp), %edx
decl %edx
jne 0x170fa
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
movq %r12, %rdi
callq 0x6400
jmp 0x172ad
jmp 0x172aa
jmp 0x172aa
jmp 0x172be
movq %rax, %rbx
leaq 0x6c(%rsp), %rdi
callq 0x12bdc
jmp 0x172c1
movq %rax, %rbx
jmp 0x172ce
movq %rax, %rbx
leaq 0x80(%rsp), %rdi
callq 0x11066
leaq 0x16(%rsp), %rdi
callq 0x10d6e
leaq 0x20(%rsp), %rdi
callq 0x10d62
movq %rbx, %rdi
callq 0x6680
nop