// File: /workspace/github_cpp_makefile_star2_O1/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
pushq %rax
movq %rsi, %rbx
movq %rdi, %r14
movslq 0x8(%rdi), %rax
incq %rax
movl %eax, 0x8(%rdi)
movq 0x8(%rsi), %rsi
subq (%rbx), %rsi
sarq $0x3, %rsi
cmpq %rax, %rsi
ja 0x4938
movl $0x0, 0x8(%r14)
movq %rcx, %rdi
subq 0x10(%r14), %rdi
xorl %eax, %eax
cmpq $0x32, %rdi
setae %r8b
jb 0x494f
movq %rcx, 0x10(%r14)
movl 0x8(%r14), %esi
movb %r8b, %al
movl %eax, -0x2c(%rbp)
movl $0x4, %ecx
xorl %r9d, %r9d
movl %esi, %r8d
movl %ecx, %r10d
movl %r8d, %r12d
cmpq $0x32, %rdi
setae %r13b
movslq 0x8(%r14), %r8
movq (%rbx), %rcx
movq (%rcx,%r8,8), %rcx
movslq 0x4(%rcx), %r11
movq (%rdx), %r15
movq (%r15,%r11,8), %r11
movslq 0x8(%rcx), %rcx
movq 0x20(%r11), %r11
movq (%r11,%rcx,8), %r11
movl (%r11), %eax
movl %eax, %r15d
andl $0x2, %r15d
movl %eax, %ecx
shrl $0x2, %ecx
andl $0x1, %ecx
orl %r15d, %ecx
cmpl %r10d, %ecx
cmovgel %r12d, %r8d
cmovgel %r10d, %ecx
testl %ecx, %ecx
setne %r10b
orb %r13b, %r10b
cmpb $0x1, %r10b
jne 0x49f2
cmpq $0x32, %rdi
jb 0x49cf
andl $-0x3, %eax
movl %eax, (%r11)
movslq 0x8(%r14), %rax
incq %rax
movl %eax, 0x8(%r14)
movq 0x8(%rbx), %r11
subq (%rbx), %r11
sarq $0x3, %r11
cmpq %rax, %r11
ja 0x49f2
movl $0x0, 0x8(%r14)
cmpl %esi, 0x8(%r14)
setne %al
incl %r9d
testb %al, %r10b
jne 0x4964
movl %r8d, 0x8(%r14)
cmpl $0x0, 0xc(%r14)
je 0x4a21
leaq 0x17eb(%rip), %rdi      # 0x6202
movl -0x2c(%rbp), %edx
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