// File: /workspace/github_cpp_makefile_star2_O3/atsidaev[P]trx2x/DiskImage.cpp
// Function: unpack_td0(unsigned char*, long&)
bool unpack_td0(unsigned char *data, long &size)
{
   if(size < 12) return false;
   if( (*(short*)data != WORD2('T','D'))&&(*(short*)data != WORD2('t','d')) )
      return false;             // non TD0
   if( TD0CRC(data,10) != *((uint16_t*)(data+0x0A)) ) 
      return false;             // CRC bad...
   if( data[4] > 21 ) 
      return false;             // version > 2.1...

   unsigned char *snbuf = (unsigned char*)new char[size*2+1500000];  // if compressed then UUUUFF ;-/
   if(!snbuf) return false;

   memcpy(snbuf, data, size);
   if( *(short*)snbuf == WORD2('t','d') ) // packed disk
   {
      if( snbuf[4] < 20 )    // unsupported Old Advanced compression
      {
         delete snbuf;
         return false;
      }
      unpack_lzh((unsigned char*)data+12, size-12, (unsigned char*)snbuf+12), *(short*)snbuf = WORD2('T','D');
   }

   td0_src = snbuf, td0_dst = data;
   td0_move(12);

   if(snbuf[7] & 0x80)  // additional info...
   {
      unsigned short *cs = (unsigned short*)(snbuf+12+2);

      if( TD0CRC(snbuf+12+2, 8+*cs) != cs[-1] )
      {
         delete snbuf;
         return false;
      }
      td0_move(10);
      td0_move( *((unsigned short*)(snbuf+12+2)) );
   }

   for(;;) 
   {
      unsigned char s = *td0_src;
      td0_move(4); 
      if(s == 0xFF) break;
      for(; s; s--) 
      {
//         unsigned char *sec = td0_src;
         unsigned size = 128; if(td0_src[3]) size <<= td0_src[3];
         td0_move(6); 
         *(unsigned short*)td0_dst = size+1; td0_dst += 2;
         *td0_dst++ = 0; 
         unsigned char *dst = td0_dst;
         unsigned src_size = *(unsigned short*)td0_src; td0_src += 2;
         unsigned char *end_packed_data = td0_src + src_size;
         memset(td0_dst, 0, size);
         switch (*td0_src++) 
         {
            case 0:
               memcpy(dst, td0_src, src_size-1); break;
            case 1:
            {
               unsigned n = *(unsigned short*)td0_src; 
               td0_src += 2;
               unsigned short data = *(unsigned short*)td0_src;
               for(; n; n--) *(unsigned short*)dst = data, dst += 2;
               break;
            }
            case 2:
            {
               unsigned short data; 
               unsigned char s;
               do 
               {
                  switch( *td0_src++ ) 
                  {
                     case 0:
                        for(s = *td0_src++; s; s--) *dst++ = *td0_src++;
                        break;
                     case 1:
                        s = *td0_src++;
                        data = *(unsigned short*)td0_src; 
                        td0_src += 2;
                        for( ; s; s--) *(unsigned short*)dst = data, dst += 2;
                        break;
                     default: shit:
                        delete snbuf;
                        return false;  // "bad TD0 file"
                  }
               } while( td0_src < end_packed_data );
               break;
            }
            default: goto shit;
         }
         td0_dst += size; 
         td0_src = end_packed_data;
      }
   }
   size = (unsigned int)(td0_dst - data);
   delete snbuf;
   return true;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq (%rsi), %r12
cmpq $0xc, %r12
jl 0x731c
movq %rdi, %r14
movzwl (%rdi), %eax
cmpl $0x6474, %eax           # imm = 0x6474
je 0x72e4
cmpl $0x4454, %eax           # imm = 0x4454
jne 0x731c
xorl %eax, %eax
leaq 0x3ef3(%rip), %rbx      # 0xb1e0
xorl %ecx, %ecx
movzwl %cx, %ecx
movb (%r14,%rax), %dl
xorb %cl, %dl
shrl $0x8, %ecx
movzbl %dl, %edx
xorw (%rbx,%rdx,2), %cx
incq %rax
cmpl $0xa, %eax
jne 0x72ef
rolw $0x8, %cx
cmpw 0xa(%r14), %cx
jne 0x731c
cmpb $0x15, 0x4(%r14)
jbe 0x732f
xorl %ebx, %ebx
movl %ebx, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rsi, 0x18(%rsp)
leaq 0x16e360(,%r12,2), %rdi
callq 0x1030
movq %rax, %r15
movq %rax, %rdi
movq %r14, %rsi
movq %r12, %rdx
callq 0x10e0
cmpw $0x6474, (%r15)         # imm = 0x6474
jne 0x737f
cmpb $0x14, 0x4(%r15)
jb 0x7883
leaq 0xc(%r14), %rdi
addl $-0xc, %r12d
leaq 0xc(%r15), %rdx
movl %r12d, %esi
callq 0x78c3
movw $0x4454, (%r15)         # imm = 0x4454
movl 0x8(%r15), %eax
movl %eax, 0x8(%r14)
movq (%r15), %rax
movq %rax, (%r14)
leaq 0xc(%r14), %rax
movq %rax, 0x4260(%rip)      # 0xb5f8
leaq 0xc(%r15), %rcx
movq %rcx, 0x425d(%rip)      # 0xb600
cmpb $0x0, 0x7(%r15)
jns 0x741f
movzwl 0xe(%r15), %r12d
leaq 0x8(%r12), %rdx
xorl %esi, %esi
xorl %edi, %edi
movzwl %di, %edi
movb 0xe(%r15,%rsi), %r8b
xorb %dil, %r8b
shrl $0x8, %edi
movzbl %r8b, %r8d
xorw (%rbx,%r8,2), %di
incq %rsi
cmpl %esi, %edx
jne 0x73b8
rolw $0x8, %di
cmpw (%rcx), %di
jne 0x7883
movzwl 0x8(%rcx), %edx
movw %dx, 0x8(%rax)
movq (%rcx), %rcx
movq %rcx, (%rax)
leaq 0x16(%r14), %rdi
leaq 0x16(%r15), %rsi
movq %r12, %rdx
callq 0x10e0
leaq (%r14,%r12), %rax
addq $0x16, %rax
movq %rax, 0x41e8(%rip)      # 0xb5f8
leaq (%r15,%r12), %rcx
addq $0x16, %rcx
movq %rcx, 0x41e1(%rip)      # 0xb600
movb (%rcx), %dl
movl (%rcx), %ecx
movl %ecx, (%rax)
movq 0x41cc(%rip), %r12      # 0xb5f8
addq $0x4, %r12
movq %r12, 0x41c1(%rip)      # 0xb5f8
movq 0x41c2(%rip), %rbx      # 0xb600
addq $0x4, %rbx
movq %rbx, 0x41b7(%rip)      # 0xb600
cmpb $-0x1, %dl
movq 0x18(%rsp), %rax
je 0x7879
movq %r15, 0x10(%rsp)
testb %dl, %dl
je 0x783f
movb %dl, 0xf(%rsp)
movb 0x3(%rbx), %cl
movl $0x80, %edx
shll %cl, %edx
movzwl 0x4(%rbx), %eax
movw %ax, 0x4(%r12)
movl (%rbx), %eax
movl %eax, (%r12)
movq 0x416f(%rip), %r15      # 0xb5f8
movq 0x4170(%rip), %r13      # 0xb600
leal 0x1(%rdx), %eax
movw %ax, 0x6(%r15)
leaq 0x9(%r15), %r12
movq %r12, 0x4155(%rip)      # 0xb5f8
movb $0x0, 0x8(%r15)
movzwl 0x6(%r13), %ebp
leaq 0x8(,%rbp), %rbx
addq %r13, %rbx
movq %r12, %rdi
xorl %esi, %esi
movq %rdx, 0x20(%rsp)
callq 0x10a0
leaq 0x9(%r13), %rsi
movq %rsi, 0x412e(%rip)      # 0xb600
movzbl 0x8(%r13), %eax
testl %eax, %eax
je 0x780e
cmpl $0x1, %eax
movdqa 0xe55(%rip), %xmm11     # 0x8340
je 0x759c
cmpl $0x2, %eax
movq 0x10(%rsp), %r15
jne 0x7883
leaq 0x1(%rsi), %rax
movq %rax, 0x40f6(%rip)      # 0xb600
movzbl (%rsi), %eax
cmpl $0x1, %eax
je 0x7554
testl %eax, %eax
jne 0x7883
leaq 0x2(%rsi), %rcx
movq %rcx, 0x40db(%rip)      # 0xb600
movb 0x1(%rsi), %al
testb %al, %al
je 0x7584
movq 0x40cd(%rip), %rcx      # 0xb600
leaq 0x1(%rcx), %rdx
movq %rdx, 0x40c2(%rip)      # 0xb600
movb (%rcx), %cl
movb %cl, (%r12)
incq %r12
decb %al
jne 0x752c
movq 0x40ae(%rip), %rsi      # 0xb600
jmp 0x7587
leaq 0x2(%rsi), %rax
movq %rax, 0x40a1(%rip)      # 0xb600
movb 0x1(%rsi), %al
movzwl 0x2(%rsi), %ecx
addq $0x4, %rsi
movq %rsi, 0x408f(%rip)      # 0xb600
testb %al, %al
je 0x7587
movw %cx, (%r12)
addq $0x2, %r12
decb %al
jne 0x7575
jmp 0x7587
movq %rcx, %rsi
cmpq %rbx, %rsi
jb 0x74ff
movq 0x4061(%rip), %r12      # 0xb5f8
jmp 0x7820
movzwl 0x9(%r13), %edx
testq %rdx, %rdx
movdqa 0xd54(%rip), %xmm7      # 0x8300
movdqa 0xd5b(%rip), %xmm8      # 0x8310
movdqa 0xd62(%rip), %xmm9      # 0x8320
movdqa 0xd69(%rip), %xmm10     # 0x8330
pcmpeqd %xmm12, %xmm12
je 0x781b
movzwl 0xb(%r13), %eax
leal 0x7(%rdx), %ecx
andl $-0x8, %ecx
decq %rdx
movq %rdx, %xmm0
pshufd $0x44, %xmm0, %xmm0     # xmm0 = xmm0[0,1,0,1]
pxor %xmm11, %xmm0
xorl %edx, %edx
movq %rdx, %xmm1
pshufd $0x44, %xmm1, %xmm1     # xmm1 = xmm1[0,1,0,1]
movdqa %xmm1, %xmm2
por %xmm10, %xmm2
pxor %xmm11, %xmm2
movdqa %xmm2, %xmm4
pcmpgtd %xmm0, %xmm4
pshufd $0xa0, %xmm4, %xmm3     # xmm3 = xmm4[0,0,2,2]
pshuflw $0xe8, %xmm3, %xmm5     # xmm5 = xmm3[0,2,2,3,4,5,6,7]
pcmpeqd %xmm0, %xmm2
pshufd $0xf5, %xmm2, %xmm2     # xmm2 = xmm2[1,1,3,3]
pshuflw $0xe8, %xmm2, %xmm6     # xmm6 = xmm2[0,2,2,3,4,5,6,7]
pand %xmm5, %xmm6
pshufd $0xf5, %xmm4, %xmm4     # xmm4 = xmm4[1,1,3,3]
pshuflw $0xe8, %xmm4, %xmm5     # xmm5 = xmm4[0,2,2,3,4,5,6,7]
por %xmm6, %xmm5
pxor %xmm12, %xmm5
packssdw %xmm5, %xmm5
movd %xmm5, %esi
testb $0x1, %sil
je 0x7654
movw %ax, 0x9(%r15,%rdx,2)
pand %xmm3, %xmm2
por %xmm4, %xmm2
packssdw %xmm2, %xmm2
pxor %xmm12, %xmm2
packssdw %xmm2, %xmm2
movd %xmm2, %esi
shrl $0x10, %esi
testb $0x1, %sil
je 0x767c
movw %ax, 0xb(%r15,%rdx,2)
movdqa %xmm1, %xmm3
por %xmm9, %xmm3
pxor %xmm11, %xmm3
movdqa %xmm3, %xmm4
pcmpgtd %xmm0, %xmm4
pshufd $0xa0, %xmm4, %xmm2     # xmm2 = xmm4[0,0,2,2]
pcmpeqd %xmm0, %xmm3
pshufd $0xf5, %xmm3, %xmm3     # xmm3 = xmm3[1,1,3,3]
movdqa %xmm3, %xmm5
pand %xmm2, %xmm5
pshufd $0xf5, %xmm4, %xmm4     # xmm4 = xmm4[1,1,3,3]
por %xmm4, %xmm5
packssdw %xmm5, %xmm5
pxor %xmm12, %xmm5
packssdw %xmm5, %xmm5
pextrw $0x2, %xmm5, %esi
testb $0x1, %sil
je 0x76cf
movw %ax, 0xd(%r15,%rdx,2)
pshufhw $0x84, %xmm2, %xmm2     # xmm2 = xmm2[0,1,2,3,4,5,4,6]
pshufhw $0x84, %xmm3, %xmm3     # xmm3 = xmm3[0,1,2,3,4,5,4,6]
pand %xmm2, %xmm3
pshufhw $0x84, %xmm4, %xmm2     # xmm2 = xmm4[0,1,2,3,4,5,4,6]
por %xmm3, %xmm2
pxor %xmm12, %xmm2
packssdw %xmm2, %xmm2
pextrw $0x3, %xmm2, %esi
testb $0x1, %sil
je 0x7700
movw %ax, 0xf(%r15,%rdx,2)
movdqa %xmm1, %xmm2
por %xmm8, %xmm2
pxor %xmm11, %xmm2
movdqa %xmm2, %xmm4
pcmpgtd %xmm0, %xmm4
pshufd $0xa0, %xmm4, %xmm3     # xmm3 = xmm4[0,0,2,2]
pshuflw $0xe8, %xmm3, %xmm5     # xmm5 = xmm3[0,2,2,3,4,5,6,7]
pcmpeqd %xmm0, %xmm2
pshufd $0xf5, %xmm2, %xmm2     # xmm2 = xmm2[1,1,3,3]
pshuflw $0xe8, %xmm2, %xmm6     # xmm6 = xmm2[0,2,2,3,4,5,6,7]
pand %xmm5, %xmm6
pshufd $0xf5, %xmm4, %xmm4     # xmm4 = xmm4[1,1,3,3]
pshuflw $0xe8, %xmm4, %xmm5     # xmm5 = xmm4[0,2,2,3,4,5,6,7]
por %xmm6, %xmm5
pxor %xmm12, %xmm5
packssdw %xmm5, %xmm5
pextrw $0x4, %xmm5, %esi
testb $0x1, %sil
je 0x775a
movw %ax, 0x11(%r15,%rdx,2)
pand %xmm3, %xmm2
por %xmm4, %xmm2
packssdw %xmm2, %xmm2
pxor %xmm12, %xmm2
packssdw %xmm2, %xmm2
pextrw $0x5, %xmm2, %esi
testb $0x1, %sil
je 0x7780
movw %ax, 0x13(%r15,%rdx,2)
por %xmm7, %xmm1
pxor %xmm11, %xmm1
movdqa %xmm1, %xmm3
pcmpgtd %xmm0, %xmm3
pshufd $0xa0, %xmm3, %xmm2     # xmm2 = xmm3[0,0,2,2]
pcmpeqd %xmm0, %xmm1
pshufd $0xf5, %xmm1, %xmm1     # xmm1 = xmm1[1,1,3,3]
movdqa %xmm1, %xmm4
pand %xmm2, %xmm4
pshufd $0xf5, %xmm3, %xmm3     # xmm3 = xmm3[1,1,3,3]
por %xmm3, %xmm4
packssdw %xmm4, %xmm4
pxor %xmm12, %xmm4
packssdw %xmm4, %xmm4
pextrw $0x6, %xmm4, %esi
testb $0x1, %sil
je 0x77ce
movw %ax, 0x15(%r15,%rdx,2)
pshufhw $0x84, %xmm2, %xmm2     # xmm2 = xmm2[0,1,2,3,4,5,4,6]
pshufhw $0x84, %xmm1, %xmm1     # xmm1 = xmm1[0,1,2,3,4,5,4,6]
pand %xmm2, %xmm1
pshufhw $0x84, %xmm3, %xmm2     # xmm2 = xmm3[0,1,2,3,4,5,4,6]
por %xmm1, %xmm2
pxor %xmm12, %xmm2
packssdw %xmm2, %xmm1
pextrw $0x7, %xmm1, %esi
testb $0x1, %sil
je 0x77ff
movw %ax, 0x17(%r15,%rdx,2)
addq $0x8, %rdx
cmpq %rdx, %rcx
jne 0x75f1
jmp 0x781b
decl %ebp
movq %r12, %rdi
movq %rbp, %rdx
callq 0x10e0
movq 0x10(%rsp), %r15
addq 0x20(%rsp), %r12
movq %r12, 0x3dcc(%rip)      # 0xb5f8
movq %rbx, 0x3dcd(%rip)      # 0xb600
movb 0xf(%rsp), %dl
decb %dl
jne 0x7464
movb (%rbx), %dl
movl (%rbx), %eax
movl %eax, (%r12)
movq 0x3daa(%rip), %r12      # 0xb5f8
addq $0x4, %r12
movq %r12, 0x3d9f(%rip)      # 0xb5f8
movq 0x3da0(%rip), %rbx      # 0xb600
addq $0x4, %rbx
movq %rbx, 0x3d95(%rip)      # 0xb600
cmpb $-0x1, %dl
movq 0x18(%rsp), %rax
jne 0x745c
subl %r14d, %r12d
movq %r12, (%rax)
movb $0x1, %bl
jmp 0x7885
xorl %ebx, %ebx
movl $0x1, %esi
movq %r15, %rdi
callq 0x1150
jmp 0x731e