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

    for (uint32_t index = 0; index < num_keys; index++) {
        DataHeader header;
        memcpy(&header, &dataBuf_[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, &dataBuf_[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, &dataBuf_[key_offset], key_len);
                key[key_len] = '\0';
                KVSlice *slice = new KVSlice(&digest, key, key_len, data, data_len);

                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, 0x48(%rsp)
callq 0x166a0
movq %rax, %rcx
movq 0x48(%rsp), %rax
movl %ecx, 0xf4(%rsp)
movq (%rax), %rdi
callq 0x238c0
movl %eax, 0xf0(%rsp)
movl $0x0, 0xec(%rsp)
movl 0xec(%rsp), %eax
cmpl 0x104(%rsp), %eax
jae 0x233ed
leaq 0xcc(%rsp), %rdi
callq 0x24d50
movq 0x48(%rsp), %rax
movq 0x40(%rax), %rax
movl 0xf4(%rsp), %ecx
addq %rcx, %rax
movq %rax, 0x38(%rsp)
callq 0x238d0
movq %rax, 0x40(%rsp)
jmp 0x23066
movq 0x40(%rsp), %rdx
movq 0x38(%rsp), %rsi
leaq 0xcc(%rsp), %rdi
callq 0x8410
movzwl 0xf0(%rsp), %esi
movq 0xf8(%rsp), %rdx
movl 0xf4(%rsp), %eax
addq %rax, %rdx
leaq 0xb2(%rsp), %rdi
callq 0x238e0
jmp 0x230a6
xorl %eax, %eax
movl %eax, %ecx
leaq 0x98(%rsp), %rdi
leaq 0xcc(%rsp), %rsi
leaq 0xb2(%rsp), %rdx
callq 0x25210
jmp 0x230c9
movq 0x48(%rsp), %rax
movq 0x8(%rax), %rdi
leaq 0x98(%rsp), %rsi
callq 0x26610
movb %al, 0x37(%rsp)
jmp 0x230e5
movb 0x37(%rsp), %al
testb $0x1, %al
jne 0x230f2
jmp 0x23369
leaq 0x84(%rsp), %rdi
leaq 0xcc(%rsp), %rsi
callq 0x23910
jmp 0x23109
leaq 0xcc(%rsp), %rdi
callq 0x195b0
movw %ax, 0x34(%rsp)
jmp 0x2311d
movw 0x34(%rsp), %ax
movw %ax, 0x82(%rsp)
movzwl 0x82(%rsp), %eax
cmpl $0x0, %eax
je 0x2334b
leaq 0xcc(%rsp), %rdi
callq 0x23940
movl %eax, 0x30(%rsp)
jmp 0x2314e
movl 0x30(%rsp), %eax
movl %eax, 0x7c(%rsp)
movzwl 0x82(%rsp), %eax
movl %eax, %edi
callq 0x8040
movq %rax, 0x28(%rsp)
jmp 0x2316c
movq 0x48(%rsp), %rax
movq 0x28(%rsp), %rcx
movq %rcx, 0x70(%rsp)
movq 0x70(%rsp), %rdi
movq 0x40(%rax), %rsi
movl 0x7c(%rsp), %eax
addq %rax, %rsi
movzwl 0x82(%rsp), %eax
movl %eax, %edx
callq 0x8410
leaq 0xcc(%rsp), %rdi
callq 0x195d0
movw %ax, 0x26(%rsp)
jmp 0x231ae
movw 0x26(%rsp), %ax
movw %ax, 0x6e(%rsp)
leaq 0xcc(%rsp), %rdi
callq 0x23950
movl %eax, 0x20(%rsp)
jmp 0x231cb
movl 0x20(%rsp), %eax
movl %eax, 0x68(%rsp)
movzwl 0x82(%rsp), %ecx
movl $0x1000, %eax           # imm = 0x1000
cmpl %ecx, %eax
jne 0x23259
movl 0x68(%rsp), %eax
movzwl 0x6e(%rsp), %ecx
subl %ecx, %eax
movl %eax, 0x64(%rsp)
jmp 0x23272
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xc0(%rsp)
movl %eax, 0xbc(%rsp)
jmp 0x233de
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xc0(%rsp)
movl %eax, 0xbc(%rsp)
jmp 0x233d1
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xc0(%rsp)
movl %eax, 0xbc(%rsp)
jmp 0x233c4
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xc0(%rsp)
movl %eax, 0xbc(%rsp)
jmp 0x2335a
movl 0x68(%rsp), %eax
movzwl 0x82(%rsp), %ecx
subl %ecx, %eax
movzwl 0x6e(%rsp), %ecx
subl %ecx, %eax
movl %eax, 0x64(%rsp)
movzwl 0x6e(%rsp), %eax
incl %eax
movl %eax, %edi
testq %rdi, %rdi
movq $-0x1, %rax
cmovsq %rax, %rdi
callq 0x8040
movq %rax, 0x18(%rsp)
jmp 0x23295
movq 0x48(%rsp), %rax
movq 0x18(%rsp), %rcx
movq %rcx, 0x58(%rsp)
movq 0x58(%rsp), %rdi
movq 0x40(%rax), %rsi
movl 0x64(%rsp), %eax
addq %rax, %rsi
movzwl 0x6e(%rsp), %eax
movl %eax, %edx
callq 0x8410
movq 0x58(%rsp), %rax
movzwl 0x6e(%rsp), %ecx
movb $0x0, (%rax,%rcx)
movl $0x40, %edi
callq 0x84f0
movq %rax, 0x10(%rsp)
jmp 0x232df
movq 0x10(%rsp), %rdi
movq 0x58(%rsp), %rdx
movzwl 0x6e(%rsp), %ecx
movq 0x70(%rsp), %r8
movzwl 0x82(%rsp), %r9d
leaq 0x84(%rsp), %rsi
callq 0x410c0
jmp 0x2330b
movq 0x10(%rsp), %rax
movq %rax, 0x50(%rsp)
movq 0x108(%rsp), %rdi
leaq 0x50(%rsp), %rsi
callq 0x235f0
jmp 0x23329
jmp 0x2334b
movq 0x10(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xc0(%rsp)
movl %eax, 0xbc(%rsp)
callq 0x84b0
jmp 0x2335a
leaq 0x84(%rsp), %rdi
callq 0x29d50
jmp 0x23369
leaq 0x84(%rsp), %rdi
callq 0x29d50
jmp 0x233c4
leaq 0xcc(%rsp), %rdi
callq 0x23950
movl %eax, 0xc(%rsp)
jmp 0x2337c
movl 0xc(%rsp), %eax
movl %eax, 0xf4(%rsp)
leaq 0x98(%rsp), %rdi
callq 0x253b0
leaq 0xb2(%rsp), %rdi
callq 0x24e40
leaq 0xcc(%rsp), %rdi
callq 0x24e00
movl 0xec(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xec(%rsp)
jmp 0x23021
leaq 0x98(%rsp), %rdi
callq 0x253b0
leaq 0xb2(%rsp), %rdi
callq 0x24e40
leaq 0xcc(%rsp), %rdi
callq 0x24e00
jmp 0x233f5
addq $0x118, %rsp            # imm = 0x118
retq
movq 0xc0(%rsp), %rdi
callq 0x8820
nopw %cs:(%rax,%rax)