// File: 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
movq %rsp, %rbp
subq $0x20a0, %rsp           # imm = 0x20A0
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq 0xfc28(%rip), %rdi      # 0x10fd8
leaq 0xcc4d(%rip), %rsi      # 0xe004
callq 0x1110
movq %rax, %rdi
movq 0xfc02(%rip), %rsi      # 0x10fc8
callq 0x1140
movq 0xfc06(%rip), %rdi      # 0x10fd8
movq 0xfbef(%rip), %rsi      # 0x10fc8
callq 0x1140
cmpl $0x3, -0x8(%rbp)
je 0x14bc
movq 0xfbe9(%rip), %rdi      # 0x10fd8
leaq 0xcc41(%rip), %rsi      # 0xe037
callq 0x1110
movq %rax, %rdi
movq 0xfbc3(%rip), %rsi      # 0x10fc8
callq 0x1140
movq 0xfbc7(%rip), %rdi      # 0x10fd8
leaq 0xcc57(%rip), %rsi      # 0xe06f
callq 0x1110
movq %rax, %rdi
movq 0xfba1(%rip), %rsi      # 0x10fc8
callq 0x1140
movq 0xfba5(%rip), %rdi      # 0x10fd8
movq 0xfb8e(%rip), %rsi      # 0x10fc8
callq 0x1140
movq %rax, %rdi
leaq 0xcc4c(%rip), %rsi      # 0xe095
callq 0x1110
movq %rax, %rdi
movq 0xfb70(%rip), %rsi      # 0x10fc8
callq 0x1140
movq 0xfb74(%rip), %rdi      # 0x10fd8
movq 0xfb5d(%rip), %rsi      # 0x10fc8
callq 0x1140
movq %rax, %rdi
leaq 0xcc4b(%rip), %rsi      # 0xe0c5
callq 0x1110
movq %rax, %rdi
movq 0xfb3f(%rip), %rsi      # 0x10fc8
callq 0x1140
movq 0xfb43(%rip), %rdi      # 0x10fd8
leaq 0xcc57(%rip), %rsi      # 0xe0f3
callq 0x1110
movq %rax, %rdi
movq 0xfb1d(%rip), %rsi      # 0x10fc8
callq 0x1140
movl $0x1, -0x4(%rbp)
jmp 0x1a5a
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rsi
callq 0x1190
cmpl $0x0, %eax
jne 0x1504
movq 0xfafb(%rip), %rdi      # 0x10fd8
leaq 0xcc2a(%rip), %rsi      # 0xe10e
callq 0x1110
movq %rax, %rdi
movq 0xfad5(%rip), %rsi      # 0x10fc8
callq 0x1140
movl $0x2, -0x4(%rbp)
jmp 0x1a5a
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rsi
movq 0xfac5(%rip), %rdi      # 0x10fd8
callq 0x1110
movq %rax, %rdi
leaq 0xcc16(%rip), %rsi      # 0xe138
callq 0x1110
movq %rax, %rdi
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rsi
callq 0x1110
movq %rax, %rdi
leaq 0xcd42(%rip), %rsi      # 0xe283
callq 0x1110
movq %rax, %rdi
movq 0xfa78(%rip), %rsi      # 0x10fc8
callq 0x1140
movq %rax, %rdi
movq 0xfa69(%rip), %rsi      # 0x10fc8
callq 0x1140
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
callq 0x1a80
testb $0x1, %al
jne 0x15a3
movq 0xfa5c(%rip), %rdi      # 0x10fd8
leaq 0xcbbc(%rip), %rsi      # 0xe13f
callq 0x1110
movq %rax, %rdi
movq 0xfa36(%rip), %rsi      # 0x10fc8
callq 0x1140
movl $0x3, -0x4(%rbp)
jmp 0x1a5a
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rdi
callq 0x1a80
testb $0x1, %al
jne 0x15b9
jmp 0x165a
movq 0xfa18(%rip), %rdi      # 0x10fd8
leaq 0xcb96(%rip), %rsi      # 0xe15d
callq 0x1110
movq %rax, %rdi
movq 0xf9f2(%rip), %rsi      # 0x10fc8
callq 0x1140
movq 0xf9f6(%rip), %rdi      # 0x10fd8
leaq 0xcb9e(%rip), %rsi      # 0xe187
callq 0x1110
movq 0xf9e3(%rip), %rdi      # 0x10fd8
callq 0x10a0
callq 0x1180
movl %eax, -0x2014(%rbp)
movq 0xf9cc(%rip), %rdi      # 0x10fd8
movq 0xf9b5(%rip), %rsi      # 0x10fc8
callq 0x1140
cmpl $0x79, -0x2014(%rbp)
je 0x1658
cmpl $0x59, -0x2014(%rbp)
je 0x1658
movq 0xf9a7(%rip), %rdi      # 0x10fd8
leaq 0xcb71(%rip), %rsi      # 0xe1a9
callq 0x1110
movq %rax, %rdi
movq 0xf981(%rip), %rsi      # 0x10fc8
callq 0x1140
movl $0x4, -0x4(%rbp)
jmp 0x1a5a
jmp 0x165a
movl $0x0, -0x2018(%rbp)
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rdi
callq 0x1ab0
movq %rax, -0x2068(%rbp)
cmpq $0x0, -0x2068(%rbp)
je 0x1778
leaq -0x2060(%rbp), %rdi
movq -0x2068(%rbp), %rsi
movl $0x40, %edx
callq 0x1080
leaq -0x2060(%rbp), %rdi
callq 0x1ad0
leaq -0x2060(%rbp), %rdi
leaq 0xcb04(%rip), %rsi      # 0xe1bc
callq 0x1190
cmpl $0x0, %eax
jne 0x16cc
movl $0x1, -0x2018(%rbp)
leaq -0x2060(%rbp), %rdi
leaq 0xcae7(%rip), %rsi      # 0xe1c1
callq 0x1190
cmpl $0x0, %eax
jne 0x16ee
movl $0x2, -0x2018(%rbp)
leaq -0x2060(%rbp), %rdi
leaq 0xcaca(%rip), %rsi      # 0xe1c6
callq 0x1190
cmpl $0x0, %eax
jne 0x1710
movl $0x3, -0x2018(%rbp)
leaq -0x2060(%rbp), %rdi
leaq 0xcaad(%rip), %rsi      # 0xe1cb
callq 0x1190
cmpl $0x0, %eax
jne 0x1732
movl $0x5, -0x2018(%rbp)
leaq -0x2060(%rbp), %rdi
leaq 0xca90(%rip), %rsi      # 0xe1d0
callq 0x1190
cmpl $0x0, %eax
jne 0x1754
movl $0x4, -0x2018(%rbp)
leaq -0x2060(%rbp), %rdi
leaq 0xca73(%rip), %rsi      # 0xe1d5
callq 0x1190
cmpl $0x0, %eax
jne 0x1776
movl $0x7, -0x2018(%rbp)
jmp 0x1778
cmpl $0x0, -0x2018(%rbp)
jne 0x17cd
movq 0xf850(%rip), %rdi      # 0x10fd8
leaq 0xca4b(%rip), %rsi      # 0xe1da
callq 0x1110
movq %rax, %rdi
leaq -0x2060(%rbp), %rsi
callq 0x1110
movq %rax, %rdi
leaq 0xca50(%rip), %rsi      # 0xe1fd
callq 0x1110
movq %rax, %rdi
movq 0xf80c(%rip), %rsi      # 0x10fc8
callq 0x1140
movl $0x5, -0x4(%rbp)
jmp 0x1a5a
cmpl $0x5, -0x2018(%rbp)
je 0x1807
movq 0xf7fb(%rip), %rdi      # 0x10fd8
leaq 0xca1c(%rip), %rsi      # 0xe200
callq 0x1110
movq %rax, %rdi
movq 0xf7d5(%rip), %rsi      # 0x10fc8
callq 0x1140
movq %rax, %rdi
movq 0xf7c6(%rip), %rsi      # 0x10fc8
callq 0x1140
movl $0x145010, %edi         # imm = 0x145010
callq 0x1120
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0x2088(%rbp)
callq 0x1ba0
jmp 0x1825
movq -0x2088(%rbp), %rax
movq %rax, -0x2070(%rbp)
movq -0x2070(%rbp), %rax
movb $0x0, 0x145009(%rax)
movq -0x2070(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rsi
movl $0x1, %edx
callq 0x4a00
movq -0x2070(%rbp), %rax
testb $0x1, 0x145006(%rax)
jne 0x18c0
movq 0xf767(%rip), %rdi      # 0x10fd8
leaq 0xc9cc(%rip), %rsi      # 0xe244
callq 0x1110
movq %rax, %rdi
movq 0xf741(%rip), %rsi      # 0x10fc8
callq 0x1140
movl $0x6, -0x4(%rbp)
jmp 0x1a5a
movq -0x2088(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x2078(%rbp)
movl %eax, -0x207c(%rbp)
movl $0x145010, %esi         # imm = 0x145010
callq 0x1130
jmp 0x1a66
movq 0xf711(%rip), %rdi      # 0x10fd8
leaq 0xc996(%rip), %rsi      # 0xe264
callq 0x1110
movq %rax, %rdi
movq 0xf6eb(%rip), %rsi      # 0x10fc8
callq 0x1140
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rdi
movl $0x242, %esi            # imm = 0x242
movl $0x1b6, %edx            # imm = 0x1B6
movb $0x0, %al
callq 0x10b0
movl %eax, -0x2080(%rbp)
cmpl $0x0, -0x2080(%rbp)
jge 0x1953
movq -0x2070(%rbp), %rax
movq %rax, -0x2090(%rbp)
cmpq $0x0, %rax
je 0x193b
movq -0x2090(%rbp), %rdi
callq 0x1ca0
movq -0x2090(%rbp), %rdi
movl $0x145010, %esi         # imm = 0x145010
callq 0x1130
leaq 0xc945(%rip), %rdi      # 0xe287
callq 0x10d0
movl $0x7, -0x4(%rbp)
jmp 0x1a5a
cmpl $0x1, -0x2018(%rbp)
jne 0x196e
movq -0x2070(%rbp), %rdi
movl -0x2080(%rbp), %esi
callq 0x2c80
cmpl $0x2, -0x2018(%rbp)
jne 0x1989
movq -0x2070(%rbp), %rdi
movl -0x2080(%rbp), %esi
callq 0x2f90
cmpl $0x3, -0x2018(%rbp)
jne 0x19a4
movq -0x2070(%rbp), %rdi
movl -0x2080(%rbp), %esi
callq 0x37a0
cmpl $0x5, -0x2018(%rbp)
jne 0x19bf
movq -0x2070(%rbp), %rdi
movl -0x2080(%rbp), %esi
callq 0x3cf0
cmpl $0x4, -0x2018(%rbp)
jne 0x19da
movq -0x2070(%rbp), %rdi
movl -0x2080(%rbp), %esi
callq 0x4150
cmpl $0x7, -0x2018(%rbp)
jne 0x19f5
movq -0x2070(%rbp), %rdi
movl -0x2080(%rbp), %esi
callq 0x44f0
movl -0x2080(%rbp), %edi
callq 0x1200
movq 0xf5d1(%rip), %rdi      # 0x10fd8
leaq 0xc885(%rip), %rsi      # 0xe293
callq 0x1110
movq %rax, %rdi
movq 0xf5ab(%rip), %rsi      # 0x10fc8
callq 0x1140
movq -0x2070(%rbp), %rax
movq %rax, -0x2098(%rbp)
cmpq $0x0, %rax
je 0x1a53
movq -0x2098(%rbp), %rdi
callq 0x1ca0
movq -0x2098(%rbp), %rdi
movl $0x145010, %esi         # imm = 0x145010
callq 0x1130
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x20a0, %rsp           # imm = 0x20A0
popq %rbp
retq
movq -0x2078(%rbp), %rdi
callq 0x11e0
nopw %cs:(%rax,%rax)
nopl (%rax)