// File: /workspace/github_cpp_makefile_star2_O2/atsidaev[P]trx2x/trx2x.cpp
// Function: main
int main(int argc, char *argv[])
{
   cout << "TRX2X converter V1.2 beta 2    (C)2002 Alex Makeev" << endl;
   cout << endl;
   if(argc!=3)
   {
      cout << "Input:  UDI, TRD, SCL, FDI, TD0, FDD, hobeta (.$*|.!*) " << endl;
      cout << "Output: UDI, TRD, SCL, FDI, TD0, FDD " << endl;
      cout << endl << "USAGE: TRX2X.EXE <filename.trd> <filename.udi> " << endl;
      cout << endl << "Based on UltraDiskImage engine by Alex Makeev" << endl;
      cout << "WWW: http://zxmak.chat.ru/" << endl;
      return 1;
   }

   char infilename[4096];
   char outfilename[4096];

   if( !strcmp(argv[1], argv[2]) )
   {
      cout << "Error: file self overwrite is prohibited!" << endl;
      return 2;
   }
   cout << argv[1] << " ---> " << argv[2] << "..." << endl << endl;

   if(!file_exists(argv[1])) // file not exists!!!
   {
      cout << "Error: source file not found!" << endl;
      return 3;
   }

   if(file_exists(argv[2])) // file exists!!!
   {
      cout << "Warning: destination file already exists!" << endl;
      cout << "Are you sure to overwrite? (y/n) ";
      cout.flush();
      int cr = getchar();
      cout << endl;
      if( (cr!='y')&&(cr!='Y') ) 
      {
         cout << "Operation aborted!" << endl;
         return 4;
      }
   }

// detect output type:
   TDiskImageType typ = DIT_UNK;

   char ext[MAX_EXT];
   const char* ext_pos = get_extension(argv[2]);
   if (ext_pos) {
      strncpy(ext, ext_pos, MAX_EXT);
      make_uppercase(ext);
      if(!strcmp(ext, ".TRD")) typ = DIT_TRD;
      if(!strcmp(ext, ".SCL")) typ = DIT_SCL;
      if(!strcmp(ext, ".FDI")) typ = DIT_FDI;
      if(!strcmp(ext, ".UDI")) typ = DIT_UDI;
      if(!strcmp(ext, ".TD0")) typ = DIT_TD0;
      if(!strcmp(ext, ".FDD")) typ = DIT_FDD;
   }

   if(typ == DIT_UNK)
   {
      cout << "Error: unsupported output format '" << ext << "'!" << endl;
      return 5;
   }

   if(typ != DIT_UDI) cout << "warning: destination file is not UDI! Some information may be loss!" << endl << endl;

   TDiskImage *img1 = new TDiskImage;
   img1->AddBOOT = false;
   img1->Open(argv[1], true);
   if(!img1->DiskPresent)
   {
      cout << "Error: load source file failed!" << endl;
      return 6;
   }
   cout << "source file successfully loaded..." << endl;

   int hf = open(argv[2], O_CREAT | O_RDWR | O_TRUNC | O_BINARY, 0666);
   if(hf < 0)
   {
      delete img1;
      perror("Create file");
      return 7;
   }

   if(typ==DIT_TRD) img1->writeTRD(hf);
   if(typ==DIT_SCL) img1->writeSCL(hf);
   if(typ==DIT_FDI) img1->writeFDI(hf);
   if(typ==DIT_UDI) img1->writeUDI(hf);
   if(typ==DIT_TD0) img1->writeTD0(hf);
   if(typ==DIT_FDD) img1->writeFDD(hf);

   close(hf);

   cout << "destination file successfully saved..." << endl;

   delete img1;
   return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %rsi, %rbx
movl %edi, %ebp
movq 0x8c62(%rip), %r14      # 0x9fd8
leaq 0x5ca3(%rip), %rsi      # 0x7020
movq %r14, %rdi
callq 0x1120
movq %rax, %rdi
callq 0x1070
movq %r14, %rdi
callq 0x1070
cmpl $0x3, %ebp
jne 0x15a5
movq 0x8(%rbx), %r14
movq 0x10(%rbx), %rsi
movq %r14, %rdi
callq 0x1190
movq 0x8c23(%rip), %rdi      # 0x9fd8
testl %eax, %eax
je 0x1633
movq %r14, %rsi
callq 0x1120
leaq 0x5d88(%rip), %rsi      # 0x7154
movq %rax, %rdi
callq 0x1120
movq 0x10(%rbx), %rsi
movq %rax, %rdi
callq 0x1120
leaq 0x5eb8(%rip), %rsi      # 0x729f
movq %rax, %rdi
callq 0x1120
movq %rax, %rdi
callq 0x1070
movq %rax, %rdi
callq 0x1070
movq 0x8(%rbx), %rdi
callq 0x182f
testb %al, %al
je 0x164b
movq 0x10(%rbx), %rdi
callq 0x182f
testb %al, %al
je 0x146d
movq 0x8bb4(%rip), %r14      # 0x9fd8
leaq 0x5d4e(%rip), %rsi      # 0x7179
movq %r14, %rdi
callq 0x1120
movq %rax, %rdi
callq 0x1070
leaq 0x5d61(%rip), %rsi      # 0x71a3
movq %r14, %rdi
callq 0x1120
movq %r14, %rdi
callq 0x10b0
callq 0x1180
movl %eax, %ebp
movq %r14, %rdi
callq 0x1070
andl $-0x21, %ebp
cmpl $0x59, %ebp
jne 0x167a
movq 0x10(%rbx), %rdi
callq 0x184d
pushq $0x5
popq %rbp
testq %rax, %rax
je 0x1569
leaq 0x10(%rsp), %r14
pushq $0x40
popq %rdx
movq %r14, %rdi
movq %rax, %rsi
callq 0x1080
movq %r14, %rdi
callq 0x1855
leaq 0x5d4d(%rip), %rsi      # 0x71f1
pushq $0x5
popq %rdx
movq %r14, %rdi
callq 0x1150
testl %eax, %eax
je 0x1699
leaq 0x5d2e(%rip), %rsi      # 0x71ec
leaq 0x10(%rsp), %r13
pushq $0x5
popq %r12
movq %r13, %rdi
movq %r12, %rdx
callq 0x1150
movl %eax, 0xc(%rsp)
leaq 0x5d0a(%rip), %rsi      # 0x71e7
movq %r13, %rdi
movq %r12, %rdx
callq 0x1150
movl %eax, 0x8(%rsp)
leaq 0x5cef(%rip), %rsi      # 0x71e2
movq %r13, %rdi
movq %r12, %rdx
callq 0x1150
movl %eax, %r14d
leaq 0x5cd5(%rip), %rsi      # 0x71dd
movq %r13, %rdi
movq %r12, %rdx
callq 0x1150
movl %eax, %r15d
leaq 0x5cbb(%rip), %rsi      # 0x71d8
movq %r13, %rdi
movq %r12, %rdx
callq 0x1150
xorl %ecx, %ecx
testl %eax, %eax
sete %cl
testl %r15d, %r15d
pushq $0x2
popq %rax
cmovnel %ecx, %eax
testl %r14d, %r14d
pushq $0x3
popq %rcx
cmovnel %eax, %ecx
cmpl $0x0, 0x8(%rsp)
cmovel %ebp, %ecx
cmpl $0x0, 0xc(%rsp)
pushq $0x4
popq %r15
cmovnel %ecx, %r15d
cmpl $0x5, %r15d
je 0x16c0
testl %r15d, %r15d
jne 0x169d
movq 0x8a68(%rip), %rdi      # 0x9fd8
leaq 0x5c7f(%rip), %rsi      # 0x71f6
callq 0x1120
leaq 0x10(%rsp), %rsi
movq %rax, %rdi
callq 0x1120
leaq 0x5c89(%rip), %rsi      # 0x7219
movq %rax, %rdi
callq 0x1120
movq %rax, %rdi
callq 0x1070
jmp 0x1669
movq 0x8a2c(%rip), %rbx      # 0x9fd8
leaq 0x5aa0(%rip), %rsi      # 0x7053
movq %rbx, %rdi
callq 0x1120
movq %rax, %rdi
callq 0x1070
leaq 0x5ac1(%rip), %rsi      # 0x708b
movq %rbx, %rdi
callq 0x1120
movq %rax, %rdi
callq 0x1070
movq %rbx, %rdi
callq 0x1070
leaq 0x5ac8(%rip), %rsi      # 0x70b1
movq %rax, %rdi
callq 0x1120
movq %rax, %rdi
callq 0x1070
movq %rbx, %rdi
callq 0x1070
leaq 0x5ad9(%rip), %rsi      # 0x70e1
movq %rax, %rdi
callq 0x1120
movq %rax, %rdi
callq 0x1070
leaq 0x5af0(%rip), %rsi      # 0x710f
movq %rbx, %rdi
callq 0x1120
movq %rax, %rdi
callq 0x1070
pushq $0x1
jmp 0x1668
leaq 0x5af0(%rip), %rsi      # 0x712a
callq 0x1120
movq %rax, %rdi
callq 0x1070
pushq $0x2
jmp 0x1668
movq 0x8986(%rip), %rdi      # 0x9fd8
leaq 0x5b02(%rip), %rsi      # 0x715b
callq 0x1120
movq %rax, %rdi
callq 0x1070
pushq $0x3
popq %rbp
movl %ebp, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x8957(%rip), %rdi      # 0x9fd8
leaq 0x5b3d(%rip), %rsi      # 0x71c5
callq 0x1120
movq %rax, %rdi
callq 0x1070
pushq $0x4
jmp 0x1668
pushq $0x7
popq %r15
movq 0x8934(%rip), %rdi      # 0x9fd8
leaq 0x5b71(%rip), %rsi      # 0x721c
callq 0x1120
movq %rax, %rdi
callq 0x1070
movq %rax, %rdi
callq 0x1070
movl $0x145010, %edi         # imm = 0x145010
callq 0x1130
movq %rax, %r14
movq %rax, %rdi
callq 0x1896
movb $0x0, 0x145009(%r14)
movq 0x8(%rbx), %rsi
pushq $0x1
popq %rdx
movq %r14, %rdi
callq 0x3280
cmpb $0x0, 0x145006(%r14)
je 0x1755
movq 0x88db(%rip), %rdi      # 0x9fd8
leaq 0x5b7c(%rip), %rsi      # 0x7280
callq 0x1120
movq %rax, %rdi
callq 0x1070
movq 0x10(%rbx), %rdi
movl $0x242, %esi            # imm = 0x242
movl $0x1b6, %edx            # imm = 0x1B6
xorl %eax, %eax
callq 0x10c0
testl %eax, %eax
js 0x17ef
movl %eax, %ebx
decl %r15d
cmpl $0x6, %r15d
ja 0x17b1
leaq 0x58c4(%rip), %rax      # 0x7004
movslq (%rax,%r15,4), %rcx
addq %rax, %rcx
jmpq *%rcx
movq %r14, %rdi
movl %ebx, %esi
callq 0x2048
jmp 0x17b1
movq 0x887c(%rip), %rdi      # 0x9fd8
leaq 0x5afd(%rip), %rsi      # 0x7260
callq 0x1120
movq %rax, %rdi
callq 0x1070
pushq $0x6
jmp 0x1668
movq %r14, %rdi
movl %ebx, %esi
callq 0x27be
jmp 0x17b1
movq %r14, %rdi
movl %ebx, %esi
callq 0x2fd2
jmp 0x17b1
movq %r14, %rdi
movl %ebx, %esi
callq 0x225a
jmp 0x17b1
movq %r14, %rdi
movl %ebx, %esi
callq 0x2d46
jmp 0x17b1
movq %r14, %rdi
movl %ebx, %esi
callq 0x2ad6
movl %ebx, %edi
callq 0x1200
movq 0x8819(%rip), %rdi      # 0x9fd8
leaq 0x5ae9(%rip), %rsi      # 0x72af
callq 0x1120
movq %rax, %rdi
callq 0x1070
movq %r14, %rdi
callq 0x18fa
movl $0x145010, %esi         # imm = 0x145010
movq %r14, %rdi
callq 0x1140
xorl %ebp, %ebp
jmp 0x1669
movq %r14, %rdi
callq 0x18fa
movl $0x145010, %esi         # imm = 0x145010
movq %r14, %rdi
callq 0x1140
leaq 0x5a98(%rip), %rdi      # 0x72a3
callq 0x10e0
pushq $0x7
jmp 0x1668
movq %rax, %rbx
movl $0x145010, %esi         # imm = 0x145010
movq %r14, %rdi
callq 0x1140
movq %rbx, %rdi
callq 0x11e0