// File: /workspace/github_cpp_makefile_star2_O2/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
movl 0x10(%rdi), %eax
movq %rax, 0x8(%rsp)
leaq 0x28(%rdi), %r14
pushq $0x20
popq %rbx
movq %r14, 0x20(%rsp)
movq %rdi, 0x10(%rsp)
movq (%r14), %r14
movl %ebx, %r15d
cmpq 0x20(%rsp), %r14
je 0x18e1b
movq 0x10(%r14), %r13
movq 0x28(%r13), %rax
movq (%rax), %rax
movq (%r13), %rsi
movq 0x10(%r13), %rcx
movq %rcx, 0x18(%rsp)
movzwl 0x18(%r13), %r12d
movzwl 0x8(%r13), %ebp
movq 0x10(%rsp), %rdx
movq 0x40(%rdx), %rcx
movups (%rax), %xmm0
movups 0x10(%rax), %xmm1
movups %xmm1, 0x10(%rcx,%r15)
movups %xmm0, (%rcx,%r15)
movq %rdx, %r15
leal 0x20(%rbx), %edi
addq 0x40(%rdx), %rdi
movq %rbp, %rdx
callq 0x6370
addl %ebp, %ebx
addl $0x20, %ebx
cmpw $0x1000, 0x18(%r13)     # imm = 0x1000
je 0x18df5
movl %ebx, %edi
addq 0x40(%r15), %rdi
movq 0x18(%rsp), %rsi
movq %r12, %rdx
callq 0x6370
addl %ebx, %r12d
movl %r12d, %ebx
jmp 0x18d69
movq 0x8(%rsp), %rax
subl %r12d, %eax
movq 0x40(%r15), %rdi
movq %rax, 0x8(%rsp)
addq %rax, %rdi
movl %r12d, %edx
movq 0x18(%rsp), %rsi
callq 0x6370
jmp 0x18d69
callq 0x1daa8
movq 0x10(%rsp), %r14
movl 0x1c(%r14), %ecx
movl %ecx, (%rsp)
leaq 0x28(%rsp), %r12
movq %r12, %rdi
movq %rax, %rsi
xorl %edx, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0x1831c
movq 0x40(%r14), %rax
movups (%r12), %xmm0
movups 0x10(%r12), %xmm1
movups %xmm1, 0x10(%rax)
movups %xmm0, (%rax)
addq 0x40(%r14), %r15
movq 0x8(%rsp), %rdx
subl %ebx, %edx
movq %r15, %rdi
xorl %esi, %esi
callq 0x6250
movq %r12, %rdi
callq 0x182f8
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq