// File: /workspace/github_cpp_makefile_star2_O3/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 $0x58, %rsp
movl %r9d, %ebx
movl %r8d, %r13d
movq %rcx, %r14
movq %rdx, %r15
movq %rsi, %r12
movq %rdi, -0x40(%rbp)
leaq -0x2d(%rbp), %rdi
callq 0x534a
movq %r12, %rdi
movq %r15, %rsi
movq %r14, %rdx
callq 0x5305
cmpl $0xfe, %eax
jne 0x275b
leaq -0x2d(%rbp), %rdi
movq %r12, %rsi
movq %r15, %rdx
movq %r14, %rcx
callq 0x5382
testl %eax, %eax
js 0x275b
leaq -0x2d(%rbp), %rdi
callq 0x536a
cmpl 0x6c21(%rip), %eax      # 0x916c
jne 0x275b
movzbl -0x2d(%rbp), %eax
cmpl %r13d, %eax
jne 0x275b
movzbl -0x2c(%rbp), %ecx
cmpl %ebx, %ecx
jne 0x275b
movzbl -0x2b(%rbp), %edx
testl %edx, %edx
je 0x275b
movl 0x6be4(%rip), %esi      # 0x9160
cmpl %edx, %esi
jb 0x275b
movl 0x6bda(%rip), %edi      # 0x9164
cmpl %ebx, %edi
jbe 0x275b
cmpl %r13d, 0x6bcf(%rip)     # 0x9168
jbe 0x275b
imulq %rax, %rdi
addq %rcx, %rdi
imulq %rsi, %rdi
movl %edx, %eax
leaq (%rax,%rdi), %r13
decq %r13
movq 0xabe6(%rip), %rcx      # 0xd1a0
movl 0xabe8(%rip), %edx      # 0xd1a8
movq 0xabc9(%rip), %rax      # 0xd190
subq %rax, %rcx
leaq (%rdx,%rcx,8), %rcx
cmpq %rcx, %r13
jae 0x276a
leaq 0x3f(%r13), %rbx
testq %r13, %r13
cmovnsq %r13, %rbx
sarq $0x6, %rbx
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
leaq 0x3f(%rcx), %rdx
andq %r13, %rdx
xorl %esi, %esi
cmpq %rcx, %rdx
setbe %sil
movl $0x1, %edx
movl %r13d, %ecx
shlq %cl, %rdx
movq %rdx, -0x48(%rbp)
leaq (%rax,%rbx,8), %rax
movq %rsi, -0x50(%rbp)
movq -0x8(%rax,%rsi,8), %rax
btq %r13, %rax
jb 0x275b
movups (%r15), %xmm0
leaq -0x70(%rbp), %rdi
movaps %xmm0, -0x10(%rdi)
leaq 0x10(%r15), %rsi
callq 0x3f2c
leaq -0x80(%rbp), %rsi
movq %r12, %rdi
movq %r14, %rdx
callq 0x51b3
movq -0x70(%rbp), %rdi
testq %rdi, %rdi
je 0x265e
movb %al, -0x34(%rbp)
callq 0x2140
movb -0x34(%rbp), %al
testb %al, %al
je 0x275b
movq %r12, %rdi
movq %r15, %rsi
movq %r14, %rdx
callq 0x5305
movl %eax, %ecx
andl $0xfe, %ecx
cmpl $0xfa, %ecx
jne 0x275b
leaq -0x2d(%rbp), %rdi
movl %eax, -0x34(%rbp)
callq 0x536a
leaq 0x6ae5(%rip), %rdi      # 0x9180
movl %eax, %esi
movq %r12, %rdx
movq %r15, %rcx
movq %r14, %r8
movl -0x34(%rbp), %r9d
callq 0x54a6
testl %eax, %eax
js 0x275b
movzbl -0x2d(%rbp), %esi
movzbl -0x2c(%rbp), %edx
movzbl -0x2b(%rbp), %eax
movb -0x2a(%rbp), %cl
movl $0x80, %r8d
shll %cl, %r8d
leaq 0x39df(%rip), %rdi      # 0x60b5
movl %eax, %ecx
xorl %eax, %eax
callq 0x2040
movl 0x6a86(%rip), %r14d     # 0x916c
imulq %r13, %r14
movq -0x40(%rbp), %r15
movq %r15, %rdi
movq %r14, %rsi
xorl %edx, %edx
callq 0x2170
movq %r15, %rdi
callq 0x2030
cmpq %r14, %rax
jne 0x275b
movl 0x6a5e(%rip), %esi      # 0x916c
leaq 0x6a6b(%rip), %rdi      # 0x9180
movl $0x1, %edx
movq -0x40(%rbp), %rcx
callq 0x2250
movq 0xaa76(%rip), %rcx      # 0xd1a0
movl 0xaa78(%rip), %edx      # 0xd1a8
movq 0xaa59(%rip), %rax      # 0xd190
subq %rax, %rcx
leaq (%rdx,%rcx,8), %rcx
cmpq %rcx, %r13
jae 0x2789
movq -0x50(%rbp), %rcx
leaq -0x8(,%rcx,8), %rcx
leaq (%rax,%rbx,8), %rax
movq -0x48(%rbp), %rdx
orq %rdx, (%rcx,%rax)
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x38af(%rip), %rdi      # 0x6020
leaq 0x38c5(%rip), %rsi      # 0x603d
leaq 0x38cb(%rip), %rcx      # 0x604a
movl $0x5c, %edx
callq 0x20e0
leaq 0x3890(%rip), %rdi      # 0x6020
leaq 0x38a6(%rip), %rsi      # 0x603d
leaq 0x38ac(%rip), %rcx      # 0x604a
movl $0x7b, %edx
callq 0x20e0
movq %rax, %rbx
movq -0x70(%rbp), %rdi
testq %rdi, %rdi
je 0x27b9
callq 0x2140
movq %rbx, %rdi
callq 0x2220