// File: /workspace/github_c_makefile_star2_O0/simh[P]simtools/crossassemblers/macro11/mlb-rsx.c
// Function: mlb_entry
BUFFER         *mlb_entry(
    MLB *mlb,
    char *name)
{
    int             i;
    MLBENT         *ent;
    BUFFER         *buf;
    char           *bp;
    int             c;
    unsigned char   module_header[022];

    for (i = 0; i < mlb->nentries; i++) {
        ent = &mlb->directory[i];
        if (strcmp(mlb->directory[i].label, name) == 0)
            break;
    }

    if (i >= mlb->nentries) {
//        fprintf(stderr, "mlb_entry: %s not found\n", name);
        return NULL;
    }

    fseek(mlb->fp, ent->position, SEEK_SET);
//    fprintf(stderr, "mlb_entry: %s at position %ld\n", name, (long)ent->position);

#define MODULE_HEADER_SIZE      022

    if (fread(module_header, MODULE_HEADER_SIZE, 1, mlb->fp) < 1) {
//        fprintf(stderr, "mlb_entry: %s at position %lx can't read 022 bytes\n", name, (long)ent->position);
        return NULL;
    }

//    for (i = 0; i < MODULE_HEADER_SIZE; i++) {
//        fprintf(stderr, "%02x ", module_header[i]);
//    }
//    fprintf(stderr, "\n");
    ent->length = (WORD(module_header + 04) << 16) +
                   WORD(module_header + 06);
    ent->length -= MODULE_HEADER_SIZE; /* length is including this header */
//    fprintf(stderr, "mlb_entry: %s at position %lx length = %d\n", name, (long)ent->position, ent->length);

    if (module_header[02] == 1) {
        fprintf(stderr, "mlb_entry: %s at position %lx deleted entry\n", name, (long)ent->position);
        /* Deleted Entry */
        return NULL;
    }

    /*
     * Allocate a buffer to hold the text.
     * The text is always shorter than the on-disk size.
     */
    buf = new_buffer();
    buffer_resize(buf, ent->length);       /* Make it large enough */
    bp = buf->buffer;

    /*
     * Check the file format: variable length records,
     * or stream of bytes.
     * Not sure if this check is the correct one; I've only
     * seen MLB and OLB files with var length records.
     */

    if (mlb->is_objlib) {
        /* In object libraries, copy the internal structure, since we
         * can consider them to be binary.
         */
        i = fread(bp, 1, ent->length, mlb->fp);
        bp += i;
    } else if (module_header[0] & 0x10) {
//        fprintf(stderr, "mlb_entry: %s at position %lx variable length records\n", name, (long)ent->position);
        /* Variable length records with size before them */
        i = ent->length;
        while (i > 0) {
            int length;

//            fprintf(stderr, "file offset:$%lx\n", (long)ftell(mlb->fp));
            c = fgetc(mlb->fp);            /* Get low byte of length */
            length = c & 0xFF;
            c = fgetc(mlb->fp);            /* Get high byte */
            length += (c & 0xFF) << 8;
            i -= 2;
//            fprintf(stderr, "line length: %d $%x\n", length, length);

            /* Odd lengths are padded with an extra 0 byte */
            int padded = length & 1;
            if (length > i) {
                fprintf(stderr, "line length %d > remaining archive member %d\n", length, i);
                length = i;
            }

            while (length > 0) {
                c = fgetc(mlb->fp);        /* Get macro byte */
                //fprintf(stderr, "%02x %c length=%d\n", c, c, length);
                i--;
                length--;
                if (c == '\r' || c == 0)   /* If it's a carriage return or 0,
                                              discard it. */
                    continue;
                *bp++ = c;
            }
            *bp++ = '\n';
            if (padded) {
                c = fgetc(mlb->fp);        /* Get pad byte; need not be 0. */
                //fprintf(stderr, "pad byte %02x %c length=%d\n", c, c, length);
                i--;
            }
        }
    } else {
//        fprintf(stderr, "mlb_entry: %s at position %lx byte stream records\n", name, (long)ent->position);
        for (i = 0; i < ent->length; i++) {
            c = fgetc(mlb->fp);            /* Get macro byte */
            if (c == '\r' || c == 0)       /* If it's a carriage return or 0,
                                              discard it. */
                continue;
            *bp++ = c;
        }
    }

    /* Now resize that buffer to the length actually read. */
    buffer_resize(buf, (int) (bp - buf->buffer));

    return buf;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x60, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movl $0x0, -0x1c(%rbp)
movl -0x1c(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpl 0x10(%rcx), %eax
jge 0x10ebd
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movslq -0x1c(%rbp), %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x28(%rbp)
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movslq -0x1c(%rbp), %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq (%rax), %rdi
movq -0x18(%rbp), %rsi
callq 0x21b0
cmpl $0x0, %eax
jne 0x10eb0
jmp 0x10ebd
jmp 0x10eb2
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
jmp 0x10e67
movl -0x1c(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpl 0x10(%rcx), %eax
jl 0x10ed6
movq $0x0, -0x8(%rbp)
jmp 0x11198
movq -0x10(%rbp), %rax
movq (%rax), %rdi
movq -0x28(%rbp), %rax
movq 0x8(%rax), %rsi
xorl %edx, %edx
callq 0x2240
leaq -0x50(%rbp), %rdi
movq -0x10(%rbp), %rax
movq (%rax), %rcx
movl $0x12, %esi
movl $0x1, %edx
callq 0x20a0
cmpq $0x1, %rax
jae 0x10f19
movq $0x0, -0x8(%rbp)
jmp 0x11198
movzbl -0x4c(%rbp), %ecx
andl $0xff, %ecx
movzbl -0x4b(%rbp), %eax
andl $0xff, %eax
shll $0x8, %eax
addl %eax, %ecx
shll $0x10, %ecx
movzbl -0x4a(%rbp), %eax
andl $0xff, %eax
movzbl -0x49(%rbp), %edx
andl $0xff, %edx
shll $0x8, %edx
addl %edx, %eax
addl %eax, %ecx
movq -0x28(%rbp), %rax
movl %ecx, 0x10(%rax)
movq -0x28(%rbp), %rax
movl 0x10(%rax), %ecx
subl $0x12, %ecx
movl %ecx, 0x10(%rax)
movzbl -0x4e(%rbp), %eax
cmpl $0x1, %eax
jne 0x10f9c
movq 0x8086(%rip), %rax      # 0x18ff8
movq (%rax), %rdi
movq -0x18(%rbp), %rdx
movq -0x28(%rbp), %rax
movq 0x8(%rax), %rcx
leaq 0x4d11(%rip), %rsi      # 0x15c99
movb $0x0, %al
callq 0x21c0
movq $0x0, -0x8(%rbp)
jmp 0x11198
callq 0x12af0
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rdi
movq -0x28(%rbp), %rax
movl 0x10(%rax), %esi
callq 0x12b50
movq -0x30(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x38(%rbp)
movq -0x10(%rbp), %rax
cmpl $0x0, 0x14(%rax)
je 0x11000
movq -0x38(%rbp), %rdi
movq -0x28(%rbp), %rax
movslq 0x10(%rax), %rdx
movq -0x10(%rbp), %rax
movq (%rax), %rcx
movl $0x1, %esi
callq 0x20a0
movl %eax, -0x1c(%rbp)
movl -0x1c(%rbp), %ecx
movq -0x38(%rbp), %rax
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, -0x38(%rbp)
jmp 0x11177
movzbl -0x50(%rbp), %eax
andl $0x10, %eax
cmpl $0x0, %eax
je 0x11122
movq -0x28(%rbp), %rax
movl 0x10(%rax), %eax
movl %eax, -0x1c(%rbp)
cmpl $0x0, -0x1c(%rbp)
jle 0x11120
movq -0x10(%rbp), %rax
movq (%rax), %rdi
callq 0x2140
movl %eax, -0x3c(%rbp)
movl -0x3c(%rbp), %eax
andl $0xff, %eax
movl %eax, -0x54(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rdi
callq 0x2140
movl %eax, -0x3c(%rbp)
movl -0x3c(%rbp), %eax
andl $0xff, %eax
shll $0x8, %eax
addl -0x54(%rbp), %eax
movl %eax, -0x54(%rbp)
movl -0x1c(%rbp), %eax
subl $0x2, %eax
movl %eax, -0x1c(%rbp)
movl -0x54(%rbp), %eax
andl $0x1, %eax
movl %eax, -0x58(%rbp)
movl -0x54(%rbp), %eax
cmpl -0x1c(%rbp), %eax
jle 0x1109c
movq 0x7f79(%rip), %rax      # 0x18ff8
movq (%rax), %rdi
movl -0x54(%rbp), %edx
movl -0x1c(%rbp), %ecx
leaq 0x4c37(%rip), %rsi      # 0x15cc6
movb $0x0, %al
callq 0x21c0
movl -0x1c(%rbp), %eax
movl %eax, -0x54(%rbp)
jmp 0x1109e
cmpl $0x0, -0x54(%rbp)
jle 0x110eb
movq -0x10(%rbp), %rax
movq (%rax), %rdi
callq 0x2140
movl %eax, -0x3c(%rbp)
movl -0x1c(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x1c(%rbp)
movl -0x54(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x54(%rbp)
cmpl $0xd, -0x3c(%rbp)
je 0x110d1
cmpl $0x0, -0x3c(%rbp)
jne 0x110d3
jmp 0x1109e
movl -0x3c(%rbp), %eax
movb %al, %cl
movq -0x38(%rbp), %rax
movq %rax, %rdx
addq $0x1, %rdx
movq %rdx, -0x38(%rbp)
movb %cl, (%rax)
jmp 0x1109e
movq -0x38(%rbp), %rax
movq %rax, %rcx
addq $0x1, %rcx
movq %rcx, -0x38(%rbp)
movb $0xa, (%rax)
cmpl $0x0, -0x58(%rbp)
je 0x1111b
movq -0x10(%rbp), %rax
movq (%rax), %rdi
callq 0x2140
movl %eax, -0x3c(%rbp)
movl -0x1c(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x1c(%rbp)
jmp 0x1101a
jmp 0x11175
movl $0x0, -0x1c(%rbp)
movl -0x1c(%rbp), %eax
movq -0x28(%rbp), %rcx
cmpl 0x10(%rcx), %eax
jge 0x11173
movq -0x10(%rbp), %rax
movq (%rax), %rdi
callq 0x2140
movl %eax, -0x3c(%rbp)
cmpl $0xd, -0x3c(%rbp)
je 0x11150
cmpl $0x0, -0x3c(%rbp)
jne 0x11152
jmp 0x11168
movl -0x3c(%rbp), %eax
movb %al, %cl
movq -0x38(%rbp), %rax
movq %rax, %rdx
addq $0x1, %rdx
movq %rdx, -0x38(%rbp)
movb %cl, (%rax)
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
jmp 0x11129
jmp 0x11175
jmp 0x11177
movq -0x30(%rbp), %rdi
movq -0x38(%rbp), %rax
movq -0x30(%rbp), %rcx
movq (%rcx), %rcx
subq %rcx, %rax
movl %eax, %esi
callq 0x12b50
movq -0x30(%rbp), %rax
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x60, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)