// File: /workspace/github_cpp_makefile_star2_O1/eva23511[P]OS-Virtual-Memory-Management/pager.cpp
// Function: AgingPager::SelectVictimFrame(std::vector<Frame*, std::allocator<Frame*>>, std::vector<Process*, std::allocator<Process*>>, unsigned long)
Frame * AgingPager::SelectVictimFrame(vector<Frame *> frame_table, vector<Process *> processes, unsigned long timestamp) {
    hand++;
    if (hand >= frame_table.size()) {
        hand = 0;
    }
    
    if (a_option) {
        printf("ASELECT %d-%lu |", hand, (hand + frame_table.size() - 1) % frame_table.size());
    }
    
    int scan_from = hand;
    unsigned int min_age = UINT32_MAX;
    int victim_frame = hand;
    
    do {
        age_table[hand] = age_table[hand] >> 1;
        if (processes[frame_table[hand]->pid]->page_table[frame_table[hand]->vpage]->referenced) {
            age_table[hand] = (age_table[hand] | 0x80000000);
            processes[frame_table[hand]->pid]->page_table[frame_table[hand]->vpage]->referenced = 0;
        }
        
        if (age_table[hand] < min_age) {
            min_age = age_table[hand];
            victim_frame = hand;
        }
        
        if (a_option) {
            printf(" %d:%x", hand, age_table[hand]);
        }
        
        hand++;
        if (hand >= frame_table.size()) {
            hand = 0;
        }
    } while (hand != scan_from);
    
    hand = victim_frame;
    age_table[hand] = 0;
    if (a_option) {
        printf(" | %d\n", hand);
    }
    return frame_table[hand];
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rdx, -0x30(%rbp)
movq %rsi, %rbx
movq %rdi, %r14
movslq 0x8(%rdi), %rax
incq %rax
movl %eax, 0x8(%rdi)
movq 0x8(%rsi), %rcx
subq (%rsi), %rcx
sarq $0x3, %rcx
cmpq %rax, %rcx
ja 0x4b06
movl $0x0, 0x8(%r14)
cmpl $0x0, 0xc(%r14)
je 0x4b2b
movslq 0x8(%r14), %rsi
leaq (%rcx,%rsi), %rax
decq %rax
xorl %edx, %edx
divq %rcx
leaq 0x16f9(%rip), %rdi      # 0x621d
xorl %eax, %eax
callq 0x2030
movl 0x8(%r14), %r15d
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
movl %r15d, %r13d
movslq 0x8(%r14), %rcx
movq 0x10(%r14), %rax
shrl (%rax,%rcx,4)
movslq 0x8(%r14), %rdx
movq (%rbx), %rcx
movq (%rcx,%rdx,8), %rdi
movslq 0x4(%rdi), %r8
movq -0x30(%rbp), %rsi
movq (%rsi), %rsi
movq (%rsi,%r8,8), %r8
movslq 0x8(%rdi), %rdi
movq 0x20(%r8), %r8
movq (%r8,%rdi,8), %rdi
testb $0x2, (%rdi)
je 0x4b92
orb $-0x80, 0x3(%rax,%rdx,4)
movslq 0x8(%r14), %rax
movq (%rcx,%rax,8), %rax
movslq 0x4(%rax), %rcx
movq (%rsi,%rcx,8), %rcx
movslq 0x8(%rax), %rax
movq 0x20(%rcx), %rcx
movq (%rcx,%rax,8), %rax
andl $-0x3, (%rax)
movslq 0x8(%r14), %rsi
movq 0x10(%r14), %rax
movl (%rax,%rsi,4), %edx
cmpl %r12d, %edx
cmovbl %edx, %r12d
cmovbl %esi, %r13d
cmpl $0x0, 0xc(%r14)
je 0x4bbd
leaq 0x1678(%rip), %rdi      # 0x622e
xorl %eax, %eax
callq 0x2030
movslq 0x8(%r14), %rax
incq %rax
movl %eax, 0x8(%r14)
movq 0x8(%rbx), %rcx
subq (%rbx), %rcx
sarq $0x3, %rcx
cmpq %rax, %rcx
ja 0x4be0
movl $0x0, 0x8(%r14)
cmpl %r15d, 0x8(%r14)
jne 0x4b38
movl %r13d, 0x8(%r14)
movslq %r13d, %rax
movq 0x10(%r14), %rcx
movl $0x0, (%rcx,%rax,4)
cmpl $0x0, 0xc(%r14)
je 0x4c15
movl 0x8(%r14), %esi
leaq 0x1627(%rip), %rdi      # 0x6235
xorl %eax, %eax
callq 0x2030
movslq 0x8(%r14), %rax
movq (%rbx), %rcx
movq (%rcx,%rax,8), %rax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop