// File: /workspace/github_c_makefile_star2_O3/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 $0x1ef8, %rsp           # imm = 0x1EF8
movq 0x6e10(%rip), %rdi      # 0xd2d0
testq %rdi, %rdi
jne 0x64f0
movq 0x6e2c(%rip), %rdi      # 0xd2f8
leaq 0x2fcb(%rip), %rsi      # 0x949e
callq 0x4f51
movq %rax, %rbx
movq 0x6de6(%rip), %r14      # 0xd2c8
testq %r14, %r14
jne 0x6501
movq %rbx, 0x17062(%rip)     # 0x1d550
jmp 0x655b
callq 0x2450
movq %rax, %rbx
movq %rax, 0x17051(%rip)     # 0x1d550
jmp 0x655b
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, 0x17025(%rip)     # 0x1d550
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 0x6582
cmpb $0x0, 0x1(%rbx)
jne 0x6582
movl $0x1, %ebx
movl $0x1, %edi
callq 0x2420
testl %eax, %eax
je 0x660d
callq 0x39a3
movq %rbx, %rdi
movl $0xc1, %esi
movl $0x1a4, %edx            # imm = 0x1A4
xorl %eax, %eax
callq 0x2140
testl %eax, %eax
jns 0x660b
callq 0x2030
cmpl $0x11, (%rax)
jne 0x6ab0
movq 0x16fa1(%rip), %rdi     # 0x1d550
leaq 0x48(%rsp), %rsi
callq 0x21c0
testl %eax, %eax
jne 0x65d2
movl $0xf000, %eax           # imm = 0xF000
andl 0x60(%rsp), %eax
movl $0x1, %esi
cmpl $0x8000, %eax           # imm = 0x8000
jne 0x65f3
movq 0x69ff(%rip), %rax      # 0xcfd8
movq (%rax), %rdi
cmpb $0x1, 0x6f45(%rip)      # 0xd528
je 0x6ab5
callq 0x38d0
movl $0x201, %esi            # imm = 0x201
movq 0x16f56(%rip), %rdi     # 0x1d550
xorl %edx, %edx
xorl %eax, %eax
callq 0x2140
testl %eax, %eax
js 0x6ab0
movl %eax, %ebx
leaq 0x359f(%rip), %rsi      # 0x9bb3
movl %ebx, %edi
callq 0x2220
movq 0x69b6(%rip), %r13      # 0xcfd8
movq (%r13), %rsi
testq %rax, %rax
je 0x6aa8
movq %rax, %rbp
leaq 0x3584(%rip), %rdi      # 0x9bbd
callq 0x2250
movq 0x6ef3(%rip), %rdi      # 0xd538
callq 0x4c94
movq %rax, %r14
movq 0x6ca4(%rip), %rdi      # 0xd2f8
leaq 0x3406(%rip), %rsi      # 0x9a61
callq 0x4f51
movq %rax, %rdi
callq 0x2150
movq %rax, %r12
movq $0x0, 0x40(%rsp)
leaq 0x184(%rsp), %rdi
movl $0x1, %esi
callq 0x6196
leaq 0xd8(%rsp), %rdi
xorl %ebx, %ebx
xorl %esi, %esi
callq 0x6196
leaq 0xeb0(%rsp), %rdi
movl $0x1c, %edx
movq %r14, 0x20(%rsp)
movq %r14, %rsi
callq 0x46fc
movl $0x54, %edi
callq 0x2120
movq %rax, %r15
cmpb $0x1, 0x6bd3(%rip)      # 0xd298
jne 0x66e2
movq (%r13), %rdi
leaq 0x3508(%rip), %rsi      # 0x9bda
movl %r15d, %edx
xorl %eax, %eax
callq 0x2340
movb 0x6bb6(%rip), %bl       # 0xd298
movl $0x8, %eax
cmpl %eax, %r15d
movl $0x8, %ecx
cmovll %r15d, %ecx
testl %r15d, %r15d
cmovlel %eax, %ecx
movl 0x6bd9(%rip), %eax      # 0xd2d8
cmpl $0x10, %eax
movl $0x10, %r14d
cmovll %eax, %r14d
testl %eax, %eax
cmovlel %ecx, %r14d
testb $0x1, %bl
je 0x672c
movq (%r13), %rdi
leaq 0x34cc(%rip), %rsi      # 0x9bee
movl %r14d, %edx
xorl %eax, %eax
callq 0x2340
testl %r14d, %r14d
jle 0x696e
movq %r12, %rcx
addq $-0x20a, %rcx           # imm = 0xFDF6
leal -0x1(%r14), %eax
movq %rax, 0x28(%rsp)
movq %r14, (%rsp)
movl %r14d, %eax
movq %rax, 0x8(%rsp)
movq %rbp, 0x10(%rsp)
movl $0xb8, %ebp
movl $0x68, %eax
movq %rax, 0x18(%rsp)
movl $0x48, %r12d
movl $0x28, %r14d
movl $0x8, %r13d
xorl %r15d, %r15d
movl $0xc0, %ebx
movq %rcx, 0x30(%rsp)
movq %r12, 0x38(%rsp)
cmpq 0x28(%rsp), %r15
leaq 0x2b0(%rsp,%rbx), %rax
leaq 0x2b0(%rsp), %rcx
cmovaeq %rcx, %rax
movq %rax, 0x1f8(%rsp,%rbp)
leaq (%rsp,%r13), %rdi
addq $0x2b0, %rdi            # imm = 0x2B0
xorl %esi, %esi
callq 0x52d9
leaq (%rsp,%r14), %rdi
addq $0x2b0, %rdi            # imm = 0x2B0
xorl %esi, %esi
callq 0x52d9
leaq (%rsp,%r12), %rdi
addq $0x2b0, %rdi            # imm = 0x2B0
xorl %esi, %esi
callq 0x52d9
movq 0x18(%rsp), %r12
leaq (%rsp,%r12), %rdi
addq $0x2b0, %rdi            # imm = 0x2B0
xorl %esi, %esi
callq 0x52d9
movq 0x30(%rsp), %rcx
movq %rcx, 0x280(%rsp,%rbp)
leaq 0x40(%rsp), %rax
movq %rax, 0x288(%rsp,%rbp)
movq 0x6ac5(%rip), %rax      # 0xd2e0
movq %rax, 0x290(%rsp,%rbp)
movq 0x10(%rsp), %rax
movq %rax, 0x298(%rsp,%rbp)
leaq 0x184(%rsp), %rax
movq %rax, 0x2a0(%rsp,%rbp)
leaq 0xd8(%rsp), %rax
movq %rax, 0x2a8(%rsp,%rbp)
leaq 0xeb0(%rsp), %rax
movq %rax, 0x2b0(%rsp,%rbp)
incq %r15
movl $0xc0, %eax
addq %rax, %rbp
addq %rax, %r12
movq %r12, 0x18(%rsp)
movq 0x38(%rsp), %r12
addq %rax, %r12
addq %rax, %r14
addq %rax, %r13
addq %rax, %rbx
cmpq %r15, 0x8(%rsp)
jne 0x6787
leaq 0x2b0(%rsp), %r12
movq 0x8(%rsp), %r15
cmpl $0x0, (%rsp)
movq 0x6731(%rip), %r13      # 0xcfd8
movq 0x10(%rsp), %rbp
jle 0x696e
xorl %r14d, %r14d
leaq -0x5d1(%rip), %rbx      # 0x62eb
movq %rbx, %rdi
movq %r12, %rsi
callq 0x5279
movq %rax, 0x230(%rsp,%r14,8)
incq %r14
addq $0xc0, %r12
cmpq %r14, %r15
jne 0x68bc
leaq 0x2b8(%rsp), %rdi
callq 0x5325
leaq 0x2d8(%rsp), %rdi
callq 0x5325
leaq 0x2f8(%rsp), %rdi
callq 0x5325
leaq 0x318(%rsp), %rbx
movq %rbx, %rdi
callq 0x5325
cmpl $0x0, (%rsp)
jle 0x69a2
xorl %r14d, %r14d
movq 0x230(%rsp,%r14,8), %rdi
callq 0x52ad
incq %r14
cmpq %r14, %r15
jne 0x6922
cmpl $0x0, (%rsp)
jle 0x69a2
leaq -0x60(%rbx), %rdi
callq 0x52ee
leaq -0x40(%rbx), %rdi
callq 0x52ee
leaq -0x20(%rbx), %rdi
callq 0x52ee
movq %rbx, %rdi
callq 0x52ee
addq $0xc0, %rbx
decq %r15
jne 0x693d
jmp 0x69a2
leaq 0x2b8(%rsp), %rdi
callq 0x5325
leaq 0x2d8(%rsp), %rdi
callq 0x5325
leaq 0x2f8(%rsp), %rdi
callq 0x5325
leaq 0x318(%rsp), %rdi
callq 0x5325
movl $0x1, %edi
xorl %esi, %esi
callq 0x513d
leaq 0x184(%rsp), %rdi
callq 0x6288
leaq 0xd8(%rsp), %rdi
callq 0x6288
movq (%r13), %rsi
leaq 0x3239(%rip), %rdi      # 0x9c0c
callq 0x2250
movq %rbp, %rdi
callq 0x21f0
testl %eax, %eax
jne 0x6aba
cmpb $0x1, 0x68b9(%rip)      # 0xd2a8
je 0x6a19
movq 0x20(%rsp), %rdi
callq 0x2290
movq 0x16b4e(%rip), %rdi     # 0x1d550
callq 0x2290
addq $0x1ef8, %rsp           # imm = 0x1EF8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x6b20(%rip), %rdi      # 0xd540
cmpb $0x2d, (%rdi)
jne 0x6a2b
cmpb $0x0, 0x1(%rdi)
je 0x6a9e
leaq 0x48(%rsp), %rsi
callq 0x21c0
testl %eax, %eax
jne 0x6a9e
movl $0xf000, %eax           # imm = 0xF000
andl 0x60(%rsp), %eax
cmpl $0x8000, %eax           # imm = 0x8000
jne 0x6a9e
movq 0x16b00(%rip), %rdi     # 0x1d550
cmpb $0x2d, (%rdi)
jne 0x6a5b
cmpb $0x0, 0x1(%rdi)
je 0x6a9e
leaq 0x48(%rsp), %rsi
callq 0x21c0
testl %eax, %eax
jne 0x6a9e
movl $0xf000, %eax           # imm = 0xF000
andl 0x60(%rsp), %eax
cmpl $0x8000, %eax           # imm = 0x8000
jne 0x6a9e
movq 0x6ac0(%rip), %rdi      # 0xd540
callq 0x20d0
movq (%r13), %rsi
testl %eax, %eax
jne 0x6abf
leaq 0x31d1(%rip), %rdi      # 0x9c65
callq 0x2250
jmp 0x69f1
callq 0x3a86
jmp 0x69f1
movq %rsi, %rdi
callq 0x3aa2
callq 0x3912
callq 0x3961
callq 0x39e2
movq %rsi, %rdi
callq 0x3a34