// File: /workspace/github_c_makefile_star2_O0/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
subq $0x50, %rsp
movzwl 0x22da1(%rip), %eax     # 0x24090
movl %eax, %ecx
leaq 0x2d98(%rip), %rax      # 0x4090
movw (%rax,%rcx,2), %ax
movw %ax, -0x2(%rbp)
movw 0x22d89(%rip), %ax      # 0x24090
incw %ax
movw %ax, 0x22d7f(%rip)      # 0x24090
movzwl -0x2(%rbp), %eax
shrl $0xc, %eax
movl %eax, %ecx
movq %rcx, -0x10(%rbp)
subl $0xf, %eax
ja 0x1a41
movq -0x10(%rbp), %rax
leaq 0xcd2(%rip), %rcx       # 0x2004
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movzwl 0x2d3e(%rip), %eax      # 0x4080
andl $0x7, %eax
movzwl -0x2(%rbp), %ecx
sarl $0x9, %ecx
andl $0x7, %ecx
andl %ecx, %eax
movw %ax, -0x4(%rbp)
cmpw $0x0, -0x4(%rbp)
je 0x1389
movzwl 0x22d2d(%rip), %eax     # 0x24090
movl %eax, -0x14(%rbp)
movzwl -0x2(%rbp), %eax
andl $0x1ff, %eax            # imm = 0x1FF
movzwl %ax, %edi
callq 0x1210
movw %ax, %cx
movl -0x14(%rbp), %eax
movzwl %cx, %ecx
addl %ecx, %eax
movw %ax, 0x22d07(%rip)      # 0x24090
jmp 0x1a41
movzwl -0x2(%rbp), %eax
andl $0x20, %eax
cmpl $0x0, %eax
je 0x13ec
movzwl -0x2(%rbp), %eax
sarl $0x6, %eax
andl $0x7, %eax
movslq %eax, %rcx
leaq 0x22cf2(%rip), %rax     # 0x240a0
movzwl (%rax,%rcx,2), %eax
movl %eax, -0x18(%rbp)
movzwl -0x2(%rbp), %eax
andl $0x1f, %eax
movzwl %ax, %edi
callq 0x11b0
movw %ax, %cx
movl -0x18(%rbp), %eax
movzwl %cx, %ecx
addl %ecx, %eax
movw %ax, %dx
movzwl -0x2(%rbp), %eax
sarl $0x9, %eax
andl $0x7, %eax
movslq %eax, %rcx
leaq 0x22cba(%rip), %rax     # 0x240a0
movw %dx, (%rax,%rcx,2)
jmp 0x1439
movzwl -0x2(%rbp), %eax
sarl $0x6, %eax
andl $0x7, %eax
movslq %eax, %rcx
leaq 0x22ca0(%rip), %rax     # 0x240a0
movzwl (%rax,%rcx,2), %eax
movzwl -0x2(%rbp), %ecx
sarl $0x0, %ecx
andl $0x7, %ecx
movslq %ecx, %rdx
leaq 0x22c88(%rip), %rcx     # 0x240a0
movzwl (%rcx,%rdx,2), %ecx
addl %ecx, %eax
movw %ax, %dx
movzwl -0x2(%rbp), %eax
sarl $0x9, %eax
andl $0x7, %eax
movslq %eax, %rcx
leaq 0x22c6b(%rip), %rax     # 0x240a0
movw %dx, (%rax,%rcx,2)
movzwl -0x2(%rbp), %eax
sarl $0x9, %eax
andl $0x7, %eax
movslq %eax, %rcx
leaq 0x22c53(%rip), %rax     # 0x240a0
movzwl (%rax,%rcx,2), %edi
callq 0x1270
jmp 0x1a41
movzwl 0x22c2e(%rip), %eax     # 0x24090
movl %eax, -0x1c(%rbp)
movzwl -0x2(%rbp), %eax
andl $0x1ff, %eax            # imm = 0x1FF
movzwl %ax, %edi
callq 0x1210
movw %ax, %cx
movl -0x1c(%rbp), %eax
movzwl %cx, %ecx
addl %ecx, %eax
movslq %eax, %rcx
leaq 0x2c05(%rip), %rax      # 0x4090
movw (%rax,%rcx,2), %dx
movzwl -0x2(%rbp), %eax
sarl $0x9, %eax
andl $0x7, %eax
movslq %eax, %rcx
leaq 0x22bfd(%rip), %rax     # 0x240a0
movw %dx, (%rax,%rcx,2)
movzwl -0x2(%rbp), %eax
sarl $0x9, %eax
andl $0x7, %eax
movslq %eax, %rcx
leaq 0x22be5(%rip), %rax     # 0x240a0
movzwl (%rax,%rcx,2), %edi
callq 0x1270
jmp 0x1a41
movzwl -0x2(%rbp), %eax
sarl $0x9, %eax
andl $0x7, %eax
movslq %eax, %rcx
leaq 0x22bc3(%rip), %rax     # 0x240a0
movw (%rax,%rcx,2), %ax
movw %ax, -0x22(%rbp)
movzwl 0x22ba4(%rip), %eax     # 0x24090
movl %eax, -0x20(%rbp)
movzwl -0x2(%rbp), %eax
andl $0x1ff, %eax            # imm = 0x1FF
movzwl %ax, %edi
callq 0x1210
movw -0x22(%rbp), %dx
movw %ax, %cx
movl -0x20(%rbp), %eax
movzwl %cx, %ecx
addl %ecx, %eax
movslq %eax, %rcx
leaq 0x2b77(%rip), %rax      # 0x4090
movw %dx, (%rax,%rcx,2)
jmp 0x1a41
movw 0x22b67(%rip), %ax      # 0x24090
movw %ax, 0x22b7e(%rip)      # 0x240ae
movzwl -0x2(%rbp), %eax
andl $0x800, %eax            # imm = 0x800
cmpl $0x0, %eax
je 0x156d
movzwl 0x22b4b(%rip), %eax     # 0x24090
movl %eax, -0x28(%rbp)
movzwl -0x2(%rbp), %eax
andl $0x7ff, %eax            # imm = 0x7FF
movzwl %ax, %edi
callq 0x1240
movw %ax, %cx
movl -0x28(%rbp), %eax
movzwl %cx, %ecx
addl %ecx, %eax
movw %ax, 0x22b25(%rip)      # 0x24090
jmp 0x158c
movzwl -0x2(%rbp), %eax
sarl $0x6, %eax
andl $0x7, %eax
movslq %eax, %rcx
leaq 0x22b1f(%rip), %rax     # 0x240a0
movw (%rax,%rcx,2), %ax
movw %ax, 0x22b04(%rip)      # 0x24090
jmp 0x1a41
movzwl -0x2(%rbp), %eax
andl $0x20, %eax
cmpl $0x0, %eax
je 0x15ef
movzwl -0x2(%rbp), %eax
sarl $0x6, %eax
andl $0x7, %eax
movslq %eax, %rcx
leaq 0x22aef(%rip), %rax     # 0x240a0
movzwl (%rax,%rcx,2), %eax
movl %eax, -0x2c(%rbp)
movzwl -0x2(%rbp), %eax
andl $0x1f, %eax
movzwl %ax, %edi
callq 0x11b0
movw %ax, %cx
movl -0x2c(%rbp), %eax
movzwl %cx, %ecx
andl %ecx, %eax
movw %ax, %dx
movzwl -0x2(%rbp), %eax
sarl $0x9, %eax
andl $0x7, %eax
movslq %eax, %rcx
leaq 0x22ab7(%rip), %rax     # 0x240a0
movw %dx, (%rax,%rcx,2)
jmp 0x163c
movzwl -0x2(%rbp), %eax
sarl $0x6, %eax
andl $0x7, %eax
movslq %eax, %rcx
leaq 0x22a9d(%rip), %rax     # 0x240a0
movzwl (%rax,%rcx,2), %eax
movzwl -0x2(%rbp), %ecx
sarl $0x0, %ecx
andl $0x7, %ecx
movslq %ecx, %rdx
leaq 0x22a85(%rip), %rcx     # 0x240a0
movzwl (%rcx,%rdx,2), %ecx
andl %ecx, %eax
movw %ax, %dx
movzwl -0x2(%rbp), %eax
sarl $0x9, %eax
andl $0x7, %eax
movslq %eax, %rcx
leaq 0x22a68(%rip), %rax     # 0x240a0
movw %dx, (%rax,%rcx,2)
movzwl -0x2(%rbp), %eax
sarl $0x9, %eax
andl $0x7, %eax
movslq %eax, %rcx
leaq 0x22a50(%rip), %rax     # 0x240a0
movzwl (%rax,%rcx,2), %edi
callq 0x1270
jmp 0x1a41
movzwl -0x2(%rbp), %eax
sarl $0x6, %eax
andl $0x7, %eax
movslq %eax, %rcx
leaq 0x22a2e(%rip), %rax     # 0x240a0
movzwl (%rax,%rcx,2), %eax
movl %eax, -0x30(%rbp)
movzwl -0x2(%rbp), %eax
andl $0x3f, %eax
movzwl %ax, %edi
callq 0x11e0
movw %ax, %cx
movl -0x30(%rbp), %eax
movzwl %cx, %ecx
addl %ecx, %eax
movslq %eax, %rcx
leaq 0x29f3(%rip), %rax      # 0x4090
movw (%rax,%rcx,2), %dx
movzwl -0x2(%rbp), %eax
sarl $0x9, %eax
andl $0x7, %eax
movslq %eax, %rcx
leaq 0x229eb(%rip), %rax     # 0x240a0
movw %dx, (%rax,%rcx,2)
movzwl -0x2(%rbp), %eax
sarl $0x9, %eax
andl $0x7, %eax
movslq %eax, %rcx
leaq 0x229d3(%rip), %rax     # 0x240a0
movzwl (%rax,%rcx,2), %edi
callq 0x1270
jmp 0x1a41
movzwl -0x2(%rbp), %eax
sarl $0x9, %eax
andl $0x7, %eax
movslq %eax, %rcx
leaq 0x229b1(%rip), %rax     # 0x240a0
movw (%rax,%rcx,2), %ax
movw %ax, -0x36(%rbp)
movzwl -0x2(%rbp), %eax
sarl $0x6, %eax
andl $0x7, %eax
movslq %eax, %rcx
leaq 0x22995(%rip), %rax     # 0x240a0
movzwl (%rax,%rcx,2), %eax
movl %eax, -0x34(%rbp)
movzwl -0x2(%rbp), %eax
andl $0x3f, %eax
movzwl %ax, %edi
callq 0x11e0
movw -0x36(%rbp), %dx
movw %ax, %cx
movl -0x34(%rbp), %eax
movzwl %cx, %ecx
addl %ecx, %eax
movslq %eax, %rcx
leaq 0x2956(%rip), %rax      # 0x4090
movw %dx, (%rax,%rcx,2)
jmp 0x1a41
movzwl 0x2936(%rip), %eax      # 0x4080
andl $0x8000, %eax           # imm = 0x8000
cmpl $0x0, %eax
je 0x176c
leaq 0x8e9(%rip), %rdi       # 0x2044
movb $0x0, %al
callq 0x1040
movl $0x1, %edi
callq 0x10a0
movw 0x22939(%rip), %ax      # 0x240ac
movw %ax, %cx
addw $0x1, %cx
movw %cx, 0x2292b(%rip)      # 0x240ac
movzwl %ax, %eax
movl %eax, %ecx
leaq 0x2903(%rip), %rax      # 0x4090
movw (%rax,%rcx,2), %ax
movw %ax, 0x228f8(%rip)      # 0x24090
movw 0x2290d(%rip), %ax      # 0x240ac
movw %ax, %cx
addw $0x1, %cx
movw %cx, 0x228ff(%rip)      # 0x240ac
movzwl %ax, %eax
movl %eax, %ecx
leaq 0x28d7(%rip), %rax      # 0x4090
movw (%rax,%rcx,2), %ax
movw %ax, 0x28bc(%rip)       # 0x4080
jmp 0x1a41
movzwl -0x2(%rbp), %eax
sarl $0x6, %eax
andl $0x7, %eax
movslq %eax, %rcx
leaq 0x228c3(%rip), %rax     # 0x240a0
movzwl (%rax,%rcx,2), %eax
xorl $-0x1, %eax
movw %ax, %dx
movzwl -0x2(%rbp), %eax
sarl $0x9, %eax
andl $0x7, %eax
movslq %eax, %rcx
leaq 0x228a5(%rip), %rax     # 0x240a0
movw %dx, (%rax,%rcx,2)
movzwl -0x2(%rbp), %eax
sarl $0x9, %eax
andl $0x7, %eax
movslq %eax, %rcx
leaq 0x2288d(%rip), %rax     # 0x240a0
movzwl (%rax,%rcx,2), %edi
callq 0x1270
jmp 0x1a41
movzwl 0x22868(%rip), %eax     # 0x24090
movl %eax, -0x3c(%rbp)
movzwl -0x2(%rbp), %eax
andl $0x1ff, %eax            # imm = 0x1FF
movzwl %ax, %edi
callq 0x1210
movw %ax, %cx
movl -0x3c(%rbp), %eax
movzwl %cx, %ecx
addl %ecx, %eax
movslq %eax, %rcx
leaq 0x283f(%rip), %rax      # 0x4090
movzwl (%rax,%rcx,2), %eax
movl %eax, %ecx
leaq 0x2832(%rip), %rax      # 0x4090
movw (%rax,%rcx,2), %dx
movzwl -0x2(%rbp), %eax
sarl $0x9, %eax
andl $0x7, %eax
movslq %eax, %rcx
leaq 0x2282a(%rip), %rax     # 0x240a0
movw %dx, (%rax,%rcx,2)
movzwl -0x2(%rbp), %eax
sarl $0x9, %eax
andl $0x7, %eax
movslq %eax, %rcx
leaq 0x22812(%rip), %rax     # 0x240a0
movzwl (%rax,%rcx,2), %edi
callq 0x1270
jmp 0x1a41
movzwl -0x2(%rbp), %eax
sarl $0x9, %eax
andl $0x7, %eax
movslq %eax, %rcx
leaq 0x227f0(%rip), %rax     # 0x240a0
movw (%rax,%rcx,2), %ax
movw %ax, -0x42(%rbp)
movzwl 0x227d1(%rip), %eax     # 0x24090
movl %eax, -0x40(%rbp)
movzwl -0x2(%rbp), %eax
andl $0x1ff, %eax            # imm = 0x1FF
movzwl %ax, %edi
callq 0x1210
movw -0x42(%rbp), %dx
movw %ax, %cx
movl -0x40(%rbp), %eax
movzwl %cx, %ecx
addl %ecx, %eax
movslq %eax, %rcx
leaq 0x27a4(%rip), %rax      # 0x4090
movzwl (%rax,%rcx,2), %eax
movl %eax, %ecx
leaq 0x2797(%rip), %rax      # 0x4090
movw %dx, (%rax,%rcx,2)
jmp 0x1a41
movzwl -0x2(%rbp), %eax
sarl $0x6, %eax
andl $0x7, %eax
movslq %eax, %rcx
leaq 0x2278a(%rip), %rax     # 0x240a0
movw (%rax,%rcx,2), %ax
movw %ax, 0x2276f(%rip)      # 0x24090
jmp 0x1a41
leaq 0x722(%rip), %rdi       # 0x204f
movb $0x0, %al
callq 0x1040
movl $0x1, %edi
callq 0x10a0
movzwl 0x2274b(%rip), %eax     # 0x24090
movl %eax, -0x48(%rbp)
movzwl -0x2(%rbp), %eax
andl $0x1ff, %eax            # imm = 0x1FF
movzwl %ax, %edi
callq 0x1210
movw %ax, %cx
movl -0x48(%rbp), %eax
movzwl %cx, %ecx
addl %ecx, %eax
movw %ax, %dx
movzwl -0x2(%rbp), %eax
sarl $0x9, %eax
andl $0x7, %eax
movslq %eax, %rcx
leaq 0x22725(%rip), %rax     # 0x240a0
movw %dx, (%rax,%rcx,2)
movzwl -0x2(%rbp), %eax
sarl $0x9, %eax
andl $0x7, %eax
movslq %eax, %rcx
leaq 0x2270d(%rip), %rax     # 0x240a0
movzwl (%rax,%rcx,2), %edi
callq 0x1270
jmp 0x1a41
movzwl -0x2(%rbp), %eax
andl $0xff, %eax
cmpl $0x67, %eax
jne 0x19c6
movzwl 0x226ea(%rip), %esi     # 0x240a0
leaq 0x6a9(%rip), %rdi       # 0x2066
movb $0x0, %al
callq 0x1040
jmp 0x1a3f
movzwl -0x2(%rbp), %eax
andl $0xff, %eax
cmpl $0x68, %eax
jne 0x19eb
movzwl 0x26a5(%rip), %esi      # 0x4080
leaq 0x691(%rip), %rdi       # 0x2073
movb $0x0, %al
callq 0x1040
jmp 0x1a3d
movzwl -0x2(%rbp), %eax
andl $0xff, %eax
cmpl $0x25, %eax
jne 0x1a0f
leaq 0x67f(%rip), %rdi       # 0x207f
movb $0x0, %al
callq 0x1040
xorl %eax, %eax
addq $0x50, %rsp
popq %rbp
retq
movw 0x2267a(%rip), %ax      # 0x24090
movw %ax, 0x22691(%rip)      # 0x240ae
movzwl -0x2(%rbp), %eax
andl $0xff, %eax
movslq %eax, %rcx
leaq 0x2660(%rip), %rax      # 0x4090
movw (%rax,%rcx,2), %ax
movw %ax, 0x22655(%rip)      # 0x24090
jmp 0x1a3d
jmp 0x1a3f
jmp 0x1a41
jmp 0x12e8
nopw %cs:(%rax,%rax)