// File: /workspace/github_c_makefile_star2_O3/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 0x303b(%rip), %rsi      # 0x655a
movq %rdi, (%rsp)
callq 0x2140
testq %rax, %rax
je 0x363f
movq %rax, %r14
movq %r14, %rdi
callq 0x3463
cmpl $-0x1, %eax
je 0x3636
andl $0x3e000, %eax          # imm = 0x3E000
cmpl $0x1a000, %eax          # imm = 0x1A000
jne 0x3563
movq %r14, %rdi
callq 0x3463
cmpl $-0x1, %eax
jne 0x3534
jmp 0x3636
cmpl $0x30000, %eax          # imm = 0x30000
jne 0x3636
movq %r14, %rdi
callq 0x3463
movl %eax, %r15d
andl $0x3e000, %eax          # imm = 0x3E000
cmpl $0x1a000, %eax          # imm = 0x1A000
jne 0x3619
movq %r14, %rdi
callq 0x3463
movl %eax, %r12d
andl $0x3e000, %eax          # imm = 0x3E000
cmpl $0x1a000, %eax          # imm = 0x1A000
jne 0x3636
leal (%r12,%r15), %ebx
movq %r14, %rdi
cmpl %r12d, %r15d
jge 0x35e9
callq 0x3463
cmpl $-0x1, %eax
je 0x3636
movl %eax, %ebp
leal 0x1(%r15), %eax
cmpl %r12d, %eax
jge 0x3636
movq %r14, %rdi
callq 0x3463
cmpl $-0x1, %eax
je 0x3636
movl %eax, %r13d
addl %ebp, %ebx
movl %ebp, %edi
movl %eax, %esi
callq 0x34bc
addl %r13d, %ebx
addl $0x2, %r15d
jmp 0x35a8
callq 0x3463
cmpl $-0x1, %eax
je 0x3636
cmpl $0x40000, %ebx          # imm = 0x40000
jb 0x360f
movl %ebx, %ecx
andl $0x3ffff, %ecx          # imm = 0x3FFFF
sarl $0x12, %ebx
leal (%rcx,%rbx), %edx
btl $0x12, %edx
adcl %ecx, %ebx
cmpl %ebx, %eax
je 0x356e
jmp 0x3636
cmpl $0x30000, %eax          # imm = 0x30000
jne 0x3636
movq %r14, %rdi
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2080
movq (%rsp), %rdi
callq 0x2349
movq (%rsp), %rdi
callq 0x236d