// File: /workspace/github_cpp_makefile_star2_O3/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 %r12
pushq %rbx
subq $0x90, %rsp
movq %rsi, %r14
movl %edi, %ebp
movq 0x9c11(%rip), %rbx      # 0xafd8
leaq 0x6c52(%rip), %rsi      # 0x8020
movl $0x32, %edx
movq %rbx, %rdi
callq 0x1170
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x1130
movsbl %al, %esi
movq %rbx, %rdi
callq 0x1040
movq %rax, %rdi
callq 0x10c0
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x1130
movsbl %al, %esi
movq %rbx, %rdi
callq 0x1040
movq %rax, %rdi
callq 0x10c0
cmpl $0x3, %ebp
jne 0x146e
movq 0x8(%r14), %r15
movq 0x10(%r14), %rsi
movq %r15, %rdi
callq 0x11a0
testl %eax, %eax
je 0x15f3
testq %r15, %r15
je 0x1638
movq %r15, %rdi
callq 0x1080
movq 0x9b7a(%rip), %rdi      # 0xafd8
movq %r15, %rsi
movq %rax, %rdx
callq 0x1170
jmp 0x164f
leaq 0x6bde(%rip), %rsi      # 0x8053
movl $0x37, %edx
movq %rbx, %rdi
callq 0x1170
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x1130
movsbl %al, %esi
movq %rbx, %rdi
callq 0x1040
movq %rax, %rdi
callq 0x10c0
leaq 0x6bdb(%rip), %rsi      # 0x808b
movl $0x25, %edx
movq %rbx, %rdi
callq 0x1170
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x1130
movsbl %al, %esi
movq %rbx, %rdi
callq 0x1040
movq %rax, %rdi
callq 0x10c0
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x1130
movsbl %al, %esi
movq %rbx, %rdi
callq 0x1040
movq %rax, %rdi
callq 0x10c0
movq %rax, %r14
leaq 0x6b9c(%rip), %rsi      # 0x80b1
movl $0x2f, %edx
movq %rax, %rdi
callq 0x1170
movq (%r14), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x1130
movsbl %al, %esi
movq %r14, %rdi
callq 0x1040
movq %rax, %rdi
callq 0x10c0
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x1130
movsbl %al, %esi
movq %rbx, %rdi
callq 0x1040
movq %rax, %rdi
callq 0x10c0
movq %rax, %r14
leaq 0x6b67(%rip), %rsi      # 0x80e1
movl $0x2d, %edx
movq %rax, %rdi
callq 0x1170
movq (%r14), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x1130
movsbl %al, %esi
movq %r14, %rdi
callq 0x1040
movq %rax, %rdi
callq 0x10c0
leaq 0x6b5a(%rip), %rsi      # 0x810f
movl $0x1a, %edx
movq %rbx, %rdi
callq 0x1170
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x1130
movsbl %al, %esi
movq %rbx, %rdi
callq 0x1040
movq %rax, %rdi
callq 0x10c0
movl $0x1, %eax
jmp 0x175b
leaq 0x6b30(%rip), %rsi      # 0x812a
movl $0x29, %edx
movq %rbx, %rdi
callq 0x1170
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x1130
movsbl %al, %esi
movq %rbx, %rdi
callq 0x1040
movq %rax, %rdi
callq 0x10c0
movl $0x2, %eax
jmp 0x175b
movq (%rbx), %rax
movq -0x18(%rax), %rax
leaq (%rbx,%rax), %rdi
movl 0x20(%rbx,%rax), %esi
orl $0x1, %esi
callq 0x11e0
movq 0x9982(%rip), %rbx      # 0xafd8
leaq 0x6af7(%rip), %rsi      # 0x8154
movl $0x6, %edx
movq %rbx, %rdi
callq 0x1170
movq 0x10(%r14), %r15
testq %r15, %r15
je 0x168f
movq %r15, %rdi
callq 0x1080
movq 0x9956(%rip), %rdi      # 0xafd8
movq %r15, %rsi
movq %rax, %rdx
callq 0x1170
jmp 0x16a6
movq (%rbx), %rax
movq -0x18(%rax), %rax
leaq (%rbx,%rax), %rdi
movl 0x20(%rbx,%rax), %esi
orl $0x1, %esi
callq 0x11e0
leaq 0x6bf2(%rip), %rsi      # 0x829f
movl $0x3, %edx
movq %rbx, %rdi
callq 0x1170
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x1130
movsbl %al, %esi
movq %rbx, %rdi
callq 0x1040
movq %rax, %rdi
callq 0x10c0
movq %rax, %r15
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r15, %rdi
movl $0xa, %esi
callq 0x1130
movsbl %al, %esi
movq %r15, %rdi
callq 0x1040
movq %rax, %rdi
callq 0x10c0
movq 0x8(%r14), %rdi
movq %rsp, %rsi
callq 0x1110
testl %eax, %eax
je 0x176b
leaq 0x6a39(%rip), %rsi      # 0x815b
movl $0x1d, %edx
movq %rbx, %rdi
callq 0x1170
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x1130
movsbl %al, %esi
movq %rbx, %rdi
callq 0x1040
movq %rax, %rdi
callq 0x10c0
movl $0x3, %eax
addq $0x90, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
movq 0x10(%r14), %rdi
movq %rsp, %rsi
callq 0x1110
testl %eax, %eax
jne 0x1810
leaq 0x69f3(%rip), %rsi      # 0x8179
movl $0x29, %edx
movq %rbx, %rdi
callq 0x1170
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x1130
movsbl %al, %esi
movq %rbx, %rdi
callq 0x1040
movq %rax, %rdi
callq 0x10c0
leaq 0x69e2(%rip), %rsi      # 0x81a3
movl $0x21, %edx
movq %rbx, %rdi
callq 0x1170
movq %rbx, %rdi
callq 0x10c0
callq 0x1190
movl %eax, %ebp
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x1130
movsbl %al, %esi
movq %rbx, %rdi
callq 0x1040
movq %rax, %rdi
callq 0x10c0
andl $-0x21, %ebp
cmpl $0x59, %ebp
jne 0x1ac8
movq 0x10(%r14), %rdi
movl $0x2e, %esi
callq 0x1160
testq %rax, %rax
je 0x18e4
movq %rsp, %r15
movl $0x40, %edx
movq %r15, %rdi
movq %rax, %rsi
callq 0x1090
movb (%r15), %al
testb %al, %al
je 0x185c
leaq 0x1(%rsp), %rcx
leal -0x61(%rax), %edx
cmpb $0x19, %dl
ja 0x1853
addb $-0x20, %al
movb %al, -0x1(%rcx)
movb (%rcx), %al
incq %rcx
testb %al, %al
jne 0x1846
movl $0x4444462e, %eax       # imm = 0x4444462E
xorl (%rsp), %eax
movzbl 0x4(%rsp), %ecx
orl %eax, %ecx
je 0x1956
movl (%rsp), %edx
movl %edx, %eax
xorl $0x3044542e, %eax       # imm = 0x3044542E
movzbl 0x4(%rsp), %ecx
movl %edx, %esi
xorl $0x4944552e, %esi       # imm = 0x4944552E
movl %edx, %edi
xorl $0x4944462e, %edi       # imm = 0x4944462E
movl %edx, %r8d
xorl $0x4c43532e, %r8d       # imm = 0x4C43532E
xorl $0x4452542e, %edx       # imm = 0x4452542E
xorl %r9d, %r9d
orl %ecx, %edx
sete %r9b
orl %ecx, %r8d
movl $0x2, %edx
cmovnel %r9d, %edx
orl %ecx, %edi
movl $0x3, %edi
cmovnel %edx, %edi
orl %ecx, %esi
movl $0x5, %edx
cmovnel %edi, %edx
orl %ecx, %eax
movl $0x4, %r12d
cmovnel %edx, %r12d
cmpl $0x5, %r12d
je 0x19c1
testl %r12d, %r12d
jne 0x195c
leaq 0x690b(%rip), %rsi      # 0x81f6
movl $0x22, %edx
movq %rbx, %rdi
callq 0x1170
movq %rsp, %r14
movq %r14, %rdi
callq 0x1080
movq %rbx, %rdi
movq %r14, %rsi
movq %rax, %rdx
callq 0x1170
leaq 0x6901(%rip), %rsi      # 0x8219
movl $0x2, %edx
movq %rbx, %rdi
callq 0x1170
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x1130
movsbl %al, %esi
movq %rbx, %rdi
callq 0x1040
movq %rax, %rdi
callq 0x10c0
movl $0x5, %eax
jmp 0x175b
movl $0x7, %r12d
leaq 0x68b9(%rip), %rsi      # 0x821c
movl $0x43, %edx
movq %rbx, %rdi
callq 0x1170
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x1130
movsbl %al, %esi
movq %rbx, %rdi
callq 0x1040
movq %rax, %rdi
callq 0x10c0
movq %rax, %r15
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r15, %rdi
movl $0xa, %esi
callq 0x1130
movsbl %al, %esi
movq %r15, %rdi
callq 0x1040
movq %rax, %rdi
callq 0x10c0
movl $0x145010, %edi         # imm = 0x145010
callq 0x1140
movq %rax, %r15
movq %rax, %rdi
callq 0x1bf2
movb $0x0, 0x145009(%r15)
movq 0x8(%r14), %rsi
movq %r15, %rdi
movl $0x1, %edx
callq 0x3b3c
cmpb $0x0, 0x145006(%r15)
je 0x1a83
leaq 0x687c(%rip), %rsi      # 0x8280
movl $0x22, %edx
movq %rbx, %rdi
callq 0x1170
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x1130
movsbl %al, %esi
movq %rbx, %rdi
callq 0x1040
movq %rax, %rdi
callq 0x10c0
movq 0x10(%r14), %rdi
movl $0x242, %esi            # imm = 0x242
movl $0x1b6, %edx            # imm = 0x1B6
xorl %eax, %eax
callq 0x10d0
testl %eax, %eax
js 0x1ba5
movl %eax, %ebp
decl %r12d
cmpl $0x6, %r12d
ja 0x1b47
leaq 0x6599(%rip), %rax      # 0x8004
movslq (%rax,%r12,4), %rcx
addq %rax, %rcx
jmpq *%rcx
movq %r15, %rdi
movl %ebp, %esi
callq 0x2498
jmp 0x1b47
leaq 0x67d6(%rip), %rsi      # 0x8260
movl $0x1f, %edx
movq %rbx, %rdi
callq 0x1170
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x1130
movsbl %al, %esi
movq %rbx, %rdi
callq 0x1040
movq %rax, %rdi
callq 0x10c0
movl $0x6, %eax
jmp 0x175b
leaq 0x66f6(%rip), %rsi      # 0x81c5
movl $0x12, %edx
movq %rbx, %rdi
callq 0x1170
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x1130
movsbl %al, %esi
movq %rbx, %rdi
callq 0x1040
movq %rax, %rdi
callq 0x10c0
movl $0x4, %eax
jmp 0x175b
movq %r15, %rdi
movl %ebp, %esi
callq 0x2f32
jmp 0x1b47
movq %r15, %rdi
movl %ebp, %esi
callq 0x386a
jmp 0x1b47
movq %r15, %rdi
movl %ebp, %esi
callq 0x279a
jmp 0x1b47
movq %r15, %rdi
movl %ebp, %esi
callq 0x3554
jmp 0x1b47
movq %r15, %rdi
movl %ebp, %esi
callq 0x32a0
movl %ebp, %edi
callq 0x1220
leaq 0x675a(%rip), %rsi      # 0x82af
movl $0x26, %edx
movq %rbx, %rdi
callq 0x1170
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x1130
movsbl %al, %esi
movq %rbx, %rdi
callq 0x1040
movq %rax, %rdi
callq 0x10c0
movq %r15, %rdi
callq 0x1c58
movl $0x145010, %esi         # imm = 0x145010
movq %r15, %rdi
callq 0x1150
xorl %eax, %eax
jmp 0x175b
movq %r15, %rdi
callq 0x1240
movl $0x7, %eax
jmp 0x175b
movq %rax, %rbx
movl $0x145010, %esi         # imm = 0x145010
movq %r15, %rdi
callq 0x1150
movq %rbx, %rdi
callq 0x1200
nop