// File: /workspace/github_cpp_makefile_star2_O3/yangwanyuan[P]HLKVDS/src/Segment.cc
// Function: hlkvds::SegBase::copyToDataBuf()
void SegBase::copyToDataBuf() {
    uint64_t offset = 0;
    vol_->CalcSegOffsetFromId(segId_, offset);

    uint32_t offset_begin = 0;
    uint32_t offset_end = segSize_;

    offset_begin += SegBase::SizeOfSegOnDisk();

    //aggregate iovec
    for (list<KVSlice *>::iterator iter = sliceList_.begin(); iter
            != sliceList_.end(); iter++) {
        KVSlice *slice = *iter;
        DataHeader *header =
                &slice->GetHashEntry().GetEntryOnDisk().GetDataHeader();
        char *data = (char *) slice->GetData();
        uint16_t data_len = slice->GetDataLen();

        char *key = (char *) slice->GetKey();
        uint16_t key_len = slice->GetKeyLen();
        memcpy(&(dataBuf_[offset_begin]), header,
               IndexManager::SizeOfDataHeader());
        offset_begin += IndexManager::SizeOfDataHeader();
        memcpy(&(dataBuf_[offset_begin]), key, key_len);
        offset_begin += key_len;

        if (slice->IsAlignedData()) {
            offset_end -= data_len;
            memcpy(&(dataBuf_[offset_end]), data, data_len);
            __DEBUG("write key = %s, data position: %u", slice->GetKey(), offset_end);
        } else {
            memcpy(&(dataBuf_[offset_begin]), data, data_len);
            __DEBUG("write key = %s, data position: %u", slice->GetKey(), offset_begin);
            offset_begin += data_len;
        }
    }

    // Generate SegHeaderOnDisk
    uint64_t timestamp = KVTime::GetNow();
    uint64_t trx_id = 0;
    uint32_t trx_segs = 0;
    uint32_t checksum_data = 0;
    uint32_t checksum_length = 0;
    SegHeaderOnDisk seg_header(timestamp, trx_id, trx_segs, checksum_data, checksum_length, keyNum_);
    memcpy(dataBuf_, &seg_header, SegBase::SizeOfSegOnDisk());

    //set 0 to free data buffer
    memset(&(dataBuf_[offset_begin]), 0, (offset_end - offset_begin));
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rdi, %r12
movl 0x10(%rdi), %eax
movq %rax, 0x8(%rsp)
leaq 0x28(%rdi), %rax
movq 0x28(%rdi), %rbx
movq %rax, 0x20(%rsp)
movl $0x20, %ebp
cmpq %rax, %rbx
je 0x1c2a5
movq %r12, 0x18(%rsp)
movq 0x10(%rbx), %r14
movq 0x28(%r14), %rax
movq (%rax), %rax
movq (%r14), %rsi
movq 0x10(%r14), %rcx
movq %rcx, 0x10(%rsp)
movzwl 0x18(%r14), %r15d
movzwl 0x8(%r14), %r13d
movq 0x40(%r12), %rcx
movl %ebp, %edx
movups (%rax), %xmm0
movups 0x10(%rax), %xmm1
movups %xmm1, 0x10(%rcx,%rdx)
movups %xmm0, (%rcx,%rdx)
leal 0x20(%rbp), %edi
addq 0x40(%r12), %rdi
movq %r13, %rdx
callq 0x6380
addl %r13d, %ebp
addl $0x20, %ebp
cmpw $0x1000, 0x18(%r14)     # imm = 0x1000
jne 0x1c278
movq 0x8(%rsp), %rax
subl %r15d, %eax
movq 0x40(%r12), %rdi
movq %rax, 0x8(%rsp)
addq %rax, %rdi
movq 0x10(%rsp), %rsi
movq %r15, %rdx
callq 0x6380
jmp 0x1c292
movl %ebp, %edi
addq 0x40(%r12), %rdi
movq 0x10(%rsp), %rsi
movq %r15, %rdx
callq 0x6380
addl %ebp, %r15d
movl %r15d, %ebp
movq (%rbx), %rbx
cmpq 0x20(%rsp), %rbx
movq 0x18(%rsp), %r12
jne 0x1c1fd
callq 0x20948
movl 0x1c(%r12), %ecx
movl %ecx, (%rsp)
leaq 0x28(%rsp), %r15
movq %r15, %rdi
movq %rax, %rsi
xorl %edx, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0x1b770
movq 0x40(%r12), %rax
movups (%r15), %xmm0
movups 0x10(%r15), %xmm1
movups %xmm1, 0x10(%rax)
movups %xmm0, (%rax)
movl %ebp, %edi
addq 0x40(%r12), %rdi
movq 0x8(%rsp), %rdx
subl %ebp, %edx
xorl %esi, %esi
callq 0x6250
movq %r15, %rdi
callq 0x1b74c
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop