// File: /workspace/github_cpp_makefile_star2_O3/eva23511[P]OS-Virtual-Memory-Management/pager.cpp
// Function: NruPager::SelectVictimFrame(std::vector<Frame*, std::allocator<Frame*>>, std::vector<Process*, std::allocator<Process*>>, unsigned long)
Frame * NruPager::SelectVictimFrame(vector<Frame *> frame_table, vector<Process *> processes, unsigned long timestamp) {
    hand++;
    if (hand >= frame_table.size()) {
        hand = 0;
    }
    
    int beginning_hand = hand; // hand at beginning of select function
    int reset_rbit = (timestamp - this->timestamp >= 50); // whether reference bit has to be reset
    int lowest_class = 4;
    int victim_frame = hand; // Victim frame selected
    int scanned_num = 0; // Number of frames scanned

    if (reset_rbit) {
        this->timestamp = timestamp;
    }

    do {
        int new_class = GetClass(processes[frame_table[hand]->pid]->page_table[frame_table[hand]->vpage]);

        if (new_class < lowest_class) {
            lowest_class = new_class;
            victim_frame = hand;
        }
        
        scanned_num++;
        
        if (lowest_class == 0 && !reset_rbit) {
            break;
        }
        
        if (reset_rbit) {
            processes[frame_table[hand]->pid]->page_table[frame_table[hand]->vpage]->referenced = 0;
        }
        
        hand++;
        if (hand >= frame_table.size()) {
            hand = 0;
        }
    } while (hand != beginning_hand);
    
    hand = victim_frame;
    if (a_option) {
        printf("ASELECT: %d %d | %d %d %d\n", beginning_hand, reset_rbit, lowest_class, hand, scanned_num);
    }
    return frame_table[hand];
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rdx, -0x40(%rbp)
movq %rsi, %rax
movq %rdi, %rbx
movslq 0x8(%rdi), %rsi
incq %rsi
movl %esi, 0x8(%rdi)
movq (%rax), %rdi
movq %rax, -0x38(%rbp)
movq 0x8(%rax), %rdx
movq %rdx, %rax
subq %rdi, %rax
sarq $0x3, %rax
cmpq %rsi, %rax
ja 0x49c8
movl $0x0, 0x8(%rbx)
xorl %esi, %esi
movq %rcx, %r11
subq 0x10(%rbx), %r11
xorl %r8d, %r8d
cmpq $0x32, %r11
setae %al
jb 0x49df
movq %rcx, 0x10(%rbx)
movb %al, %r8b
movl %r8d, -0x2c(%rbp)
movl $0x4, %ecx
movl $0x1, %r13d
movl %esi, %r12d
movl %esi, %r8d
movq %rbx, %r10
movl %ecx, %ebx
movl %r8d, %r9d
movslq %r12d, %r8
movq (%rdi,%r8,8), %rax
movslq 0x4(%rax), %rcx
movq -0x40(%rbp), %r14
movq (%r14), %r15
movq (%r15,%rcx,8), %rcx
movslq 0x8(%rax), %rax
movq 0x20(%rcx), %rcx
movq (%rcx,%rax,8), %rax
movl (%rax), %r14d
movl %r14d, %r15d
andl $0x2, %r15d
movl %r14d, %ecx
shrl $0x2, %ecx
andl $0x1, %ecx
orl %r15d, %ecx
cmpl %ebx, %ecx
cmovgel %r9d, %r8d
movl %r13d, %r9d
cmovgel %ebx, %ecx
cmpq $0x31, %r11
ja 0x4a4d
testl %ecx, %ecx
je 0x4a9b
cmpq $0x32, %r11
movq %r10, %rbx
jb 0x4a6c
andl $-0x3, %r14d
movl %r14d, (%rax)
movl 0x8(%rbx), %r12d
movq -0x38(%rbp), %rax
movq (%rax), %rdi
movq 0x8(%rax), %rdx
incl %r12d
movslq %r12d, %r12
movq %rdx, %rax
subq %rdi, %rax
sarq $0x3, %rax
cmpq %r12, %rax
movl $0x0, %eax
cmovbel %eax, %r12d
movl %r12d, 0x8(%rbx)
leal 0x1(%r9), %r13d
cmpl %esi, %r12d
jne 0x49f7
jmp 0x4a9e
movq %r10, %rbx
movl %r8d, 0x8(%rbx)
cmpl $0x0, 0xc(%rbx)
je 0x4ac4
leaq 0x1753(%rip), %rdi      # 0x6202
movl -0x2c(%rbp), %edx
xorl %eax, %eax
callq 0x2030
movl 0x8(%rbx), %r8d
movq -0x38(%rbp), %rax
movq (%rax), %rdi
movslq %r8d, %rax
movq (%rdi,%rax,8), %rax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq