// File: /workspace/github_cpp_makefile_star2_O1/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 $0x18, %rsp
movq (%rsi), %r12
cmpq $0xc, %r12
jl 0x76b5
movq %rsi, %r14
movq %rdi, %r15
movzwl (%rdi), %eax
cmpl $0x6474, %eax           # imm = 0x6474
je 0x732d
cmpl $0x4454, %eax           # imm = 0x4454
jne 0x76b5
xorl %eax, %eax
leaq 0x3eaa(%rip), %rbx      # 0xb1e0
xorl %ecx, %ecx
movzwl %cx, %ecx
movb (%r15,%rax), %dl
xorb %cl, %dl
shrl $0x8, %ecx
movzbl %dl, %edx
xorw (%rbx,%rdx,2), %cx
incq %rax
cmpl $0xa, %eax
jne 0x7338
rolw $0x8, %cx
cmpw 0xa(%r15), %cx
jne 0x76b5
cmpb $0x15, 0x4(%r15)
ja 0x76b5
leaq 0x16e360(,%r12,2), %rdi
callq 0x1030
movq %rax, %r13
movq %rax, %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0x10e0
cmpw $0x6474, (%r13)         # imm = 0x6474
jne 0x73bd
cmpb $0x14, 0x4(%r13)
jae 0x73a2
xorl %ebx, %ebx
jmp 0x76a6
leaq 0xc(%r15), %rdi
addl $-0xc, %r12d
leaq 0xc(%r13), %rdx
movl %r12d, %esi
callq 0x76f4
movw $0x4454, (%r13)         # imm = 0x4454
movq %r13, 0x423c(%rip)      # 0xb600
movq %r15, 0x422d(%rip)      # 0xb5f8
movl 0x8(%r13), %eax
movl %eax, 0x8(%r15)
movq (%r13), %rax
movq %rax, (%r15)
leaq 0xc(%r15), %rax
movq %rax, 0x4213(%rip)      # 0xb5f8
leaq 0xc(%r13), %rcx
movq %rcx, 0x4210(%rip)      # 0xb600
cmpb $0x0, 0x7(%r13)
jns 0x7494
movzwl 0xe(%r13), %r12d
leaq 0x8(%r12), %rdx
xorl %esi, %esi
xorl %ebp, %ebp
movzwl %bp, %ebp
movb 0xe(%r13,%rsi), %dil
xorb %bpl, %dil
shrl $0x8, %ebp
movzbl %dil, %edi
xorw (%rbx,%rdi,2), %bp
incq %rsi
cmpl %esi, %edx
jne 0x7409
rolw $0x8, %bp
movzwl (%rcx), %ebx
cmpw %bx, %bp
jne 0x747e
movzwl 0x8(%rcx), %edx
movw %dx, 0x8(%rax)
movq (%rcx), %rcx
movq %rcx, (%rax)
leaq 0x16(%r15), %rdi
movq %rdi, 0x41ad(%rip)      # 0xb5f8
leaq 0x16(%r13), %rsi
movq %rsi, 0x41aa(%rip)      # 0xb600
movq %r12, %rdx
callq 0x10e0
leaq (%r15,%r12), %rax
addq $0x16, %rax
movq %rax, 0x418b(%rip)      # 0xb5f8
leaq (%r12,%r13), %rax
addq $0x16, %rax
movq %rax, 0x4184(%rip)      # 0xb600
jmp 0x748b
movl $0x1, %esi
movq %r13, %rdi
callq 0x1150
cmpw %bx, %bp
jne 0x76b5
movq %r13, (%rsp)
movq %r15, 0x8(%rsp)
movq %r14, 0x10(%rsp)
movq 0x4157(%rip), %rax      # 0xb600
movzbl (%rax), %r15d
movq 0x4144(%rip), %rcx      # 0xb5f8
movl (%rax), %eax
movl %eax, (%rcx)
addq $0x4, 0x4138(%rip)      # 0xb5f8
addq $0x4, 0x4138(%rip)      # 0xb600
testl %r15d, %r15d
je 0x766a
movl $0x2, %eax
cmpl $0xff, %r15d
je 0x7681
movq 0x4116(%rip), %rax      # 0xb600
movb 0x3(%rax), %cl
movl $0x80, %r12d
shll %cl, %r12d
movq 0x40fb(%rip), %rcx      # 0xb5f8
movzwl 0x4(%rax), %edx
movw %dx, 0x4(%rcx)
movl (%rax), %eax
movl %eax, (%rcx)
movq 0x40e8(%rip), %rax      # 0xb5f8
leaq 0x6(%rax), %rcx
movq %rcx, 0x40dd(%rip)      # 0xb5f8
movq 0x40de(%rip), %r14      # 0xb600
leaq 0x6(%r14), %rcx
movq %rcx, 0x40d3(%rip)      # 0xb600
leal 0x1(%r12), %ecx
movw %cx, 0x6(%rax)
leaq 0x9(%rax), %r13
movq %r13, 0x40b7(%rip)      # 0xb5f8
movb $0x0, 0x8(%rax)
movzwl 0x6(%r14), %ebp
leaq (%r14,%rbp), %rbx
addq $0x8, %rbx
movq %r13, %rdi
xorl %esi, %esi
movq %r12, %rdx
callq 0x10a0
leaq 0x9(%r14), %rsi
movq %rsi, 0x4096(%rip)      # 0xb600
movzbl 0x8(%r14), %eax
testl %eax, %eax
je 0x7646
cmpl $0x1, %eax
je 0x761b
cmpl $0x2, %eax
jne 0x766e
movq 0x4070(%rip), %rdx      # 0xb600
leaq 0x1(%rdx), %rax
movq %rax, 0x4065(%rip)      # 0xb600
movzbl (%rdx), %eax
cmpl $0x1, %eax
je 0x75de
testl %eax, %eax
jne 0x766e
leaq 0x2(%rdx), %rax
movq %rax, 0x404a(%rip)      # 0xb600
movb 0x1(%rdx), %al
testb %al, %al
je 0x760c
movq 0x403c(%rip), %rcx      # 0xb600
leaq 0x1(%rcx), %rdx
movq %rdx, 0x4031(%rip)      # 0xb600
movb (%rcx), %cl
movb %cl, (%r13)
incq %r13
decb %al
jne 0x75bd
jmp 0x760c
leaq 0x2(%rdx), %rax
movq %rax, 0x4017(%rip)      # 0xb600
movb 0x1(%rdx), %al
movzwl 0x2(%rdx), %ecx
addq $0x4, %rdx
movq %rdx, 0x4005(%rip)      # 0xb600
testb %al, %al
je 0x760c
movw %cx, (%r13)
addq $0x2, %r13
decb %al
jne 0x75ff
cmpq %rbx, 0x3fed(%rip)      # 0xb600
jb 0x7589
jmp 0x7653
movzwl 0x9(%r14), %eax
leaq 0xb(%r14), %rcx
movq %rcx, 0x3fd5(%rip)      # 0xb600
testq %rax, %rax
je 0x7653
movzwl 0xb(%r14), %ecx
xorl %edx, %edx
movw %cx, (%r13,%rdx,2)
incq %rdx
cmpl %edx, %eax
jne 0x7637
jmp 0x7653
decl %ebp
movq %r13, %rdi
movq %rbp, %rdx
callq 0x10e0
addq %r12, 0x3f9e(%rip)      # 0xb5f8
movq %rbx, 0x3f9f(%rip)      # 0xb600
decb %r15b
jne 0x74e3
xorl %eax, %eax
jmp 0x7681
movl $0x1, %esi
movq (%rsp), %rdi
callq 0x1150
movl $0x1, %eax
testl %eax, %eax
je 0x74a2
cmpl $0x2, %eax
jne 0x76b5
movl 0x3f64(%rip), %eax      # 0xb5f8
subl 0x8(%rsp), %eax
movq 0x10(%rsp), %rcx
movq %rax, (%rcx)
movb $0x1, %bl
movq (%rsp), %r13
movl $0x1, %esi
movq %r13, %rdi
callq 0x1150
jmp 0x76b7
xorl %ebx, %ebx
movl %ebx, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq