// File: /workspace/github_cpp_makefile_star2_O2/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
pushq %rax
movq %rsi, %r14
movq %rdi, %rbx
movq 0x10(%rdi), %rax
cmpq %rax, 0x18(%rdi)
je 0x3a4a
movq %rax, 0x18(%rbx)
leaq 0x10(%rbx), %r15
movw $0x1, 0x4(%rbx)
andl $0x0, (%rbx)
movq %r14, %rdi
callq 0x2030
movq %rax, 0x8(%rbx)
leaq 0x18d6(%rip), %r12      # 0x5340
movq %r14, %rdi
callq 0x2200
testl %eax, %eax
js 0x3b6a
cmpl $0x7, %eax
jbe 0x3abd
leal -0x8(%rax), %ecx
cmpl $0x5, %ecx
ja 0x3b6e
movslq (%r12,%rcx,4), %rax
addq %r12, %rax
jmpq *%rax
movq %r14, %rdi
callq 0x2200
testl %eax, %eax
js 0x3b6a
movq %r14, %rdi
callq 0x2200
testl %eax, %eax
jns 0x3a6a
jmp 0x3b6a
addl $0x10000, (%rbx)        # imm = 0x10000
jmp 0x3a6a
shll $0x8, %eax
addl %eax, (%rbx)
movq %r14, %rdi
callq 0x2200
testl %eax, %eax
js 0x3b6a
movzbl %al, %eax
jmp 0x3b71
movq %r14, %rdi
callq 0x2200
testl %eax, %eax
js 0x3b6a
shll $0x8, %eax
movzwl %ax, %eax
addl %eax, (%rbx)
jmp 0x3ac2
movq %r14, %rdi
callq 0x2200
testl %eax, %eax
js 0x3b6a
movl %eax, %r12d
movq %r14, %rdi
callq 0x2200
testl %eax, %eax
js 0x3b6a
movl %eax, %r13d
movq %r14, %rdi
callq 0x2200
testl %eax, %eax
js 0x3b6a
movl %r12d, -0x2c(%rbp)
movzbl %r13b, %r12d
shll $0x8, %eax
movzwl %ax, %r13d
leal (%r12,%r13), %esi
movq 0x10(%rbx), %rax
cmpq %rax, 0x18(%rbx)
je 0x3b3f
movq %rax, 0x18(%rbx)
movq %r15, %rdi
callq 0x46d2
orl %r12d, %r13d
xorl %r12d, %r12d
cmpq %r12, %r13
je 0x3b84
movq %r14, %rdi
callq 0x2200
testl %eax, %eax
js 0x3b6a
movq (%r15), %rcx
movb %al, (%rcx,%r12)
incq %r12
jmp 0x3b4d
xorl %eax, %eax
jmp 0x3b75
movzbl %al, %eax
addl %eax, (%rbx)
movb $0x1, %al
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movb $0x2, 0x4(%rbx)
movl -0x2c(%rbp), %eax
movb %al, 0x5(%rbx)
jmp 0x3b73