// File: /workspace/github_c_makefile_star2_O0/simh[P]simtools/crossassemblers/macro11/dumpobj.c
// Function: readrec
char           *readrec(
    FILE *fp,
    int *len)
{
    int             c,
                    i;
    int             chksum;
    char           *buf;

    chksum = 0;

#if RT11
    while (c = fgetc(fp), c != EOF && c == 0) ;

    if (c == EOF)
        return NULL;

    if (c != 1) {
        fprintf(stderr, "Improperly formatted OBJ file (1)\n");
        return NULL;                   /* Not a properly formatted file. */
    }

    chksum -= c;

    c = fgetc(fp);
    if (c != 0) {
        fprintf(stderr, "Improperly formatted OBJ file (2)\n");
        return NULL;                   /* Not properly formatted */
    }

    chksum -= c;                       /* even though for 0 the checksum isn't changed... */
#endif /* RT11 */

    c = fgetc(fp);
    if (c == EOF) {
#if RT11
        fprintf(stderr, "Improperly formatted OBJ file (3)\n");
#endif /* RT11 */
        return NULL;
    }
    *len = c;

    chksum -= c;

    c = fgetc(fp);
    if (c == EOF) {
        fprintf(stderr, "Improperly formatted OBJ file (4)\n");
        return NULL;
    }

    *len += (c << 8);

    chksum -= c;

#if RT11
    *len -= 4;                         /* Subtract header and length bytes from length */
#endif
    if (*len < 0) {
        fprintf(stderr, "Improperly formatted OBJ file (5)\n");
        return NULL;
    }

    buf = malloc(*len);
    if (buf == NULL) {
        fprintf(stderr, "Out of memory allocating %d bytes\n", *len);
        return NULL;                   /* Bad alloc */
    }

    i = fread(buf, 1, *len, fp);
    if (i < *len) {
        free(buf);
        fprintf(stderr, "Improperly formatted OBJ file (6)\n");
        return NULL;
    }

    for (i = 0; i < *len; i++)
        chksum -= (buf[i] & 0xff);

#if RT11
    c = fgetc(fp);
    c &= 0xff;
    chksum &= 0xff;

    if (c != chksum) {
        free(buf);
        fprintf(stderr, "Bad record checksum, " "calculated=$%04x, recorded=$%04x\n", chksum, c);
        return NULL;
    }
#else
    if (*len & 1) {
        /* skip 1 byte of padding */
        c = fgetc(fp);
        if (c == EOF) {
            free(buf);
            fprintf(stderr, "EOF where padding byte should be\n");
            return NULL;
        }

    }
#endif /* RT11 */

    return buf;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x30, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movl $0x0, -0x24(%rbp)
movq -0x10(%rbp), %rdi
callq 0x10e0
movl %eax, -0x1c(%rbp)
cmpl $-0x1, -0x1c(%rbp)
jne 0x12d6
movq $0x0, -0x8(%rbp)
jmp 0x148b
movl -0x1c(%rbp), %ecx
movq -0x18(%rbp), %rax
movl %ecx, (%rax)
movl -0x1c(%rbp), %ecx
movl -0x24(%rbp), %eax
subl %ecx, %eax
movl %eax, -0x24(%rbp)
movq -0x10(%rbp), %rdi
callq 0x10e0
movl %eax, -0x1c(%rbp)
cmpl $-0x1, -0x1c(%rbp)
jne 0x1321
movq 0x3cf5(%rip), %rax      # 0x4ff8
movq (%rax), %rdi
leaq 0x1dbb(%rip), %rsi      # 0x30c8
movb $0x0, %al
callq 0x1110
movq $0x0, -0x8(%rbp)
jmp 0x148b
movl -0x1c(%rbp), %ecx
shll $0x8, %ecx
movq -0x18(%rbp), %rax
addl (%rax), %ecx
movl %ecx, (%rax)
movl -0x1c(%rbp), %ecx
movl -0x24(%rbp), %eax
subl %ecx, %eax
movl %eax, -0x24(%rbp)
movq -0x18(%rbp), %rax
cmpl $0x0, (%rax)
jge 0x1368
movq 0x3cae(%rip), %rax      # 0x4ff8
movq (%rax), %rdi
leaq 0x1d97(%rip), %rsi      # 0x30eb
movb $0x0, %al
callq 0x1110
movq $0x0, -0x8(%rbp)
jmp 0x148b
movq -0x18(%rbp), %rax
movslq (%rax), %rdi
callq 0x1120
movq %rax, -0x30(%rbp)
cmpq $0x0, -0x30(%rbp)
jne 0x13aa
movq 0x3c72(%rip), %rax      # 0x4ff8
movq (%rax), %rdi
movq -0x18(%rbp), %rax
movl (%rax), %edx
leaq 0x1d78(%rip), %rsi      # 0x310e
movb $0x0, %al
callq 0x1110
movq $0x0, -0x8(%rbp)
jmp 0x148b
movq -0x30(%rbp), %rdi
movq -0x18(%rbp), %rax
movslq (%rax), %rdx
movq -0x10(%rbp), %rcx
movl $0x1, %esi
callq 0x1070
movl %eax, -0x20(%rbp)
movl -0x20(%rbp), %eax
movq -0x18(%rbp), %rcx
cmpl (%rcx), %eax
jge 0x13ff
movq -0x30(%rbp), %rdi
callq 0x1030
movq 0x3c17(%rip), %rax      # 0x4ff8
movq (%rax), %rdi
leaq 0x1d46(%rip), %rsi      # 0x3131
movb $0x0, %al
callq 0x1110
movq $0x0, -0x8(%rbp)
jmp 0x148b
movl $0x0, -0x20(%rbp)
movl -0x20(%rbp), %eax
movq -0x18(%rbp), %rcx
cmpl (%rcx), %eax
jge 0x1436
movq -0x30(%rbp), %rax
movslq -0x20(%rbp), %rcx
movsbl (%rax,%rcx), %ecx
andl $0xff, %ecx
movl -0x24(%rbp), %eax
subl %ecx, %eax
movl %eax, -0x24(%rbp)
movl -0x20(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x20(%rbp)
jmp 0x1406
movq -0x18(%rbp), %rax
movl (%rax), %eax
andl $0x1, %eax
cmpl $0x0, %eax
je 0x1483
movq -0x10(%rbp), %rdi
callq 0x10e0
movl %eax, -0x1c(%rbp)
cmpl $-0x1, -0x1c(%rbp)
jne 0x1481
movq -0x30(%rbp), %rdi
callq 0x1030
movq 0x3b92(%rip), %rax      # 0x4ff8
movq (%rax), %rdi
leaq 0x1ce4(%rip), %rsi      # 0x3154
movb $0x0, %al
callq 0x1110
movq $0x0, -0x8(%rbp)
jmp 0x148b
jmp 0x1483
movq -0x30(%rbp), %rax
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x30, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)