// File: /workspace/github_cpp_makefile_star2_O1/joncampbell123[P]floppytools/kryocomm.cpp
// Function: kryoflux_read(kryoflux_event&, _IO_FILE*)
bool kryoflux_read(struct kryoflux_event &ev,FILE *fp) {
    int c;

    ev.msg.clear();
    ev.oob_code = 0;
    ev.message = MSG_FLUX;
    ev.flux_interval = 0;
    ev.offset = ftell(fp);

    do {
        if ((c = fgetc(fp)) < 0)
            return false;

        if (c <= 0x07) {
            ev.flux_interval += ((unsigned char)c << 8u);

            if ((c = fgetc(fp)) < 0)
                return false;
            ev.flux_interval +=  (unsigned char)c;

            return true;
        }
        else if (c == 0x08) {
            // ignore
        }
        else if (c == 0x09) {
            if ((c = fgetc(fp)) < 0)
                return false;

            // ignore
        }
        else if (c == 0x0A) {
            if ((c = fgetc(fp)) < 0)
                return false;
            if ((c = fgetc(fp)) < 0)
                return false;

            // ignore
        }
        else if (c == 0x0B) {
            // overflow16
            ev.flux_interval += 0x10000u;
        }
        else if (c == 0x0C) {
            // value16
            if ((c = fgetc(fp)) < 0)
                return false;
            ev.flux_interval += ((unsigned char)c << 8u);

            if ((c = fgetc(fp)) < 0)
                return false;
            ev.flux_interval +=  (unsigned char)c;

            return true;
        }
        else if (c == 0x0D) {
            unsigned long oob_len;

            // OOB
            if ((c = fgetc(fp)) < 0)
                return false;
            unsigned char type = (unsigned char)c;

            if ((c = fgetc(fp)) < 0)
                return false;
            oob_len  = (unsigned char)c;

            if ((c = fgetc(fp)) < 0)
                return false;
            oob_len += (unsigned char)c << 8u;

            ev.msg.clear();
            ev.msg.resize(oob_len);
            for (unsigned long i=0;i < oob_len;i++) {
                if ((c = fgetc(fp)) < 0)
                    return false;

                ev.msg[i] = (unsigned char)c;
            }

            ev.message = MSG_OOB;
            ev.oob_code = type;

            return true;
        }
        else {
            ev.flux_interval +=  (unsigned char)c;

            return true;
        }
    } while (1);

    return false;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rsi, %r14
movq %rdi, %rbx
movq 0x10(%rdi), %rax
cmpq %rax, 0x18(%rdi)
je 0x3d19
movq %rax, 0x18(%rbx)
movw $0x1, 0x4(%rbx)
movl $0x0, (%rbx)
movq %r14, %rdi
callq 0x2030
movq %rax, 0x8(%rbx)
movq %r14, %rdi
callq 0x21f0
testl %eax, %eax
js 0x3e71
leaq 0x10(%rbx), %r15
leaq 0x1614(%rip), %r12      # 0x5360
cmpl $0x7, %eax
jbe 0x3d9a
leal -0x8(%rax), %ecx
cmpl $0x5, %ecx
ja 0x3db4
movslq (%r12,%rcx,4), %rax
addq %r12, %rax
jmpq *%rax
movq %r14, %rdi
callq 0x21f0
testl %eax, %eax
js 0x3e71
movq %r14, %rdi
callq 0x21f0
testl %eax, %eax
jns 0x3d89
jmp 0x3e71
addl $0x10000, (%rbx)        # imm = 0x10000
movq %r14, %rdi
callq 0x21f0
testl %eax, %eax
jns 0x3d4c
jmp 0x3e71
shll $0x8, %eax
addl %eax, (%rbx)
movq %r14, %rdi
callq 0x21f0
testl %eax, %eax
js 0x3e71
movzbl %al, %eax
jmp 0x3db7
movzbl %al, %eax
addl %eax, (%rbx)
movb $0x1, %al
jmp 0x3e73
movq %r14, %rdi
callq 0x21f0
testl %eax, %eax
js 0x3e71
movl %eax, %r12d
movq %r14, %rdi
callq 0x21f0
testl %eax, %eax
js 0x3e71
movl %eax, %r13d
movq %r14, %rdi
callq 0x21f0
testl %eax, %eax
js 0x3e71
movl %r12d, -0x2c(%rbp)
movzbl %r13b, %ecx
shll $0x8, %eax
movzwl %ax, %r13d
movq %rcx, -0x38(%rbp)
leal (%rcx,%r13), %r12d
movq 0x10(%rbx), %rax
cmpq %rax, 0x18(%rbx)
je 0x3e17
movq %rax, 0x18(%rbx)
movq %r15, %rdi
movq %r12, %rsi
callq 0x4ad4
testq %r12, %r12
je 0x3e49
addl -0x38(%rbp), %r13d
xorl %r12d, %r12d
movq %r14, %rdi
callq 0x21f0
testl %eax, %eax
js 0x3e71
movq (%r15), %rcx
movb %al, (%rcx,%r12)
incq %r12
cmpq %r12, %r13
jne 0x3e2e
movb $0x2, 0x4(%rbx)
movl -0x2c(%rbp), %eax
movb %al, 0x5(%rbx)
jmp 0x3db9
movq %r14, %rdi
callq 0x21f0
testl %eax, %eax
js 0x3e71
shll $0x8, %eax
movzwl %ax, %eax
addl %eax, (%rbx)
jmp 0x3d9f
xorl %eax, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq