// File: atsidaev[P]trx2x/DiskImage.cpp
// Function: TDiskImage::ApplySectorCRC(VGFIND_SECTOR)
void TDiskImage::ApplySectorCRC(VGFIND_SECTOR vgfs)
{
   unsigned char *TrackPtr = vgfs.vgfa.TrackPointer;
   unsigned int TrackLen = vgfs.vgfa.TrackLength;
   unsigned int len = vgfs.OffsetEndSector - vgfs.MarkedOffsetSector;
   if(vgfs.OffsetEndSector < vgfs.MarkedOffsetSector)
      len = (TrackLen-vgfs.MarkedOffsetSector)+vgfs.OffsetEndSector;

   unsigned int off1 = vgfs.MarkedOffsetSector;
   unsigned int len1 = TrackLen-vgfs.MarkedOffsetSector;
   if(len1 > len) len1 = len;
   unsigned int off2 = 0;
   unsigned int len2 = 0;
   if(len1 < len) len2 = len - len1;

   unsigned int i;
   unsigned short CRC = 0xFFFF;
   for(i=0; i < len1; i++)
   {
      CRC ^= TrackPtr[off1+i] << 8;
      for(unsigned int j=0; j < 8; j++)
      {
         if(CRC & 0x8000) CRC = (CRC << 1) ^ 0x1021;
         else CRC <<= 1;
      }
   }
   for(i=0; i < len2; i++)
   {
      CRC ^= TrackPtr[off2+i] << 8;
      for(unsigned int j=0; j < 8; j++)
      {
         if(CRC & 0x8000) CRC = (CRC << 1) ^ 0x1021;
         else CRC <<= 1;
      }
   }
   unsigned int crcoff = (off1+len1)%TrackLen;
   if(len2) crcoff = (off2+len2)%TrackLen;

   TrackPtr[crcoff] = (unsigned char)(CRC>>8);
   TrackPtr[(crcoff+1)%TrackLen] = (unsigned char)(CRC&0xFF);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
leaq 0x10(%rbp), %rcx
movq %rcx, -0x48(%rbp)
movq %rdi, -0x8(%rbp)
movq (%rcx), %rax
movq %rax, -0x10(%rbp)
movl 0x10(%rcx), %eax
movl %eax, -0x14(%rbp)
movl 0x4c(%rcx), %eax
subl 0x44(%rcx), %eax
movl %eax, -0x18(%rbp)
movl 0x4c(%rcx), %eax
cmpl 0x44(%rcx), %eax
jae 0x207e
movq -0x48(%rbp), %rcx
movl -0x14(%rbp), %eax
subl 0x44(%rcx), %eax
addl 0x4c(%rcx), %eax
movl %eax, -0x18(%rbp)
movq -0x48(%rbp), %rcx
movl 0x44(%rcx), %eax
movl %eax, -0x1c(%rbp)
movl -0x14(%rbp), %eax
subl 0x44(%rcx), %eax
movl %eax, -0x20(%rbp)
movl -0x20(%rbp), %eax
cmpl -0x18(%rbp), %eax
jbe 0x209f
movl -0x18(%rbp), %eax
movl %eax, -0x20(%rbp)
movl $0x0, -0x24(%rbp)
movl $0x0, -0x28(%rbp)
movl -0x20(%rbp), %eax
cmpl -0x18(%rbp), %eax
jae 0x20be
movl -0x18(%rbp), %eax
subl -0x20(%rbp), %eax
movl %eax, -0x28(%rbp)
movw $0xffff, -0x2e(%rbp)    # imm = 0xFFFF
movl $0x0, -0x2c(%rbp)
movl -0x2c(%rbp), %eax
cmpl -0x20(%rbp), %eax
jae 0x2140
movq -0x10(%rbp), %rax
movl -0x1c(%rbp), %ecx
addl -0x2c(%rbp), %ecx
movl %ecx, %ecx
movzbl (%rax,%rcx), %ecx
shll $0x8, %ecx
movzwl -0x2e(%rbp), %eax
xorl %ecx, %eax
movw %ax, -0x2e(%rbp)
movl $0x0, -0x34(%rbp)
cmpl $0x8, -0x34(%rbp)
jae 0x2133
movzwl -0x2e(%rbp), %eax
andl $0x8000, %eax           # imm = 0x8000
cmpl $0x0, %eax
je 0x211c
movzwl -0x2e(%rbp), %eax
shll %eax
xorl $0x1021, %eax           # imm = 0x1021
movw %ax, -0x2e(%rbp)
jmp 0x2126
movzwl -0x2e(%rbp), %eax
shll %eax
movw %ax, -0x2e(%rbp)
jmp 0x2128
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0x20f7
jmp 0x2135
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0x20cb
movl $0x0, -0x2c(%rbp)
movl -0x2c(%rbp), %eax
cmpl -0x28(%rbp), %eax
jae 0x21bc
movq -0x10(%rbp), %rax
movl -0x24(%rbp), %ecx
addl -0x2c(%rbp), %ecx
movl %ecx, %ecx
movzbl (%rax,%rcx), %ecx
shll $0x8, %ecx
movzwl -0x2e(%rbp), %eax
xorl %ecx, %eax
movw %ax, -0x2e(%rbp)
movl $0x0, -0x38(%rbp)
cmpl $0x8, -0x38(%rbp)
jae 0x21af
movzwl -0x2e(%rbp), %eax
andl $0x8000, %eax           # imm = 0x8000
cmpl $0x0, %eax
je 0x2198
movzwl -0x2e(%rbp), %eax
shll %eax
xorl $0x1021, %eax           # imm = 0x1021
movw %ax, -0x2e(%rbp)
jmp 0x21a2
movzwl -0x2e(%rbp), %eax
shll %eax
movw %ax, -0x2e(%rbp)
jmp 0x21a4
movl -0x38(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x38(%rbp)
jmp 0x2173
jmp 0x21b1
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0x2147
movl -0x1c(%rbp), %eax
addl -0x20(%rbp), %eax
xorl %edx, %edx
divl -0x14(%rbp)
movl %edx, -0x3c(%rbp)
cmpl $0x0, -0x28(%rbp)
je 0x21de
movl -0x24(%rbp), %eax
addl -0x28(%rbp), %eax
xorl %edx, %edx
divl -0x14(%rbp)
movl %edx, -0x3c(%rbp)
movzwl -0x2e(%rbp), %eax
sarl $0x8, %eax
movb %al, %dl
movq -0x10(%rbp), %rax
movl -0x3c(%rbp), %ecx
movb %dl, (%rax,%rcx)
movzwl -0x2e(%rbp), %eax
andl $0xff, %eax
movb %al, -0x49(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x58(%rbp)
movl -0x3c(%rbp), %eax
addl $0x1, %eax
xorl %edx, %edx
divl -0x14(%rbp)
movq -0x58(%rbp), %rax
movl %edx, %ecx
movb -0x49(%rbp), %dl
movl %ecx, %ecx
movb %dl, (%rax,%rcx)
popq %rbp
retq