// File: /workspace/github_cpp_makefile_star2_O1/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 $0x10438, %rsp          # imm = 0x10438
movl %esi, %ebx
movq %rdi, %rbp
movl $0xffffffff, 0x4(%rsp)  # imm = 0xFFFFFFFF
leaq 0x20(%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 0x30(%rsp), %rsi
movl $0x10, %edx
movl %ebx, %edi
callq 0x11b0
testq %rax, %rax
js 0x32fe
movdqu 0x20(%rsp), %xmm0
pcmpeqb 0x30(%rsp), %xmm0
pmovmskb %xmm0, %eax
cmpl $0xffff, %eax           # imm = 0xFFFF
je 0x331b
leaq 0x50e7(%rip), %rsi      # 0x83ec
addq $0x10438, %rsp          # imm = 0x10438
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x23fa
leaq 0x40000(%rbp), %rax
movq %rax, 0x8(%rsp)
xorl %r15d, %r15d
movq %r15, %rcx
shlq $0xa, %rcx
addq %rbp, %rcx
movq %rcx, 0x18(%rsp)
movq %r15, 0x10(%rsp)
shlq $0xc, %r15
addq 0x8(%rsp), %r15
xorl %r13d, %r13d
movb $0x0, 0x1(%rsp)
movl $0x1, %edx
movl %ebx, %edi
leaq 0x1(%rsp), %rsi
callq 0x1060
movq 0x18(%rsp), %rax
movl (%rax,%r13,4), %r14d
movw %r14w, 0x2(%rsp)
movl $0x2, %edx
movl %ebx, %edi
leaq 0x2(%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
movl %ecx, %edx
je 0x33ed
addq %r15, %r12
movq 0x8(%r12), %rax
xorl %ecx, %ecx
movb $0x0, 0x430(%rsp,%rcx)
movb $0x1, %sil
xorl %edi, %edi
cmpb $0x0, (%rax,%rdi)
je 0x33d5
orb %sil, 0x430(%rsp,%rcx)
addb %sil, %sil
incq %rdi
cmpq $0x8, %rdi
jne 0x33c7
incq %rcx
addq $0x8, %rax
cmpq %rdx, %rcx
jne 0x33ba
movl %ebx, %edi
leaq 0x430(%rsp), %rsi
callq 0x1060
leaq 0x1(%r13), %rax
movzbl 0x145008(%rbp), %ecx
cmpq %rcx, %r13
movq %rax, %r13
jb 0x334a
movq 0x10(%rsp), %rdx
leaq 0x1(%rdx), %r15
movzbl 0x145007(%rbp), %ecx
cmpq %rcx, %rdx
jb 0x332a
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)
movl $0xffffffff, 0x4(%rsp)  # imm = 0xFFFFFFFF
testq %r14, %r14
jle 0x34cd
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 0x34a4
incl %eax
cmpq %rax, %r14
jg 0x3497
notl %r13d
jmp 0x34d3
movl $0xffffffff, %r13d      # imm = 0xFFFFFFFF
leaq 0x4(%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 $0x10438, %rsp          # imm = 0x10438
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop