// File: 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
subq $0xa0, %rsp
movq %rsi, -0x58(%rbp)
movq %rdi, %rax
movq -0x58(%rbp), %rdi
movq %rdi, -0x50(%rbp)
movq %rdx, -0x48(%rbp)
movq %rax, -0x8(%rbp)
movq %rdi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x40(%rbp)
movl 0x8(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x8(%rax)
movslq 0x8(%rax), %rax
movq %rax, -0x38(%rbp)
callq 0x7450
movq %rax, %rcx
movq -0x38(%rbp), %rax
cmpq %rcx, %rax
jb 0xb243
movq -0x40(%rbp), %rax
movl $0x0, 0x8(%rax)
movq -0x40(%rbp), %rax
cmpl $0x0, 0xc(%rax)
je 0xb2a0
movq -0x50(%rbp), %rdi
movq -0x40(%rbp), %rax
movl 0x8(%rax), %ecx
movl %ecx, -0x64(%rbp)
movslq 0x8(%rax), %rax
movq %rax, -0x70(%rbp)
callq 0x7450
movq -0x50(%rbp), %rdi
movq %rax, %rcx
movq -0x70(%rbp), %rax
addq %rcx, %rax
subq $0x1, %rax
movq %rax, -0x60(%rbp)
callq 0x7450
movl -0x64(%rbp), %esi
movq %rax, %rcx
movq -0x60(%rbp), %rax
xorl %edx, %edx
divq %rcx
leaq 0x305e(%rip), %rdi      # 0xe2f7
movb $0x0, %al
callq 0x3030
movq -0x40(%rbp), %rax
movl 0x8(%rax), %ecx
movl %ecx, -0x24(%rbp)
movl $0xffffffff, -0x28(%rbp) # imm = 0xFFFFFFFF
movl 0x8(%rax), %eax
movl %eax, -0x2c(%rbp)
movq -0x40(%rbp), %rax
movq %rax, %rdi
addq $0x10, %rdi
movslq 0x8(%rax), %rsi
callq 0xbb20
movq %rax, %rcx
movq -0x40(%rbp), %rax
movl (%rcx), %ecx
shrl %ecx
movl %ecx, -0x7c(%rbp)
movq %rax, %rdi
addq $0x10, %rdi
movslq 0x8(%rax), %rsi
callq 0xbb20
movl -0x7c(%rbp), %edx
movq -0x50(%rbp), %rdi
movq %rax, %rcx
movq -0x40(%rbp), %rax
movl %edx, (%rcx)
movslq 0x8(%rax), %rsi
callq 0x5510
movq -0x48(%rbp), %rdi
movq (%rax), %rax
movslq 0x4(%rax), %rsi
callq 0x5410
movq -0x50(%rbp), %rdi
movq %rax, %rcx
movq -0x40(%rbp), %rax
movq (%rcx), %rcx
addq $0x20, %rcx
movq %rcx, -0x78(%rbp)
movslq 0x8(%rax), %rsi
callq 0x5510
movq -0x78(%rbp), %rdi
movq (%rax), %rax
movslq 0x8(%rax), %rsi
callq 0x5430
movq (%rax), %rax
movl (%rax), %eax
shrl %eax
andl $0x1, %eax
cmpl $0x0, %eax
je 0xb3fb
movq -0x40(%rbp), %rax
movq %rax, %rdi
addq $0x10, %rdi
movslq 0x8(%rax), %rsi
callq 0xbb20
movq %rax, %rcx
movq -0x40(%rbp), %rax
movl (%rcx), %ecx
orl $0x80000000, %ecx       # imm = 0x80000000
movl %ecx, -0x8c(%rbp)
movq %rax, %rdi
addq $0x10, %rdi
movslq 0x8(%rax), %rsi
callq 0xbb20
movl -0x8c(%rbp), %edx
movq -0x50(%rbp), %rdi
movq %rax, %rcx
movq -0x40(%rbp), %rax
movl %edx, (%rcx)
movslq 0x8(%rax), %rsi
callq 0x5510
movq -0x48(%rbp), %rdi
movq (%rax), %rax
movslq 0x4(%rax), %rsi
callq 0x5410
movq -0x50(%rbp), %rdi
movq %rax, %rcx
movq -0x40(%rbp), %rax
movq (%rcx), %rcx
addq $0x20, %rcx
movq %rcx, -0x88(%rbp)
movslq 0x8(%rax), %rsi
callq 0x5510
movq -0x88(%rbp), %rdi
movq (%rax), %rax
movslq 0x8(%rax), %rsi
callq 0x5430
movq (%rax), %rax
movl (%rax), %ecx
andl $-0x3, %ecx
orl $0x0, %ecx
movl %ecx, (%rax)
movq -0x40(%rbp), %rax
movq %rax, %rdi
addq $0x10, %rdi
movslq 0x8(%rax), %rsi
callq 0xbb20
movl (%rax), %eax
cmpl -0x28(%rbp), %eax
jae 0xb43c
movq -0x40(%rbp), %rax
movq %rax, %rdi
addq $0x10, %rdi
movslq 0x8(%rax), %rsi
callq 0xbb20
movq %rax, %rcx
movq -0x40(%rbp), %rax
movl (%rcx), %ecx
movl %ecx, -0x28(%rbp)
movl 0x8(%rax), %eax
movl %eax, -0x2c(%rbp)
movq -0x40(%rbp), %rax
cmpl $0x0, 0xc(%rax)
je 0xb479
movq -0x40(%rbp), %rax
movl 0x8(%rax), %ecx
movl %ecx, -0x90(%rbp)
movq %rax, %rdi
addq $0x10, %rdi
movslq 0x8(%rax), %rsi
callq 0xbb20
movl -0x90(%rbp), %esi
movl (%rax), %edx
leaq 0x2e96(%rip), %rdi      # 0xe308
movb $0x0, %al
callq 0x3030
movq -0x50(%rbp), %rdi
movq -0x40(%rbp), %rax
movl 0x8(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x8(%rax)
movslq 0x8(%rax), %rax
movq %rax, -0x98(%rbp)
callq 0x7450
movq %rax, %rcx
movq -0x98(%rbp), %rax
cmpq %rcx, %rax
jb 0xb4b4
movq -0x40(%rbp), %rax
movl $0x0, 0x8(%rax)
jmp 0xb4b6
movq -0x40(%rbp), %rax
movl 0x8(%rax), %eax
cmpl -0x24(%rbp), %eax
jne 0xb2b7
movq -0x40(%rbp), %rax
movl -0x2c(%rbp), %ecx
movl %ecx, 0x8(%rax)
movq %rax, %rdi
addq $0x10, %rdi
movslq 0x8(%rax), %rsi
callq 0xbb20
movq %rax, %rcx
movq -0x40(%rbp), %rax
movl $0x0, (%rcx)
cmpl $0x0, 0xc(%rax)
je 0xb508
movq -0x40(%rbp), %rax
movl 0x8(%rax), %esi
leaq 0x2e0e(%rip), %rdi      # 0xe30f
movb $0x0, %al
callq 0x3030
movq -0x50(%rbp), %rdi
movq -0x40(%rbp), %rax
movslq 0x8(%rax), %rsi
callq 0x5510
movq (%rax), %rax
addq $0xa0, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)