// File: joncampbell123[P]floppytools/kryo_ibm.cpp
// Function: process_sync(_IO_FILE*, flux_bits&, kryoflux_event&, _IO_FILE*, unsigned int, unsigned int)
void process_sync(FILE *dsk_fp,struct flux_bits &fb,struct kryoflux_event &ev,FILE *fp,unsigned int log_track,unsigned int log_head) {
    unsigned char tmp[128];
    mfm_crc16fd_t check;
    unsigned int count;
    mfm_sector_id sid;
    int c;

    // A1 A1 A1
    // FE
    // track number
    // side
    // sector number
    // sector size as power of 2
    // <TWO BYTE CRC>
    // 22 bytes of 4E
    // 12 bytes of 00
    //
    // A1 A1 A1
    // FA/FB
    // <SECTOR DATA>
    // <TWO BYTE CRC>
    //
    // (inter-sector gap filled with 4E)

    // Read A1 sync bytes (min 3) followed by first byte after
    if (flux_bits_mfm_read_sync_and_byte(fb,ev,fp) != 0xFE) return;
    // then read the rest of the sector ID
    if (flux_bits_mfm_read_sector_id(sid,fb,ev,fp) < 0) return;

    if (sid.sector_size() != sector_size)
        return;
    if (sid.track != log_track || sid.side != log_head)
        return;

    if (sid.sector < 1 || sid.sector > sectors ||
        sid.side < 0   || sid.side >= heads ||
        sid.track < 0  || sid.track >= tracks) {
        return;
    }

    unsigned long sector_num;

    sector_num  = (unsigned long)sid.track;
    sector_num *= (unsigned long)heads;
    sector_num += (unsigned long)sid.side;
    sector_num *= (unsigned long)sectors;
    sector_num += (unsigned long)sid.sector - 1;

    assert(sector_num < captured.size());
    if (captured[sector_num])
        return;

    // Find next sync header
    if (!mfm_find_sync(fb,ev,fp)) {
//      printf("* Failed to find sync\n");
        return;
    }

    // Read A1 sync bytes (min 3) followed by first byte after. Store the value in 'c' because 0xFA/0xFB is part of the checksum.
    if (((c=flux_bits_mfm_read_sync_and_byte(fb,ev,fp))&0xFE) != 0xFA) {
//      printf("* Failed to find sector data sync, err=0x%x\n",-c);
        return;
    }

    // Then read the rest of the sector
    if ((c=flux_bits_mfm_read_sector_data(sector_buf,sid.sector_size(),fb,ev,fp,c)) < 0) {
//      printf("* Failed to read sector data, err=0x%x\n",-c);
        return;
    }

    printf("Sector: track=%u side=%u sector=%u ssize=%u\n",sid.track,sid.side,sid.sector,128 << sid.sector_size_code);

    unsigned long sector_ofs = sector_num * (unsigned long)sector_size;

    fseek(dsk_fp,sector_ofs,SEEK_SET);
    if (ftell(dsk_fp) != sector_ofs) return;

    fwrite(sector_buf,sector_size,1,dsk_fp);

    assert(sector_num < captured.size());
    captured[sector_num] = true;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x160, %rsp            # imm = 0x160
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movl %r8d, -0x24(%rbp)
movl %r9d, -0x28(%rbp)
leaq -0xbd(%rbp), %rdi
callq 0x93e0
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
movq -0x20(%rbp), %rdx
callq 0x9380
cmpl $0xfe, %eax
je 0x245c
jmp 0x27ff
movq -0x10(%rbp), %rsi
movq -0x18(%rbp), %rdx
movq -0x20(%rbp), %rcx
leaq -0xbd(%rbp), %rdi
callq 0x9470
cmpl $0x0, %eax
jge 0x247e
jmp 0x27ff
leaq -0xbd(%rbp), %rdi
callq 0x9430
cmpl 0xdd0c(%rip), %eax      # 0x1019c
je 0x2497
jmp 0x27ff
movzbl -0xbd(%rbp), %eax
cmpl -0x24(%rbp), %eax
jne 0x24af
movzbl -0xbc(%rbp), %eax
cmpl -0x28(%rbp), %eax
je 0x24b4
jmp 0x27ff
movzbl -0xbb(%rbp), %eax
cmpl $0x1, %eax
jl 0x2505
movzbl -0xbb(%rbp), %eax
cmpl 0xdcc3(%rip), %eax      # 0x10190
ja 0x2505
movzbl -0xbc(%rbp), %eax
cmpl $0x0, %eax
jl 0x2505
movzbl -0xbc(%rbp), %eax
cmpl 0xdcac(%rip), %eax      # 0x10194
jae 0x2505
movzbl -0xbd(%rbp), %eax
cmpl $0x0, %eax
jl 0x2505
movzbl -0xbd(%rbp), %eax
cmpl 0xdc95(%rip), %eax      # 0x10198
jb 0x250a
jmp 0x27ff
movzbl -0xbd(%rbp), %eax
movq %rax, -0xd0(%rbp)
movl 0xdc76(%rip), %eax      # 0x10194
imulq -0xd0(%rbp), %rax
movq %rax, -0xd0(%rbp)
movzbl -0xbc(%rbp), %eax
addq -0xd0(%rbp), %rax
movq %rax, -0xd0(%rbp)
movl 0xdc48(%rip), %eax      # 0x10190
imulq -0xd0(%rbp), %rax
movq %rax, -0xd0(%rbp)
movzbl -0xbb(%rbp), %eax
subq $0x1, %rax
addq -0xd0(%rbp), %rax
movq %rax, -0xd0(%rbp)
movq -0xd0(%rbp), %rax
movq %rax, -0x138(%rbp)
leaq 0x11c3b(%rip), %rdi     # 0x141c0
callq 0x45e0
movq %rax, %rcx
movq -0x138(%rbp), %rax
cmpq %rcx, %rax
jae 0x259b
jmp 0x25ba
leaq 0x8a62(%rip), %rdi      # 0xb004
leaq 0x8a78(%rip), %rsi      # 0xb021
movl $0x5c, %edx
leaq 0x8a79(%rip), %rcx      # 0xb02e
callq 0x2130
movq -0xd0(%rbp), %rsi
leaq 0x11bf8(%rip), %rdi     # 0x141c0
callq 0x4660
movq %rax, -0xe0(%rbp)
movq %rdx, -0xd8(%rbp)
leaq -0xe0(%rbp), %rdi
callq 0x46c0
testb $0x1, %al
jne 0x25ed
jmp 0x25f2
jmp 0x27ff
movq -0x10(%rbp), %rax
movq %rax, -0x150(%rbp)
movq -0x18(%rbp), %rsi
leaq -0x108(%rbp), %rdi
movq %rdi, -0x148(%rbp)
callq 0x46f0
movq -0x150(%rbp), %rdi
movq -0x148(%rbp), %rsi
movq -0x20(%rbp), %rdx
callq 0x9150
movb %al, -0x139(%rbp)
jmp 0x2633
movb -0x139(%rbp), %al
xorb $-0x1, %al
movb %al, -0x151(%rbp)
leaq -0x108(%rbp), %rdi
callq 0x4730
movb -0x151(%rbp), %al
testb $0x1, %al
jne 0x2659
jmp 0x2681
jmp 0x27ff
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x110(%rbp)
movl %eax, -0x114(%rbp)
leaq -0x108(%rbp), %rdi
callq 0x4730
jmp 0x2808
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
movq -0x20(%rbp), %rdx
callq 0x9380
movl %eax, -0xc4(%rbp)
andl $0xfe, %eax
cmpl $0xfa, %eax
je 0x26a9
jmp 0x27ff
leaq -0xbd(%rbp), %rdi
callq 0x9430
movl %eax, %esi
movq -0x10(%rbp), %rdx
movq -0x18(%rbp), %rcx
movq -0x20(%rbp), %r8
movl -0xc4(%rbp), %r9d
leaq 0xdadf(%rip), %rdi      # 0x101b0
callq 0x9610
movl %eax, -0xc4(%rbp)
cmpl $0x0, %eax
jge 0x26e6
jmp 0x27ff
movzbl -0xbd(%rbp), %esi
movzbl -0xbc(%rbp), %edx
movzbl -0xbb(%rbp), %eax
movl %eax, -0x158(%rbp)
movzbl -0xba(%rbp), %ecx
movl $0x80, %r8d
shll %cl, %r8d
movl -0x158(%rbp), %ecx
leaq 0x897b(%rip), %rdi      # 0xb099
movb $0x0, %al
callq 0x2050
movq -0xd0(%rbp), %rax
movl 0xda6a(%rip), %ecx      # 0x1019c
imulq %rcx, %rax
movq %rax, -0x120(%rbp)
movq -0x8(%rbp), %rdi
movq -0x120(%rbp), %rsi
xorl %edx, %edx
callq 0x21b0
movq -0x8(%rbp), %rdi
callq 0x2030
cmpq -0x120(%rbp), %rax
je 0x2766
jmp 0x27ff
movl 0xda30(%rip), %eax      # 0x1019c
movl %eax, %esi
movq -0x8(%rbp), %rcx
leaq 0xda37(%rip), %rdi      # 0x101b0
movl $0x1, %edx
callq 0x22c0
movq -0xd0(%rbp), %rax
movq %rax, -0x160(%rbp)
leaq 0x11a28(%rip), %rdi     # 0x141c0
callq 0x45e0
movq %rax, %rcx
movq -0x160(%rbp), %rax
cmpq %rcx, %rax
jae 0x27ae
jmp 0x27cd
leaq 0x884f(%rip), %rdi      # 0xb004
leaq 0x8865(%rip), %rsi      # 0xb021
movl $0x7b, %edx
leaq 0x8866(%rip), %rcx      # 0xb02e
callq 0x2130
movq -0xd0(%rbp), %rsi
leaq 0x119e5(%rip), %rdi     # 0x141c0
callq 0x4660
movq %rax, -0x130(%rbp)
movq %rdx, -0x128(%rbp)
leaq -0x130(%rbp), %rdi
movl $0x1, %esi
callq 0x4750
addq $0x160, %rsp            # imm = 0x160
popq %rbp
retq
movq -0x110(%rbp), %rdi
callq 0x2290
nopw %cs:(%rax,%rax)