// File: /workspace/github_cpp_makefile_star2_O2/dsesami[P]superscalar-processor-model/issue.cc
// Function: issue()
void issue(){

    for(u_int i=0; i<WIDTH; i++) {
        int min=50000;
        int min_index=-1;
        // stall if there isn't enough room to issue something.
        if(execute_list.size() == WIDTH*5) return;

        // Move through the IQ and locate the oldest instruction.
        for(u_int j=0; j<IQ_SIZE; j++) {
            if(!IQ.at(j).valid) continue;
            // skip this if its sources aren't ready.
            if(IQ.at(j).instruction.rn_src_1 != -1) continue;
            if(IQ.at(j).instruction.rn_src_2 != -1) continue;
            if(IQ.at(j).valid && (IQ.at(j).instruction.fetch_first < min) && ready(j)) {
                min = IQ.at(j).instruction.fetch_first;
                min_index = j;
            }
        }

        // oh, but first, age up all of the instructions in the IQ.
        for(u_int j=0; j<IQ_SIZE; j++) {
            if(!IQ.at(j).valid) continue;
            if(IQ.at(j).valid) {
                if(IQ.at(j).instruction.issue_first == -1) {
                    IQ.at(j).instruction.issue_first = cycles;
                }
            }
        }

        // If there wasn't an oldest valid instruction, leave.
        if(min_index == -1) return;

        // add the instruction to the execute list.
        int latency;
        if(IQ.at(min_index).instruction.op_type == 0) latency = 1;
        if(IQ.at(min_index).instruction.op_type == 1) latency = 2;
        if(IQ.at(min_index).instruction.op_type == 2) latency = 5;
        IQ.at(min_index).instruction.issue_time = cycles - IQ.at(min_index).instruction.issue_first+1;
        ex_entry ex_instruction = {IQ.at(min_index).instruction, latency};
        execute_list.push_back(ex_instruction);
        // invalidate that instruction in the IQ.
        IQ.at(min_index).valid=0;
    }
    return;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
xorl %ecx, %ecx
leaq 0x4d5e(%rip), %r15      # 0x9168
leaq 0x4faf(%rip), %r14      # 0x93c0
leaq 0x4d54(%rip), %rax      # 0x916c
movl (%rax), %eax
movq %rcx, (%rsp)
cmpl %eax, %ecx
jae 0x45e5
leaq 0x4fab(%rip), %rdx      # 0x93d8
movq 0x8(%rdx), %rcx
subq (%rdx), %rcx
sarq $0x7, %rcx
leal (%rax,%rax,4), %eax
cmpq %rax, %rcx
je 0x45e5
movl $0xc350, %ebp           # imm = 0xC350
xorl %r13d, %r13d
pushq $-0x1
popq %r12
movl (%r15), %eax
cmpq %rax, %r13
jae 0x44cc
movq %r14, %rdi
movq %r13, %rsi
callq 0x2eb4
cmpb $0x1, (%rax)
jne 0x44c7
movq %r14, %rdi
movq %r13, %rsi
callq 0x2eb4
cmpl $-0x1, 0x20(%rax)
jne 0x44c7
movq %r14, %rdi
movq %r13, %rsi
callq 0x2eb4
cmpl $-0x1, 0x28(%rax)
jne 0x44c7
movq %r14, %rdi
movq %r13, %rsi
callq 0x2eb4
cmpb $0x1, (%rax)
jne 0x44c7
movq %r14, %rdi
movq %r13, %rsi
callq 0x2eb4
cmpl %ebp, 0x2c(%rax)
jge 0x44c7
movl %r13d, %edi
callq 0x45f7
testb %al, %al
je 0x44c7
movq %r14, %rdi
movq %r13, %rsi
callq 0x2eb4
movl 0x2c(%rax), %ebp
movl %r13d, %r12d
incq %r13
jmp 0x4450
xorl %r13d, %r13d
movl %eax, %eax
cmpq %rax, %r13
jae 0x4526
movq %r14, %rdi
movq %r13, %rsi
callq 0x2eb4
cmpb $0x1, (%rax)
jne 0x451e
movq %r14, %rdi
movq %r13, %rsi
callq 0x2eb4
cmpb $0x1, (%rax)
jne 0x451e
movq %r14, %rdi
movq %r13, %rsi
callq 0x2eb4
cmpl $-0x1, 0x54(%rax)
jne 0x451e
leaq 0x4c6e(%rip), %rax      # 0x917c
movl (%rax), %ebp
movq %r14, %rdi
movq %r13, %rsi
callq 0x2eb4
movl %ebp, 0x54(%rax)
incq %r13
movl (%r15), %eax
jmp 0x44cf
cmpl $-0x1, %r12d
je 0x45e5
movslq %r12d, %r13
movq %r14, %rdi
movq %r13, %rsi
callq 0x2eb4
cmpl $0x0, 0x10(%rax)
pushq $0x1
popq %rax
cmovel %eax, %ebx
movq %r14, %rdi
movq %r13, %rsi
callq 0x2eb4
cmpl $0x1, 0x10(%rax)
pushq $0x2
popq %rax
cmovel %eax, %ebx
movq %r14, %rdi
movq %r13, %rsi
callq 0x2eb4
cmpl $0x2, 0x10(%rax)
pushq $0x5
popq %rax
cmovel %eax, %ebx
leaq 0x4c03(%rip), %rax      # 0x917c
movl (%rax), %ebp
movq %r14, %rdi
movq %r13, %rsi
callq 0x2eb4
subl 0x54(%rax), %ebp
incl %ebp
movq %r14, %rdi
movq %r13, %rsi
callq 0x2eb4
movl %ebp, 0x58(%rax)
movq %r14, %rdi
movq %r13, %rsi
callq 0x2eb4
leaq 0x8(%rax), %rsi
pushq $0xf
popq %rcx
leaq 0x8(%rsp), %rax
movq %rax, %rdi
rep  movsq (%rsi), %es:(%rdi)
movl %ebx, 0x80(%rsp)
leaq 0x4e14(%rip), %rdi      # 0x93d8
movq %rax, %rsi
callq 0x462c
movq %r14, %rdi
movq %r13, %rsi
callq 0x2eb4
movb $0x0, (%rax)
movq (%rsp), %rcx
incl %ecx
jmp 0x4411
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq