// File: 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
subq $0x80, %rsp
movq %rsi, -0x60(%rbp)
movq %rdi, %rax
movq -0x60(%rbp), %rdi
movq %rdi, -0x58(%rbp)
movq %rdx, -0x50(%rbp)
movq %rax, -0x8(%rbp)
movq %rdi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x48(%rbp)
movl 0x8(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x8(%rax)
movslq 0x8(%rax), %rax
movq %rax, -0x40(%rbp)
callq 0x7450
movq %rax, %rcx
movq -0x40(%rbp), %rax
cmpq %rcx, %rax
jb 0xaf23
movq -0x48(%rbp), %rax
movl $0x0, 0x8(%rax)
movq -0x48(%rbp), %rax
movl 0x8(%rax), %ecx
movl %ecx, -0x24(%rbp)
movq -0x20(%rbp), %rcx
subq 0x10(%rax), %rcx
cmpq $0x32, %rcx
setae %cl
andb $0x1, %cl
movzbl %cl, %ecx
movl %ecx, -0x28(%rbp)
movl $0x4, -0x2c(%rbp)
movl 0x8(%rax), %eax
movl %eax, -0x30(%rbp)
movl $0x0, -0x34(%rbp)
cmpl $0x0, -0x28(%rbp)
je 0xaf6b
movq -0x48(%rbp), %rax
movq -0x20(%rbp), %rcx
movq %rcx, 0x10(%rax)
jmp 0xaf6d
movq -0x48(%rbp), %rax
movq -0x58(%rbp), %rdi
movslq 0x8(%rax), %rsi
callq 0x5510
movq -0x50(%rbp), %rdi
movq (%rax), %rax
movslq 0x4(%rax), %rsi
callq 0x5410
movq -0x58(%rbp), %rdi
movq %rax, %rcx
movq -0x48(%rbp), %rax
movq (%rcx), %rcx
addq $0x20, %rcx
movq %rcx, -0x68(%rbp)
movslq 0x8(%rax), %rsi
callq 0x5510
movq -0x68(%rbp), %rdi
movq (%rax), %rax
movslq 0x8(%rax), %rsi
callq 0x5430
movq -0x48(%rbp), %rdi
movq (%rax), %rsi
callq 0xb100
movl %eax, -0x38(%rbp)
movl -0x38(%rbp), %eax
cmpl -0x2c(%rbp), %eax
jge 0xafe4
movq -0x48(%rbp), %rax
movl -0x38(%rbp), %ecx
movl %ecx, -0x2c(%rbp)
movl 0x8(%rax), %eax
movl %eax, -0x30(%rbp)
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
cmpl $0x0, -0x2c(%rbp)
jne 0xaffe
cmpl $0x0, -0x28(%rbp)
jne 0xaffe
jmp 0xb0a8
cmpl $0x0, -0x28(%rbp)
je 0xb061
movq -0x58(%rbp), %rdi
movq -0x48(%rbp), %rax
movslq 0x8(%rax), %rsi
callq 0x5510
movq -0x50(%rbp), %rdi
movq (%rax), %rax
movslq 0x4(%rax), %rsi
callq 0x5410
movq -0x58(%rbp), %rdi
movq %rax, %rcx
movq -0x48(%rbp), %rax
movq (%rcx), %rcx
addq $0x20, %rcx
movq %rcx, -0x70(%rbp)
movslq 0x8(%rax), %rsi
callq 0x5510
movq -0x70(%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 -0x58(%rbp), %rdi
movq -0x48(%rbp), %rax
movl 0x8(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x8(%rax)
movslq 0x8(%rax), %rax
movq %rax, -0x78(%rbp)
callq 0x7450
movq %rax, %rcx
movq -0x78(%rbp), %rax
cmpq %rcx, %rax
jb 0xb096
movq -0x48(%rbp), %rax
movl $0x0, 0x8(%rax)
jmp 0xb098
movq -0x48(%rbp), %rax
movl 0x8(%rax), %eax
cmpl -0x24(%rbp), %eax
jne 0xaf6d
movq -0x48(%rbp), %rax
movl -0x30(%rbp), %ecx
movl %ecx, 0x8(%rax)
cmpl $0x0, 0xc(%rax)
je 0xb0db
movq -0x48(%rbp), %rax
movl -0x24(%rbp), %esi
movl -0x28(%rbp), %edx
movl -0x2c(%rbp), %ecx
movl 0x8(%rax), %r8d
movl -0x34(%rbp), %r9d
leaq 0x3208(%rip), %rdi      # 0xe2dc
movb $0x0, %al
callq 0x3030
movq -0x58(%rbp), %rdi
movq -0x48(%rbp), %rax
movslq 0x8(%rax), %rsi
callq 0x5510
movq (%rax), %rax
addq $0x80, %rsp
popq %rbp
retq
nopl (%rax,%rax)