// File: /workspace/github_cpp_makefile_star2_O3/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x10448, %rsp          # imm = 0x10448
movl %esi, %ebx
movq %rdi, %rbp
leaq 0x30(%rsp), %r15
movq $0x21494455, (%r15)     # imm = 0x21494455
xorl %eax, %eax
movb %al, 0x8(%r15)
movb 0x145007(%rdi), %cl
movb %cl, 0x9(%r15)
movb 0x145008(%rdi), %cl
movb %cl, 0xa(%r15)
movb %al, 0xb(%r15)
movl $0x0, 0xc(%r15)
movl %esi, %edi
xorl %esi, %esi
xorl %edx, %edx
callq 0x1210
movl $0x10, %edx
movl %ebx, %edi
movq %r15, %rsi
callq 0x1060
movl %ebx, %edi
xorl %esi, %esi
xorl %edx, %edx
callq 0x1210
leaq 0x40(%rsp), %rsi
movl $0x10, %edx
movl %ebx, %edi
callq 0x11b0
testq %rax, %rax
js 0x333a
movdqu 0x30(%rsp), %xmm0
pcmpeqb 0x40(%rsp), %xmm0
pmovmskb %xmm0, %eax
cmpl $0xffff, %eax           # imm = 0xFFFF
je 0x3357
leaq 0x5107(%rip), %rsi      # 0x8448
addq $0x10448, %rsp          # imm = 0x10448
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2410
leaq 0x40000(%rbp), %rax
movq %rax, 0x10(%rsp)
xorl %r15d, %r15d
movq %r15, %rcx
shlq $0xa, %rcx
addq %rbp, %rcx
movq %rcx, 0x20(%rsp)
movq %r15, 0x18(%rsp)
shlq $0xc, %r15
addq 0x10(%rsp), %r15
xorl %r13d, %r13d
movb $0x0, 0xd(%rsp)
movl $0x1, %edx
movl %ebx, %edi
leaq 0xd(%rsp), %rsi
callq 0x1060
movq 0x20(%rsp), %rax
movl (%rax,%r13,4), %r14d
movw %r14w, 0xe(%rsp)
movl $0x2, %edx
movl %ebx, %edi
leaq 0xe(%rsp), %rsi
callq 0x1060
movq %r13, %r12
shlq $0x4, %r12
movq (%r12,%r15), %rsi
movzwl %r14w, %edx
movl %ebx, %edi
callq 0x1060
movl %r14d, %eax
shrl $0x3, %eax
andl $0x1fff, %eax           # imm = 0x1FFF
xorl %ecx, %ecx
testb $0x7, %r14b
setne %cl
addl %eax, %ecx
je 0x3432
addq %r15, %r12
movq 0x8(%r12), %rax
movl %ecx, %edx
xorl %ecx, %ecx
movb $0x0, 0x440(%rsp,%rcx)
movb $0x1, %sil
xorl %edi, %edi
xorl %r8d, %r8d
cmpb $0x0, (%rax,%r8)
je 0x3418
orb %sil, %dil
movb %dil, 0x440(%rsp,%rcx)
addb %sil, %sil
incq %r8
cmpq $0x8, %r8
jne 0x3406
incq %rcx
addq $0x8, %rax
cmpq %rdx, %rcx
jne 0x33f6
jmp 0x3434
xorl %edx, %edx
movl %ebx, %edi
leaq 0x440(%rsp), %rsi
callq 0x1060
leaq 0x1(%r13), %rax
movzbl 0x145008(%rbp), %ecx
cmpq %rcx, %r13
movq %rax, %r13
jb 0x3386
movq 0x18(%rsp), %rdx
leaq 0x1(%rdx), %r15
movzbl 0x145007(%rbp), %ecx
cmpq %rcx, %rdx
jb 0x3366
xorl %r13d, %r13d
movl %ebx, %edi
xorl %esi, %esi
xorl %edx, %edx
callq 0x1210
movl %ebx, %edi
xorl %esi, %esi
movl $0x1, %edx
callq 0x1210
movq %rax, %r15
movl %ebx, %edi
xorl %esi, %esi
movl $0x2, %edx
callq 0x1210
movq %rax, %r14
movl %ebx, %edi
movq %r15, %rsi
xorl %edx, %edx
callq 0x1210
leaq 0x10000(%r14), %rdi
callq 0x1030
movq %rax, %r15
movl %ebx, %edi
movq %rax, %rsi
movq %r14, %rdx
callq 0x11b0
movl %r14d, 0x4(%r15)
testq %r14, %r14
jle 0x350c
xorl %eax, %eax
movzbl (%r15,%rax), %ecx
xorl %ecx, %r13d
movl $0xfffffff8, %ecx       # imm = 0xFFFFFFF8
movl %r13d, %edx
movl %r13d, %esi
andl $0x1, %esi
negl %esi
sarl %edx
andl $0xedb88320, %esi       # imm = 0xEDB88320
movl %esi, %r13d
xorl %edx, %r13d
incl %ecx
jne 0x34e3
incl %eax
cmpq %rax, %r14
jg 0x34d6
notl %r13d
jmp 0x3512
movl $0xffffffff, %r13d      # imm = 0xFFFFFFFF
leaq 0x2c(%rsp), %r12
movl %r13d, (%r12)
movl %ebx, %edi
xorl %esi, %esi
xorl %edx, %edx
callq 0x1210
movl %ebx, %edi
movq %r15, %rsi
movq %r14, %rdx
callq 0x1060
movl $0x4, %edx
movl %ebx, %edi
movq %r12, %rsi
callq 0x1060
addq $0x10448, %rsp          # imm = 0x10448
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq