// File: 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
subq $0x40, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq -0x10(%rbp), %rdi
addq $0x10, %rdi
callq 0x8480
movq -0x10(%rbp), %rax
movb $0x0, 0x5(%rax)
movq -0x10(%rbp), %rax
movb $0x1, 0x4(%rax)
movq -0x10(%rbp), %rax
movl $0x0, (%rax)
movq -0x18(%rbp), %rdi
callq 0x2030
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x18(%rbp), %rdi
callq 0x2240
movl %eax, -0x1c(%rbp)
cmpl $0x0, %eax
jge 0x6df5
movb $0x0, -0x1(%rbp)
jmp 0x7059
cmpl $0x7, -0x1c(%rbp)
jg 0x6e3d
movl -0x1c(%rbp), %eax
movzbl %al, %ecx
shll $0x8, %ecx
movq -0x10(%rbp), %rax
addl (%rax), %ecx
movl %ecx, (%rax)
movq -0x18(%rbp), %rdi
callq 0x2240
movl %eax, -0x1c(%rbp)
cmpl $0x0, %eax
jge 0x6e26
movb $0x0, -0x1(%rbp)
jmp 0x7059
movl -0x1c(%rbp), %eax
movzbl %al, %ecx
movq -0x10(%rbp), %rax
addl (%rax), %ecx
movl %ecx, (%rax)
movb $0x1, -0x1(%rbp)
jmp 0x7059
cmpl $0x8, -0x1c(%rbp)
jne 0x6e48
jmp 0x7045
cmpl $0x9, -0x1c(%rbp)
jne 0x6e6d
movq -0x18(%rbp), %rdi
callq 0x2240
movl %eax, -0x1c(%rbp)
cmpl $0x0, %eax
jge 0x6e68
movb $0x0, -0x1(%rbp)
jmp 0x7059
jmp 0x7043
cmpl $0xa, -0x1c(%rbp)
jne 0x6eac
movq -0x18(%rbp), %rdi
callq 0x2240
movl %eax, -0x1c(%rbp)
cmpl $0x0, %eax
jge 0x6e8d
movb $0x0, -0x1(%rbp)
jmp 0x7059
movq -0x18(%rbp), %rdi
callq 0x2240
movl %eax, -0x1c(%rbp)
cmpl $0x0, %eax
jge 0x6ea7
movb $0x0, -0x1(%rbp)
jmp 0x7059
jmp 0x7041
cmpl $0xb, -0x1c(%rbp)
jne 0x6ec5
movq -0x10(%rbp), %rax
movl (%rax), %ecx
addl $0x10000, %ecx          # imm = 0x10000
movl %ecx, (%rax)
jmp 0x703f
cmpl $0xc, -0x1c(%rbp)
jne 0x6f27
movq -0x18(%rbp), %rdi
callq 0x2240
movl %eax, -0x1c(%rbp)
cmpl $0x0, %eax
jge 0x6ee5
movb $0x0, -0x1(%rbp)
jmp 0x7059
movl -0x1c(%rbp), %eax
movzbl %al, %ecx
shll $0x8, %ecx
movq -0x10(%rbp), %rax
addl (%rax), %ecx
movl %ecx, (%rax)
movq -0x18(%rbp), %rdi
callq 0x2240
movl %eax, -0x1c(%rbp)
cmpl $0x0, %eax
jge 0x6f10
movb $0x0, -0x1(%rbp)
jmp 0x7059
movl -0x1c(%rbp), %eax
movzbl %al, %ecx
movq -0x10(%rbp), %rax
addl (%rax), %ecx
movl %ecx, (%rax)
movb $0x1, -0x1(%rbp)
jmp 0x7059
cmpl $0xd, -0x1c(%rbp)
jne 0x702b
movq -0x18(%rbp), %rdi
callq 0x2240
movl %eax, -0x1c(%rbp)
cmpl $0x0, %eax
jge 0x6f4b
movb $0x0, -0x1(%rbp)
jmp 0x7059
movl -0x1c(%rbp), %eax
movb %al, -0x29(%rbp)
movq -0x18(%rbp), %rdi
callq 0x2240
movl %eax, -0x1c(%rbp)
cmpl $0x0, %eax
jge 0x6f6b
movb $0x0, -0x1(%rbp)
jmp 0x7059
movl -0x1c(%rbp), %eax
movzbl %al, %eax
movq %rax, -0x28(%rbp)
movq -0x18(%rbp), %rdi
callq 0x2240
movl %eax, -0x1c(%rbp)
cmpl $0x0, %eax
jge 0x6f8f
movb $0x0, -0x1(%rbp)
jmp 0x7059
movl -0x1c(%rbp), %eax
movzbl %al, %eax
shll $0x8, %eax
cltq
addq -0x28(%rbp), %rax
movq %rax, -0x28(%rbp)
movq -0x10(%rbp), %rdi
addq $0x10, %rdi
callq 0x8480
movq -0x10(%rbp), %rdi
addq $0x10, %rdi
movq -0x28(%rbp), %rsi
callq 0x84a0
movq $0x0, -0x38(%rbp)
movq -0x38(%rbp), %rax
cmpq -0x28(%rbp), %rax
jae 0x7013
movq -0x18(%rbp), %rdi
callq 0x2240
movl %eax, -0x1c(%rbp)
cmpl $0x0, %eax
jge 0x6fe9
movb $0x0, -0x1(%rbp)
jmp 0x7059
movl -0x1c(%rbp), %eax
movb %al, -0x39(%rbp)
movq -0x10(%rbp), %rdi
addq $0x10, %rdi
movq -0x38(%rbp), %rsi
callq 0x8530
movb -0x39(%rbp), %cl
movb %cl, (%rax)
movq -0x38(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x38(%rbp)
jmp 0x6fc8
movq -0x10(%rbp), %rax
movb $0x2, 0x4(%rax)
movb -0x29(%rbp), %cl
movq -0x10(%rbp), %rax
movb %cl, 0x5(%rax)
movb $0x1, -0x1(%rbp)
jmp 0x7059
movl -0x1c(%rbp), %eax
movzbl %al, %ecx
movq -0x10(%rbp), %rax
addl (%rax), %ecx
movl %ecx, (%rax)
movb $0x1, -0x1(%rbp)
jmp 0x7059
jmp 0x7041
jmp 0x7043
jmp 0x7045
jmp 0x7047
jmp 0x7049
movb $0x1, %al
testb $0x1, %al
jne 0x6ddb
jmp 0x7055
movb $0x0, -0x1(%rbp)
movb -0x1(%rbp), %al
andb $0x1, %al
addq $0x40, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)