// File: /workspace/github_c_makefile_star2_O3/dwelch67[P]lc3/lc3sim.c
// Function: sim
int sim ( void )
{
    unsigned short inst;
    unsigned short test;
    while(1)
    {
        inst=mem[pc]; pc=(pc+1)&MEMMASK;
        switch((inst>>12)&0xF)
        {
            case 0x0: //br
            {
                test=(psr&7)&((inst>>9)&7);
                if(test)
                {
                    pc=pc+sext9(inst&0x1FF);
                }
                break;
            }
            case 0x1: //add
            {
                if(inst&0x20)
                {
                    reg[(inst>>9)&7]=reg[(inst>>6)&7]+sext5(inst&0x1F);
                }
                else
                {
                    reg[(inst>>9)&7]=reg[(inst>>6)&7]+reg[(inst>>0)&7];
                }
                docc(reg[(inst>>9)&7]);
                break;
            }
            case 0x2: //ld
            {
                reg[(inst>>9)&7]=mem[pc+sext9(inst&0x1FF)];
                docc(reg[(inst>>9)&7]);
                break;
            }
            case 0x3: //st
            {
                mem[pc+sext9(inst&0x1FF)]=reg[(inst>>9)&7];
                break;
            }
            case 0x4: //jsr/jsrr
            {
                reg[7]=pc;
                if(inst&0x0800)
                {
                    pc=pc+sext11(inst&0x07FF);
                }
                else
                {
                    pc=reg[(inst>>6)&7];
                }
                break;
            }
            case 0x5: //and
            {
                if(inst&0x20)
                {
                    reg[(inst>>9)&7]=reg[(inst>>6)&7]&sext5(inst&0x1F);
                }
                else
                {
                    reg[(inst>>9)&7]=reg[(inst>>6)&7]&reg[(inst>>0)&7];
                }
                docc(reg[(inst>>9)&7]);
                break;
            }
            case 0x6: //ldr
            {
                reg[(inst>>9)&7]=mem[reg[(inst>>6)&7]+sext6(inst&0x3F)];
                docc(reg[(inst>>9)&7]);
                break;
            }
            case 0x7: //str
            {
                mem[reg[(inst>>6)&7]+sext6(inst&0x3F)]=reg[(inst>>9)&7];
                break;
            }
            case 0x8: //rti
            {
                if(psr&0x8000)
                {
                    printf("exception\n");
                    exit(1);
                }
                else
                {
                    pc=mem[reg[6]++];
                    psr=mem[reg[6]++];
                }
                break;
            }
            case 0x9: //not
            {
                reg[(inst>>9)&7]=~reg[(inst>>6)&7];
                docc(reg[(inst>>9)&7]);
                break;
            }
            case 0xA: //ldi
            {
                reg[(inst>>9)&7]=mem[mem[pc+sext9(inst&0x1FF)]];
                docc(reg[(inst>>9)&7]);
                break;
            }
            case 0xB: //sti
            {
                mem[mem[pc+sext9(inst&0x1FF)]]=reg[(inst>>9)&7];
                break;
            }
            case 0xC: //jmp/ret
            {
                pc=reg[(inst>>6)&7];
                break;
            }
            case 0xD: //undefined
            {
                printf("undefined instruction\n");
                exit(1);
                break;
            }
            case 0xE: //lea
            {
                reg[(inst>>9)&7]=pc+sext9(inst&0x1FF);
                docc(reg[(inst>>9)&7]);
                break;
            }
            case 0xF: //trap
            {
                if((inst&0xFF)==0x67)
                {
                    printf("show 0x%04X\n",reg[0]);
                }
                else
                if((inst&0xFF)==0x68)
                {
                    printf("psr 0x%04X\n",psr);
                }
                else
                if((inst&0xFF)==0x25)
                {
                    printf("halt\n");
                    return(0);
                }
                else
                {
                    reg[7]=pc;
                    pc=mem[inst&0xFF];
                }
                break;
            }
        }
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
leaq 0x2de4(%rip), %r15      # 0x40a0
leaq 0xd41(%rip), %r12       # 0x2004
leaq 0x22de6(%rip), %r13     # 0x240b0
movl $0x4, %ebx
leaq 0xd97(%rip), %r14       # 0x206d
movzwl 0x22dc3(%rip), %ecx     # 0x240a0
movzwl (%r15,%rcx,2), %eax
incl %ecx
movw %cx, 0x22db5(%rip)      # 0x240a0
movl %eax, %edx
shrl $0xc, %edx
movslq (%r12,%rdx,4), %rdx
addq %r12, %rdx
jmpq *%rdx
movl %eax, %edx
shrl $0x9, %edx
andw 0x2d8b(%rip), %dx       # 0x4090
testb $0x7, %dl
je 0x12d6
movl %eax, %edx
andl $0x1ff, %edx            # imm = 0x1FF
shll $0x7, %eax
cwtl
sarl $0xf, %eax
andl $0xfffffe00, %eax       # imm = 0xFFFFFE00
jmp 0x165c
movl %eax, %edx
andl $0x1ff, %edx            # imm = 0x1FF
movl %eax, %esi
shll $0x7, %esi
movswl %si, %esi
sarl $0xf, %esi
andl $0xfffffe00, %esi       # imm = 0xFFFFFE00
orl %edx, %esi
xorl %edx, %edx
movl %esi, %edi
addw %cx, %di
sete %dl
incl %edx
addw %cx, %si
cmovsl %ebx, %edx
shrl $0x8, %eax
andl $0xe, %eax
movw %si, (%rax,%r13)
jmp 0x16a5
movl %eax, %edx
andl $0x1ff, %edx            # imm = 0x1FF
movl %eax, %esi
shll $0x7, %esi
movswl %si, %esi
sarl $0xf, %esi
andl $0xfffffe00, %esi       # imm = 0xFFFFFE00
orl %edx, %esi
movzwl %si, %edx
movzwl %cx, %ecx
addl %edx, %ecx
movzwl (%r15,%rcx,2), %ecx
jmp 0x142c
movl %eax, %edx
shrl $0x8, %edx
andl $0xe, %edx
movzwl (%rdx,%r13), %edx
movl %eax, %esi
andl $0x1ff, %esi            # imm = 0x1FF
shll $0x7, %eax
cwtl
sarl $0xf, %eax
andl $0xfffffe00, %eax       # imm = 0xFFFFFE00
orl %esi, %eax
movzwl %ax, %eax
movzwl %cx, %ecx
addl %eax, %ecx
movzwl (%r15,%rcx,2), %eax
movw %dx, (%r15,%rax,2)
jmp 0x12d6
cmpw $0x0, 0x2cc1(%rip)      # 0x4090
js 0x170b
movzwl 0x22ce0(%rip), %eax     # 0x240bc
leal 0x1(%rax), %ecx
movzwl (%r15,%rax,2), %edx
movw %dx, 0x22cb5(%rip)      # 0x240a0
addl $0x2, %eax
movw %ax, 0x22cc7(%rip)      # 0x240bc
movzwl %cx, %eax
movzwl (%r15,%rax,2), %eax
movw %ax, 0x2c8c(%rip)       # 0x4090
jmp 0x12d6
movl %eax, %edx
andl $0x1ff, %edx            # imm = 0x1FF
movl %eax, %esi
shll $0x7, %esi
movswl %si, %esi
sarl $0xf, %esi
andl $0xfffffe00, %esi       # imm = 0xFFFFFE00
orl %edx, %esi
movzwl %si, %edx
movzwl %cx, %ecx
addl %edx, %ecx
movzwl (%r15,%rcx,2), %edx
jmp 0x1494
movl %eax, %edx
shrl $0x8, %edx
andl $0xe, %edx
movzwl (%rdx,%r13), %edx
movl %eax, %esi
andl $0x1ff, %esi            # imm = 0x1FF
shll $0x7, %eax
cwtl
sarl $0xf, %eax
andl $0xfffffe00, %eax       # imm = 0xFFFFFE00
orl %esi, %eax
movzwl %ax, %eax
movzwl %cx, %ecx
addl %eax, %ecx
movw %dx, (%r15,%rcx,2)
jmp 0x12d6
movl %eax, %ecx
shrl $0x5, %ecx
andl $0xe, %ecx
movzwl (%rcx,%r13), %ecx
movl %eax, %edx
andl $0x3f, %edx
movl %eax, %esi
shll $0xa, %esi
movswl %si, %esi
sarl $0xf, %esi
andl $-0x40, %esi
orl %edx, %esi
movzwl %si, %edx
addl %ecx, %edx
movzwl (%r15,%rdx,2), %edx
xorl %ecx, %ecx
testw %dx, %dx
sete %sil
jns 0x14a6
movl $0x4, %ecx
jmp 0x14ab
movb %sil, %cl
incl %ecx
shrl $0x8, %eax
andl $0xe, %eax
movw %dx, (%rax,%r13)
movzwl 0x2bd3(%rip), %eax      # 0x4090
andl $0xffff8000, %eax       # imm = 0xFFFF8000
orl %ecx, %eax
movw %ax, 0x2bc5(%rip)       # 0x4090
jmp 0x12d6
testb $0x20, %al
jne 0x15ee
movl %eax, %ecx
andl $0x7, %ecx
movzwl (%r13,%rcx,2), %ecx
jmp 0x1603
movw %cx, 0x22bcf(%rip)      # 0x240be
btl $0xb, %eax
jb 0x1648
shrl $0x5, %eax
andl $0xe, %eax
movzwl (%rax,%r13), %eax
movw %ax, 0x22b95(%rip)      # 0x240a0
jmp 0x12d6
movl %eax, %ecx
shrl $0x5, %ecx
andl $0xe, %ecx
movzwl (%rcx,%r13), %ecx
testb $0x20, %al
jne 0x166c
movl %eax, %edx
andl $0x7, %edx
andw (%r13,%rdx,2), %cx
jmp 0x1683
movl %eax, %ecx
shrl $0x5, %ecx
andl $0xe, %ecx
movzwl (%rcx,%r13), %ecx
shrl $0x8, %eax
andl $0xe, %eax
movzwl 0x2b41(%rip), %edx      # 0x4090
andl $0xffff8000, %edx       # imm = 0xFFFF8000
xorl %esi, %esi
cmpw $-0x1, %cx
sete %sil
incl %esi
testw %cx, %cx
notl %ecx
movw %cx, (%rax,%r13)
cmovnsl %ebx, %esi
orl %edx, %esi
movw %si, 0x2b19(%rip)       # 0x4090
jmp 0x12d6
movl %eax, %ecx
shrl $0x8, %ecx
andl $0xe, %ecx
movzwl (%rcx,%r13), %ecx
movl %eax, %edx
shrl $0x5, %edx
andl $0xe, %edx
movzwl (%rdx,%r13), %edx
movl %eax, %esi
andl $0x3f, %esi
shll $0xa, %eax
cwtl
sarl $0xf, %eax
andl $-0x40, %eax
orl %esi, %eax
movzwl %ax, %eax
addl %edx, %eax
movw %cx, (%r15,%rax,2)
jmp 0x12d6
cmpb $0x67, %al
je 0x16cb
movzbl %al, %edx
cmpl $0x68, %edx
je 0x16bf
cmpl $0x25, %edx
je 0x16e5
movzbl %al, %eax
movw %cx, 0x22ae1(%rip)      # 0x240be
movzwl (%r15,%rax,2), %eax
movw %ax, 0x22ab7(%rip)      # 0x240a0
jmp 0x12d6
movl %eax, %edx
andl $0x1f, %edx
movl %eax, %ecx
shll $0xb, %ecx
movswl %cx, %ecx
sarl $0xf, %ecx
andl $-0x20, %ecx
orl %edx, %ecx
movl %eax, %edx
shrl $0x5, %edx
andl $0xe, %edx
movzwl (%rdx,%r13), %edx
xorl %esi, %esi
movl %ecx, %edi
addw %dx, %di
sete %sil
incl %esi
addw %dx, %cx
cmovsl %ebx, %esi
shrl $0x8, %eax
andl $0xe, %eax
movw %cx, (%rax,%r13)
movzwl 0x2a5b(%rip), %eax      # 0x4090
andl $0xffff8000, %eax       # imm = 0xFFFF8000
orl %esi, %eax
movw %ax, 0x2a4d(%rip)       # 0x4090
jmp 0x12d6
movl %eax, %edx
andl $0x7ff, %edx            # imm = 0x7FF
shll $0x5, %eax
cwtl
sarl $0xf, %eax
andl $0xfffff800, %eax       # imm = 0xFFFFF800
addl %edx, %ecx
addl %eax, %ecx
movw %cx, 0x22a39(%rip)      # 0x240a0
jmp 0x12d6
movl %eax, %edx
andl $0x1f, %edx
movl %eax, %esi
shll $0xb, %esi
movswl %si, %esi
sarl $0xf, %esi
andl $-0x20, %esi
orl %edx, %esi
andl %esi, %ecx
xorl %edx, %edx
testw %cx, %cx
sete %sil
jns 0x1695
movl $0x4, %edx
jmp 0x169a
movb %sil, %dl
incl %edx
shrl $0x8, %eax
andl $0xe, %eax
movw %cx, (%rax,%r13)
movzwl 0x29e4(%rip), %eax      # 0x4090
andl $0xffff8000, %eax       # imm = 0xFFFF8000
orl %edx, %eax
movw %ax, 0x29d6(%rip)       # 0x4090
jmp 0x12d6
movzwl 0x29ca(%rip), %esi      # 0x4090
movq %r14, %rdi
jmp 0x16d9
movzwl 0x229de(%rip), %esi     # 0x240b0
leaq 0x987(%rip), %rdi       # 0x2060
xorl %eax, %eax
callq 0x1050
jmp 0x12d6
leaq 0x9c2(%rip), %rdi       # 0x20ae
callq 0x1030
xorl %eax, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x9aa(%rip), %rdi       # 0x20b3
jmp 0x1712
leaq 0x9b7(%rip), %rdi       # 0x20c9
callq 0x1030
movl $0x1, %edi
callq 0x10b0