// File: /workspace/github_cpp_makefile_star2_O1/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, %r13d
movl %r8d, %ebx
movq %rcx, %r14
movq %rdx, %r15
movq %rsi, %r12
movq %rdi, -0x40(%rbp)
leaq -0x2d(%rbp), %rdi
callq 0x5286
movq %r12, %rdi
movq %r15, %rsi
movq %r14, %rdx
callq 0x5241
cmpl $0xfe, %eax
jne 0x275e
leaq -0x2d(%rbp), %rdi
movq %r12, %rsi
movq %r15, %rdx
movq %r14, %rcx
callq 0x52be
testl %eax, %eax
js 0x275e
leaq -0x2d(%rbp), %rdi
callq 0x52a6
cmpl 0x6c21(%rip), %eax      # 0x916c
jne 0x275e
movzbl -0x2d(%rbp), %eax
cmpl %ebx, %eax
jne 0x275e
movzbl -0x2c(%rbp), %edx
cmpl %r13d, %edx
jne 0x275e
movzbl -0x2b(%rbp), %ecx
testl %ecx, %ecx
je 0x275e
movl 0x6be4(%rip), %esi      # 0x9160
cmpl %ecx, %esi
jb 0x275e
movl 0x6bda(%rip), %edi      # 0x9164
cmpl %edx, %edi
jbe 0x275e
cmpl %eax, 0x6bd0(%rip)      # 0x9168
jbe 0x275e
movl %eax, %eax
imulq %rax, %rdi
movl %edx, %eax
addq %rdi, %rax
imulq %rsi, %rax
movl %ecx, %ecx
leaq (%rcx,%rax), %r13
decq %r13
movq 0xabe3(%rip), %rcx      # 0xd1a0
movl 0xabe5(%rip), %edx      # 0xd1a8
movq 0xabc6(%rip), %rax      # 0xd190
subq %rax, %rcx
leaq (%rdx,%rcx,8), %rcx
cmpq %rcx, %r13
jae 0x276d
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 0x275e
movups (%r15), %xmm0
leaq -0x70(%rbp), %rdi
movaps %xmm0, -0x10(%rdi)
leaq 0x10(%r15), %rsi
callq 0x3e48
leaq -0x80(%rbp), %rsi
movq %r12, %rdi
movq %r14, %rdx
callq 0x50f2
movq -0x70(%rbp), %rdi
testq %rdi, %rdi
je 0x2661
movb %al, -0x34(%rbp)
callq 0x2140
movb -0x34(%rbp), %al
testb %al, %al
je 0x275e
movq %r12, %rdi
movq %r15, %rsi
movq %r14, %rdx
callq 0x5241
movl %eax, %ecx
andl $0xfe, %ecx
cmpl $0xfa, %ecx
jne 0x275e
leaq -0x2d(%rbp), %rdi
movl %eax, -0x34(%rbp)
callq 0x52a6
leaq 0x6ae2(%rip), %rdi      # 0x9180
movl %eax, %esi
movq %r12, %rdx
movq %r15, %rcx
movq %r14, %r8
movl -0x34(%rbp), %r9d
callq 0x53e2
testl %eax, %eax
js 0x275e
movzbl -0x2d(%rbp), %esi
movzbl -0x2c(%rbp), %edx
movzbl -0x2b(%rbp), %eax
movb -0x2a(%rbp), %cl
movl $0x80, %r8d
shll %cl, %r8d
leaq 0x39dc(%rip), %rdi      # 0x60b5
movl %eax, %ecx
xorl %eax, %eax
callq 0x2040
movl 0x6a83(%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 0x275e
movl 0x6a5b(%rip), %esi      # 0x916c
leaq 0x6a68(%rip), %rdi      # 0x9180
movl $0x1, %edx
movq -0x40(%rbp), %rcx
callq 0x2250
movq 0xaa73(%rip), %rcx      # 0xd1a0
movl 0xaa75(%rip), %edx      # 0xd1a8
movq 0xaa56(%rip), %rax      # 0xd190
subq %rax, %rcx
leaq (%rdx,%rcx,8), %rcx
cmpq %rcx, %r13
jae 0x278c
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 0x38ac(%rip), %rdi      # 0x6020
leaq 0x38c2(%rip), %rsi      # 0x603d
leaq 0x38c8(%rip), %rcx      # 0x604a
movl $0x5c, %edx
callq 0x20e0
leaq 0x388d(%rip), %rdi      # 0x6020
leaq 0x38a3(%rip), %rsi      # 0x603d
leaq 0x38a9(%rip), %rcx      # 0x604a
movl $0x7b, %edx
callq 0x20e0
movq %rax, %rbx
movq -0x70(%rbp), %rdi
testq %rdi, %rdi
je 0x27bc
callq 0x2140
movq %rbx, %rdi
callq 0x2220