// File: /workspace/github_cpp_makefile_star2_O2/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, %r14
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)
andl $0x0, 0xc(%r15)
movl %esi, %edi
xorl %esi, %esi
xorl %edx, %edx
callq 0x11f0
pushq $0x10
popq %r12
movl %ebx, %edi
movq %r15, %rsi
movq %r12, %rdx
callq 0x1050
movl %ebx, %edi
xorl %esi, %esi
xorl %edx, %edx
callq 0x11f0
leaq 0x40(%rsp), %rsi
movl %ebx, %edi
movq %r12, %rdx
callq 0x11a0
testq %rax, %rax
js 0x2b6d
movdqu 0x30(%rsp), %xmm0
pcmpeqb 0x40(%rsp), %xmm0
pmovmskb %xmm0, %eax
cmpl $0xffff, %eax           # imm = 0xFFFF
je 0x2b8a
leaq 0x4878(%rip), %rsi      # 0x73ec
addq $0x10448, %rsp          # imm = 0x10448
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x201c
leaq 0x40000(%r14), %rax
movq %rax, 0x10(%rsp)
xorl %r13d, %r13d
movzbl 0x145007(%r14), %eax
cmpq %rax, %r13
ja 0x2cb1
movq %r13, %rax
shlq $0xa, %rax
addq %r14, %rax
movq %rax, 0x20(%rsp)
movq %r13, 0x18(%rsp)
shlq $0xc, %r13
addq 0x10(%rsp), %r13
xorl %ebp, %ebp
movzbl 0x145008(%r14), %eax
cmpq %rax, %rbp
ja 0x2ca4
movq %r14, %r12
movb $0x0, 0xd(%rsp)
movl %ebx, %edi
leaq 0xd(%rsp), %rsi
pushq $0x1
popq %rdx
callq 0x1050
movq 0x20(%rsp), %rax
movl (%rax,%rbp,4), %r14d
movw %r14w, 0xe(%rsp)
movl %ebx, %edi
leaq 0xe(%rsp), %rsi
pushq $0x2
popq %rdx
callq 0x1050
movq %rbp, %rax
shlq $0x4, %rax
leaq (%rax,%r13), %r15
movq (%rax,%r13), %rsi
movzwl %r14w, %edx
movl %ebx, %edi
callq 0x1050
movl %r14d, %edx
shrl $0x3, %edx
andl $0x1fff, %edx           # imm = 0x1FFF
andl $0x7, %r14d
cmpl $0x1, %r14d
sbbl $-0x1, %edx
xorl %eax, %eax
xorl %ecx, %ecx
cmpq %rdx, %rcx
je 0x2c8a
movb $0x0, 0x440(%rsp,%rcx)
movq 0x8(%r15), %rsi
addq %rax, %rsi
movb $0x1, %dil
xorl %r8d, %r8d
xorl %r9d, %r9d
cmpq $0x8, %r9
je 0x2c81
cmpb $0x0, (%rsi,%r9)
je 0x2c79
orb %dil, %r8b
movb %r8b, 0x440(%rsp,%rcx)
addb %dil, %dil
incq %r9
jmp 0x2c61
incq %rcx
addq $0x8, %rax
jmp 0x2c44
movl %ebx, %edi
leaq 0x440(%rsp), %rsi
callq 0x1050
incq %rbp
movq %r12, %r14
jmp 0x2bc9
movq 0x18(%rsp), %r13
incq %r13
jmp 0x2b99
xorl %r12d, %r12d
movl %ebx, %edi
xorl %esi, %esi
xorl %edx, %edx
callq 0x11f0
movl %ebx, %edi
callq 0x6db6
movq %rax, %r14
leaq 0x10000(%rax), %rdi
callq 0x1030
movq %rax, %r15
movl %ebx, %edi
movq %rax, %rsi
movq %r14, %rdx
callq 0x11a0
movl %r14d, 0x4(%r15)
pushq $-0x1
popq %rax
movl %r12d, %r12d
cmpq %r12, %r14
jle 0x2d05
movzbl (%r15,%r12), %esi
movl %eax, %edi
callq 0x1870
incl %r12d
jmp 0x2cec
leaq 0x2c(%rsp), %r12
movl %eax, (%r12)
movl %ebx, %edi
xorl %esi, %esi
xorl %edx, %edx
callq 0x11f0
movl %ebx, %edi
movq %r15, %rsi
movq %r14, %rdx
callq 0x1050
pushq $0x4
popq %rdx
movl %ebx, %edi
movq %r12, %rsi
callq 0x1050
addq $0x10448, %rsp          # imm = 0x10448
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop