// File: /workspace/github_c_makefile_star2_O2/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 0x2e44(%rip), %r15      # 0x40a0
leaq 0xda1(%rip), %r12       # 0x2004
leaq 0x22e46(%rip), %r13     # 0x240b0
leaq 0xdef(%rip), %rbx       # 0x2060
leaq 0xdf5(%rip), %r14       # 0x206d
movzwl 0x22e21(%rip), %ecx     # 0x240a0
movzwl (%r15,%rcx,2), %eax
incl %ecx
movw %cx, 0x22e13(%rip)      # 0x240a0
movl %eax, %edx
shrl $0xc, %edx
movslq (%r12,%rdx,4), %rsi
addq %r12, %rsi
movzwl %cx, %edx
jmpq *%rsi
movl %eax, %edx
shrl $0x9, %edx
andw 0x2de6(%rip), %dx       # 0x4090
testb $0x7, %dl
je 0x1278
movl %eax, %edx
andl $0x1ff, %edx            # imm = 0x1FF
shll $0x7, %eax
cwtl
sarl $0xf, %eax
andl $0xfffffe00, %eax       # imm = 0xFFFFFE00
jmp 0x155a
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
addl %esi, %ecx
shrl $0x8, %eax
andl $0xe, %eax
movw %cx, (%rax,%r13)
movzwl %cx, %edi
callq 0x1218
jmp 0x1278
movl %eax, %ecx
andl $0x1ff, %ecx            # imm = 0x1FF
movl %eax, %esi
shll $0x7, %esi
movswl %si, %esi
sarl $0xf, %esi
andl $0xfffffe00, %esi       # imm = 0xFFFFFE00
orl %ecx, %esi
movzwl %si, %ecx
addl %edx, %ecx
movzwl (%r15,%rcx,2), %ecx
jmp 0x13c0
movl %eax, %ecx
shrl $0x8, %ecx
andl $0xe, %ecx
movzwl (%rcx,%r13), %ecx
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
addl %edx, %eax
movzwl (%r15,%rax,2), %eax
movw %cx, (%r15,%rax,2)
jmp 0x1278
cmpw $0x0, 0x2d2a(%rip)      # 0x4090
js 0x15e1
movzwl 0x22d49(%rip), %eax     # 0x240bc
leal 0x1(%rax), %ecx
movzwl (%r15,%rax,2), %edx
movw %dx, 0x22d1e(%rip)      # 0x240a0
addl $0x2, %eax
movw %ax, 0x22d30(%rip)      # 0x240bc
movzwl %cx, %eax
movzwl (%r15,%rax,2), %eax
movw %ax, 0x2cf5(%rip)       # 0x4090
jmp 0x1278
movl %eax, %ecx
andl $0x1ff, %ecx            # imm = 0x1FF
movl %eax, %esi
shll $0x7, %esi
movswl %si, %esi
sarl $0xf, %esi
andl $0xfffffe00, %esi       # imm = 0xFFFFFE00
orl %ecx, %esi
movzwl %si, %ecx
addl %edx, %ecx
movzwl (%r15,%rcx,2), %edi
jmp 0x1419
movl %eax, %ecx
shrl $0x8, %ecx
andl $0xe, %ecx
movzwl (%rcx,%r13), %ecx
movl %eax, %esi
andl $0x1ff, %esi            # imm = 0x1FF
shll $0x7, %eax
cwtl
sarl $0xf, %eax
andl $0xfffffe00, %eax       # imm = 0xFFFFFE00
jmp 0x14d7
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), %edi
shrl $0x8, %eax
andl $0xe, %eax
jmp 0x149f
testb $0x20, %al
jne 0x1520
movl %eax, %ecx
andl $0x7, %ecx
movzwl (%r13,%rcx,2), %ecx
jmp 0x1535
movw %cx, 0x22c7e(%rip)      # 0x240be
btl $0xb, %eax
jb 0x1546
shrl $0x5, %eax
andl $0xe, %eax
movzwl (%rax,%r13), %eax
movw %ax, 0x22c44(%rip)      # 0x240a0
jmp 0x1278
movl %eax, %ecx
shrl $0x5, %ecx
andl $0xe, %ecx
movzwl (%rcx,%r13), %ecx
testb $0x20, %al
jne 0x156a
movl %eax, %edx
andl $0x7, %edx
andw (%r13,%rdx,2), %cx
jmp 0x1581
movl %eax, %ecx
shrl $0x5, %ecx
andl $0xe, %ecx
movzwl (%rcx,%r13), %edi
shrl $0x8, %eax
andl $0xe, %eax
xorl $0xffff, %edi           # imm = 0xFFFF
movw %di, (%rax,%r13)
callq 0x1218
jmp 0x1278
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 0x1278
cmpb $0x67, %al
je 0x15a5
movzbl %al, %edx
cmpl $0x68, %edx
je 0x1599
cmpl $0x25, %edx
je 0x15bb
movzbl %al, %eax
movw %cx, 0x22baf(%rip)      # 0x240be
movzwl (%r15,%rax,2), %eax
movw %ax, 0x22b85(%rip)      # 0x240a0
jmp 0x1278
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
addl %edx, %ecx
jmp 0x1581
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, 0x22b3b(%rip)      # 0x240a0
jmp 0x1278
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
shrl $0x8, %eax
andl $0xe, %eax
movw %cx, (%rax,%r13)
movzwl %cx, %edi
callq 0x1218
jmp 0x1278
movzwl 0x2af0(%rip), %esi      # 0x4090
movq %r14, %rdi
jmp 0x15af
movzwl 0x22b04(%rip), %esi     # 0x240b0
movq %rbx, %rdi
xorl %eax, %eax
callq 0x1050
jmp 0x1278
leaq 0xaec(%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 0xad4(%rip), %rdi       # 0x20b3
jmp 0x15e8
leaq 0xae1(%rip), %rdi       # 0x20c9
callq 0x1030
pushq $0x1
popq %rdi
callq 0x10b0