// File: /workspace/github_cpp_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rcx, %r9
movq %rdx, -0x48(%rbp)
movq %rsi, %rbx
movq %rdi, %r14
movslq 0x8(%rdi), %rdi
incq %rdi
movl %edi, 0x8(%r14)
movq 0x8(%rsi), %rcx
subq (%rsi), %rcx
sarq $0x3, %rcx
cmpq %rdi, %rcx
ja 0x4a3e
andl $0x0, 0x8(%r14)
xorl %edi, %edi
cmpl $0x0, 0xc(%r14)
je 0x4a72
movslq %edi, %rsi
leaq (%rcx,%rsi), %rax
decq %rax
xorl %edx, %edx
divq %rcx
leaq 0x7c2(%rip), %rdi       # 0x521d
xorl %eax, %eax
movq %rbx, %r15
movq %r9, %rbx
callq 0x2030
movq %rbx, %r9
movq %r15, %rbx
movl 0x8(%r14), %edi
pushq $0x1
popq %r13
movl %edi, %esi
movq $0x0, -0x38(%rbp)
movl %edi, -0x2c(%rbp)
movq %rbx, -0x40(%rbp)
movslq %esi, %rax
movq (%rbx), %rcx
movq (%rcx,%rax,8), %rax
movslq 0x4(%rax), %rcx
movq -0x48(%rbp), %rdx
movq (%rdx), %rdx
movq (%rdx,%rcx,8), %rdx
movslq 0x8(%rax), %r8
movq 0x20(%rdx), %rdx
movq (%rdx,%r8,8), %r12
cmpl $0x0, 0xc(%r14)
je 0x4ae4
movl (%r12), %edx
shrl %edx
andl $0x1, %edx
movq %r9, %rbx
movl 0xc(%rax), %r9d
movq %r14, %r15
movq %rdi, %r14
leaq 0x76c(%rip), %rdi       # 0x523c
xorl %eax, %eax
callq 0x2030
movq %r14, %rdi
movq %r15, %r14
movq %rbx, %r9
movq -0x40(%rbp), %rbx
movl (%r12), %eax
testb $0x2, %al
jne 0x4b2c
movslq 0x8(%r14), %r12
movq (%rbx), %rax
movq (%rax,%r12,8), %rcx
movl 0xc(%rcx), %ecx
movq %r9, %rdx
subq %rcx, %rdx
movslq 0x10(%r14), %rsi
cmpq %rsi, %rdx
ja 0x4b73
movq -0x38(%rbp), %r8
movslq %r8d, %r8
movl %r9d, %esi
subl %ecx, %esi
cmpq %r8, %rdx
movl -0x2c(%rbp), %ecx
cmoval %r12d, %ecx
movl %ecx, -0x2c(%rbp)
cmoval %esi, %r8d
movq %r8, -0x38(%rbp)
jmp 0x4b42
andl $-0x3, %eax
movl %eax, (%r12)
movslq 0x8(%r14), %r12
movq (%rbx), %rax
movq (%rax,%r12,8), %rcx
movl %r9d, 0xc(%rcx)
incl %r12d
movl %r12d, 0x8(%r14)
movslq %r12d, %rsi
movq 0x8(%rbx), %rcx
subq %rax, %rcx
sarq $0x3, %rcx
cmpq %rsi, %rcx
movl $0x0, %eax
cmovbel %eax, %esi
movl %esi, 0x8(%r14)
incl %r13d
cmpl %edi, %esi
jne 0x4a87
jmp 0x4b8f
cmpl $0x0, 0xc(%r14)
je 0x4bb4
leaq 0x6cb(%rip), %rdi       # 0x524c
movl %r13d, %esi
xorl %eax, %eax
callq 0x2030
movl %r12d, -0x2c(%rbp)
movl -0x2c(%rbp), %esi
movl %esi, 0x8(%r14)
cmpl $0x0, 0xc(%r14)
je 0x4bb1
leaq 0x691(%rip), %rdi       # 0x5235
xorl %eax, %eax
callq 0x2030
movl 0x8(%r14), %r12d
jmp 0x4bb4
movl %esi, %r12d
movslq %r12d, %rax
movq (%rbx), %rcx
movq (%rcx,%rax,8), %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop