// File: /workspace/github_c_makefile_star2_O1/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 0x6056(%rip), %rdi      # 0xc2c0
testq %rdi, %rdi
jne 0x629a
movq 0x6072(%rip), %rdi      # 0xc2e8
leaq 0x2151(%rip), %rsi      # 0x83ce
callq 0x4d54
movq %rax, %rbx
movq 0x602c(%rip), %r14      # 0xc2b8
testq %r14, %r14
jne 0x62a8
movq %rbx, 0x162a8(%rip)     # 0x1c540
jmp 0x62ff
callq 0x2440
movq %rax, 0x1629a(%rip)     # 0x1c540
jmp 0x62ff
movq %r14, %rdi
callq 0x20c0
movq %rax, %r15
movq %rbx, %rdi
callq 0x20c0
leaq (%r15,%rax), %rdi
addq $0x2, %rdi
callq 0x2300
movq %rax, %r15
movq %rax, 0x1626e(%rip)     # 0x1c540
movq %rax, %rdi
movq %r14, %rsi
callq 0x21d0
movq %r15, %rdi
callq 0x20c0
movw $0x2f, (%r15,%rax)
movq %r15, %rdi
movq %rbx, %rsi
callq 0x2420
movq %rbx, %rdi
callq 0x2280
movq 0x1623a(%rip), %rbx     # 0x1c540
leaq 0x2594(%rip), %rsi      # 0x88a1
movq %rbx, %rdi
callq 0x2320
testl %eax, %eax
jne 0x6331
movl $0x1, %ebx
movl $0x1, %edi
callq 0x2410
testl %eax, %eax
je 0x6347
callq 0x36dc
movq %rbx, %rdi
movl $0xc1, %esi
movl $0x1a4, %edx            # imm = 0x1A4
xorl %eax, %eax
callq 0x2130
movl %eax, %ebx
testl %ebx, %ebx
jns 0x63b2
callq 0x2030
cmpl $0x11, (%rax)
jne 0x63b2
movq 0x161e4(%rip), %rdi     # 0x1c540
leaq 0x48(%rsp), %rsi
callq 0x21b0
testl %eax, %eax
jne 0x637f
movl $0xf000, %eax           # imm = 0xF000
andl 0x60(%rsp), %eax
movl $0x1, %esi
cmpl $0x8000, %eax           # imm = 0x8000
jne 0x63a0
movq 0x5c52(%rip), %rax      # 0xbfd8
movq (%rax), %rdi
cmpb $0x1, 0x6188(%rip)      # 0xc518
je 0x6835
callq 0x371b
movl $0x201, %esi            # imm = 0x201
movq 0x16199(%rip), %rdi     # 0x1c540
xorl %edx, %edx
xorl %eax, %eax
callq 0x2130
movl %eax, %ebx
testl %ebx, %ebx
js 0x6828
leaq 0x2724(%rip), %rsi      # 0x8ae5
movl %ebx, %edi
callq 0x2210
movq 0x5c09(%rip), %r14      # 0xbfd8
movq (%r14), %rsi
testq %rax, %rax
je 0x682d
movq %rax, %r12
leaq 0x270a(%rip), %rdi      # 0x8aef
callq 0x2240
movq 0x6137(%rip), %rdi      # 0xc528
callq 0x4a90
movq %rax, %rbx
movq 0x5ee8(%rip), %rdi      # 0xc2e8
leaq 0x258c(%rip), %rsi      # 0x8993
callq 0x4d54
movq %rax, %rdi
callq 0x2140
movq %rax, %r13
movq $0x0, 0x40(%rsp)
leaq 0x184(%rsp), %rdi
movl $0x1, %esi
callq 0x5f40
leaq 0xd8(%rsp), %rdi
xorl %esi, %esi
callq 0x5f40
leaq 0xeb0(%rsp), %rdi
movl $0x1c, %edx
movq %rbx, 0x18(%rsp)
movq %rbx, %rsi
callq 0x44e4
movl $0x54, %edi
callq 0x2120
movq %rax, %r15
cmpb $0x1, 0x5e19(%rip)      # 0xc288
jne 0x6485
movq (%r14), %rdi
leaq 0x2691(%rip), %rsi      # 0x8b0c
movl %r15d, %edx
xorl %eax, %eax
callq 0x2330
movq %r12, 0x8(%rsp)
movl $0x8, %eax
cmpl %eax, %r15d
movl $0x8, %ecx
cmovll %r15d, %ecx
testl %r15d, %r15d
cmovlel %eax, %ecx
movl 0x5e21(%rip), %eax      # 0xc2c8
cmpl $0x10, %eax
movl $0x10, %ebx
cmovll %eax, %ebx
testl %eax, %eax
cmovlel %ecx, %ebx
cmpb $0x1, 0x5dca(%rip)      # 0xc288
jne 0x64d3
movq (%r14), %rdi
leaq 0x2656(%rip), %rsi      # 0x8b20
movl %ebx, %edx
xorl %eax, %eax
callq 0x2330
movl %ebx, %edx
movq %rbx, (%rsp)
testl %ebx, %ebx
jle 0x6636
movq %r13, %rcx
addq $-0x20a, %rcx           # imm = 0xFDF6
movq (%rsp), %rax
decl %eax
cltq
movq %rax, 0x20(%rsp)
movl $0xb8, %ebp
movl $0x68, %eax
movq %rax, 0x10(%rsp)
movl $0x48, %r14d
movl $0x28, %r12d
movl $0x8, %r13d
xorl %r15d, %r15d
movl $0xc0, %ebx
movq %rcx, 0x30(%rsp)
movq %rdx, 0x28(%rsp)
movq %r14, 0x38(%rsp)
cmpq 0x20(%rsp), %r15
leaq 0x2b0(%rsp,%rbx), %rax
leaq 0x2b0(%rsp), %rcx
cmovgeq %rcx, %rax
movq %rax, 0x1f8(%rsp,%rbp)
leaq (%rsp,%r13), %rdi
addq $0x2b0, %rdi            # imm = 0x2B0
xorl %esi, %esi
callq 0x50d6
leaq (%rsp,%r12), %rdi
addq $0x2b0, %rdi            # imm = 0x2B0
xorl %esi, %esi
callq 0x50d6
leaq (%rsp,%r14), %rdi
addq $0x2b0, %rdi            # imm = 0x2B0
xorl %esi, %esi
callq 0x50d6
movq 0x10(%rsp), %r14
leaq (%rsp,%r14), %rdi
addq $0x2b0, %rdi            # imm = 0x2B0
xorl %esi, %esi
callq 0x50d6
leaq 0x184(%rsp), %rdi
movq 0x8(%rsp), %rsi
movq 0x28(%rsp), %rdx
movq 0x30(%rsp), %rcx
movq %rcx, 0x280(%rsp,%rbp)
leaq 0x40(%rsp), %rax
movq %rax, 0x288(%rsp,%rbp)
movq 0x5cff(%rip), %rax      # 0xc2d0
movq %rax, 0x290(%rsp,%rbp)
movq %rsi, 0x298(%rsp,%rbp)
movq %rdi, 0x2a0(%rsp,%rbp)
leaq 0xeb0(%rsp), %rdi
leaq 0xd8(%rsp), %rax
movq %rax, 0x2a8(%rsp,%rbp)
movq %rdi, 0x2b0(%rsp,%rbp)
incq %r15
movl $0xc0, %eax
addq %rax, %rbp
addq %rax, %r14
movq %r14, 0x10(%rsp)
movq 0x38(%rsp), %r14
addq %rax, %r14
addq %rax, %r12
addq %rax, %r13
addq %rax, %rbx
cmpq %r15, %rdx
jne 0x652b
movq (%rsp), %r15
testl %r15d, %r15d
movq %rdx, %r13
jle 0x6676
leaq 0x2b0(%rsp), %rbx
xorl %r14d, %r14d
leaq -0x5bf(%rip), %r12      # 0x6095
movq %r12, %rdi
movq %rbx, %rsi
callq 0x5076
movq %rax, 0x230(%rsp,%r14,8)
incq %r14
addq $0xc0, %rbx
cmpq %r14, %r13
jne 0x6654
leaq 0x2b8(%rsp), %rdi
callq 0x512b
leaq 0x2d8(%rsp), %rdi
callq 0x512b
leaq 0x2f8(%rsp), %rdi
callq 0x512b
leaq 0x318(%rsp), %rbx
movq %rbx, %rdi
callq 0x512b
testl %r15d, %r15d
jle 0x66ca
xorl %r14d, %r14d
movq 0x230(%rsp,%r14,8), %rdi
callq 0x50aa
incq %r14
cmpq %r14, %r13
jne 0x66b5
testl %r15d, %r15d
movq 0x8(%rsp), %r15
jle 0x6703
leaq -0x60(%rbx), %rdi
callq 0x50eb
leaq -0x40(%rbx), %rdi
callq 0x50eb
leaq -0x20(%rbx), %rdi
callq 0x50eb
movq %rbx, %rdi
callq 0x50eb
addq $0xc0, %rbx
decq %r13
jne 0x66d4
movl $0x1, %edi
xorl %esi, %esi
callq 0x4f42
leaq 0x184(%rsp), %rdi
callq 0x6032
leaq 0xd8(%rsp), %rdi
callq 0x6032
movq 0x58a8(%rip), %r14      # 0xbfd8
movq (%r14), %rsi
leaq 0x2404(%rip), %rdi      # 0x8b3e
callq 0x2240
movq %r15, %rdi
callq 0x21e0
testl %eax, %eax
jne 0x683a
cmpb $0x1, 0x5b42(%rip)      # 0xc298
je 0x6780
movq 0x18(%rsp), %rdi
callq 0x2280
movq 0x15dd7(%rip), %rdi     # 0x1c540
callq 0x2280
addq $0x1ef8, %rsp           # imm = 0x1EF8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x5da9(%rip), %rbx      # 0xc530
leaq 0x2113(%rip), %rsi      # 0x88a1
movq %rbx, %rdi
callq 0x2320
testl %eax, %eax
je 0x681e
leaq 0x48(%rsp), %rsi
movq %rbx, %rdi
callq 0x21b0
testl %eax, %eax
jne 0x681e
movl $0xf000, %eax           # imm = 0xF000
andl 0x60(%rsp), %eax
cmpl $0x8000, %eax           # imm = 0x8000
jne 0x681e
movq 0x15d7a(%rip), %rbx     # 0x1c540
leaq 0x20d4(%rip), %rsi      # 0x88a1
movq %rbx, %rdi
callq 0x2320
testl %eax, %eax
je 0x681e
leaq 0x48(%rsp), %rsi
movq %rbx, %rdi
callq 0x21b0
testl %eax, %eax
jne 0x681e
movl $0xf000, %eax           # imm = 0xF000
andl 0x60(%rsp), %eax
cmpl $0x8000, %eax           # imm = 0x8000
jne 0x681e
movq 0x5d2f(%rip), %rdi      # 0xc530
callq 0x20d0
movq (%r14), %rsi
testl %eax, %eax
jne 0x683f
leaq 0x2383(%rip), %rdi      # 0x8b97
callq 0x2240
jmp 0x6758
callq 0x3801
jmp 0x6758
callq 0x386f
movq %rsi, %rdi
callq 0x381d
callq 0x38be
callq 0x375d
movq %rsi, %rdi
callq 0x37af