// File: atsidaev[P]trx2x/DiskImage.cpp
// Function: TDiskImage::writeUDI(int)
void TDiskImage::writeUDI(int hfile)
{
   uint32_t CRC = -1l;
   unsigned int i;

   UDI_HEADER hudi;
   memcpy(hudi.ID, "UDI!", 4);
   hudi.UnpackedLength = 0;
   hudi.Version = 0x00;
   hudi.MaxCylinder = MaxTrack;
   hudi.MaxSide = MaxSide;
   hudi._zero = 0x00;
   hudi.ExtHdrLength = 0;

   lseek(hfile, 0, SEEK_SET);
   write(hfile, &hudi, sizeof(UDI_HEADER));
   lseek(hfile, 0, SEEK_SET);
   char testbuf[1024];
   long trs = read(hfile, testbuf, 16);
   if( (trs < 0)|| (memcmp(testbuf, &hudi, 16)!=0 ) )
   {
      ShowError(ERR_BADOPENED);
      return;
   }


   unsigned char valB;
   unsigned short valS;
   unsigned char clkbuf[65536];

   for(unsigned int trk=0; trk <= unsigned(MaxTrack); trk++)
      for(unsigned int side=0; side <= unsigned(MaxSide); side++)
      {
         valB = 0x00;
         write(hfile, &valB, 1);
         valS = FTrackLength[trk][side];
         write(hfile, &valS, 2);
         write(hfile, FTracksPtr[trk][side][0], valS);

         unsigned int cptr=0;
         unsigned char mask;
         for(i=0; i < unsigned(valS/8 + ((valS-(valS/8)*8)? 1:0)); i++)
         {
            clkbuf[cptr] = 0x00;
            mask = 0x01;
            for(int j=0; j < 8; j++)
            {

               if(FTracksPtr[trk][side][1][i*8+j]) clkbuf[cptr] |= mask;
               mask <<= 1;
            }
            cptr++;
         }

         write(hfile, clkbuf, (valS/8 + ((valS-(valS/8)*8)? 1:0)) );
      }

   lseek(hfile, 0, SEEK_SET);
   long len = filelength(hfile);
   char *cbuf = new char[len+0x10000];
   UDI_HEADER *newudi = (UDI_HEADER*)cbuf;
   read(hfile, cbuf, len);
   newudi->UnpackedLength = len;

   CRC = -1l;
   for(i=0; i < len; i++) CRC = CalcCRC32(CRC, cbuf[i]);

   lseek(hfile, 0, SEEK_SET);
   write(hfile, cbuf, len);
   write(hfile, &CRC, 4);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x10480, %rsp          # imm = 0x10480
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x10478(%rbp)
movl $0xffffffff, -0x10(%rbp) # imm = 0xFFFFFFFF
movl 0xa6c5(%rip), %ecx      # 0xe3df
movl %ecx, -0x24(%rbp)
movl $0x0, -0x20(%rbp)
movb $0x0, -0x1c(%rbp)
movb 0x145007(%rax), %cl
movb %cl, -0x1b(%rbp)
movb 0x145008(%rax), %al
movb %al, -0x1a(%rbp)
movb $0x0, -0x19(%rbp)
movl $0x0, -0x18(%rbp)
movl -0xc(%rbp), %edi
xorl %eax, %eax
movl %eax, %esi
xorl %edx, %edx
callq 0x11f0
movl -0xc(%rbp), %edi
leaq -0x24(%rbp), %rsi
movl $0x10, %edx
callq 0x1050
movl -0xc(%rbp), %edi
xorl %eax, %eax
movl %eax, %esi
xorl %edx, %edx
callq 0x11f0
movl -0xc(%rbp), %edi
leaq -0x430(%rbp), %rsi
movl $0x10, %edx
callq 0x11a0
movq %rax, -0x438(%rbp)
cmpq $0x0, -0x438(%rbp)
jl 0x3dbd
leaq -0x430(%rbp), %rax
movups (%rax), %xmm0
movups -0x24(%rbp), %xmm1
pcmpeqb %xmm1, %xmm0
pmovmskb %xmm0, %eax
subl $0xffff, %eax           # imm = 0xFFFF
setne %al
movzbl %al, %eax
cmpl $0x0, %eax
je 0x3dd5
movq -0x10478(%rbp), %rdi
leaq 0xa619(%rip), %rsi      # 0xe3e4
callq 0x2c30
jmp 0x4144
movl $0x0, -0x10444(%rbp)
movq -0x10478(%rbp), %rcx
movl -0x10444(%rbp), %eax
movzbl 0x145007(%rcx), %ecx
cmpl %ecx, %eax
ja 0x4063
movl $0x0, -0x10448(%rbp)
movq -0x10478(%rbp), %rcx
movl -0x10448(%rbp), %eax
movzbl 0x145008(%rcx), %ecx
cmpl %ecx, %eax
ja 0x404d
movb $0x0, -0x439(%rbp)
movl -0xc(%rbp), %edi
leaq -0x439(%rbp), %rsi
movl $0x1, %edx
callq 0x1050
movq -0x10478(%rbp), %rax
movl -0x10444(%rbp), %ecx
shlq $0xa, %rcx
addq %rcx, %rax
movl -0x10448(%rbp), %ecx
movl (%rax,%rcx,4), %eax
movw %ax, -0x43c(%rbp)
movl -0xc(%rbp), %edi
leaq -0x43c(%rbp), %rsi
movl $0x2, %edx
callq 0x1050
movq -0x10478(%rbp), %rax
movl -0xc(%rbp), %edi
addq $0x40000, %rax          # imm = 0x40000
movl -0x10444(%rbp), %ecx
shlq $0xc, %rcx
addq %rcx, %rax
movl -0x10448(%rbp), %ecx
shlq $0x4, %rcx
addq %rcx, %rax
movq (%rax), %rsi
movzwl -0x43c(%rbp), %eax
movl %eax, %edx
callq 0x1050
movl $0x0, -0x1044c(%rbp)
movl $0x0, -0x14(%rbp)
movl -0x14(%rbp), %eax
movl %eax, -0x1047c(%rbp)
movzwl -0x43c(%rbp), %eax
movl $0x8, %ecx
cltd
idivl %ecx
movl %eax, %ecx
movzwl -0x43c(%rbp), %edi
movzwl -0x43c(%rbp), %eax
movl $0x8, %esi
cltd
idivl %esi
movl %eax, %edx
movl -0x1047c(%rbp), %eax
shll $0x3, %edx
subl %edx, %edi
xorl %edx, %edx
movl $0x1, %esi
cmpl $0x0, %edi
cmovnel %esi, %edx
addl %edx, %ecx
cmpl %ecx, %eax
jae 0x3fdd
movl -0x1044c(%rbp), %eax
movb $0x0, -0x10440(%rbp,%rax)
movb $0x1, -0x1044d(%rbp)
movl $0x0, -0x10454(%rbp)
cmpl $0x8, -0x10454(%rbp)
jge 0x3fc0
movq -0x10478(%rbp), %rax
addq $0x40000, %rax          # imm = 0x40000
movl -0x10444(%rbp), %ecx
shlq $0xc, %rcx
addq %rcx, %rax
movl -0x10448(%rbp), %ecx
shlq $0x4, %rcx
addq %rcx, %rax
movq 0x8(%rax), %rax
movl -0x14(%rbp), %ecx
shll $0x3, %ecx
addl -0x10454(%rbp), %ecx
movl %ecx, %ecx
cmpb $0x0, (%rax,%rcx)
je 0x3f9d
movzbl -0x1044d(%rbp), %edx
movl -0x1044c(%rbp), %eax
movzbl -0x10440(%rbp,%rax), %ecx
orl %edx, %ecx
movb %cl, -0x10440(%rbp,%rax)
movzbl -0x1044d(%rbp), %eax
shll %eax
movb %al, -0x1044d(%rbp)
movl -0x10454(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x10454(%rbp)
jmp 0x3f33
movl -0x1044c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1044c(%rbp)
movl -0x14(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x14(%rbp)
jmp 0x3ec0
movl -0xc(%rbp), %edi
leaq -0x10440(%rbp), %rsi
movzwl -0x43c(%rbp), %eax
movl $0x8, %ecx
cltd
idivl %ecx
movl %eax, -0x10480(%rbp)
movzwl -0x43c(%rbp), %r8d
movzwl -0x43c(%rbp), %eax
movl $0x8, %ecx
cltd
idivl %ecx
movl %eax, %ecx
movl -0x10480(%rbp), %eax
shll $0x3, %ecx
subl %ecx, %r8d
xorl %ecx, %ecx
movl $0x1, %edx
cmpl $0x0, %r8d
cmovnel %edx, %ecx
addl %ecx, %eax
movslq %eax, %rdx
callq 0x1050
movl -0x10448(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x10448(%rbp)
jmp 0x3e05
jmp 0x404f
movl -0x10444(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x10444(%rbp)
jmp 0x3ddf
movl -0xc(%rbp), %edi
xorl %eax, %eax
movl %eax, %esi
xorl %edx, %edx
callq 0x11f0
movl -0xc(%rbp), %edi
callq 0xd6a0
movq %rax, -0x10460(%rbp)
movq -0x10460(%rbp), %rdi
addq $0x10000, %rdi          # imm = 0x10000
callq 0x1030
movq %rax, -0x10468(%rbp)
movq -0x10468(%rbp), %rax
movq %rax, -0x10470(%rbp)
movl -0xc(%rbp), %edi
movq -0x10468(%rbp), %rsi
movq -0x10460(%rbp), %rdx
callq 0x11a0
movq -0x10460(%rbp), %rax
movl %eax, %ecx
movq -0x10470(%rbp), %rax
movl %ecx, 0x4(%rax)
movl $0xffffffff, -0x10(%rbp) # imm = 0xFFFFFFFF
movl $0x0, -0x14(%rbp)
movl -0x14(%rbp), %eax
cmpq -0x10460(%rbp), %rax
jge 0x410f
movl -0x10(%rbp), %edi
movq -0x10468(%rbp), %rax
movl -0x14(%rbp), %ecx
movzbl (%rax,%rcx), %esi
callq 0x1b20
movl %eax, -0x10(%rbp)
movl -0x14(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x14(%rbp)
jmp 0x40df
movl -0xc(%rbp), %edi
xorl %eax, %eax
movl %eax, %esi
xorl %edx, %edx
callq 0x11f0
movl -0xc(%rbp), %edi
movq -0x10468(%rbp), %rsi
movq -0x10460(%rbp), %rdx
callq 0x1050
movl -0xc(%rbp), %edi
leaq -0x10(%rbp), %rsi
movl $0x4, %edx
callq 0x1050
addq $0x10480, %rsp          # imm = 0x10480
popq %rbp
retq
nopl (%rax)