// File: eva23511[P]OS-Virtual-Memory-Management/pager.cpp
// Function: WorkingSetPager::SelectVictimFrame(std::vector<Frame*, std::allocator<Frame*>>, std::vector<Process*, std::allocator<Process*>>, unsigned long)
Frame * WorkingSetPager::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;
    int victim_frame = hand; // Victim frame selected
    int scanned_num = 0; // Number of frames scanned
    int max_age = 0; // oldest “time_last_used” frame
    
    do {
        scanned_num++;
        Pte * pte = processes[frame_table[hand]->pid]->page_table[frame_table[hand]->vpage];
        
        if (a_option) {
            printf(" %d(%d %d:%d %d", hand, pte->referenced, frame_table[hand]->pid, frame_table[hand]->vpage, frame_table[hand]->timestamp);
        }
        
        // R = 0 && age > τ -> select this frame
        if (!pte->referenced && timestamp - frame_table[hand]->timestamp > kTau) {
            victim_frame = hand;
            if (a_option) {
                printf(" STOP(%d)", scanned_num);
            }
            break;
        }
        
        if (!pte->referenced && timestamp - frame_table[hand]->timestamp > max_age) {
            victim_frame = hand;
            max_age = (int)timestamp - frame_table[hand]->timestamp;
        }
        
        if (pte->referenced) {
            pte->referenced = 0;
            frame_table[hand]->timestamp = (unsigned int)timestamp;
        }
        
        hand++;
        if (hand >= frame_table.size()) {
            hand = 0;
        }
    } while (hand != scan_from);
    
    hand = victim_frame;
    if (a_option) {
        printf(" | %d\n", hand);
    }
    return frame_table[hand];
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xb0, %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 0xb653
movq -0x48(%rbp), %rax
movl $0x0, 0x8(%rax)
movq -0x48(%rbp), %rax
cmpl $0x0, 0xc(%rax)
je 0xb6b0
movq -0x58(%rbp), %rdi
movq -0x48(%rbp), %rax
movl 0x8(%rax), %ecx
movl %ecx, -0x6c(%rbp)
movslq 0x8(%rax), %rax
movq %rax, -0x78(%rbp)
callq 0x7450
movq -0x58(%rbp), %rdi
movq %rax, %rcx
movq -0x78(%rbp), %rax
addq %rcx, %rax
subq $0x1, %rax
movq %rax, -0x68(%rbp)
callq 0x7450
movl -0x6c(%rbp), %esi
movq %rax, %rcx
movq -0x68(%rbp), %rax
xorl %edx, %edx
divq %rcx
leaq 0x2c4e(%rip), %rdi      # 0xe2f7
movb $0x0, %al
callq 0x3030
movq -0x48(%rbp), %rax
movl 0x8(%rax), %ecx
movl %ecx, -0x24(%rbp)
movl 0x8(%rax), %eax
movl %eax, -0x28(%rbp)
movl $0x0, -0x2c(%rbp)
movl $0x0, -0x30(%rbp)
movq -0x48(%rbp), %rax
movq -0x58(%rbp), %rdi
movl -0x2c(%rbp), %ecx
addl $0x1, %ecx
movl %ecx, -0x2c(%rbp)
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, -0x80(%rbp)
movslq 0x8(%rax), %rsi
callq 0x5510
movq -0x80(%rbp), %rdi
movq (%rax), %rax
movslq 0x8(%rax), %rsi
callq 0x5430
movq %rax, %rcx
movq -0x48(%rbp), %rax
movq (%rcx), %rcx
movq %rcx, -0x38(%rbp)
cmpl $0x0, 0xc(%rax)
je 0xb7d8
movq -0x58(%rbp), %rdi
movq -0x48(%rbp), %rax
movl 0x8(%rax), %ecx
movl %ecx, -0x90(%rbp)
movq -0x38(%rbp), %rcx
movl (%rcx), %ecx
shrl %ecx
andl $0x1, %ecx
movl %ecx, -0x8c(%rbp)
movslq 0x8(%rax), %rsi
callq 0x5510
movq -0x58(%rbp), %rdi
movq %rax, %rcx
movq -0x48(%rbp), %rax
movq (%rcx), %rcx
movl 0x4(%rcx), %ecx
movl %ecx, -0x88(%rbp)
movslq 0x8(%rax), %rsi
callq 0x5510
movq -0x58(%rbp), %rdi
movq %rax, %rcx
movq -0x48(%rbp), %rax
movq (%rcx), %rcx
movl 0x8(%rcx), %ecx
movl %ecx, -0x84(%rbp)
movslq 0x8(%rax), %rsi
callq 0x5510
movl -0x90(%rbp), %esi
movl -0x8c(%rbp), %edx
movl -0x88(%rbp), %ecx
movl -0x84(%rbp), %r8d
movq (%rax), %rax
movl 0xc(%rax), %r9d
leaq 0x2b45(%rip), %rdi      # 0xe316
movb $0x0, %al
callq 0x3030
movq -0x38(%rbp), %rax
movl (%rax), %eax
shrl %eax
andl $0x1, %eax
cmpl $0x0, %eax
jne 0xb84a
movq -0x48(%rbp), %rax
movq -0x58(%rbp), %rdi
movq -0x20(%rbp), %rcx
movq %rcx, -0x98(%rbp)
movslq 0x8(%rax), %rsi
callq 0x5510
movq -0x48(%rbp), %rcx
movq %rax, %rdx
movq -0x98(%rbp), %rax
movq (%rdx), %rdx
movl 0xc(%rdx), %edx
subq %rdx, %rax
movslq 0x10(%rcx), %rcx
cmpq %rcx, %rax
jbe 0xb84a
movq -0x48(%rbp), %rax
movl 0x8(%rax), %ecx
movl %ecx, -0x28(%rbp)
cmpl $0x0, 0xc(%rax)
je 0xb845
movl -0x2c(%rbp), %esi
leaq 0x2ae8(%rip), %rdi      # 0xe326
movb $0x0, %al
callq 0x3030
jmp 0xb957
movq -0x38(%rbp), %rax
movl (%rax), %eax
shrl %eax
andl $0x1, %eax
cmpl $0x0, %eax
jne 0xb8c5
movq -0x58(%rbp), %rdi
movq -0x48(%rbp), %rax
movq -0x20(%rbp), %rcx
movq %rcx, -0xa0(%rbp)
movslq 0x8(%rax), %rsi
callq 0x5510
movq %rax, %rcx
movq -0xa0(%rbp), %rax
movq (%rcx), %rcx
movl 0xc(%rcx), %ecx
subq %rcx, %rax
movslq -0x30(%rbp), %rcx
cmpq %rcx, %rax
jbe 0xb8c5
movq -0x58(%rbp), %rdi
movq -0x48(%rbp), %rax
movl 0x8(%rax), %ecx
movl %ecx, -0x28(%rbp)
movq -0x20(%rbp), %rcx
movl %ecx, -0xa4(%rbp)
movslq 0x8(%rax), %rsi
callq 0x5510
movq %rax, %rcx
movl -0xa4(%rbp), %eax
movq (%rcx), %rcx
subl 0xc(%rcx), %eax
movl %eax, -0x30(%rbp)
movq -0x38(%rbp), %rax
movl (%rax), %eax
shrl %eax
andl $0x1, %eax
cmpl $0x0, %eax
je 0xb90a
movq -0x58(%rbp), %rdi
movq -0x48(%rbp), %rax
movq -0x38(%rbp), %rcx
movl (%rcx), %edx
andl $-0x3, %edx
orl $0x0, %edx
movl %edx, (%rcx)
movq -0x20(%rbp), %rcx
movl %ecx, -0xa8(%rbp)
movslq 0x8(%rax), %rsi
callq 0x5510
movl -0xa8(%rbp), %ecx
movq (%rax), %rax
movl %ecx, 0xc(%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, -0xb0(%rbp)
callq 0x7450
movq %rax, %rcx
movq -0xb0(%rbp), %rax
cmpq %rcx, %rax
jb 0xb945
movq -0x48(%rbp), %rax
movl $0x0, 0x8(%rax)
jmp 0xb947
movq -0x48(%rbp), %rax
movl 0x8(%rax), %eax
cmpl -0x24(%rbp), %eax
jne 0xb6ce
movq -0x48(%rbp), %rax
movl -0x28(%rbp), %ecx
movl %ecx, 0x8(%rax)
cmpl $0x0, 0xc(%rax)
je 0xb97c
movq -0x48(%rbp), %rax
movl 0x8(%rax), %esi
leaq 0x299a(%rip), %rdi      # 0xe30f
movb $0x0, %al
callq 0x3030
movq -0x58(%rbp), %rdi
movq -0x48(%rbp), %rax
movslq 0x8(%rax), %rsi
callq 0x5510
movq (%rax), %rax
addq $0xb0, %rsp
popq %rbp
retq
nopl (%rax)