// File: 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
movq %rsp, %rbp
subq $0xb0, %rsp
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
leaq 0x9038(%rip), %rcx      # 0x111c4
cmpl (%rcx), %eax
jae 0x844c
movl $0xc350, -0x8(%rbp)     # imm = 0xC350
movl $0xffffffff, -0xc(%rbp) # imm = 0xFFFFFFFF
leaq 0x9287(%rip), %rdi      # 0x11430
callq 0x3730
leaq 0x900f(%rip), %rcx      # 0x111c4
imull $0x5, (%rcx), %ecx
movl %ecx, %ecx
cmpq %rcx, %rax
jne 0x81c4
jmp 0x844e
movl $0x0, -0x10(%rbp)
movl -0x10(%rbp), %eax
leaq 0x8feb(%rip), %rcx      # 0x111c0
cmpl (%rcx), %eax
jae 0x8294
movl -0x10(%rbp), %eax
movl %eax, %esi
leaq 0x922f(%rip), %rdi      # 0x11418
callq 0x3750
testb $0x1, (%rax)
jne 0x81f8
jmp 0x8286
movl -0x10(%rbp), %eax
movl %eax, %esi
leaq 0x9214(%rip), %rdi      # 0x11418
callq 0x3750
cmpl $-0x1, 0x20(%rax)
je 0x8211
jmp 0x8286
movl -0x10(%rbp), %eax
movl %eax, %esi
leaq 0x91fb(%rip), %rdi      # 0x11418
callq 0x3750
cmpl $-0x1, 0x28(%rax)
je 0x822a
jmp 0x8286
movl -0x10(%rbp), %eax
movl %eax, %esi
leaq 0x91e2(%rip), %rdi      # 0x11418
callq 0x3750
testb $0x1, (%rax)
je 0x8284
movl -0x10(%rbp), %eax
movl %eax, %esi
leaq 0x91cc(%rip), %rdi      # 0x11418
callq 0x3750
movl 0x2c(%rax), %eax
cmpl -0x8(%rbp), %eax
jge 0x8284
movl -0x10(%rbp), %edi
callq 0x8460
testb $0x1, %al
jne 0x8267
jmp 0x8284
movl -0x10(%rbp), %eax
movl %eax, %esi
leaq 0x91a5(%rip), %rdi      # 0x11418
callq 0x3750
movl 0x2c(%rax), %eax
movl %eax, -0x8(%rbp)
movl -0x10(%rbp), %eax
movl %eax, -0xc(%rbp)
jmp 0x8286
movl -0x10(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x10(%rbp)
jmp 0x81cb
movl $0x0, -0x14(%rbp)
movl -0x14(%rbp), %eax
leaq 0x8f1b(%rip), %rcx      # 0x111c0
cmpl (%rcx), %eax
jae 0x832d
movl -0x14(%rbp), %eax
movl %eax, %esi
leaq 0x915f(%rip), %rdi      # 0x11418
callq 0x3750
testb $0x1, (%rax)
jne 0x82c5
jmp 0x831f
movl -0x14(%rbp), %eax
movl %eax, %esi
leaq 0x9147(%rip), %rdi      # 0x11418
callq 0x3750
testb $0x1, (%rax)
je 0x831d
movl -0x14(%rbp), %eax
movl %eax, %esi
leaq 0x9131(%rip), %rdi      # 0x11418
callq 0x3750
cmpl $-0x1, 0x54(%rax)
jne 0x831b
leaq 0x8edb(%rip), %rax      # 0x111d4
movl (%rax), %eax
movl %eax, -0x9c(%rbp)
movl -0x14(%rbp), %eax
movl %eax, %esi
leaq 0x910b(%rip), %rdi      # 0x11418
callq 0x3750
movl -0x9c(%rbp), %ecx
movl %ecx, 0x54(%rax)
jmp 0x831d
jmp 0x831f
movl -0x14(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x14(%rbp)
jmp 0x829b
cmpl $-0x1, -0xc(%rbp)
jne 0x8338
jmp 0x844e
movslq -0xc(%rbp), %rsi
leaq 0x90d5(%rip), %rdi      # 0x11418
callq 0x3750
cmpl $0x0, 0x10(%rax)
jne 0x8355
movl $0x1, -0x18(%rbp)
movslq -0xc(%rbp), %rsi
leaq 0x90b8(%rip), %rdi      # 0x11418
callq 0x3750
cmpl $0x1, 0x10(%rax)
jne 0x8372
movl $0x2, -0x18(%rbp)
movslq -0xc(%rbp), %rsi
leaq 0x909b(%rip), %rdi      # 0x11418
callq 0x3750
cmpl $0x2, 0x10(%rax)
jne 0x838f
movl $0x5, -0x18(%rbp)
leaq 0x8e3e(%rip), %rax      # 0x111d4
movl (%rax), %eax
movl %eax, -0xb0(%rbp)
movslq -0xc(%rbp), %rsi
leaq 0x906f(%rip), %rdi      # 0x11418
callq 0x3750
movq %rax, %rcx
movl -0xb0(%rbp), %eax
subl 0x54(%rcx), %eax
addl $0x1, %eax
movl %eax, -0xac(%rbp)
movslq -0xc(%rbp), %rsi
leaq 0x904a(%rip), %rdi      # 0x11418
callq 0x3750
movl -0xac(%rbp), %ecx
movl %ecx, 0x58(%rax)
leaq -0x98(%rbp), %rax
movq %rax, -0xa8(%rbp)
movslq -0xc(%rbp), %rsi
leaq 0x9023(%rip), %rdi      # 0x11418
callq 0x3750
movq -0xa8(%rbp), %rdi
movq %rax, %rsi
addq $0x8, %rsi
movl $0x78, %edx
callq 0x2160
movl -0x18(%rbp), %eax
movl %eax, -0x20(%rbp)
leaq 0x9011(%rip), %rdi      # 0x11430
leaq -0x98(%rbp), %rsi
callq 0x84b0
movslq -0xc(%rbp), %rsi
leaq 0x8fe2(%rip), %rdi      # 0x11418
callq 0x3750
movb $0x0, (%rax)
movl -0x4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x4(%rbp)
jmp 0x8182
jmp 0x844e
addq $0xb0, %rsp
popq %rbp
retq
nopw (%rax,%rax)