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

    sid.clear();

    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)0xFE;

    int track = flux_bits_mfm_decode(fb,ev,fp);
    if (track < 0)
        return -MFM_READ_DECODE_ERROR;
    tmp[4] = sid.track = (unsigned char)track;

    int side = flux_bits_mfm_decode(fb,ev,fp);
    if (side < 0)
        return -MFM_READ_DECODE_ERROR;
    tmp[5] = sid.side = (unsigned char)side;

    int sector = flux_bits_mfm_decode(fb,ev,fp);
    if (sector < 0)
        return -MFM_READ_DECODE_ERROR;
    tmp[6] = sid.sector = (unsigned char)sector;

    int ssize = flux_bits_mfm_decode(fb,ev,fp);
    if (ssize < 0)
        return -MFM_READ_DECODE_ERROR;
    tmp[7] = sid.sector_size_code = (unsigned char)ssize;

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

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

    check = mfm_crc16fd_update(0xffff,tmp,8);
    if (check != crc)
        return -MFM_CRC_ERROR;

    sid.crc_ok = true;
    return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movq -0x10(%rbp), %rdi
callq 0x8120
movb $-0x5f, -0x30(%rbp)
movb $-0x5f, -0x2f(%rbp)
movb $-0x5f, -0x2e(%rbp)
movb $-0x2, -0x2d(%rbp)
movq -0x18(%rbp), %rdi
movq -0x20(%rbp), %rsi
movq -0x28(%rbp), %rdx
callq 0x7800
movl %eax, -0x3c(%rbp)
cmpl $0x0, -0x3c(%rbp)
jge 0x81e7
movl $0xfffffeff, -0x4(%rbp) # imm = 0xFFFFFEFF
jmp 0x831a
movl -0x3c(%rbp), %eax
movq -0x10(%rbp), %rcx
movb %al, (%rcx)
movb %al, -0x2c(%rbp)
movq -0x18(%rbp), %rdi
movq -0x20(%rbp), %rsi
movq -0x28(%rbp), %rdx
callq 0x7800
movl %eax, -0x40(%rbp)
cmpl $0x0, -0x40(%rbp)
jge 0x8219
movl $0xfffffeff, -0x4(%rbp) # imm = 0xFFFFFEFF
jmp 0x831a
movl -0x40(%rbp), %eax
movq -0x10(%rbp), %rcx
movb %al, 0x1(%rcx)
movb %al, -0x2b(%rbp)
movq -0x18(%rbp), %rdi
movq -0x20(%rbp), %rsi
movq -0x28(%rbp), %rdx
callq 0x7800
movl %eax, -0x44(%rbp)
cmpl $0x0, -0x44(%rbp)
jge 0x824c
movl $0xfffffeff, -0x4(%rbp) # imm = 0xFFFFFEFF
jmp 0x831a
movl -0x44(%rbp), %eax
movq -0x10(%rbp), %rcx
movb %al, 0x2(%rcx)
movb %al, -0x2a(%rbp)
movq -0x18(%rbp), %rdi
movq -0x20(%rbp), %rsi
movq -0x28(%rbp), %rdx
callq 0x7800
movl %eax, -0x48(%rbp)
cmpl $0x0, -0x48(%rbp)
jge 0x827f
movl $0xfffffeff, -0x4(%rbp) # imm = 0xFFFFFEFF
jmp 0x831a
movl -0x48(%rbp), %eax
movq -0x10(%rbp), %rcx
movb %al, 0x3(%rcx)
movb %al, -0x29(%rbp)
movq -0x18(%rbp), %rdi
movq -0x20(%rbp), %rsi
movq -0x28(%rbp), %rdx
callq 0x7800
movl %eax, -0x38(%rbp)
cmpl $0x0, %eax
jge 0x82ae
movl $0xfffffeff, -0x4(%rbp) # imm = 0xFFFFFEFF
jmp 0x831a
movl -0x38(%rbp), %eax
shll $0x8, %eax
movl %eax, -0x4c(%rbp)
movq -0x18(%rbp), %rdi
movq -0x20(%rbp), %rsi
movq -0x28(%rbp), %rdx
callq 0x7800
movl %eax, -0x38(%rbp)
cmpl $0x0, %eax
jge 0x82d9
movl $0xfffffeff, -0x4(%rbp) # imm = 0xFFFFFEFF
jmp 0x831a
movl -0x38(%rbp), %eax
addl -0x4c(%rbp), %eax
movl %eax, -0x4c(%rbp)
leaq -0x30(%rbp), %rsi
movl $0xffff, %edi           # imm = 0xFFFF
movl $0x8, %edx
callq 0x78f0
movw %ax, -0x32(%rbp)
movzwl -0x32(%rbp), %eax
cmpl -0x4c(%rbp), %eax
je 0x830b
movl $0xffffff00, -0x4(%rbp) # imm = 0xFFFFFF00
jmp 0x831a
movq -0x10(%rbp), %rax
movb $0x1, 0x4(%rax)
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x50, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)