// File: /workspace/github_c_makefile_star2_O2/otrtool[P]otrtool/src/main.c
// Function: decryptFile
void decryptFile() {
  int fd;
  char *headerFN;
  struct stat st;
  FILE *destfile;

  if (opts.destfile == NULL) {
    headerFN = queryGetParam(header, "FN");
    if (opts.destdir != NULL) {
      destfilename = malloc(strlen(opts.destdir) + strlen(headerFN) + 2);
      strcpy(destfilename, opts.destdir);
      strcat(destfilename, "/");
      strcat(destfilename, headerFN);
      free(headerFN);
    }
    else {
      destfilename = headerFN;
    }
  }
  else {
    destfilename = strdup(opts.destfile);
  }
  
  if (strcmp(destfilename, "-") == 0) {
    if (isatty(1)) ERROR("error: cowardly refusing to output to a terminal");
    fd = 1;
  }
  else
    fd = open(destfilename, O_WRONLY|O_CREAT|O_EXCL, CREAT_MODE);
  if (fd < 0 && errno == EEXIST) {
    if (stat(destfilename, &st) != 0 || S_ISREG(st.st_mode)) {
      if (!interactive) ERROR("Destination file exists: %s", destfilename);
      fprintf(stderr, "Destination file exists: %s\nType y to overwrite: ",
        destfilename);
      if (fgetc(ttyfile) != 'y') exit(EXIT_FAILURE);
      while (fgetc(ttyfile) != '\n');
      fd = open(destfilename, O_WRONLY|O_TRUNC, 0);
    }
    else
      fd = open(destfilename, O_WRONLY, 0);
  }
  if (fd < 0)
    PERROR("Error opening destination file: %s", destfilename);
  if ((destfile = fdopen(fd, "wb")) == NULL)
    PERROR("fdopen");
  
  fputs("Decrypting and verifying...\n", stderr); // -----------------------
  
  void *key = hex2bin(keyphrase);
  unsigned long long length = atoll(queryGetParam(header, "SZ")) - 522;
  unsigned long long position = 0;
  vfy_t vfy_in, vfy_out;
  bf_state_t blowfish;
  
  verifyFile_init(&vfy_in, 1);
  verifyFile_init(&vfy_out, 0);
  bf_init(&blowfish, key, 28);

  enum {
    N_WRK_MAX = MT ? 16 : 1,
    /* Maximum multithreading speedup should be around 6 to 7. In case the
       number of CPUs is unknown, rather have too many threads than too few. */
    N_WRK_DEFAULT = MT ? 8 : 1,
  };
  thread_t wrk_id[N_WRK_MAX];
  struct worker_info wrk_info[N_WRK_MAX];
  int n_wrk = N_WRK_DEFAULT;
  int i;

#ifdef _SC_NPROCESSORS_ONLN
  i = sysconf(_SC_NPROCESSORS_ONLN);
  if (opts.verbosity >= VERB_DEBUG)
    fprintf(stderr, "number of CPUs: %d\n", i);
  if (i > 0) n_wrk = MIN(i, N_WRK_DEFAULT);
#endif
  if (opts.n_threads > 0)
    n_wrk = MIN(opts.n_threads, N_WRK_MAX);
  if (opts.verbosity >= VERB_DEBUG) {
  #if MT
    fprintf(stderr, "number of worker threads: %d\n", n_wrk);
  #else
    fputs("multi-threading disabled at compile-time\n", stderr);
  #endif
  }

  /* Create worker threads */
  for (i=0; i < n_wrk; i++) {
    wrk_info[i].next = (i < n_wrk - 1) ? wrk_info + i + 1 : wrk_info;
    initSemaphore(&wrk_info[i].read_sema, 0);
    initSemaphore(&wrk_info[i].ivfy_sema, 0);
    initSemaphore(&wrk_info[i].ovfy_sema, 0);
    initSemaphore(&wrk_info[i].write_sema, 0);
    wrk_info[i].length = length;
    wrk_info[i].pposition = &position;
    wrk_info[i].file = file;
    wrk_info[i].destfile = destfile;
    wrk_info[i].vfy_inp = &vfy_in;
    wrk_info[i].vfy_outp = &vfy_out;
    wrk_info[i].blowfishp = &blowfish;
  }
  for (i=0; i < n_wrk; i++)
    wrk_id[i] = createThread(decryptFileWorker, &wrk_info[i]);

  /* Go! */
  postSemaphore(&wrk_info[0].read_sema);
  postSemaphore(&wrk_info[0].ivfy_sema);
  postSemaphore(&wrk_info[0].ovfy_sema);
  postSemaphore(&wrk_info[0].write_sema);

  /* Wait... */
  for (i=0; i < n_wrk; i++)
    joinThread(wrk_id[i]);

  for (i=0; i < n_wrk; i++) {
    destroySemaphore(&wrk_info[i].read_sema);
    destroySemaphore(&wrk_info[i].ivfy_sema);
    destroySemaphore(&wrk_info[i].ovfy_sema);
    destroySemaphore(&wrk_info[i].write_sema);
  }
  showProgress(1, 0);

  verifyFile_final(&vfy_in);
  verifyFile_final(&vfy_out);
  fputs("OK checksums from header match\n", stderr);
  
  if (fclose(destfile) != 0)
    PERROR("Error closing destination file.");

  if (opts.unlinkmode) {
    if (strcmp(filename, "-") != 0 &&
        stat(filename, &st) == 0 && S_ISREG(st.st_mode) &&
        strcmp(destfilename, "-") != 0 &&
        stat(destfilename, &st) == 0 && S_ISREG(st.st_mode)) {
      if (unlink(filename) != 0)
        PERROR("Cannot delete input file");
      else
        fputs("info: input file has been deleted\n", stderr);
    }
    else {
      fputs("Warning: Not deleting input file (input or "
          "output is not a regular file)\n", stderr);
    }
  }
  
  free(key);
  free(destfilename);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1ed8, %rsp           # imm = 0x1ED8
movq 0x6790(%rip), %rdi      # 0xc2d0
testq %rdi, %rdi
jne 0x5b70
movq 0x67ac(%rip), %rdi      # 0xc2f8
leaq 0x287b(%rip), %rsi      # 0x83ce
callq 0x3911
movq %rax, %rbx
movq 0x6766(%rip), %r14      # 0xc2c8
testq %r14, %r14
jne 0x5b81
movq %rbx, 0x169e2(%rip)     # 0x1c550
jmp 0x5bdb
callq 0x2450
movq %rax, %rbx
movq %rax, 0x169d1(%rip)     # 0x1c550
jmp 0x5bdb
movq %r14, %rdi
callq 0x20c0
movq %rax, %r15
movq %rbx, %rdi
callq 0x20c0
leaq (%r15,%rax), %rdi
addq $0x2, %rdi
callq 0x2310
movq %rax, %r15
movq %rax, 0x169a5(%rip)     # 0x1c550
movq %rax, %rdi
movq %r14, %rsi
callq 0x21e0
movq %r15, %rdi
callq 0x20c0
movw $0x2f, (%r15,%rax)
movq %r15, %rdi
movq %rbx, %rsi
callq 0x2430
movq %rbx, %rdi
callq 0x2290
movq %r15, %rbx
cmpb $0x2d, (%rbx)
jne 0x5c26
cmpb $0x0, 0x1(%rbx)
jne 0x5c26
pushq $0x1
popq %rbx
movl %ebx, %edi
callq 0x2420
testl %eax, %eax
je 0x5cee
movq 0x63d9(%rip), %r14      # 0xbfd8
movq (%r14), %rsi
pushq $0xa
popq %rbx
movl %ebx, %edi
callq 0x22d0
movq (%r14), %rcx
leaq 0x2e2b(%rip), %rdi      # 0x8a41
pushq $0x30
popq %rsi
pushq $0x1
popq %rdx
callq 0x2480
jmp 0x614c
movq %rbx, %rdi
movl $0xc1, %esi
movl $0x1a4, %edx            # imm = 0x1A4
xorl %eax, %eax
callq 0x2140
testl %eax, %eax
jns 0x5cec
callq 0x2030
cmpl $0x11, (%rax)
jne 0x60df
movq 0x168f9(%rip), %rdi     # 0x1c550
leaq 0x28(%rsp), %rsi
callq 0x21c0
testl %eax, %eax
jne 0x5c75
movl $0xf000, %eax           # imm = 0xF000
andl 0x40(%rsp), %eax
cmpl $0x8000, %eax           # imm = 0x8000
jne 0x5cd1
movq 0x635c(%rip), %r14      # 0xbfd8
movq (%r14), %rax
cmpb $0x1, 0x68a2(%rip)      # 0xc528
je 0x6127
movq 0x168bd(%rip), %rdx     # 0x1c550
leaq 0x2df4(%rip), %rsi      # 0x8a8e
movq %rax, %rdi
xorl %eax, %eax
callq 0x2340
movq 0x6885(%rip), %rdi      # 0xc530
callq 0x23b0
cmpl $0x79, %eax
jne 0x6156
movq 0x6870(%rip), %rdi      # 0xc530
callq 0x23b0
cmpl $0xa, %eax
jne 0x5cb9
movl $0x201, %esi            # imm = 0x201
jmp 0x5cd4
pushq $0x1
popq %rsi
movq 0x16875(%rip), %rdi     # 0x1c550
xorl %edx, %edx
xorl %eax, %eax
callq 0x2140
testl %eax, %eax
js 0x60df
movl %eax, %ebx
leaq 0x2dee(%rip), %rsi      # 0x8ae3
movl %ebx, %edi
callq 0x2220
movq 0x62d5(%rip), %r14      # 0xbfd8
movq (%r14), %rsi
movq %rax, 0x8(%rsp)
testq %rax, %rax
je 0x60ad
leaq 0x2dd2(%rip), %rdi      # 0x8aed
callq 0x2250
movq 0x6811(%rip), %rdi      # 0xc538
callq 0x3673
movq %rax, %r15
movq 0x65c2(%rip), %rdi      # 0xc2f8
leaq 0x2c54(%rip), %rsi      # 0x8991
callq 0x3911
movq %rax, %rdi
callq 0x2150
movq %rax, (%rsp)
andq $0x0, 0x20(%rsp)
leaq 0x164(%rsp), %rdi
pushq $0x1
popq %rsi
callq 0x558a
leaq 0xb8(%rsp), %rdi
xorl %ebx, %ebx
xorl %esi, %esi
callq 0x558a
leaq 0xe90(%rsp), %rdi
pushq $0x1c
popq %rdx
movq %r15, 0x10(%rsp)
movq %r15, %rsi
callq 0x308c
pushq $0x54
popq %rdi
callq 0x2120
movq %rax, %r15
cmpb $0x1, 0x64f9(%rip)      # 0xc298
jne 0x5dbb
movq (%r14), %rdi
leaq 0x2d5f(%rip), %rsi      # 0x8b0a
movl %r15d, %edx
xorl %eax, %eax
callq 0x2340
movb 0x64dd(%rip), %bl       # 0xc298
pushq $0x8
popq %rax
cmpl %eax, %r15d
movl %eax, %ecx
cmovll %r15d, %ecx
testl %r15d, %r15d
cmovlel %eax, %ecx
movl 0x6505(%rip), %eax      # 0xc2d8
cmpl $0x10, %eax
pushq $0x10
popq %r15
cmovll %eax, %r15d
testl %eax, %eax
cmovlel %ecx, %r15d
testb $0x1, %bl
je 0x5dfd
movq (%r14), %rdi
leaq 0x2d2b(%rip), %rsi      # 0x8b1e
movl %r15d, %edx
xorl %eax, %eax
callq 0x2340
addq $-0x20a, (%rsp)         # imm = 0xFDF6
leal -0x1(%r15), %eax
cltq
movq %rax, 0x18(%rsp)
xorl %ebx, %ebx
testl %r15d, %r15d
cmovlel %ebx, %r15d
movl $0xc0, %r12d
leaq 0x290(%rsp), %r13
xorl %ebp, %ebp
cmpq %rbp, %r15
je 0x5eeb
leaq (%rsp,%rbx), %rax
addq $0x290, %rax            # imm = 0x290
leaq (%rax,%r12), %rcx
cmpq 0x18(%rsp), %rbp
cmovgeq %r13, %rcx
movq %rcx, (%rax)
leaq (%rsp,%rbx), %rdi
addq $0x298, %rdi            # imm = 0x298
xorl %esi, %esi
callq 0x3f66
leaq (%rsp,%rbx), %rdi
addq $0x2b8, %rdi            # imm = 0x2B8
xorl %esi, %esi
callq 0x3f66
leaq (%rsp,%rbx), %rdi
addq $0x2d8, %rdi            # imm = 0x2D8
xorl %esi, %esi
callq 0x3f66
leaq (%rsp,%rbx), %r14
addq $0x2f8, %r14            # imm = 0x2F8
movq %r14, %rdi
xorl %esi, %esi
callq 0x3f66
movq (%rsp), %rax
movq %rax, 0x20(%r14)
leaq 0x20(%rsp), %rax
movq %rax, 0x28(%r14)
movq 0x6431(%rip), %rax      # 0xc2e0
movq %rax, 0x30(%r14)
movq 0x8(%rsp), %rax
movq %rax, 0x38(%r14)
leaq 0x164(%rsp), %rax
movq %rax, 0x40(%r14)
leaq 0xb8(%rsp), %rax
movq %rax, 0x48(%r14)
leaq 0xe90(%rsp), %rax
movq %rax, 0x50(%r14)
incq %rbp
addq %r12, %rbx
jmp 0x5e29
xorl %r14d, %r14d
leaq -0x5e2(%rip), %rbx      # 0x5913
cmpq %r14, %r15
je 0x5f19
movq %rbx, %rdi
movq %r13, %rsi
callq 0x3e78
movq %rax, 0x210(%rsp,%r14,8)
incq %r14
addq $0xc0, %r13
jmp 0x5ef5
leaq 0x298(%rsp), %rdi
callq 0x40a8
leaq 0x2b8(%rsp), %rdi
callq 0x40a8
leaq 0x2d8(%rsp), %rdi
callq 0x40a8
leaq 0x2f8(%rsp), %rbx
movq %rbx, %rdi
callq 0x40a8
xorl %r14d, %r14d
cmpq %r14, %r15
je 0x5f6a
movq 0x210(%rsp,%r14,8), %rdi
callq 0x3ef3
incq %r14
jmp 0x5f53
movq 0x6067(%rip), %r14      # 0xbfd8
subq $0x1, %r15
jb 0x5fa3
leaq -0x60(%rbx), %rdi
callq 0x3fcd
leaq -0x40(%rbx), %rdi
callq 0x3fcd
leaq -0x20(%rbx), %rdi
callq 0x3fcd
movq %rbx, %rdi
callq 0x3fcd
addq $0xc0, %rbx
jmp 0x5f71
pushq $0x1
popq %rdi
xorl %esi, %esi
callq 0x3cee
leaq 0x164(%rsp), %rdi
callq 0x56d8
leaq 0xb8(%rsp), %rdi
callq 0x56d8
movq (%r14), %rsi
leaq 0x2b6b(%rip), %rdi      # 0x8b3c
callq 0x2250
movq 0x8(%rsp), %rdi
callq 0x21f0
testl %eax, %eax
jne 0x615e
cmpb $0x1, 0x62b9(%rip)      # 0xc2a8
je 0x6019
movq 0x10(%rsp), %rdi
callq 0x2290
movq 0x1654e(%rip), %rdi     # 0x1c550
callq 0x2290
addq $0x1ed8, %rsp           # imm = 0x1ED8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x6520(%rip), %rdi      # 0xc540
cmpb $0x2d, (%rdi)
jne 0x602b
cmpb $0x0, 0x1(%rdi)
je 0x6099
leaq 0x28(%rsp), %rsi
callq 0x21c0
testl %eax, %eax
jne 0x6099
movl $0xf000, %eax           # imm = 0xF000
andl 0x40(%rsp), %eax
cmpl $0x8000, %eax           # imm = 0x8000
jne 0x6099
movq 0x16500(%rip), %rdi     # 0x1c550
cmpb $0x2d, (%rdi)
jne 0x605b
cmpb $0x0, 0x1(%rdi)
je 0x6099
leaq 0x28(%rsp), %rsi
callq 0x21c0
testl %eax, %eax
jne 0x6099
movl $0xf000, %eax           # imm = 0xF000
andl 0x40(%rsp), %eax
cmpl $0x8000, %eax           # imm = 0x8000
jne 0x6099
movq 0x64c0(%rip), %rdi      # 0xc540
callq 0x20d0
movq (%r14), %rsi
testl %eax, %eax
jne 0x617a
leaq 0x2afe(%rip), %rdi      # 0x8b95
jmp 0x60a3
movq (%r14), %rsi
leaq 0x2b15(%rip), %rdi      # 0x8bb8
callq 0x2250
jmp 0x5ff1
pushq $0xa
popq %rdi
callq 0x22d0
movq (%r14), %rcx
leaq 0x2a27(%rip), %rdi      # 0x8ae6
pushq $0x6
popq %rsi
pushq $0x1
popq %rbx
movq %rbx, %rdx
callq 0x2480
movq (%r14), %rcx
leaq 0x2660(%rip), %rdi      # 0x8737
pushq $0x2
popq %rsi
movq %rbx, %rdx
jmp 0x6119
movq 0x5ef2(%rip), %rbx      # 0xbfd8
movq (%rbx), %rsi
pushq $0xa
popq %rdi
callq 0x22d0
movq (%rbx), %rdi
movq 0x16455(%rip), %rdx     # 0x1c550
leaq 0x29be(%rip), %rsi      # 0x8ac0
xorl %eax, %eax
callq 0x2340
movq (%rbx), %rcx
leaq 0x2624(%rip), %rdi      # 0x8737
pushq $0x2
popq %rsi
pushq $0x1
popq %rdx
callq 0x2480
xorl %edi, %edi
callq 0x2180
jmp 0x6156
pushq $0xa
popq %rbx
movl %ebx, %edi
movq %rax, %rsi
callq 0x22d0
movq (%r14), %rdi
movq 0x16412(%rip), %rdx     # 0x1c550
leaq 0x292d(%rip), %rsi      # 0x8a72
xorl %eax, %eax
callq 0x2340
movq (%r14), %rsi
movl %ebx, %edi
callq 0x22d0
pushq $0x1
popq %rdi
callq 0x22a0
movq (%r14), %rsi
pushq $0xa
popq %rdi
callq 0x22d0
movq (%r14), %rcx
leaq 0x29e9(%rip), %rdi      # 0x8b5c
pushq $0x1f
jmp 0x60c1
pushq $0xa
popq %rdi
callq 0x22d0
movq (%r14), %rcx
leaq 0x29f0(%rip), %rdi      # 0x8b7c
pushq $0x18
jmp 0x60c1