// File: dsesami[P]superscalar-processor-model/rename.cc
// Function: rename()
void rename(){
    for(deque<bundle>::iterator j=RN_register.begin(); j!=RN_register.end(); j++) {

        // set first cycle and duration of this instruction.
        if(RN_register.size() && (j->rename_first == -1)) {
            j->rename_first = cycles;
        }
    }
    if((RR_register.size() == WIDTH) || isrobfull()) {
                return;
            }

    for(u_int i=0; i<WIDTH; i++) {
        if(RN_register.size()) {
            // if the RR register cannot accept things, or the rob does not have enough entries, stall.


            // map the instruction to the ROB.
            ROB.at(tail).valid = true;
            ROB.at(tail).address = RN_register.front().address;
            ROB.at(tail).dyn = RN_register.front().dyn;
            ROB.at(tail).ready = false;
            ROB.at(tail).instruction = RN_register.front();
            // rename the source registers if applicable.
            if(RN_register.front().src_1 != -1) {
                if(RMT.at(RN_register.front().src_1).valid)
                    RN_register.front().rn_src_1 = RMT.at(RN_register.front().src_1).rob_tag;
            }

            // rename source register two similarly.
            if(RN_register.front().src_2 != -1) {
                if(RMT.at(RN_register.front().src_2).valid)
                    RN_register.front().rn_src_2 = RMT.at(RN_register.front().src_2).rob_tag;
            }

            // map the destination register to the ROB and RMT and rename the destination register.
            ROB.at(tail).dest = RN_register.front().dest;
            if(RN_register.front().dest != -1) {
                RMT.at(RN_register.front().dest).valid = true;
                RMT.at(RN_register.front().dest).rob_tag = tail;
            }
            RN_register.front().rn_dest = tail;

            // move the tail down for the next instruction.
            tail++;
            if(tail == ROBSIZE) tail=0;

            // finally, send the instruction on its merry way.
            RN_register.front().rename_time = cycles - RN_register.front().rename_first+1;
            RR_register.push_back(RN_register.front());
            RN_register.pop_front();
        }
    }
    return;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xa0, %rsp
leaq -0x20(%rbp), %rdi
leaq 0x9eca(%rip), %rsi      # 0x114a0
callq 0x5050
leaq -0x40(%rbp), %rdi
leaq 0x9eba(%rip), %rsi      # 0x114a0
callq 0x5080
leaq -0x20(%rbp), %rdi
leaq -0x40(%rbp), %rsi
callq 0x7350
testb $0x1, %al
jne 0x75fe
jmp 0x764d
leaq 0x9e9b(%rip), %rdi      # 0x114a0
callq 0x3700
cmpq $0x0, %rax
je 0x763a
leaq -0x20(%rbp), %rdi
callq 0x7380
cmpl $-0x1, 0x34(%rax)
jne 0x763a
leaq 0x9bae(%rip), %rax      # 0x111d4
movl (%rax), %eax
movl %eax, -0x68(%rbp)
leaq -0x20(%rbp), %rdi
callq 0x7380
movl -0x68(%rbp), %ecx
movl %ecx, 0x34(%rax)
jmp 0x763c
leaq -0x60(%rbp), %rdi
leaq -0x20(%rbp), %rsi
xorl %edx, %edx
callq 0x73a0
jmp 0x75db
leaq 0x9e9c(%rip), %rdi      # 0x114f0
callq 0x3700
leaq 0x9b64(%rip), %rcx      # 0x111c4
movl (%rcx), %ecx
cmpq %rcx, %rax
je 0x7672
callq 0x79e0
testb $0x1, %al
jne 0x7672
jmp 0x7677
jmp 0x79c8
movl $0x0, -0x64(%rbp)
movl -0x64(%rbp), %eax
leaq 0x9b3c(%rip), %rcx      # 0x111c4
cmpl (%rcx), %eax
jae 0x79c6
leaq 0x9e09(%rip), %rdi      # 0x114a0
callq 0x3700
cmpq $0x0, %rax
je 0x79b6
leaq 0x9af7(%rip), %rax      # 0x111a4
movl (%rax), %eax
movl %eax, %esi
leaq 0x9d30(%rip), %rdi      # 0x113e8
callq 0x3790
movb $0x1, (%rax)
leaq 0x9dd9(%rip), %rdi      # 0x114a0
callq 0x73e0
movq (%rax), %rax
movq %rax, -0x80(%rbp)
leaq 0x9aca(%rip), %rax      # 0x111a4
movl (%rax), %eax
movl %eax, %esi
leaq 0x9d03(%rip), %rdi      # 0x113e8
callq 0x3790
movq -0x80(%rbp), %rcx
movq %rcx, 0x10(%rax)
leaq 0x9da7(%rip), %rdi      # 0x114a0
callq 0x73e0
movl 0x70(%rax), %eax
movl %eax, -0x74(%rbp)
leaq 0x9a99(%rip), %rax      # 0x111a4
movl (%rax), %eax
movl %eax, %esi
leaq 0x9cd2(%rip), %rdi      # 0x113e8
callq 0x3790
movl -0x74(%rbp), %ecx
movl %ecx, 0x4(%rax)
leaq 0x9a7c(%rip), %rax      # 0x111a4
movl (%rax), %eax
movl %eax, %esi
leaq 0x9cb5(%rip), %rdi      # 0x113e8
callq 0x3790
movb $0x0, 0x8(%rax)
leaq 0x9d5d(%rip), %rdi      # 0x114a0
callq 0x73e0
movq %rax, -0x70(%rbp)
leaq 0x9a51(%rip), %rax      # 0x111a4
movl (%rax), %eax
movl %eax, %esi
leaq 0x9c8a(%rip), %rdi      # 0x113e8
callq 0x3790
movq -0x70(%rbp), %rsi
movq %rax, %rdi
addq $0x20, %rdi
movl $0x78, %edx
callq 0x2160
leaq 0x9d21(%rip), %rdi      # 0x114a0
callq 0x73e0
cmpl $-0x1, 0x14(%rax)
je 0x77e7
leaq 0x9d0f(%rip), %rdi      # 0x114a0
callq 0x73e0
movslq 0x14(%rax), %rsi
leaq 0x9c5f(%rip), %rdi      # 0x11400
callq 0x7a60
testb $0x1, (%rax)
je 0x77e5
leaq 0x9cee(%rip), %rdi      # 0x114a0
callq 0x73e0
movslq 0x14(%rax), %rsi
leaq 0x9c3e(%rip), %rdi      # 0x11400
callq 0x7a60
movl 0x4(%rax), %eax
movl %eax, -0x84(%rbp)
leaq 0x9cc9(%rip), %rdi      # 0x114a0
callq 0x73e0
movl -0x84(%rbp), %ecx
movl %ecx, 0x18(%rax)
jmp 0x77e7
leaq 0x9cb2(%rip), %rdi      # 0x114a0
callq 0x73e0
cmpl $-0x1, 0x1c(%rax)
je 0x7856
leaq 0x9ca0(%rip), %rdi      # 0x114a0
callq 0x73e0
movslq 0x1c(%rax), %rsi
leaq 0x9bf0(%rip), %rdi      # 0x11400
callq 0x7a60
testb $0x1, (%rax)
je 0x7854
leaq 0x9c7f(%rip), %rdi      # 0x114a0
callq 0x73e0
movslq 0x1c(%rax), %rsi
leaq 0x9bcf(%rip), %rdi      # 0x11400
callq 0x7a60
movl 0x4(%rax), %eax
movl %eax, -0x88(%rbp)
leaq 0x9c5a(%rip), %rdi      # 0x114a0
callq 0x73e0
movl -0x88(%rbp), %ecx
movl %ecx, 0x20(%rax)
jmp 0x7856
leaq 0x9c43(%rip), %rdi      # 0x114a0
callq 0x73e0
movl 0xc(%rax), %eax
movl %eax, -0x8c(%rbp)
leaq 0x9932(%rip), %rax      # 0x111a4
movl (%rax), %eax
movl %eax, %esi
leaq 0x9b6b(%rip), %rdi      # 0x113e8
callq 0x3790
movl -0x8c(%rbp), %ecx
movl %ecx, 0x18(%rax)
leaq 0x9c0e(%rip), %rdi      # 0x114a0
callq 0x73e0
cmpl $-0x1, 0xc(%rax)
je 0x78f0
leaq 0x9bfc(%rip), %rdi      # 0x114a0
callq 0x73e0
movslq 0xc(%rax), %rsi
leaq 0x9b4c(%rip), %rdi      # 0x11400
callq 0x7a60
movb $0x1, (%rax)
leaq 0x98e1(%rip), %rax      # 0x111a4
movl (%rax), %eax
movl %eax, -0x90(%rbp)
leaq 0x9bce(%rip), %rdi      # 0x114a0
callq 0x73e0
movslq 0xc(%rax), %rsi
leaq 0x9b1e(%rip), %rdi      # 0x11400
callq 0x7a60
movl -0x90(%rbp), %ecx
movl %ecx, 0x4(%rax)
leaq 0x98ad(%rip), %rax      # 0x111a4
movl (%rax), %eax
movl %eax, -0x94(%rbp)
leaq 0x9b9a(%rip), %rdi      # 0x114a0
callq 0x73e0
movl -0x94(%rbp), %ecx
movl %ecx, 0x10(%rax)
leaq 0x9889(%rip), %rax      # 0x111a4
movl (%rax), %ecx
addl $0x1, %ecx
leaq 0x987d(%rip), %rax      # 0x111a4
movl %ecx, (%rax)
leaq 0x9874(%rip), %rax      # 0x111a4
movl (%rax), %eax
leaq 0x9883(%rip), %rcx      # 0x111bc
cmpl (%rcx), %eax
jne 0x794a
leaq 0x9860(%rip), %rax      # 0x111a4
movl $0x0, (%rax)
leaq 0x9883(%rip), %rax      # 0x111d4
movl (%rax), %eax
movl %eax, -0x9c(%rbp)
leaq 0x9b40(%rip), %rdi      # 0x114a0
callq 0x73e0
movq %rax, %rcx
movl -0x9c(%rbp), %eax
subl 0x34(%rcx), %eax
addl $0x1, %eax
movl %eax, -0x98(%rbp)
leaq 0x9b1f(%rip), %rdi      # 0x114a0
callq 0x73e0
movl -0x98(%rbp), %ecx
movl %ecx, 0x38(%rax)
leaq 0x9b0a(%rip), %rdi      # 0x114a0
callq 0x73e0
movq %rax, %rsi
leaq 0x9b4b(%rip), %rdi      # 0x114f0
callq 0x6960
leaq 0x9aef(%rip), %rdi      # 0x114a0
callq 0x7410
jmp 0x79b8
movl -0x64(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x64(%rbp)
jmp 0x767e
jmp 0x79c8
addq $0xa0, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)