// File: /workspace/github_c_makefile_star2_O1/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 0x4623(%rip), %rsi      # 0x5d20
leaq 0x20(%rsp), %rdi
movl $0xc0, %edx
callq 0x1080
cmpl $0x1, %ebp
je 0x193c
xorl %eax, %eax
leaq 0x236e(%rip), %r13      # 0x3a8c
leaq 0x20(%rsp), %rbx
movq 0x48ae(%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 0x1779
movq (%r12), %rax
movzbl (%rax), %eax
xorl %r14d, %r14d
cmpl $0x65, %eax
je 0x1741
cmpl $0x64, %eax
jne 0x1838
movl $0x1, %r14d
jmp 0x1741
leal -0x68(%rax), %ecx
cmpl $0x7, %ecx
ja 0x17f8
leaq 0x22e4(%rip), %rdx      # 0x3a6c
movslq (%rdx,%rcx,4), %rax
addq %rdx, %rax
jmpq *%rax
movq (%r12), %rdi
leaq 0x237f(%rip), %rsi      # 0x3b1b
callq 0x1090
movq %rax, 0x8(%rsp)
testq %rax, %rax
jne 0x1741
jmp 0x18e6
movq (%r12), %rdi
leaq 0x2318(%rip), %rsi      # 0x3ad3
callq 0x1090
movq %rax, 0x10(%rsp)
testq %rax, %rax
jne 0x1741
jmp 0x18c2
movq (%r12), %rax
testq %rax, %rax
jne 0x173c
movq 0x47e9(%rip), %rax      # 0x5fd0
movq (%rax), %rcx
leaq 0x2347(%rip), %rdi      # 0x3b38
movl $0x1b, %esi
jmp 0x1866
cmpl $-0x1, %eax
jne 0x1910
movq 0x18(%rsp), %rdi
testq %rdi, %rdi
je 0x1941
callq 0x13a9
testq %rax, %rax
je 0x1850
testl %r14d, %r14d
je 0x187c
movq 0x8(%rsp), %r14
movq %r14, %rdi
movq 0x10(%rsp), %rbx
movq %rbx, %rsi
movq %rax, %rdx
callq 0x15af
jmp 0x1894
movq 0x4791(%rip), %rax      # 0x5fd0
movq (%rax), %rcx
leaq 0x2269(%rip), %rdi      # 0x3ab2
movl $0x20, %esi
jmp 0x1866
movq 0x4779(%rip), %rax      # 0x5fd0
movq (%rax), %rcx
leaq 0x22f3(%rip), %rdi      # 0x3b54
movl $0x18, %esi
movl $0x1, %edx
callq 0x10b0
movq 0x10(%rsp), %rbx
movq 0x8(%rsp), %r14
jmp 0x1894
movq 0x8(%rsp), %r14
movq %r14, %rdi
movq 0x10(%rsp), %rbx
movq %rbx, %rsi
movq %rax, %rdx
callq 0x1432
testq %r14, %r14
je 0x18a1
movq %r14, %rdi
callq 0x1040
testq %rbx, %rbx
je 0x18ae
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 0x4707(%rip), %rax      # 0x5fd0
movq (%rax), %rcx
leaq 0x2203(%rip), %rdi      # 0x3ad6
movl $0x44, %esi
movl $0x1, %edx
callq 0x10b0
xorl %ebx, %ebx
jmp 0x1875
movq 0x46e3(%rip), %rax      # 0x5fd0
movq (%rax), %rcx
leaq 0x2226(%rip), %rdi      # 0x3b1d
movl $0x1a, %esi
movl $0x1, %edx
callq 0x10b0
xorl %r14d, %r14d
movq 0x10(%rsp), %rbx
jmp 0x1894
callq 0x10f5
movq 0x46b4(%rip), %rax      # 0x5fd0
movq (%rax), %rcx
leaq 0x2247(%rip), %rdi      # 0x3b6d
movl $0x123, %esi            # imm = 0x123
movl $0x1, %edx
callq 0x10b0
xorl %edi, %edi
callq 0x10a0
callq 0x111a
callq 0x10d0