// File: /workspace/github_c_makefile_star2_O3/dwelch67[P]lc3/lc3asm.c
// Function: main
int main ( int argc, char *argv[] )
{
    int ret;
    unsigned int ra;
    unsigned int rb;
    unsigned int inst;
    unsigned int inst2;

    if(argc!=2)
    {
        printf("lc3asm filename\n");
        return(1);
    }

    fpin=fopen(argv[1],"rt");
    if(fpin==NULL)
    {
        printf("Error opening file [%s]\n",argv[1]);
        return(1);
    }
    ret=assemble();
    fclose(fpin);

    if(ret) return(1);
    for(ra=0;ra<nlabs;ra++)
    {
        printf("label%04u: [0x%08X] [%s] %u\n",ra,lab_struct[ra].addr,lab_struct[ra].name,lab_struct[ra].type);

        if(lab_struct[ra].type)
        {
            for(rb=0;rb<nlabs;rb++)
            {
                if(lab_struct[rb].type) continue;
                if(strcmp(lab_struct[ra].name,lab_struct[rb].name)==0)
                {
                    rx=lab_struct[rb].addr;
                    inst=mem[lab_struct[ra].addr];

                    if((inst&0xF000)==0x0000)
                    {
                        //br[n][z][p]
                        rs=0x3000+(lab_struct[ra].addr);
                        rd=rx-(rs+1);
                        if(check_offset9(rd))
                        {
                            printf("<%u> Error: branch too far\n",lab_struct[ra].line);
                            return(1);
                        }
                        inst|=rd&0x01FF;
                        lab_struct[ra].type++;
                    }
                    if((inst&0xF000)==0x2000)
                    {
                        //ld
                        rs=0x3000+(lab_struct[ra].addr);
                        rd=rx-(rs+1);
                        if(check_offset9(rd))
                        {
                            printf("<%u> Error: ld too far\n",lab_struct[ra].line);
                            return(1);
                        }
                        inst|=rd&0x01FF;
                        lab_struct[ra].type++;
                    }
                    if((inst&0xF000)==0xA000)
                    {
                        //ldi
                        rs=0x3000+(lab_struct[ra].addr);
                        rd=rx-(rs+1);
                        if(check_offset9(rd))
                        {
                            printf("<%u> Error: ldi too far\n",lab_struct[ra].line);
                            return(1);
                        }
                        inst|=rd&0x01FF;
                        lab_struct[ra].type++;
                    }
                    if((inst&0xF800)==0x4800)
                    {
                        //jsr
                        rs=0x3000+(lab_struct[ra].addr);
                        rd=rx-(rs+1);
                        if(check_offset11(rd))
                        {
                            printf("<%u> Error: jsr too far\n",lab_struct[ra].line);
                            return(1);
                        }
                        inst|=rd&0x07FF;
                        lab_struct[ra].type++;
                    }
                    if((inst&0xF000)==0xE000)
                    {
                        //lea
                        rs=0x3000+(lab_struct[ra].addr);
                        rd=rx-(rs+1);
                        if(check_offset9(rd))
                        {
                            printf("<%u> Error: lea too far\n",lab_struct[ra].line);
                            return(1);
                        }
                        inst|=rd&0x01FF;
                        lab_struct[ra].type++;
                    }
                    if((inst&0xF000)==0x3000)
                    {
                        //st
                        rs=0x3000+(lab_struct[ra].addr);
                        rd=rx-(rs+1);
                        if(check_offset9(rd))
                        {
                            printf("<%u> Error: st too far\n",lab_struct[ra].line);
                            return(1);
                        }
                        inst|=rd&0x01FF;
                        lab_struct[ra].type++;
                    }
                    if((inst&0xF000)==0xB000)
                    {
                        //sti
                        rs=0x3000+(lab_struct[ra].addr);
                        rd=rx-(rs+1);
                        if(check_offset9(rd))
                        {
                            printf("<%u> Error: sti too far\n",lab_struct[ra].line);
                            return(1);
                        }
                        inst|=rd&0x01FF;
                        lab_struct[ra].type++;
                    }

                    if(lab_struct[ra].type==1)
                    {
                        printf("<%u> Error: internal error, unknown instruction 0x%08X\n",lab_struct[ra].line,inst);
                        return(1);
                    }
                    mem[lab_struct[ra].addr]=inst;
                    break;
                }
            }
            if(rb<nlabs) ; else
            {
                printf("<%u> Error: unresolved label\n",lab_struct[ra].line);
                return(1);
            }
        }
    }
    sprintf(newline,"%s.hex",argv[1]);
    fpout=fopen(newline,"wt");
    if(fpout==NULL)
    {
        printf("Error creating file [%s]\n",newline);
        return(1);
    }

    for(ra=0;ra<=ADDMASK;ra++)
    {
        curradd=0x3000+ra;
        if(mark[ra]&0x80000000)
        {
            printf("0x%04X: 0x%04X\n",curradd,mem[ra]);

            rb=0x02;
            rb+=(curradd>>8)&0xFF;
            rb+=(curradd>>0)&0xFF;
            rb+=0x00;
            rb+=(mem[ra]>> 8)&0xFF;
            rb+=(mem[ra]>> 0)&0xFF;
            rb=(-rb)&0xFF;
            fprintf(fpout,":%02X%04X%02X%04X%02X\n",0x02,curradd&0xFFFF,0x00,mem[ra],rb);
        }
    }
    fclose(fpout);
    return(0);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
cmpl $0x2, %edi
jne 0x302d
movq %rsi, %r14
movq 0x8(%rsi), %rdi
leaq 0x16a5(%rip), %rsi      # 0x4268
callq 0x10b0
movq %rax, 0x543f31(%rip)    # 0x546b00
testq %rax, %rax
je 0x303b
callq 0x1854
movl %eax, %r15d
movq 0x543f19(%rip), %rdi    # 0x546b00
callq 0x1050
movl $0x1, %ebx
testl %r15d, %r15d
jne 0x3069
cmpl $0x0, 0x4c3eef(%rip)    # 0x4c6af0
je 0x2f50
leaq 0x3ee2(%rip), %r12      # 0x6af0
leaq 0x166f(%rip), %r13      # 0x4284
xorl %esi, %esi
imulq $0x4c, %rsi, %r15
leaq (%r12,%r15), %rcx
movl 0x40(%r15,%r12), %edx
movl 0x48(%r15,%r12), %r8d
movq %r13, %rdi
movq %rsi, -0x40(%rbp)
movq %rcx, -0x30(%rbp)
xorl %eax, %eax
callq 0x1060
movl 0x48(%r15,%r12), %eax
movl 0x4c3ea9(%rip), %r8d    # 0x4c6af0
testl %eax, %eax
je 0x2f40
movl %eax, -0x34(%rbp)
testq %r8, %r8
je 0x304f
movq %r12, %r13
movq %r8, %rbx
movq %r8, %r12
movq %r13, %r15
cmpl $0x0, 0x48(%r15)
jne 0x2c7e
movq -0x30(%rbp), %rdi
movq %r15, %rsi
callq 0x1090
testl %eax, %eax
je 0x2c8c
addq $0x4c, %r15
decq %r12
jne 0x2c67
jmp 0x304f
movl 0x40(%r15), %ecx
movl %ecx, 0x3e3a(%rip)      # 0x6ad0
movq -0x30(%rbp), %rax
movl 0x40(%rax), %eax
leaq 0x4c3e5c(%rip), %rdx    # 0x4c6b00
movl (%rdx,%rax,4), %edx
testl $0xf000, %edx           # imm = 0xF000
je 0x2cc0
movq %r13, %r12
movq %rbx, %r8
movl $0x1, %ebx
movl -0x34(%rbp), %r9d
jmp 0x2d18
leal 0x3000(%rax), %esi
movl %esi, 0x3e0c(%rip)      # 0x6ad8
movl %ecx, %esi
subl %eax, %esi
addl $0xffffcfff, %esi       # imm = 0xFFFFCFFF
movl %esi, 0x3df8(%rip)      # 0x6ad4
movl %esi, %edi
shrl $0x8, %edi
testb %dil, %dil
movq %r13, %r12
movq %rbx, %r8
movl -0x34(%rbp), %r9d
je 0x2d00
movzbl %dil, %edi
cmpl $0xff, %edi
jne 0x3112
andl $0x1ff, %esi            # imm = 0x1FF
orl %esi, %edx
incl %r9d
movq -0x30(%rbp), %rsi
movl %r9d, 0x48(%rsi)
movl $0x1, %ebx
movl %edx, %esi
andl $0xf000, %esi           # imm = 0xF000
cmpl $0x2000, %esi           # imm = 0x2000
leaq 0x1557(%rip), %r13      # 0x4284
jne 0x2d78
leal 0x3000(%rax), %esi
movl %esi, 0x3d9d(%rip)      # 0x6ad8
movl %ecx, %esi
subl %eax, %esi
addl $0xffffcfff, %esi       # imm = 0xFFFFCFFF
movl %esi, 0x3d89(%rip)      # 0x6ad4
movl %esi, %edi
shrl $0x8, %edi
testb %dil, %dil
je 0x2d65
movzbl %dil, %edi
cmpl $0xff, %edi
jne 0x30a8
andl $0x1ff, %esi            # imm = 0x1FF
orl %esi, %edx
incl %r9d
movq -0x30(%rbp), %rsi
movl %r9d, 0x48(%rsi)
movl %edx, %esi
andl $0xf000, %esi           # imm = 0xF000
cmpl $0xa000, %esi           # imm = 0xA000
jne 0x2dd1
leal 0x3000(%rax), %esi
movl %esi, 0x3d44(%rip)      # 0x6ad8
movl %ecx, %esi
subl %eax, %esi
addl $0xffffcfff, %esi       # imm = 0xFFFFCFFF
movl %esi, 0x3d30(%rip)      # 0x6ad4
movl %esi, %edi
shrl $0x8, %edi
testb %dil, %dil
je 0x2dbe
movzbl %dil, %edi
cmpl $0xff, %edi
jne 0x30b8
andl $0x1ff, %esi            # imm = 0x1FF
orl %esi, %edx
incl %r9d
movq -0x30(%rbp), %rsi
movl %r9d, 0x48(%rsi)
movl %edx, %esi
andl $0xf800, %esi           # imm = 0xF800
cmpl $0x4800, %esi           # imm = 0x4800
jne 0x2e23
leal 0x3000(%rax), %esi
movl %esi, 0x3ceb(%rip)      # 0x6ad8
movl %ecx, %esi
subl %eax, %esi
addl $0xffffcfff, %esi       # imm = 0xFFFFCFFF
movl %esi, 0x3cd7(%rip)      # 0x6ad4
movl %esi, %edi
shrl $0xa, %edi
andl $0x3f, %edi
je 0x2e10
cmpl $0x3f, %edi
jne 0x30c8
andl $0x7ff, %esi            # imm = 0x7FF
orl %esi, %edx
incl %r9d
movq -0x30(%rbp), %rsi
movl %r9d, 0x48(%rsi)
movl %edx, %esi
andl $0xf000, %esi           # imm = 0xF000
cmpl $0xe000, %esi           # imm = 0xE000
jne 0x2e7c
leal 0x3000(%rax), %esi
movl %esi, 0x3c99(%rip)      # 0x6ad8
movl %ecx, %esi
subl %eax, %esi
addl $0xffffcfff, %esi       # imm = 0xFFFFCFFF
movl %esi, 0x3c85(%rip)      # 0x6ad4
movl %esi, %edi
shrl $0x8, %edi
testb %dil, %dil
je 0x2e69
movzbl %dil, %edi
cmpl $0xff, %edi
jne 0x30d8
andl $0x1ff, %esi            # imm = 0x1FF
orl %esi, %edx
incl %r9d
movq -0x30(%rbp), %rsi
movl %r9d, 0x48(%rsi)
movl %edx, %esi
andl $0xf000, %esi           # imm = 0xF000
cmpl $0x3000, %esi           # imm = 0x3000
jne 0x2ed5
leal 0x3000(%rax), %esi
movl %esi, 0x3c40(%rip)      # 0x6ad8
movl %ecx, %esi
subl %eax, %esi
addl $0xffffcfff, %esi       # imm = 0xFFFFCFFF
movl %esi, 0x3c2c(%rip)      # 0x6ad4
movl %esi, %edi
shrl $0x8, %edi
testb %dil, %dil
je 0x2ec2
movzbl %dil, %edi
cmpl $0xff, %edi
jne 0x30e8
andl $0x1ff, %esi            # imm = 0x1FF
orl %esi, %edx
incl %r9d
movq -0x30(%rbp), %rsi
movl %r9d, 0x48(%rsi)
movl %edx, %esi
andl $0xf000, %esi           # imm = 0xF000
cmpl $0xb000, %esi           # imm = 0xB000
jne 0x2f2c
leal 0x3000(%rax), %esi
movl %esi, 0x3be7(%rip)      # 0x6ad8
subl %eax, %ecx
addl $0xffffcfff, %ecx       # imm = 0xFFFFCFFF
movl %ecx, 0x3bd5(%rip)      # 0x6ad4
movl %ecx, %esi
shrl $0x8, %esi
testb %sil, %sil
je 0x2f19
movzbl %sil, %esi
cmpl $0xff, %esi
jne 0x30f8
andl $0x1ff, %ecx            # imm = 0x1FF
orl %ecx, %edx
incl %r9d
movq -0x30(%rbp), %rcx
movl %r9d, 0x48(%rcx)
cmpl $0x1, %r9d
je 0x3091
leaq 0x4c3bc3(%rip), %rcx    # 0x4c6b00
movl %edx, (%rcx,%rax,4)
movq -0x40(%rbp), %rsi
incq %rsi
cmpq %r8, %rsi
jb 0x2c17
movq 0x8(%r14), %rdx
leaq 0x3365(%rip), %r14      # 0x62c0
leaq 0x1395(%rip), %rsi      # 0x42f7
movq %r14, %rdi
xorl %eax, %eax
callq 0x10d0
leaq 0x138b(%rip), %rsi      # 0x42fe
movq %r14, %rdi
callq 0x10b0
movq %rax, 0x543b8e(%rip)    # 0x546b10
testq %rax, %rax
je 0x307a
movl $0x3000, %ebx           # imm = 0x3000
leaq 0x503b69(%rip), %r12    # 0x506b00
leaq 0x4c3b62(%rip), %r13    # 0x4c6b00
leaq 0x1386(%rip), %r15      # 0x432b
xorl %r14d, %r14d
movl %ebx, 0x4c3b46(%rip)    # 0x4c6af4
cmpl $0x0, (%r14,%r12)
jns 0x300e
movl (%r14,%r13), %edx
leaq 0x135b(%rip), %rdi      # 0x431b
movl %ebx, %esi
xorl %eax, %eax
callq 0x1060
movl 0x4c3b25(%rip), %eax    # 0x4c6af4
movl %eax, %ecx
shrl $0x8, %ecx
movl (%r14,%r13), %r9d
movl %r9d, %edx
shrl $0x8, %edx
addl %eax, %ecx
addl %r9d, %ecx
addl %edx, %ecx
movl $0xfe, %edx
subl %ecx, %edx
movzbl %dl, %edx
movq 0x543b1a(%rip), %rdi    # 0x546b10
movzwl %ax, %ecx
movl %edx, (%rsp)
movq %r15, %rsi
movl $0x2, %edx
xorl %r8d, %r8d
xorl %eax, %eax
callq 0x10a0
incl %ebx
addq $0x4, %r14
cmpq $0x40000, %r14          # imm = 0x40000
jne 0x2fa8
movq 0x543aec(%rip), %rdi    # 0x546b10
callq 0x1050
xorl %ebx, %ebx
jmp 0x3069
leaq 0x130e(%rip), %rdi      # 0x4342
callq 0x1040
jmp 0x3064
movq 0x8(%r14), %rsi
leaq 0x1225(%rip), %rdi      # 0x426b
xorl %eax, %eax
callq 0x1060
jmp 0x3064
movq -0x30(%rbp), %rax
movl 0x44(%rax), %esi
leaq 0x127c(%rip), %rdi      # 0x42d9
xorl %eax, %eax
callq 0x1060
movl $0x1, %ebx
movl %ebx, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1280(%rip), %rdi      # 0x4301
leaq 0x3238(%rip), %rsi      # 0x62c0
xorl %eax, %eax
callq 0x1060
jmp 0x3069
movq -0x30(%rbp), %rax
movl 0x44(%rax), %esi
leaq 0x1202(%rip), %rdi      # 0x42a1
xorl %eax, %eax
callq 0x1060
jmp 0x3069
movq -0x30(%rbp), %rax
movl 0x44(%rax), %esi
leaq 0x10f7(%rip), %rdi      # 0x41ad
jmp 0x3106
movq -0x30(%rbp), %rax
movl 0x44(%rax), %esi
leaq 0x1104(%rip), %rdi      # 0x41ca
jmp 0x3106
movq -0x30(%rbp), %rax
movl 0x44(%rax), %esi
leaq 0x10be(%rip), %rdi      # 0x4194
jmp 0x3106
movq -0x30(%rbp), %rax
movl 0x44(%rax), %esi
leaq 0x1107(%rip), %rdi      # 0x41ed
jmp 0x3106
movq -0x30(%rbp), %rax
movl 0x44(%rax), %esi
leaq 0x1110(%rip), %rdi      # 0x4206
jmp 0x3106
movq -0x30(%rbp), %rax
movl 0x44(%rax), %esi
leaq 0x111d(%rip), %rdi      # 0x4223
xorl %eax, %eax
callq 0x1060
jmp 0x3069
movq -0x30(%rbp), %rax
movl 0x44(%rax), %esi
leaq 0x1030(%rip), %rdi      # 0x4150
jmp 0x305d