// File: /workspace/github_c_makefile_star2_O1/simh[P]simtools/crossassemblers/macro1/macro1.c
// Function: read_symbols
void
read_symbols(char *fname)
{
    FILE *f;

    f = fopen(fname, "rb");
    if (!f) {
	perror(fname);
	exit(1);
    }

    /* skip loader */
    for (;;) {
	WORD32 w;

	w = getw(f);
	if (w == -1)
	    goto err;			/* XXX complain? */
	if ((w & OP_CODE) == JMP)
	    break;
	if ((w & OP_CODE) != DIO)
	    goto err;			/* XXX complain? */
	w = getw(f);
	if (w == -1)
	    goto err;			/* XXX complain? */
    }


    /* XXX should push block reader down into a co-routine */
    for (;;) {
	WORD32 start, end, sum;

	start = getw(f);
	if ((start & OP_CODE) == JMP) {
	    fclose(f);
	    return;
	}

	if (start == -1 || (start & OP_CODE) != DIO)
	    goto err;

	end = getw(f);
	if (end == -1 || (end & OP_CODE) != DIO)
	    goto err;			/* XXX complain? */

	sum = start + end;
	while (start < end) {
	    WORD32 sym, val;
	    sym = getw(f);
	    if (sym == -1)
		goto err;
	    sum += sym;
	    start++;
	    /* XXX handle block boundaries? */
	    if (start >= end)
		goto err;
	    val = getw(f);
	    if (val == -1)
		goto err;
	    /*printf("%06o %06o\n", sym, val);*/
	    addsym(sym, val);
	    sum += val;
	    start++;
	}
	start = getw(f);		/* eat checksum XXX verify? */
	if (start == -1)
	    goto err;
	/* roll over all the overflows at once */
	if (sum & ~0777777) {
	    sum = (sum & 0777777) + (sum >> 18);
	    if (sum & 01000000)			/* one more time */
		sum++;
	}
	if (start != sum)
	    goto err;
    }
err:
    fprintf(stderr, "error reading symbol file %s\n", fname);
    exit(1);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rdi, %rbx
leaq 0x3052(%rip), %rsi      # 0x652a
callq 0x2140
testq %rax, %rax
je 0x366b
movq %rax, %r13
movq %r13, %rdi
callq 0x3416
movl $0x4, %ecx
cmpl $-0x1, %eax
je 0x352d
andl $0x3e000, %eax          # imm = 0x3E000
cmpl $0x1a000, %eax          # imm = 0x1A000
je 0x351a
movl $0x2, %ecx
cmpl $0x30000, %eax          # imm = 0x30000
je 0x352d
movl $0x4, %ecx
jmp 0x352d
movq %r13, %rdi
callq 0x3416
xorl %ecx, %ecx
cmpl $-0x1, %eax
sete %cl
shll $0x2, %ecx
testl %ecx, %ecx
je 0x34e9
movq %rbx, 0x10(%rsp)
cmpl $0x2, %ecx
jne 0x3661
movq %r13, 0x8(%rsp)
movq %r13, %rdi
callq 0x3416
movl %eax, %r15d
andl $0x3e000, %eax          # imm = 0x3E000
movl $0x4, %r14d
cmpl $0x1a000, %eax          # imm = 0x1A000
je 0x357f
cmpl $0x30000, %eax          # imm = 0x30000
jne 0x3643
movq %r13, %rdi
callq 0x2080
movl $0x1, %r14d
jmp 0x3643
movq %r13, %rdi
callq 0x3416
movl %eax, %ecx
andl $0x3e000, %ecx          # imm = 0x3E000
cmpl $0x1a000, %ecx          # imm = 0x1A000
jne 0x3643
movl %r15d, %ebp
movl %eax, 0x4(%rsp)
addl %eax, %ebp
movq %r13, %rdi
callq 0x3416
movl %eax, %r12d
movl 0x4(%rsp), %eax
movl $0x4, %r14d
cmpl %eax, %r15d
jge 0x3614
cmpl $-0x1, %r12d
je 0x35fb
addl %r12d, %ebp
leal 0x1(%r15), %ebx
cmpl %eax, %ebx
jge 0x35ff
movq %r13, %rdi
callq 0x3416
movl %eax, %r13d
xorl %eax, %eax
cmpl $-0x1, %r13d
je 0x3606
movl %r12d, %edi
movl %r13d, %esi
callq 0x346f
addl %r13d, %ebp
addl $0x2, %r15d
movb $0x1, %al
xorl %r14d, %r14d
jmp 0x3609
xorl %eax, %eax
jmp 0x360e
xorl %eax, %eax
movl %ebx, %r15d
jmp 0x360e
movl %ebx, %r15d
movq 0x8(%rsp), %r13
testb %al, %al
jne 0x35a4
jmp 0x3643
cmpl $-0x1, %r12d
je 0x3643
cmpl $0x40000, %ebp          # imm = 0x40000
jb 0x3635
movl %ebp, %eax
andl $0x3ffff, %eax          # imm = 0x3FFFF
sarl $0x12, %ebp
leal (%rax,%rbp), %ecx
btl $0x12, %ecx
adcl %eax, %ebp
xorl %r14d, %r14d
cmpl %ebp, %r12d
setne %r14b
shll $0x2, %r14d
testl %r14d, %r14d
je 0x3544
cmpl $0x4, %r14d
je 0x3661
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x10(%rsp), %rdi
callq 0x2349
movq %rbx, %rdi
callq 0x236d