// File: /workspace/github_c_makefile_star2_O3/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 0x175a
movl %esi, %r15d
leaq 0x2c93(%rip), %r13      # 0x42d0
movq %rdx, (%rsp)
movq (%rdx), %r14
movq %r14, %rdi
leaq 0xb37(%rip), %rsi       # 0x2185
callq 0x10d0
testq %rax, %rax
je 0x17d1
movq %rax, %r12
movq 0x2a3a(%rip), %rsi      # 0x40a0
movq %r13, %rdi
callq 0x1050
movl $0x5, %edx
movq %r14, %rdi
leaq 0xb29(%rip), %rsi       # 0x21a6
callq 0x1040
testl %eax, %eax
jne 0x1701
movq %r14, %rdi
callq 0x1070
cmpq $0xb, %rax
ja 0x16cc
cmpq $0x6, %rax
jb 0x16b7
movl $0x5, %ecx
movb (%r14,%rcx), %dl
cmpb $0x26, %dl
je 0x16cc
addb $-0x5f, %dl
cmpb $-0x3f, %dl
jb 0x16cc
incq %rcx
cmpl %eax, %ecx
jb 0x169f
leaq 0x5(%r14), %rsi
addq $-0x5, %rax
movq %r13, %rdi
movq %rax, %rdx
callq 0x1030
jmp 0x1701
movq 0x2925(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
leaq 0xacf(%rip), %rsi       # 0x21ac
movq %r14, %rdx
xorl %eax, %eax
callq 0x10c0
jmp 0x1701
movq %r13, %rdi
callq 0x1070
decl %eax
cltq
cmpb $0xa, (%rax,%r13)
jne 0x1701
movb $0x0, (%rax,%r13)
movq %rbx, %rdi
callq 0x125c
testl %eax, %eax
jne 0x179e
movq %r13, %rdi
movl $0x51, %esi
movq %r12, %rdx
callq 0x10b0
testq %rax, %rax
jne 0x16e9
movq 0x297b(%rip), %rsi      # 0x40a8
movq %r13, %rdi
callq 0x1050
movq %rbx, %rdi
callq 0x125c
testl %eax, %eax
jne 0x179e
movq %r12, %rdi
callq 0x1060
movq (%rsp), %rdx
addq $0x8, %rdx
decl %r15d
jne 0x163d
movslq 0x2b63(%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
xorl %ebp, %ebp
subl %ecx, %eax
je 0x1812
movslq %eax, %rdx
leaq 0x2ba1(%rip), %rdi      # 0x4330
movl $0x1, %esi
movq %rbx, %rcx
callq 0x1110
jmp 0x1812
movl %eax, %ebp
cmpl $0x2, %eax
je 0x17d1
cmpl $0x3, %ebp
je 0x17f3
movq 0x2847(%rip), %rax      # 0x3ff8
cmpl $0x4, %ebp
jne 0x17ff
movq (%rax), %rdi
leaq 0xa4a(%rip), %rsi       # 0x220a
movq %r14, %rdx
xorl %eax, %eax
callq 0x10c0
movl $0x4, %ebp
jmp 0x1812
movq 0x2820(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
leaq 0x9f5(%rip), %rsi       # 0x21d7
movq %r14, %rdx
xorl %eax, %eax
callq 0x10c0
movl $0x2, %ebp
jmp 0x1812
callq 0x114e
movl $0x3, %ebp
jmp 0x1812
movq (%rax), %rdi
leaq 0xa21(%rip), %rsi       # 0x222a
movl %ebp, %edx
xorl %eax, %eax
callq 0x10c0
movq %rbx, %rdi
callq 0x1060
movl %ebp, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq