// File: /workspace/github_cpp_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movl %r9d, %ebx
movl %r8d, %r13d
movq %rcx, %r14
movq %rdx, %r15
movq %rsi, %r12
movq %rdi, -0x38(%rbp)
leaq -0x2d(%rbp), %rdi
callq 0x4d1a
movq %r12, %rdi
movq %r15, %rsi
movq %r14, %rdx
callq 0x4cd4
cmpl $0xfe, %eax
jne 0x25cf
leaq -0x2d(%rbp), %rdi
movq %r12, %rsi
movq %r15, %rdx
movq %r14, %rcx
callq 0x4d4e
testl %eax, %eax
js 0x25cf
leaq -0x2d(%rbp), %rdi
callq 0x4d36
cmpl 0x6d51(%rip), %eax      # 0x916c
jne 0x25cf
movzbl -0x2d(%rbp), %eax
cmpl %r13d, %eax
jne 0x25cf
movzbl -0x2c(%rbp), %ecx
cmpl %ebx, %ecx
jne 0x25cf
movzbl -0x2b(%rbp), %edx
testl %edx, %edx
je 0x25cf
movl 0x6d14(%rip), %esi      # 0x9160
cmpl %edx, %esi
jb 0x25cf
movl 0x6d0a(%rip), %edi      # 0x9164
cmpl %ebx, %edi
jbe 0x25cf
cmpl %r13d, 0x6cff(%rip)     # 0x9168
jbe 0x25cf
imulq %rax, %rdi
addq %rcx, %rdi
imulq %rsi, %rdi
movl %edx, %eax
leaq (%rax,%rdi), %r13
decq %r13
movq 0xad16(%rip), %rax      # 0xd1a0
movl 0xad18(%rip), %ecx      # 0xd1a8
subq 0xacf9(%rip), %rax      # 0xd190
leaq (%rcx,%rax,8), %rax
cmpq %rax, %r13
jae 0x25de
leaq 0xace5(%rip), %rdi      # 0xd190
movq %r13, %rsi
callq 0x3810
testq %rdx, (%rax)
jne 0x25cf
leaq -0x60(%rbp), %rbx
movq %rbx, %rdi
movq %r15, %rsi
callq 0x3832
movq %r12, %rdi
movq %rbx, %rsi
movq %r14, %rdx
callq 0x4b79
movl %eax, %ebx
leaq -0x50(%rbp), %rdi
callq 0x3972
testb %bl, %bl
je 0x25cf
movq %r12, %rdi
movq %r15, %rsi
movq %r14, %rdx
callq 0x4cd4
movl %eax, %ebx
andl $0xfe, %eax
cmpl $0xfa, %eax
jne 0x25cf
leaq -0x2d(%rbp), %rdi
callq 0x4d36
leaq 0x6c64(%rip), %rdi      # 0x9180
movl %eax, %esi
movq %r12, %rdx
movq %r15, %rcx
movq %r14, %r8
movl %ebx, %r9d
callq 0x4e51
testl %eax, %eax
js 0x25cf
movzbl -0x2d(%rbp), %esi
movzbl -0x2c(%rbp), %edx
movzbl -0x2b(%rbp), %eax
movb -0x2a(%rbp), %cl
movl $0x80, %r8d
shll %cl, %r8d
leaq 0x3b43(%rip), %rdi      # 0x6099
movl %eax, %ecx
xorl %eax, %eax
callq 0x2040
movl 0x6c07(%rip), %ebx      # 0x916c
imulq %r13, %rbx
movq -0x38(%rbp), %r14
movq %r14, %rdi
movq %rbx, %rsi
xorl %edx, %edx
callq 0x2160
movq %r14, %rdi
callq 0x2030
cmpq %rbx, %rax
jne 0x25cf
movl 0x6bdf(%rip), %esi      # 0x916c
leaq 0x6bec(%rip), %rdi      # 0x9180
pushq $0x1
popq %rdx
movq -0x38(%rbp), %rcx
callq 0x2250
movq 0xabf9(%rip), %rax      # 0xd1a0
movl 0xabfb(%rip), %ecx      # 0xd1a8
subq 0xabdc(%rip), %rax      # 0xd190
leaq (%rcx,%rax,8), %rax
cmpq %rax, %r13
jae 0x25fb
leaq 0xabcc(%rip), %rdi      # 0xd190
movq %r13, %rsi
callq 0x3810
orq %rdx, (%rax)
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x3a1f(%rip), %rdi      # 0x6004
leaq 0x3a35(%rip), %rsi      # 0x6021
leaq 0x3a3b(%rip), %rcx      # 0x602e
pushq $0x5c
popq %rdx
callq 0x20f0
leaq 0x3a02(%rip), %rdi      # 0x6004
leaq 0x3a18(%rip), %rsi      # 0x6021
leaq 0x3a1e(%rip), %rcx      # 0x602e
pushq $0x7b
jmp 0x25f5
movq %rax, %rbx
leaq -0x50(%rbp), %rdi
callq 0x3972
movq %rbx, %rdi
callq 0x2230