// File: /workspace/github_cpp_makefile_star2_O2/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:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x98, %rsp
movq %rcx, %r14
movl %edx, %ebp
movq %rsi, 0x48(%rsp)
movq (%rdi), %rax
movzwl 0x28(%rax), %eax
movw %ax, 0xc(%rsp)
pushq $0x20
popq %rbx
leaq 0x6c(%rsp), %r13
movq %rcx, 0x50(%rsp)
movq %rdi, 0x18(%rsp)
subl $0x1, %ebp
jb 0xdfda
movq %rdi, %r15
leaq 0x20(%rsp), %r12
movq %r12, %rdi
callq 0xe33c
movq 0x40(%r15), %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 0xc(%rsp), %eax
movw %ax, 0xe(%rsp)
movq %rcx, 0x10(%rsp)
leaq 0x80(%rsp), %rbx
movq %rbx, %rdi
movq %r12, %rsi
leaq 0xe(%rsp), %rdx
xorl %ecx, %ecx
callq 0xe56a
movq 0x8(%r15), %rdi
movq %rbx, %rsi
callq 0xeee8
testb %al, %al
je 0xdfab
movq %r13, %rdi
leaq 0x20(%rsp), %rsi
callq 0x10244
movzwl 0x36(%rsp), %ebx
testl %ebx, %ebx
je 0xdfa3
movl 0x38(%rsp), %r13d
movl %ebx, %r12d
movq %r12, %rdi
callq 0x6040
movq %rax, %rcx
movq 0x18(%rsp), %rax
movq 0x40(%rax), %r15
addq %r15, %r13
movq %rcx, 0x58(%rsp)
movq %rcx, %rdi
movq %r13, %rsi
movq %r12, %rdx
callq 0x6370
movzwl 0x34(%rsp), %r13d
movl 0x3c(%rsp), %r14d
leaq 0x1(%r13), %rdi
callq 0x6040
movq %rax, %r12
cmpl $0x1000, %ebx           # imm = 0x1000
movl %ebx, %eax
movl $0x0, %ecx
cmovel %ecx, %eax
addl %r13d, %eax
subl %eax, %r14d
addq %r14, %r15
movl %r13d, %r14d
movq %r12, %rdi
movq %r15, %rsi
movq %r14, %rdx
callq 0x6370
movb $0x0, (%r12,%r14)
pushq $0x40
popq %rdi
callq 0x6410
movq 0x50(%rsp), %r14
movq %rax, %r15
movq %rax, %rdi
leaq 0x6c(%rsp), %rsi
movq %r12, %rdx
movl %r13d, %ecx
movq 0x58(%rsp), %r8
movl %ebx, %r9d
callq 0x185c2
movq %r15, 0x60(%rsp)
movq 0x48(%rsp), %rdi
leaq 0x60(%rsp), %rsi
callq 0xe040
leaq 0x6c(%rsp), %r13
movq %r13, %rdi
callq 0x10252
movl 0x3c(%rsp), %ebx
leaq 0x80(%rsp), %rdi
callq 0xe636
leaq 0xe(%rsp), %rdi
callq 0xe398
leaq 0x20(%rsp), %rdi
callq 0xe38c
movq 0x18(%rsp), %rdi
jmp 0xde5b
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
movq %r15, %rdi
callq 0x63f0
jmp 0xe002
jmp 0xdfff
jmp 0xdfff
jmp 0xe013
movq %rax, %rbx
leaq 0x6c(%rsp), %rdi
callq 0x10252
jmp 0xe016
movq %rax, %rbx
jmp 0xe023
movq %rax, %rbx
leaq 0x80(%rsp), %rdi
callq 0xe636
leaq 0xe(%rsp), %rdi
callq 0xe398
leaq 0x20(%rsp), %rdi
callq 0xe38c
movq %rbx, %rdi
callq 0x6680
nop