// File: /workspace/github_c_makefile_star2_O2/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
pushq %rax
leaq 0x32cd(%rip), %rsi      # 0x652a
movq %rdi, (%rsp)
callq 0x2140
testq %rax, %rax
je 0x3394
movq %rax, %r14
movq %r14, %rdi
callq 0x3197
cmpl $-0x1, %eax
je 0x3370
andl $0x3e000, %eax          # imm = 0x3E000
cmpl $0x1a000, %eax          # imm = 0x1A000
jne 0x32a1
movq %r14, %rdi
callq 0x3197
cmpl $-0x1, %eax
jne 0x3272
jmp 0x3370
cmpl $0x30000, %eax          # imm = 0x30000
jne 0x3370
movq %r14, %rdi
callq 0x3197
movl %eax, %ebp
andl $0x3e000, %eax          # imm = 0x3E000
cmpl $0x1a000, %eax          # imm = 0x1A000
jne 0x3353
movq %r14, %rdi
callq 0x3197
movl %eax, %r15d
andl $0x3e000, %eax          # imm = 0x3E000
cmpl $0x1a000, %eax          # imm = 0x1A000
jne 0x3370
movl %ebp, %ebx
addl %r15d, %ebx
movq %r14, %rdi
callq 0x3197
movl %eax, %r12d
cmpl %r15d, %ebp
jge 0x3327
cmpl $-0x1, %r12d
je 0x3370
incl %ebp
cmpl %r15d, %ebp
jge 0x3370
movq %r14, %rdi
callq 0x3197
cmpl $-0x1, %eax
je 0x3370
movl %eax, %r13d
addl %r12d, %ebx
movl %r12d, %edi
movl %eax, %esi
callq 0x31fc
addl %r13d, %ebx
incl %ebp
jmp 0x32e6
cmpl $-0x1, %r12d
je 0x3370
cmpl $0x40000, %ebx          # imm = 0x40000
jb 0x3348
movl %ebx, %eax
andl $0x3ffff, %eax          # imm = 0x3FFFF
sarl $0x12, %ebx
leal (%rax,%rbx), %ecx
btl $0x12, %ecx
adcl %eax, %ebx
cmpl %ebx, %r12d
je 0x32ac
jmp 0x3370
cmpl $0x30000, %eax          # imm = 0x30000
jne 0x3370
movq %r14, %rdi
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2080
movq 0x5c81(%rip), %rax      # 0x8ff8
movq (%rax), %rdi
leaq 0x31ac(%rip), %rsi      # 0x652d
movq (%rsp), %rdx
xorl %eax, %eax
callq 0x2110
pushq $0x1
popq %rdi
callq 0x2170
movq (%rsp), %rdi
callq 0x2150
jmp 0x338c