// File: Ziang95[P]Tomasulo_simulator/FU.cpp
// Function: FU_Q::enQ(opCode, int, memCell*, memCell*, memCell*, LSQEntry*, int, bool*)
const FU_QEntry* FU_Q::enQ(opCode c, int d, memCell *r, memCell *op1, memCell *op2, LSQEntry *_lsqe, int offst, bool* busy)
{
    pthread_mutex_lock(&Q_lock);
    if ((rear+1)%Q_LEN == front)
    {
        pthread_mutex_unlock(&Q_lock);
        throw -1;
    }
    int index = rear;
    rear = (++rear)%Q_LEN;
    int pos = 0;
    for (; (front + pos)%Q_LEN != index && queue[(front + pos)%Q_LEN].dest < d; pos++);
    for (int i = index; (Q_LEN + i)%Q_LEN != (front + pos)%Q_LEN; i--)
        queue[(Q_LEN+i)%Q_LEN] = queue[(Q_LEN+i-1)%Q_LEN];
    index = (front+pos)%Q_LEN;
    queue[index].code = c;
    queue[index].dest = d;
    queue[index].res = r;
    queue[index].oprnd1 = op1;
    queue[index].oprnd2 = op2;
    queue[index].offset = offst;
    queue[index].lsqe = _lsqe;
    queue[index].busy = busy;
    pthread_mutex_unlock(&Q_lock);
    return &queue[index];
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x50, %rsp
movq 0x20(%rbp), %rax
movl 0x18(%rbp), %eax
movq 0x10(%rbp), %rax
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movl %edx, -0x10(%rbp)
movq %rcx, -0x18(%rbp)
movq %r8, -0x20(%rbp)
movq %r9, -0x28(%rbp)
movq -0x8(%rbp), %rdi
movq %rdi, -0x40(%rbp)
addq $0x3808, %rdi           # imm = 0x3808
callq 0x4510
movq -0x40(%rbp), %rax
movl 0x3804(%rax), %eax
addl $0x1, %eax
movl $0x100, %ecx            # imm = 0x100
cltd
idivl %ecx
movq -0x40(%rbp), %rax
cmpl 0x3800(%rax), %edx
jne 0x5481
movq -0x40(%rbp), %rdi
addq $0x3808, %rdi           # imm = 0x3808
callq 0x42c0
movl $0x4, %edi
callq 0x41c0
movq %rax, %rdi
movl $0xffffffff, (%rdi)     # imm = 0xFFFFFFFF
movq 0x29b20(%rip), %rsi     # 0x2ef98
xorl %eax, %eax
movl %eax, %edx
callq 0x45c0
movq -0x40(%rbp), %rcx
movl 0x3804(%rcx), %eax
movl %eax, -0x2c(%rbp)
movl 0x3804(%rcx), %eax
addl $0x1, %eax
movl %eax, 0x3804(%rcx)
movl $0x100, %ecx            # imm = 0x100
cltd
idivl %ecx
movq -0x40(%rbp), %rax
movl %edx, 0x3804(%rax)
movl $0x0, -0x30(%rbp)
movq -0x40(%rbp), %rax
movl 0x3800(%rax), %eax
addl -0x30(%rbp), %eax
movl $0x100, %ecx            # imm = 0x100
cltd
idivl %ecx
xorl %eax, %eax
cmpl -0x2c(%rbp), %edx
movb %al, -0x41(%rbp)
je 0x5504
movq -0x40(%rbp), %rax
movl 0x3800(%rax), %eax
addl -0x30(%rbp), %eax
movl $0x100, %ecx            # imm = 0x100
cltd
idivl %ecx
movq -0x40(%rbp), %rax
movslq %edx, %rcx
imulq $0x38, %rcx, %rcx
addq %rcx, %rax
movl 0x4(%rax), %eax
cmpl -0x10(%rbp), %eax
setl %al
movb %al, -0x41(%rbp)
movb -0x41(%rbp), %al
testb $0x1, %al
jne 0x550d
jmp 0x551a
jmp 0x550f
movl -0x30(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x30(%rbp)
jmp 0x54b6
movl -0x2c(%rbp), %eax
movl %eax, -0x34(%rbp)
movl -0x34(%rbp), %eax
addl $0x100, %eax            # imm = 0x100
movl $0x100, %ecx            # imm = 0x100
cltd
idivl %ecx
movq -0x40(%rbp), %rax
movl %edx, -0x48(%rbp)
movl 0x3800(%rax), %eax
addl -0x30(%rbp), %eax
movl $0x100, %ecx            # imm = 0x100
cltd
idivl %ecx
movl %edx, %eax
movl -0x48(%rbp), %edx
cmpl %eax, %edx
je 0x55a7
movq -0x40(%rbp), %rdi
movl -0x34(%rbp), %eax
addl $0x100, %eax            # imm = 0x100
subl $0x1, %eax
movl $0x100, %ecx            # imm = 0x100
cltd
idivl %ecx
movslq %edx, %rax
imulq $0x38, %rax, %rax
movq %rdi, %rsi
addq %rax, %rsi
movl -0x34(%rbp), %eax
addl $0x100, %eax            # imm = 0x100
movl $0x100, %ecx            # imm = 0x100
cltd
idivl %ecx
movslq %edx, %rax
imulq $0x38, %rax, %rax
addq %rax, %rdi
movl $0x38, %edx
callq 0x42f0
movl -0x34(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0x5520
movq -0x40(%rbp), %rdi
movl 0x3800(%rdi), %eax
addl -0x30(%rbp), %eax
movl $0x100, %ecx            # imm = 0x100
cltd
idivl %ecx
movl %edx, -0x2c(%rbp)
movl -0xc(%rbp), %ecx
movslq -0x2c(%rbp), %rax
imulq $0x38, %rax, %rdx
movq %rdi, %rax
addq %rdx, %rax
movl %ecx, (%rax)
movl -0x10(%rbp), %ecx
movslq -0x2c(%rbp), %rax
imulq $0x38, %rax, %rdx
movq %rdi, %rax
addq %rdx, %rax
movl %ecx, 0x4(%rax)
movq -0x18(%rbp), %rcx
movslq -0x2c(%rbp), %rax
imulq $0x38, %rax, %rdx
movq %rdi, %rax
addq %rdx, %rax
movq %rcx, 0x8(%rax)
movq -0x20(%rbp), %rcx
movslq -0x2c(%rbp), %rax
imulq $0x38, %rax, %rdx
movq %rdi, %rax
addq %rdx, %rax
movq %rcx, 0x10(%rax)
movq -0x28(%rbp), %rcx
movslq -0x2c(%rbp), %rax
imulq $0x38, %rax, %rdx
movq %rdi, %rax
addq %rdx, %rax
movq %rcx, 0x18(%rax)
movl 0x18(%rbp), %ecx
movslq -0x2c(%rbp), %rax
imulq $0x38, %rax, %rdx
movq %rdi, %rax
addq %rdx, %rax
movl %ecx, 0x28(%rax)
movq 0x10(%rbp), %rcx
movslq -0x2c(%rbp), %rax
imulq $0x38, %rax, %rdx
movq %rdi, %rax
addq %rdx, %rax
movq %rcx, 0x20(%rax)
movq 0x20(%rbp), %rcx
movslq -0x2c(%rbp), %rax
imulq $0x38, %rax, %rdx
movq %rdi, %rax
addq %rdx, %rax
movq %rcx, 0x30(%rax)
addq $0x3808, %rdi           # imm = 0x3808
callq 0x42c0
movq -0x40(%rbp), %rax
movslq -0x2c(%rbp), %rcx
imulq $0x38, %rcx, %rcx
addq %rcx, %rax
addq $0x50, %rsp
popq %rbp
retq
nopl (%rax)