// File: /workspace/github_cpp_makefile_star2_O1/yangwanyuan[P]HLKVDS/src/Migrate.cc
// Function: hlkvds::Migrate::doMigrate(unsigned int, unsigned int)
uint32_t Migrate::doMigrate(uint32_t mt_vol_id, uint32_t max_seg_num) {
    Volume *ft_vol = ft_->GetVolume();
    Volume *mt_vol = mt_->GetVolume(mt_vol_id);

    std::multimap < uint32_t, uint32_t > cands_map;
    ft_vol->SortSegsByTS(cands_map, max_seg_num);

    //do merge
    bool ret = false;
    std::vector <uint32_t> free_seg_vec;

    std::list<KVSlice*> slice_list;
    std::list<KVSlice*> recycle_list;

    SegForMigrate *mig_seg = new SegForMigrate(mt_vol, idxMgr_);
    for (std::multimap<uint32_t, uint32_t>::iterator iter = cands_map.begin(); iter
            != cands_map.end(); ++iter) {
        uint32_t seg_id = iter->second;

        ret = loadKvList(seg_id, slice_list);
        if (!ret) {
            continue;
        }

        if (mig_seg->TryPutList(slice_list)) {
            mig_seg->PutList(slice_list);
            free_seg_vec.push_back(seg_id);
            recycle_list.splice(recycle_list.end(), slice_list);
        }
        else {
            recycle_list.splice(recycle_list.end(), slice_list);
            break;
        }
    }
    
    //Allocate segment id from volume
    uint32_t mig_seg_id;
    if (!mt_vol->Alloc(mig_seg_id)) {
        __WARN("The Medium Tier Volume[%d] is Full, can not allocate segment", mig_seg_id);
        delete mig_seg;
        cleanKvList(recycle_list);
        return 0;
    }
    mig_seg->SetSegId(mig_seg_id);

    //Write to volume device
    ret = mig_seg->WriteSegToDevice();
    if (!ret) {
        __ERROR("Write First GC segment to device failed, free 0 segments");
        delete mig_seg;
        mt_vol->FreeForFailed(mig_seg_id);
        cleanKvList(recycle_list);
        return 0;
    }

    uint32_t free_size = mig_seg->GetFreeSize();
    mt_vol->Use(mig_seg_id, free_size);

    mig_seg->UpdateToIndex();


    //clean work for first segment
    for (std::vector<uint32_t>::iterator iter = free_seg_vec.begin(); iter
            != free_seg_vec.end(); ++iter) {
        ft_vol->FreeForGC(*iter);
    }
    delete mig_seg;
    cleanKvList(recycle_list);

    uint32_t total_free = free_seg_vec.size();

    cands_map.clear();
    __DEBUG("Once doMigrate total free %d segments in Fast Tier!", total_free);
    return total_free;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x98, %rsp
movl %edx, %ebx
movq %rdi, %r13
movq (%rdi), %rax
movq 0x8(%rdi), %rdi
movq 0x1d8(%rax), %r12
callq 0x2057e
movq %rax, %r15
leaq 0x70(%rsp), %rcx
movl $0x0, (%rcx)
xorl %eax, %eax
movq %rax, 0x8(%rcx)
movq %rcx, 0x10(%rcx)
movq %rcx, 0x18(%rcx)
movq %rax, 0x20(%rcx)
leaq 0x68(%rsp), %rsi
movq %r12, %rdi
movl %ebx, %edx
callq 0x20da8
xorps %xmm0, %xmm0
movaps %xmm0, 0x10(%rsp)
xorl %eax, %eax
movq %rax, 0x20(%rsp)
leaq 0x50(%rsp), %rbx
movq %rbx, 0x8(%rbx)
movq %rbx, (%rbx)
movq %rax, 0x10(%rbx)
leaq 0x38(%rsp), %rcx
movq %rcx, 0x8(%rcx)
movq %rcx, (%rcx)
movq %rax, 0x10(%rcx)
movl $0x50, %edi
callq 0x6420
movq %rax, %r14
movq 0x10(%r13), %rdx
movq %rax, %rdi
movq %r15, 0x30(%rsp)
movq %r15, %rsi
callq 0x1ca80
movq 0x80(%rsp), %rbp
leaq 0x70(%rsp), %rax
cmpq %rax, %rbp
je 0x16d1a
movl 0x24(%rbp), %esi
movl %esi, 0xc(%rsp)
movq %r13, %rdi
movq %rbx, %rdx
callq 0x16f8c
movl $0x4, %r15d
testb %al, %al
je 0x16d04
movq %r14, %rdi
movq %rbx, %rsi
callq 0x1beb0
testb %al, %al
je 0x16cbb
movq %r14, %rdi
movq %rbx, %rsi
callq 0x1bee8
movq 0x18(%rsp), %rsi
cmpq 0x20(%rsp), %rsi
je 0x16cc8
movl 0xc(%rsp), %eax
movl %eax, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x18(%rsp)
jmp 0x16cd7
movq 0x50(%rsp), %rsi
movl $0x2, %r15d
jmp 0x16cdf
leaq 0x10(%rsp), %rdi
leaq 0xc(%rsp), %rdx
callq 0x10bd6
movq 0x50(%rsp), %rsi
xorl %r15d, %r15d
cmpq %rbx, %rsi
je 0x16d04
leaq 0x38(%rsp), %rdi
movq %rbx, %rdx
callq 0x6210
movq 0x60(%rsp), %rax
addq %rax, 0x48(%rsp)
movq $0x0, 0x60(%rsp)
testb $0x3, %r15b
jne 0x16d1a
movq %rbp, %rdi
callq 0x62e0
movq %rax, %rbp
jmp 0x16c56
leaq 0xc(%rsp), %rsi
movq 0x30(%rsp), %rdi
leaq 0x38(%rsp), %rbp
callq 0x20ce2
testb %al, %al
je 0x16e1c
movl 0xc(%rsp), %eax
movl %eax, (%r14)
movq %r14, %rdi
callq 0x1bf16
testb %al, %al
je 0x16e33
movl 0x18(%r14), %edx
subl 0x14(%r14), %edx
movl 0xc(%rsp), %esi
movq 0x30(%rsp), %rdi
callq 0x20dd0
movq %r14, %rdi
callq 0x1caba
movq 0x10(%rsp), %r15
cmpq 0x18(%rsp), %r15
je 0x16d88
movl (%r15), %esi
movq %r12, %rdi
callq 0x20dc6
addq $0x4, %r15
jmp 0x16d70
movq %r14, %rdi
callq 0x1bd18
movq %r14, %rdi
callq 0x6400
leaq 0x38(%rsp), %rsi
callq 0x17040
movq 0x18(%rsp), %r12
subq 0x10(%rsp), %r12
shrq $0x2, %r12
leaq 0x68(%rsp), %rdi
callq 0x10ba8
movq 0x38(%rsp), %rdi
cmpq %rbp, %rdi
je 0x16dd4
movq (%rdi), %r14
callq 0x6400
movq %r14, %rdi
cmpq %rbp, %r14
jne 0x16dc4
movq 0x50(%rsp), %rdi
cmpq %rbx, %rdi
je 0x16dee
movq (%rdi), %r14
callq 0x6400
movq %r14, %rdi
cmpq %rbx, %r14
jne 0x16dde
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x16dfd
callq 0x6400
leaq 0x68(%rsp), %rdi
callq 0x10b5e
movl %r12d, %eax
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xc(%rsp), %rdi
leaq 0x38(%rsp), %rdx
movq %r14, %rsi
callq 0x84be
xorl %r12d, %r12d
jmp 0x16dba
movq 0x1d19e(%rip), %r15     # 0x33fd8
movq (%r15), %r13
callq 0x63d0
leaq 0xb47c(%rip), %rsi      # 0x222c5
leaq 0xc6a0(%rip), %rcx      # 0x234f0
leaq 0xc6a3(%rip), %r8       # 0x234fa
xorl %r12d, %r12d
movq %r13, %rdi
movq %rax, %rdx
movl $0x5e, %r9d
xorl %eax, %eax
callq 0x6570
movq (%r15), %rcx
leaq 0xbb3a(%rip), %rdi      # 0x229b1
movl $0x38, %esi
movl $0x1, %edx
callq 0x6740
movq (%r15), %rsi
movl $0xa, %edi
callq 0x64d0
movq %r14, %rdi
callq 0x1bd18
movq %r14, %rdi
callq 0x6400
movl 0xc(%rsp), %esi
movq 0x30(%rsp), %rdi
callq 0x20dbc
leaq 0x38(%rsp), %rsi
callq 0x17040
jmp 0x16dba
movq %rax, %r12
movq %r14, %rdi
callq 0x6400
jmp 0x16edd
jmp 0x16eda
movq %rax, %r12
jmp 0x16f25
jmp 0x16eda
jmp 0x16eda
jmp 0x16eda
movq %rax, %r12
movq 0x38(%rsp), %rdi
leaq 0x38(%rsp), %r15
cmpq %r15, %rdi
je 0x16efc
movq (%rdi), %r14
callq 0x6400
movq %r14, %rdi
cmpq %r15, %r14
jne 0x16eec
movq 0x50(%rsp), %rdi
cmpq %rbx, %rdi
je 0x16f16
movq (%rdi), %r14
callq 0x6400
movq %r14, %rdi
cmpq %rbx, %r14
jne 0x16f06
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x16f25
callq 0x6400
leaq 0x68(%rsp), %rdi
callq 0x10b5e
movq %r12, %rdi
callq 0x6680
nop