// File: /workspace/github_cpp_makefile_star2_O3/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 $0x88, %rsp
movq %rcx, 0x50(%rsp)
movq %rsi, 0x40(%rsp)
testl %edx, %edx
je 0x10c30
movl %edx, %ebp
movq %rdi, %r15
movq (%rdi), %rax
movzwl 0x28(%rax), %eax
movw %ax, 0x4(%rsp)
movl $0x20, %ebx
leaq 0x10(%rsp), %r12
leaq 0x70(%rsp), %r13
leaq 0x6(%rsp), %r14
movq %rdi, 0x38(%rsp)
movq %r12, %rdi
callq 0x10e40
movq 0x40(%r15), %rax
movl %ebx, %ecx
movups (%rax,%rcx), %xmm0
movups 0x10(%rax,%rcx), %xmm1
movaps %xmm1, 0x20(%rsp)
movaps %xmm0, 0x10(%rsp)
addq 0x50(%rsp), %rcx
movzwl 0x4(%rsp), %eax
movw %ax, 0x6(%rsp)
movq %rcx, 0x8(%rsp)
movq %r13, %rdi
movq %r12, %rsi
movq %r14, %rdx
xorl %ecx, %ecx
callq 0x110c0
movq 0x8(%r15), %rdi
movq %r13, %rsi
callq 0x11a44
testb %al, %al
je 0x10bfd
leaq 0x5c(%rsp), %rdi
leaq 0x10(%rsp), %rsi
callq 0x12cf2
movzwl 0x26(%rsp), %ebx
testl %ebx, %ebx
je 0x10bf3
movl 0x28(%rsp), %r13d
movl %ebx, %r12d
movq %r12, %rdi
callq 0x6040
movq 0x40(%r15), %r15
addq %r15, %r13
movq %rax, 0x48(%rsp)
movq %rax, %rdi
movq %r13, %rsi
movq %r12, %rdx
callq 0x6380
movzwl 0x24(%rsp), %r13d
movl 0x2c(%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 0x6380
movb $0x0, (%r12,%r14)
movl $0x40, %edi
callq 0x6410
movq %rax, %r15
movq %rax, %rdi
leaq 0x5c(%rsp), %rsi
movq %r12, %rdx
movl %r13d, %ecx
movq 0x48(%rsp), %r8
movl %ebx, %r9d
callq 0x1ba20
movl $0x18, %edi
callq 0x6410
movq %r15, 0x10(%rax)
movq %rax, %rdi
movq 0x40(%rsp), %rbx
movq %rbx, %rsi
callq 0x6080
incq 0x10(%rbx)
movq 0x38(%rsp), %r15
leaq 0x5c(%rsp), %rdi
callq 0x12d00
movl 0x2c(%rsp), %ebx
leaq 0x70(%rsp), %r13
movq %r13, %rdi
callq 0x1119a
leaq 0x6(%rsp), %r14
movq %r14, %rdi
callq 0x10ea2
leaq 0x10(%rsp), %r12
movq %r12, %rdi
callq 0x10e96
decl %ebp
jne 0x10abf
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
movq %r15, %rdi
callq 0x63f0
jmp 0x10c58
jmp 0x10c55
jmp 0x10c55
jmp 0x10c69
movq %rax, %rbx
leaq 0x5c(%rsp), %rdi
callq 0x12d00
jmp 0x10c6c
movq %rax, %rbx
jmp 0x10c76
movq %rax, %rbx
leaq 0x70(%rsp), %rdi
callq 0x1119a
leaq 0x6(%rsp), %rdi
callq 0x10ea2
leaq 0x10(%rsp), %rdi
callq 0x10e96
movq %rbx, %rdi
callq 0x6670