// File: /workspace/github_c_makefile_star2_O1/simh[P]simtools/converters/cosy/cosy.c
// Function: doCompress
int doCompress(
  FILE *dest,
  int argc,
  char *argv[]
)
{
  int status = 0;
  unsigned int i;
  char *filename;
  FILE *src;

  while (argc != 0) {
    filename = argv[0];

    if ((src = fopen(filename, "r")) == NULL) {
      status = 2;
      break;
    }

    /*
     * Build a leading CSY/ card.
     */
    strcpy(card, csy);
    if (strncmp(filename, "deck_", 5) == 0) {
      /*
       * Possible deck name derived from filename.
       */
      if (strlen(filename) <= 11) {
        for (i = 5; i < strlen(filename); i++)
          if (!VALID(filename[i]))
            goto noname;

        strncpy(card, &filename[5], strlen(filename) - 5);
        goto named;
      }
    noname:
      fprintf(stderr, "Unable to use filename (%s) for deck name\n", filename);
    }
  named:

    if ((status = compressCard(dest)) != 0)
      break;

    /*
     * Copy the source file to the destination while compressing.
     */
    while (fgets(card, sizeof(card), src) != NULL) {
      int len = strlen(card);

      if (card[len - 1] == '\n')
        card[len - 1] = '\0';

      if ((status = compressCard(dest)) != 0)
        goto error;
    }

    /*
     * Build a trailing END/ card.
     */
    strcpy(card, end);
    if ((status = compressCard(dest)) != 0)
      break;

    fclose(src);

    argc--, argv++;
  }
 error:

  /*
   * Report possible error.
   */
  switch (status) {
    case 0:
      if ((outcount % RECLEN) != 0)
        fwrite(pad, sizeof(char), outcount % RECLEN, dest);
      break;

    case 2:
      fprintf(stderr, "Failed to open file - %s\n", filename);
      break;

    case 3:
      fprintf(stderr, "Error writing COSY file\n");
      break;

    case 4:
      fprintf(stderr, "Invalid character in file - %s\n", filename);
      break;

    default:
      fprintf(stderr, "Unknown exit status - %u\n", status);
      break;
  }
  fclose(dest);
  return status;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rdi, %rbx
testl %esi, %esi
je 0x17a0
movl %esi, %ebp
leaq 0xb30(%rip), %rsi       # 0x2185
leaq 0x2c74(%rip), %r13      # 0x42d0
leaq 0xb43(%rip), %r15       # 0x21a6
movq %rdx, (%rsp)
movq (%rdx), %r14
movq %r14, %rdi
callq 0x10d0
testq %rax, %rax
je 0x1856
movq %rax, %r12
movq 0x2a1b(%rip), %rsi      # 0x40a0
movq %r13, %rdi
callq 0x1050
movl $0x5, %edx
movq %r14, %rdi
movq %r15, %rsi
callq 0x1040
testl %eax, %eax
jne 0x16f1
movq %r14, %rdi
callq 0x1070
cmpq $0xb, %rax
ja 0x177c
cmpq $0x6, %rax
jb 0x16de
movl $0x5, %ecx
movb (%r14,%rcx), %dl
cmpb $0x26, %dl
je 0x177c
addb $-0x5f, %dl
cmpb $-0x3f, %dl
jb 0x177c
incq %rcx
cmpl %eax, %ecx
jb 0x16be
leaq 0x5(%r14), %rsi
addq $-0x5, %rax
movq %r13, %rdi
movq %rax, %rdx
callq 0x1030
movl %ebp, %r15d
movq %rbx, %rdi
callq 0x125c
testl %eax, %eax
jne 0x179c
movq %r13, %rdi
movl $0x51, %esi
movq %r12, %rdx
callq 0x10b0
testq %rax, %rax
je 0x1733
movq %r13, %rdi
callq 0x1070
decl %eax
cltq
cmpb $0xa, (%rax,%r13)
jne 0x16f4
movb $0x0, (%rax,%r13)
jmp 0x16f4
movq 0x296e(%rip), %rsi      # 0x40a8
movq %r13, %rdi
callq 0x1050
movq %rbx, %rdi
callq 0x125c
testl %eax, %eax
jne 0x179c
movq %r12, %rdi
callq 0x1060
movq (%rsp), %rdx
addq $0x8, %rdx
movl %r15d, %ebp
decl %ebp
leaq 0xa1b(%rip), %rsi       # 0x2185
leaq 0xa35(%rip), %r15       # 0x21a6
jne 0x1663
jmp 0x1860
movq 0x2875(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
leaq 0xa1f(%rip), %rsi       # 0x21ac
movq %r14, %rdx
xorl %eax, %eax
callq 0x10c0
jmp 0x16f1
movl %eax, %ebp
jmp 0x17a2
xorl %ebp, %ebp
testl %ebp, %ebp
jne 0x17fb
movslq 0x2b17(%rip), %rax      # 0x42c4
imulq $0x2aaaaaab, %rax, %rcx # imm = 0x2AAAAAAB
movq %rcx, %rdx
shrq $0x3f, %rdx
shrq $0x26, %rcx
addl %edx, %ecx
shll $0x7, %ecx
leal (%rcx,%rcx,2), %ecx
subl %ecx, %eax
je 0x17e2
movslq %eax, %rdx
leaq 0x2b5b(%rip), %rdi      # 0x4330
movl $0x1, %esi
movq %rbx, %rcx
callq 0x1110
movq %rbx, %rdi
callq 0x1060
movl %ebp, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpl $0x2, %ebp
je 0x181d
cmpl $0x3, %ebp
je 0x183a
movq 0x27ec(%rip), %rax      # 0x3ff8
cmpl $0x4, %ebp
jne 0x1841
movq (%rax), %rdi
leaq 0x9ef(%rip), %rsi       # 0x220a
jmp 0x182e
movq 0x27d4(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
leaq 0x9a9(%rip), %rsi       # 0x21d7
movq %r14, %rdx
xorl %eax, %eax
callq 0x10c0
jmp 0x17e2
callq 0x114e
jmp 0x17e2
movq (%rax), %rdi
leaq 0x9df(%rip), %rsi       # 0x222a
movl %ebp, %edx
xorl %eax, %eax
callq 0x10c0
jmp 0x17e2
movl $0x2, %ebp
jmp 0x17a2
xorl %ebp, %ebp
jmp 0x17a2