// File: /workspace/github_cpp_makefile_star2_O3/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %r8, -0x58(%rbp)
movq %rcx, -0x50(%rbp)
movq %rdx, -0x48(%rbp)
leaq -0x2b(%rbp), %rax
movw $0xa1a1, -0x1(%rax)     # imm = 0xA1A1
movb $-0x5f, 0x1(%rax)
movb %r9b, 0x2(%rax)
movw $0xffff, %r13w          # imm = 0xFFFF
xorl %eax, %eax
leaq 0xa29(%rip), %r8        # 0x5380
movzwl %r13w, %ecx
shrl $0x8, %ecx
movzbl -0x2c(%rbp,%rax), %edx
xorl %ecx, %edx
shll $0x8, %r13d
xorw (%r8,%rdx,2), %r13w
incq %rax
cmpq $0x4, %rax
jne 0x4957
testl %esi, %esi
je 0x4a01
movl %esi, -0x3c(%rbp)
movl %esi, %r8d
xorl %r12d, %r12d
movq %rdi, -0x38(%rbp)
movq %r8, -0x60(%rbp)
movq -0x48(%rbp), %r15
movq -0x50(%rbp), %r14
movq -0x58(%rbp), %rbx
movq %r15, %rdi
movq %r14, %rsi
movq %rbx, %rdx
callq 0x3e32
movq %r15, %rdi
movq %r14, %rsi
movq %rbx, %rdx
callq 0x41ae
testl %eax, %eax
js 0x4a67
movq -0x38(%rbp), %rdi
movb %al, (%rdi,%r12)
incq %r12
movq -0x60(%rbp), %r8
cmpq %r12, %r8
jne 0x499c
cmpl $0x0, -0x3c(%rbp)
je 0x4a01
xorl %eax, %eax
leaq 0x99d(%rip), %rsi       # 0x5380
movzwl %r13w, %ecx
shrl $0x8, %ecx
movzbl (%rdi,%rax), %edx
xorl %ecx, %edx
shll $0x8, %r13d
xorw (%rsi,%rdx,2), %r13w
incq %rax
cmpq %rax, %r8
jne 0x49e3
leaq -0x2c(%rbp), %rbx
movb $0x1, %al
movq -0x48(%rbp), %r12
movq -0x50(%rbp), %r15
movq -0x58(%rbp), %r14
movl %eax, -0x38(%rbp)
movq %r12, %rdi
movq %r15, %rsi
movq %r14, %rdx
callq 0x3e32
movq %r12, %rdi
movq %r15, %rsi
movq %r14, %rdx
callq 0x41ae
testl %eax, %eax
js 0x4a67
movb %al, (%rbx)
leaq -0x2b(%rbp), %rbx
testb $0x1, -0x38(%rbp)
movl $0x0, %eax
jne 0x4a13
movzwl -0x2c(%rbp), %eax
rolw $0x8, %ax
movzwl %ax, %ecx
movzwl %r13w, %edx
xorl %eax, %eax
cmpl %edx, %ecx
sete %al
shll $0x8, %eax
addl $0xffffff00, %eax       # imm = 0xFFFFFF00
jmp 0x4a6c
movl $0xfffffeff, %eax       # imm = 0xFFFFFEFF
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop