// File: /workspace/github_cpp_makefile_star2_O1/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, -0x40(%rbp)
movq %rdx, %r12
movq %rsi, %rbx
movq %rdi, %r14
movslq 0x8(%rdi), %rax
incq %rax
movl %eax, 0x8(%rdi)
movq 0x8(%rsi), %rcx
subq (%rsi), %rcx
sarq $0x3, %rcx
cmpq %rax, %rcx
ja 0x4cba
movl $0x0, 0x8(%r14)
cmpl $0x0, 0xc(%r14)
je 0x4cdf
movslq 0x8(%r14), %rsi
leaq (%rcx,%rsi), %rax
decq %rax
xorl %edx, %edx
divq %rcx
leaq 0x1545(%rip), %rdi      # 0x621d
xorl %eax, %eax
callq 0x2030
movl 0x8(%r14), %eax
movl $0x1, %r13d
movq $0x0, -0x50(%rbp)
movl %eax, -0x44(%rbp)
movl %eax, -0x2c(%rbp)
movq %r12, -0x38(%rbp)
movslq 0x8(%r14), %rsi
movq (%rbx), %rax
movq (%rax,%rsi,8), %rax
movslq 0x4(%rax), %rcx
movq (%r12), %rdx
movq (%rdx,%rcx,8), %rdx
movslq 0x8(%rax), %r8
movq 0x20(%rdx), %rdx
movq (%rdx,%r8,8), %r15
cmpl $0x0, 0xc(%r14)
je 0x4d3f
movl (%r15), %edx
shrl %edx
andl $0x1, %edx
movl 0xc(%rax), %r9d
leaq 0x1504(%rip), %rdi      # 0x623c
xorl %eax, %eax
callq 0x2030
movl (%r15), %eax
testb $0x2, %al
jne 0x4dca
movslq 0x8(%r14), %r12
movq (%rbx), %rcx
movq (%rcx,%r12,8), %rcx
movl 0xc(%rcx), %ecx
movq -0x40(%rbp), %rdx
subq %rcx, %rdx
movslq 0x10(%r14), %rcx
cmpq %rcx, %rdx
jbe 0x4d8d
movb $0x1, %r15b
cmpl $0x0, 0xc(%r14)
je 0x4d83
leaq 0x14d3(%rip), %rdi      # 0x624c
movl %r13d, %esi
xorl %eax, %eax
callq 0x2030
movl %r12d, -0x2c(%rbp)
movq -0x38(%rbp), %r12
jmp 0x4e0c
testb $0x2, %al
jne 0x4dc6
movslq 0x8(%r14), %rcx
movq (%rbx), %rdx
movq (%rdx,%rcx,8), %rdx
movl 0xc(%rdx), %edx
movq -0x40(%rbp), %rdi
movq %rdi, %rsi
subq %rdx, %rsi
movq -0x50(%rbp), %r8
movslq %r8d, %r8
subl %edx, %edi
cmpq %r8, %rsi
cmoval %edi, %r8d
movq %r8, -0x50(%rbp)
movl -0x2c(%rbp), %edx
cmoval %ecx, %edx
movl %edx, -0x2c(%rbp)
movq -0x38(%rbp), %r12
testb $0x2, %al
je 0x4de6
andl $-0x3, %eax
movl %eax, (%r15)
movslq 0x8(%r14), %rax
movq (%rbx), %rcx
movq (%rcx,%rax,8), %rax
movq -0x40(%rbp), %rcx
movl %ecx, 0xc(%rax)
movslq 0x8(%r14), %rax
incq %rax
movl %eax, 0x8(%r14)
movq 0x8(%rbx), %rcx
subq (%rbx), %rcx
sarq $0x3, %rcx
cmpq %rax, %rcx
ja 0x4e09
movl $0x0, 0x8(%r14)
xorl %r15d, %r15d
testb %r15b, %r15b
jne 0x4e21
incl %r13d
movl -0x44(%rbp), %eax
cmpl %eax, 0x8(%r14)
jne 0x4cfb
movl -0x2c(%rbp), %esi
movl %esi, 0x8(%r14)
cmpl $0x0, 0xc(%r14)
je 0x4e3d
leaq 0x13ff(%rip), %rdi      # 0x6235
xorl %eax, %eax
callq 0x2030
movslq 0x8(%r14), %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