// File: /workspace/github_c_makefile_star2_O2/dubeyalok7[P]crypto-algo/des/des/main.c
// Function: main
int main(int argc, char *argv[])
{
  uint64_t key = 0;
  const char *keystr = NULL;
  FILE *ifile = NULL, *ofile=NULL;
  int op;

  int optc = 0;

  const char *short_opts = "t:o:k:i:h:";
  const struct option long_opts[]=
  {
    {"help",           no_argument, NULL, 'h'},
    {"task",      required_argument, NULL, 't'},
    {"output",   required_argument, NULL, 'o'},
    {"input",   required_argument, NULL, 'i'},
    {"key",      required_argument, NULL, 'k'},
    {NULL,                       0, NULL,   0}
  };
  if(argc == 1)
    help(EXIT_FAILURE);

  while((optc = getopt_long(argc, argv, short_opts, long_opts, NULL))!= -1)
  {
    switch(optc)
    {
      case 't':
        if(optarg[0]=='e')
          op = ENCRYPT;
        else if(optarg[0] == 'd')
          op = DECRYPT;
        else{
          fprintf(stdout, "error: in-valid type operations\n");
          goto exit;
        }
        break;
      case 'h':
        help(EXIT_SUCCESS);
        break;
      case 'o':
        ofile = fopen(optarg, "w+");
        if(!ofile) {
          fprintf(stdout, "error: not have permission to create/open file or invalid file name\n"); 
          goto exit;
        }
        break;
      case 'i':
        ifile = fopen(optarg, "r");
        if(!ifile) {
          fprintf(stdout, "error: file doesn't exist\n");
          goto exit;
        }
        break;
      case 'k':
        keystr = optarg;
        if(!keystr) {
          fprintf(stdout, "error: key is not provided\n");
          goto exit;
        }
        break;
      default:
        help(EXIT_FAILURE);
    }
  }
  if(!keystr)
    help(EXIT_FAILURE);

  key = key_convert(keystr);
  if(!key){
    fprintf(stdout, "error: check key length\n");
    goto exit;
  }
    
  if(op)
    decrypt(ifile, ofile, key);
  else
    encrypt(ifile, ofile, key);

exit:
  if(ifile)
    fclose(ifile);
  if(ofile)
    fclose(ofile);
  return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xe8, %rsp
movq %rsi, %r15
movl %edi, %ebp
leaq 0x471f(%rip), %rsi      # 0x5d20
leaq 0x20(%rsp), %rdi
movl $0xc0, %edx
callq 0x1080
cmpl $0x1, %ebp
je 0x17fd
xorl %eax, %eax
leaq 0x246a(%rip), %r13      # 0x3a8c
leaq 0x20(%rsp), %rbx
movq 0x49aa(%rip), %r12      # 0x5fd8
movq $0x0, 0x8(%rsp)
movq $0x0, 0x10(%rsp)
movq %rax, 0x18(%rsp)
movl %ebp, %edi
movq %r15, %rsi
movq %r13, %rdx
movq %rbx, %rcx
xorl %r8d, %r8d
callq 0x1060
cmpl $0x74, %eax
jne 0x167b
movq (%r12), %rax
movzbl (%rax), %eax
xorl %r14d, %r14d
cmpl $0x65, %eax
je 0x1645
cmpl $0x64, %eax
jne 0x1737
pushq $0x1
popq %r14
jmp 0x1645
leal -0x68(%rax), %ecx
cmpl $0x7, %ecx
ja 0x16f7
leaq 0x23e2(%rip), %rdx      # 0x3a6c
movslq (%rdx,%rcx,4), %rax
addq %rdx, %rax
jmpq *%rax
movq (%r12), %rdi
leaq 0x247d(%rip), %rsi      # 0x3b1b
callq 0x1090
movq %rax, 0x8(%rsp)
testq %rax, %rax
jne 0x1645
jmp 0x17da
movq (%r12), %rdi
leaq 0x2416(%rip), %rsi      # 0x3ad3
callq 0x1090
movq %rax, 0x10(%rsp)
testq %rax, %rax
jne 0x1645
jmp 0x17ba
movq (%r12), %rax
testq %rax, %rax
jne 0x1640
movq 0x48e7(%rip), %rax      # 0x5fd0
movq (%rax), %rcx
leaq 0x2445(%rip), %rdi      # 0x3b38
pushq $0x1b
jmp 0x175f
cmpl $-0x1, %eax
jne 0x17fd
movq 0x18(%rsp), %rdi
testq %rdi, %rdi
je 0x17fd
callq 0x1364
testq %rax, %rax
je 0x174c
testl %r14d, %r14d
je 0x1774
movq 0x8(%rsp), %r14
movq %r14, %rdi
movq 0x10(%rsp), %rbx
movq %rbx, %rsi
movq %rax, %rdx
callq 0x150d
jmp 0x178c
movq 0x4892(%rip), %rax      # 0x5fd0
movq (%rax), %rcx
leaq 0x236a(%rip), %rdi      # 0x3ab2
pushq $0x20
jmp 0x175f
movq 0x487d(%rip), %rax      # 0x5fd0
movq (%rax), %rcx
leaq 0x23f7(%rip), %rdi      # 0x3b54
pushq $0x18
popq %rsi
pushq $0x1
popq %rdx
callq 0x10b0
movq 0x10(%rsp), %rbx
movq 0x8(%rsp), %r14
jmp 0x178c
movq 0x8(%rsp), %r14
movq %r14, %rdi
movq 0x10(%rsp), %rbx
movq %rbx, %rsi
movq %rax, %rdx
callq 0x13ec
testq %r14, %r14
je 0x1799
movq %r14, %rdi
callq 0x1040
testq %rbx, %rbx
je 0x17a6
movq %rbx, %rdi
callq 0x1040
xorl %eax, %eax
addq $0xe8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x480f(%rip), %rax      # 0x5fd0
movq (%rax), %rcx
leaq 0x230b(%rip), %rdi      # 0x3ad6
pushq $0x44
popq %rsi
pushq $0x1
popq %rdx
callq 0x10b0
xorl %ebx, %ebx
jmp 0x176d
movq 0x47ef(%rip), %rax      # 0x5fd0
movq (%rax), %rcx
leaq 0x2332(%rip), %rdi      # 0x3b1d
pushq $0x1a
popq %rsi
pushq $0x1
popq %rdx
callq 0x10b0
movq 0x10(%rsp), %rbx
jmp 0x1799
pushq $0x1
popq %rdi
callq 0x180c
xorl %edi, %edi
callq 0x180c