// File: /workspace/github_c_makefile_star2_O0/AK6DN[P]dec-utilities-for-pdp/d8tape/main.c
// Function: dumprim
static int
dumprim (unsigned char *t, int n)
{
    int         state;
    int         i;
    uint16_t    addr, data;
    uint16_t    cur_addr;

    state = RIM_Initial;

    cur_addr = 0xffff;                      // impossible address for PDP-8

    for (i = 0; i < n; i++) {
        if (optv > 2) {
            printf ("[%03o] ", t [i]); fflush (stdout);
            if ((i % 13) == 12) {
                printf ("\n");
            }
        }
        switch (state) {
        case    RIM_Initial:
            if (t [i] & 0100) {             // indicates 1st part of address
                addr = (t [i] & 0077) << 6; // store top part
                state = RIM_Addr;
            }
            break;
        case    RIM_Addr:
            addr |= (t [i] & 0077);
            state = RIM_Data1;
            break;
        case    RIM_Data1:
            data = (t [i] & 0077) << 6;     // store top part
            state = RIM_Data2;
            break;
        case    RIM_Data2:                  // final decode complete, store data
            data |= (t [i] & 0077);
            core [addr] = data;             // stash data into core image

            // segment management -- if it's the next byte, add, else create new
            if (addr == cur_addr) {
                cur_addr++;
                seg_more (1);
            } else {
                seg_add (addr, 1);
                cur_addr = addr + 1;
            }
            state = RIM_Initial;
            break;
        }
    }
    if (optv > 2) {
        printf ("\n");
    }
    return (1);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x30, %rsp
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movl $0x1, -0x10(%rbp)
movw $0xffff, -0x1a(%rbp)    # imm = 0xFFFF
movl $0x0, -0x14(%rbp)
movl -0x14(%rbp), %eax
cmpl -0xc(%rbp), %eax
jge 0x44e5
cmpl $0x2, 0xabda(%rip)      # 0xef50
jle 0x43c1
movq -0x8(%rbp), %rax
movslq -0x14(%rbp), %rcx
movzbl (%rax,%rcx), %esi
leaq 0x4397(%rip), %rdi      # 0x8722
movb $0x0, %al
callq 0x30a0
movq 0x9c37(%rip), %rax      # 0xdfd0
movq (%rax), %rdi
callq 0x3110
movl -0x14(%rbp), %eax
movl $0xd, %ecx
cltd
idivl %ecx
cmpl $0xc, %edx
jne 0x43bf
leaq 0x3e0b(%rip), %rdi      # 0x81c3
movb $0x0, %al
callq 0x30a0
jmp 0x43c1
movl -0x10(%rbp), %eax
decl %eax
movl %eax, %ecx
movq %rcx, -0x28(%rbp)
subl $0x3, %eax
ja 0x44d5
movq -0x28(%rbp), %rax
leaq 0x3cb0(%rip), %rcx      # 0x8090
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq -0x8(%rbp), %rax
movslq -0x14(%rbp), %rcx
movzbl (%rax,%rcx), %eax
andl $0x40, %eax
cmpl $0x0, %eax
je 0x441a
movq -0x8(%rbp), %rax
movslq -0x14(%rbp), %rcx
movzbl (%rax,%rcx), %eax
andl $0x3f, %eax
shll $0x6, %eax
movw %ax, -0x16(%rbp)
movl $0x2, -0x10(%rbp)
jmp 0x44d5
movq -0x8(%rbp), %rax
movslq -0x14(%rbp), %rcx
movzbl (%rax,%rcx), %ecx
andl $0x3f, %ecx
movzwl -0x16(%rbp), %eax
orl %ecx, %eax
movw %ax, -0x16(%rbp)
movl $0x3, -0x10(%rbp)
jmp 0x44d5
movq -0x8(%rbp), %rax
movslq -0x14(%rbp), %rcx
movzbl (%rax,%rcx), %eax
andl $0x3f, %eax
shll $0x6, %eax
movw %ax, -0x18(%rbp)
movl $0x4, -0x10(%rbp)
jmp 0x44d5
movq -0x8(%rbp), %rax
movslq -0x14(%rbp), %rcx
movzbl (%rax,%rcx), %ecx
andl $0x3f, %ecx
movzwl -0x18(%rbp), %eax
orl %ecx, %eax
movw %ax, -0x18(%rbp)
movw -0x18(%rbp), %dx
movzwl -0x16(%rbp), %eax
movl %eax, %ecx
leaq 0xaad3(%rip), %rax      # 0xef60
movw %dx, (%rax,%rcx,2)
movzwl -0x16(%rbp), %eax
movzwl -0x1a(%rbp), %ecx
cmpl %ecx, %eax
jne 0x44b5
movw -0x1a(%rbp), %ax
addw $0x1, %ax
movw %ax, -0x1a(%rbp)
movl $0x1, %edi
callq 0x4900
jmp 0x44ce
movl $0x1, %esi
movzwl -0x16(%rbp), %edi
callq 0x4990
movzwl -0x16(%rbp), %eax
addl $0x1, %eax
movw %ax, -0x1a(%rbp)
movl $0x1, -0x10(%rbp)
jmp 0x44d7
movl -0x14(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x14(%rbp)
jmp 0x4363
cmpl $0x2, 0xaa64(%rip)      # 0xef50
jle 0x44fc
leaq 0x3cce(%rip), %rdi      # 0x81c3
movb $0x0, %al
callq 0x30a0
movl $0x1, %eax
addq $0x30, %rsp
popq %rbp
retq
nopw (%rax,%rax)