// File: /workspace/github_cpp_makefile_star2_O3/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 0x3cc9
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 0x3e21
leaq 0x10(%rbx), %r15
leaq 0x1664(%rip), %r12      # 0x5360
cmpl $0x7, %eax
jbe 0x3d4a
leal -0x8(%rax), %ecx
cmpl $0x5, %ecx
ja 0x3d64
movslq (%r12,%rcx,4), %rax
addq %r12, %rax
jmpq *%rax
movq %r14, %rdi
callq 0x21f0
testl %eax, %eax
js 0x3e21
movq %r14, %rdi
callq 0x21f0
testl %eax, %eax
jns 0x3d39
jmp 0x3e21
addl $0x10000, (%rbx)        # imm = 0x10000
movq %r14, %rdi
callq 0x21f0
testl %eax, %eax
jns 0x3cfc
jmp 0x3e21
shll $0x8, %eax
addl %eax, (%rbx)
movq %r14, %rdi
callq 0x21f0
testl %eax, %eax
js 0x3e21
movzbl %al, %eax
jmp 0x3d67
movzbl %al, %eax
addl %eax, (%rbx)
movb $0x1, %al
jmp 0x3e23
movq %r14, %rdi
callq 0x21f0
testl %eax, %eax
js 0x3e21
movl %eax, %r12d
movq %r14, %rdi
callq 0x21f0
testl %eax, %eax
js 0x3e21
movl %eax, %r13d
movq %r14, %rdi
callq 0x21f0
testl %eax, %eax
js 0x3e21
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 0x3dc7
movq %rax, 0x18(%rbx)
movq %r15, %rdi
movq %r12, %rsi
callq 0x4a7c
testq %r12, %r12
je 0x3df9
addl -0x38(%rbp), %r13d
xorl %r12d, %r12d
movq %r14, %rdi
callq 0x21f0
testl %eax, %eax
js 0x3e21
movq (%r15), %rcx
movb %al, (%rcx,%r12)
incq %r12
cmpq %r12, %r13
jne 0x3dde
movb $0x2, 0x4(%rbx)
movl -0x2c(%rbp), %eax
movb %al, 0x5(%rbx)
jmp 0x3d69
movq %r14, %rdi
callq 0x21f0
testl %eax, %eax
js 0x3e21
shll $0x8, %eax
movzwl %ax, %eax
addl %eax, (%rbx)
jmp 0x3d4f
xorl %eax, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq