// File: /workspace/github_cpp_makefile_star2_O3/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), %r15
incq %r15
movl %r15d, 0x8(%rdi)
movq (%rsi), %rax
movq 0x8(%rsi), %rcx
subq %rax, %rcx
sarq $0x3, %rcx
cmpq %r15, %rcx
ja 0x4d69
movl $0x0, 0x8(%r14)
xorl %r15d, %r15d
cmpl $0x0, 0xc(%r14)
movq %rbx, -0x38(%rbp)
je 0x4da2
movslq %r15d, %rsi
leaq (%rcx,%rsi), %rax
decq %rax
xorl %edx, %edx
divq %rcx
leaq 0x1493(%rip), %rdi      # 0x621d
xorl %eax, %eax
movq %r9, %rbx
callq 0x2030
movq %rbx, %r9
movq -0x38(%rbp), %rbx
movl 0x8(%r14), %r15d
movq (%rbx), %rax
movl $0x1, %r13d
movl %r15d, %esi
movq $0x0, -0x40(%rbp)
movl %r15d, -0x2c(%rbp)
movslq %esi, %rcx
movq (%rax,%rcx,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 0x4e05
movl (%r12), %edx
shrl %edx
andl $0x1, %edx
movq %r9, %rbx
movl 0xc(%rax), %r9d
leaq 0x1445(%rip), %rdi      # 0x623c
xorl %eax, %eax
callq 0x2030
movq %rbx, %r9
movq -0x38(%rbp), %rbx
movl (%r12), %eax
testb $0x2, %al
jne 0x4e4d
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 0x4e95
movq -0x40(%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, -0x40(%rbp)
jmp 0x4e63
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, %ecx
cmovbel %ecx, %esi
movl %esi, 0x8(%r14)
incl %r13d
cmpl %r15d, %esi
jne 0x4db7
jmp 0x4eb1
cmpl $0x0, 0xc(%r14)
je 0x4ed6
leaq 0x13a9(%rip), %rdi      # 0x624c
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 0x4ed3
leaq 0x136f(%rip), %rdi      # 0x6235
xorl %eax, %eax
callq 0x2030
movl 0x8(%r14), %r12d
jmp 0x4ed6
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