// File: /workspace/github_cpp_makefile_star2_O2/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 $0x28, %rsp
movq %rdx, -0x48(%rbp)
movq %rdi, %rbx
movslq 0x8(%rdi), %rdx
incq %rdx
movl %edx, 0x8(%rdi)
movq (%rsi), %rdi
movq %rsi, -0x40(%rbp)
movq 0x8(%rsi), %rsi
movq %rsi, %rax
subq %rdi, %rax
sarq $0x3, %rax
movq %rdx, -0x30(%rbp)
cmpq %rdx, %rax
ja 0x46c8
andl $0x0, 0x8(%rbx)
movq $0x0, -0x30(%rbp)
movq %rcx, %r11
subq 0x10(%rbx), %r11
xorl %edx, %edx
cmpq $0x32, %r11
setae %al
jb 0x46de
movq %rcx, 0x10(%rbx)
movb %al, %dl
movl %edx, -0x34(%rbp)
pushq $0x4
popq %rcx
pushq $0x1
popq %rax
movq -0x30(%rbp), %r8
movl %r8d, %r15d
movl %ecx, %r12d
movl %r8d, %edx
movl %eax, %r9d
cmpq $0x32, %r11
setae %r14b
movslq %r15d, %r8
movq (%rdi,%r8,8), %rax
movslq 0x4(%rax), %rcx
movq -0x48(%rbp), %r10
movq (%r10), %r13
movq (%r13,%rcx,8), %rcx
movslq 0x8(%rax), %rax
movq 0x20(%rcx), %rcx
movq (%rcx,%rax,8), %r13
movl (%r13), %r10d
movl %r10d, %eax
andl $0x2, %eax
movl %r10d, %ecx
shrl $0x2, %ecx
andl $0x1, %ecx
orl %eax, %ecx
cmpl %r12d, %ecx
cmovgel %edx, %r8d
cmovgel %r12d, %ecx
testl %ecx, %ecx
setne %r12b
orb %r14b, %r12b
cmpb $0x1, %r12b
jne 0x47a1
cmpq $0x32, %r11
jb 0x4770
andl $-0x3, %r10d
movl %r10d, (%r13)
movl 0x8(%rbx), %r15d
movq -0x40(%rbp), %rax
movq (%rax), %rdi
movq 0x8(%rax), %rsi
incl %r15d
movslq %r15d, %rax
movq %rsi, %r10
subq %rdi, %r10
sarq $0x3, %r10
cmpq %rax, %r10
ja 0x4788
xorl %r15d, %r15d
movl %r15d, 0x8(%rbx)
cmpl -0x30(%rbp), %r15d
setne %r10b
leal 0x1(%r9), %eax
testb %r10b, %r12b
jne 0x46f0
movl %r8d, 0x8(%rbx)
cmpl $0x0, 0xc(%rbx)
je 0x47cb
leaq 0xa50(%rip), %rdi       # 0x5202
movq -0x30(%rbp), %rsi
movl -0x34(%rbp), %edx
xorl %eax, %eax
callq 0x2030
movl 0x8(%rbx), %r8d
movq -0x40(%rbp), %rax
movq (%rax), %rdi
movslq %r8d, %rax
movq (%rdi,%rax,8), %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop