// File: /workspace/github_cpp_makefile_star2_O3/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, %r14
movq %rdi, %rbx
movslq 0x8(%rdi), %r13
incq %r13
movl %r13d, 0x8(%rdi)
movq (%rsi), %rax
movq 0x8(%rsi), %rcx
subq %rax, %rcx
sarq $0x3, %rcx
cmpq %r13, %rcx
ja 0x4baa
movl $0x0, 0x8(%rbx)
xorl %r13d, %r13d
cmpl $0x0, 0xc(%rbx)
je 0x4bd4
movslq %r13d, %rsi
leaq (%rcx,%rsi), %rax
decq %rax
xorl %edx, %edx
divq %rcx
leaq 0x1657(%rip), %rdi      # 0x621d
xorl %eax, %eax
callq 0x2030
movl 0x8(%rbx), %r13d
movq (%r14), %rax
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
movl %r13d, %esi
movl %r13d, %r15d
movslq %esi, %rdx
movq 0x10(%rbx), %rcx
shrl (%rcx,%rdx,4)
movl 0x8(%rbx), %esi
movslq %esi, %rdx
movq (%rax,%rdx,8), %r8
movslq 0x4(%r8), %r9
movq -0x30(%rbp), %rdi
movq (%rdi), %rdi
movq (%rdi,%r9,8), %r9
movslq 0x8(%r8), %r8
movq 0x20(%r9), %r9
movq (%r9,%r8,8), %r8
testb $0x2, (%r8)
je 0x4c43
orb $-0x80, 0x3(%rcx,%rdx,4)
movslq 0x8(%rbx), %rcx
movq (%rax,%rcx,8), %rax
movslq 0x4(%rax), %rcx
movq (%rdi,%rcx,8), %rcx
movslq 0x8(%rax), %rax
movq 0x20(%rcx), %rcx
movq (%rcx,%rax,8), %rax
andl $-0x3, (%rax)
movl 0x8(%rbx), %esi
movq 0x10(%rbx), %rcx
movslq %esi, %rdx
movl (%rcx,%rdx,4), %edx
cmpl %r12d, %edx
cmovbl %edx, %r12d
cmovbl %esi, %r15d
cmpl $0x0, 0xc(%rbx)
je 0x4c68
leaq 0x15d0(%rip), %rdi      # 0x622e
xorl %eax, %eax
callq 0x2030
movl 0x8(%rbx), %esi
incl %esi
movl %esi, 0x8(%rbx)
movslq %esi, %rcx
movq (%r14), %rax
movq 0x8(%r14), %rdx
subq %rax, %rdx
sarq $0x3, %rdx
cmpq %rcx, %rdx
ja 0x4c8c
movl $0x0, 0x8(%rbx)
xorl %esi, %esi
cmpl %r13d, %esi
jne 0x4be0
movl %r15d, 0x8(%rbx)
movslq %r15d, %rcx
movq 0x10(%rbx), %rdx
movl $0x0, (%rdx,%rcx,4)
cmpl $0x0, 0xc(%rbx)
je 0x4cc1
movl 0x8(%rbx), %esi
leaq 0x157e(%rip), %rdi      # 0x6235
xorl %eax, %eax
callq 0x2030
movq (%r14), %rax
movslq 0x8(%rbx), %rcx
movq (%rax,%rcx,8), %rax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq