// File: yangwanyuan[P]HLKVDS/src/SegmentManager.cc
// Function: hlkvds::SegmentManager::SortSegsByTS(std::multimap<unsigned int, unsigned int, std::less<unsigned int>, std::allocator<std::pair<unsigned int const, unsigned int>>>&, unsigned int)
void SegmentManager::SortSegsByTS(std::multimap<uint32_t, uint32_t> &cand_map, uint32_t max_seg_num)
{
    std::unique_lock<std::mutex> lck(mtx_, std::defer_lock);
    uint32_t used_size;

    lck.lock();
    std::multimap<uint32_t, uint32_t> used_map;
    for (uint32_t index = 0; index < segNum_; index++) {
        if (segTable_[index].state == SegUseStat::USED) {
            used_size = segSize_ - (segTable_[index].free_size
                    + segTable_[index].death_size
                    + (uint32_t) SegBase::SizeOfSegOnDisk());
            used_map.insert(std::pair<uint32_t, uint32_t>(used_size, index));
        }
    }
    lck.unlock();

    for(std::multimap<uint32_t, uint32_t>::iterator iter = used_map.begin(); iter != used_map.end(); iter++) {
        if (max_seg_num == 0) {
            break;
        }
        cand_map.insert(*iter);
        max_seg_num --;
    }
}

// Assembly:
subq $0xe8, %rsp
movq %rdi, 0xe0(%rsp)
movq %rsi, 0xd8(%rsp)
movl %edx, 0xd4(%rsp)
movq 0xe0(%rsp), %rsi
movq %rsi, 0x30(%rsp)
addq $0x40, %rsi
leaq 0xc0(%rsp), %rdi
movq %rdi, 0x38(%rsp)
callq 0x16db0
movq 0x38(%rsp), %rdi
callq 0x16dd0
jmp 0x4773d
leaq 0x78(%rsp), %rdi
callq 0x234b0
movl $0x0, 0x74(%rsp)
movq 0x30(%rsp), %rcx
movl 0x74(%rsp), %eax
cmpl 0x20(%rcx), %eax
jae 0x47865
movq 0x30(%rsp), %rdi
movl 0x74(%rsp), %eax
movl %eax, %esi
callq 0x47aa0
cmpl $0x1, (%rax)
jne 0x47853
movq 0x30(%rsp), %rdi
movl 0x18(%rdi), %eax
movl %eax, 0x1c(%rsp)
movl 0x74(%rsp), %eax
movl %eax, %esi
callq 0x47aa0
movq 0x30(%rsp), %rdi
movl 0x4(%rax), %eax
movl %eax, 0x20(%rsp)
movl 0x74(%rsp), %eax
movl %eax, %esi
callq 0x47aa0
movq %rax, %rcx
movl 0x20(%rsp), %eax
movl 0x8(%rcx), %ecx
addl %ecx, %eax
movl %eax, 0x24(%rsp)
callq 0x166a0
movq %rax, 0x28(%rsp)
jmp 0x477c4
movl 0x1c(%rsp), %eax
movl 0x24(%rsp), %ecx
movq 0x28(%rsp), %rdx
addl %edx, %ecx
subl %ecx, %eax
movl %eax, 0xb8(%rsp)
leaq 0x6c(%rsp), %rdi
leaq 0xb8(%rsp), %rsi
leaq 0x74(%rsp), %rdx
callq 0x47b90
jmp 0x477f5
leaq 0x78(%rsp), %rdi
leaq 0x6c(%rsp), %rsi
callq 0x47b60
movq %rax, 0x10(%rsp)
jmp 0x4780b
movq 0x10(%rsp), %rax
movq %rax, 0x60(%rsp)
jmp 0x47853
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xb0(%rsp)
movl %eax, 0xac(%rsp)
jmp 0x4792c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xb0(%rsp)
movl %eax, 0xac(%rsp)
leaq 0x78(%rsp), %rdi
callq 0x234f0
jmp 0x4792c
jmp 0x47855
movl 0x74(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x74(%rsp)
jmp 0x4774f
leaq 0xc0(%rsp), %rdi
callq 0x16e30
jmp 0x47874
leaq 0x78(%rsp), %rdi
callq 0x23550
movq %rax, 0x58(%rsp)
leaq 0x78(%rsp), %rdi
callq 0x235a0
movq %rax, 0x50(%rsp)
leaq 0x58(%rsp), %rdi
leaq 0x50(%rsp), %rsi
callq 0x23580
testb $0x1, %al
jne 0x478a7
jmp 0x4790d
cmpl $0x0, 0xd4(%rsp)
jne 0x478b3
jmp 0x4790d
movq 0xd8(%rsp), %rax
movq %rax, (%rsp)
leaq 0x58(%rsp), %rdi
callq 0x47bf0
movq (%rsp), %rdi
movq %rax, %rsi
callq 0x47bc0
movq %rax, 0x8(%rsp)
jmp 0x478dc
movq 0x8(%rsp), %rax
movq %rax, 0x48(%rsp)
movl 0xd4(%rsp), %eax
addl $-0x1, %eax
movl %eax, 0xd4(%rsp)
leaq 0x58(%rsp), %rdi
xorl %esi, %esi
callq 0x47c10
movq %rax, 0x40(%rsp)
jmp 0x47883
leaq 0x78(%rsp), %rdi
callq 0x234f0
leaq 0xc0(%rsp), %rdi
callq 0x15c30
addq $0xe8, %rsp
retq
leaq 0xc0(%rsp), %rdi
callq 0x15c30
movq 0xb0(%rsp), %rdi
callq 0x8820
nopw %cs:(%rax,%rax)