// File: joncampbell123[P]floppytools/kryocomm.cpp
// Function: flux_bits_mfm_read_sector_data(unsigned char*, unsigned int, flux_bits&, kryoflux_event&, _IO_FILE*, int)
int flux_bits_mfm_read_sector_data(unsigned char *buf,unsigned int sector_size,struct flux_bits &fb,struct kryoflux_event &ev,FILE *fp,int c) {
    mfm_crc16fd_t check;
    unsigned char tmp[4];

    // A1 A1 A1 FA/FB
    tmp[0] = (unsigned char)MFM_A1_SYNC_BYTE;
    tmp[1] = (unsigned char)MFM_A1_SYNC_BYTE;
    tmp[2] = (unsigned char)MFM_A1_SYNC_BYTE;
    tmp[3] = (unsigned char)c;

    /* begin checksum */
    check = mfm_crc16fd_update(0xffff,tmp,4);

    // sector data follows
    for (unsigned int b=0;b < sector_size;b++) {
        kryoflux_bits_refill(fb,ev,fp);

        if ((c=flux_bits_mfm_decode(fb,ev,fp)) < 0)
            return -MFM_READ_DECODE_ERROR;

        buf[b] = (unsigned char)c;
    }
    check = mfm_crc16fd_update(check,buf,sector_size);

    // followed by checksum
    for (unsigned int b=0;b < 2;b++) {
        kryoflux_bits_refill(fb,ev,fp);

        if ((c=flux_bits_mfm_decode(fb,ev,fp)) < 0)
            return -MFM_READ_DECODE_ERROR;

        tmp[b] = (unsigned char)c;
    }

    unsigned int crc;
    crc  = (unsigned int)tmp[0u] << 8u;
    crc += (unsigned int)tmp[1u];

    if (check != crc)
        return -MFM_CRC_ERROR;

    return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movq %r8, -0x30(%rbp)
movl %r9d, -0x34(%rbp)
movb $-0x5f, -0x3a(%rbp)
movb $-0x5f, -0x39(%rbp)
movb $-0x5f, -0x38(%rbp)
movl -0x34(%rbp), %eax
movb %al, -0x37(%rbp)
leaq -0x3a(%rbp), %rsi
movl $0xffff, %edi           # imm = 0xFFFF
movl $0x4, %edx
callq 0x78f0
movw %ax, -0x36(%rbp)
movl $0x0, -0x40(%rbp)
movl -0x40(%rbp), %eax
cmpl -0x14(%rbp), %eax
jae 0x83d7
movq -0x20(%rbp), %rdi
movq -0x28(%rbp), %rsi
movq -0x30(%rbp), %rdx
callq 0x7070
movq -0x20(%rbp), %rdi
movq -0x28(%rbp), %rsi
movq -0x30(%rbp), %rdx
callq 0x7800
movl %eax, -0x34(%rbp)
cmpl $0x0, %eax
jge 0x83bd
movl $0xfffffeff, -0x4(%rbp) # imm = 0xFFFFFEFF
jmp 0x8474
movl -0x34(%rbp), %eax
movb %al, %dl
movq -0x10(%rbp), %rax
movl -0x40(%rbp), %ecx
movb %dl, (%rax,%rcx)
movl -0x40(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x40(%rbp)
jmp 0x837f
movw -0x36(%rbp), %ax
movq -0x10(%rbp), %rsi
movl -0x14(%rbp), %ecx
movl %ecx, %edx
movzwl %ax, %edi
callq 0x78f0
movw %ax, -0x36(%rbp)
movl $0x0, -0x44(%rbp)
cmpl $0x2, -0x44(%rbp)
jae 0x8447
movq -0x20(%rbp), %rdi
movq -0x28(%rbp), %rsi
movq -0x30(%rbp), %rdx
callq 0x7070
movq -0x20(%rbp), %rdi
movq -0x28(%rbp), %rsi
movq -0x30(%rbp), %rdx
callq 0x7800
movl %eax, -0x34(%rbp)
cmpl $0x0, %eax
jge 0x8430
movl $0xfffffeff, -0x4(%rbp) # imm = 0xFFFFFEFF
jmp 0x8474
movl -0x34(%rbp), %eax
movb %al, %cl
movl -0x44(%rbp), %eax
movb %cl, -0x3a(%rbp,%rax)
movl -0x44(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x44(%rbp)
jmp 0x83f7
movzbl -0x3a(%rbp), %eax
shll $0x8, %eax
movl %eax, -0x48(%rbp)
movzbl -0x39(%rbp), %eax
addl -0x48(%rbp), %eax
movl %eax, -0x48(%rbp)
movzwl -0x36(%rbp), %eax
cmpl -0x48(%rbp), %eax
je 0x846d
movl $0xffffff00, -0x4(%rbp) # imm = 0xFFFFFF00
jmp 0x8474
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x50, %rsp
popq %rbp
retq
nopl (%rax)