// File: /workspace/github_c_makefile_star2_O2/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
subq $0x18, %rsp
movq %rdx, %rbp
movq %rdi, %rbx
leaq 0x2ce1(%rip), %r13      # 0x42d0
pushq $0x51
popq %r12
subl $0x1, %esi
jb 0x1740
movl %esi, 0xc(%rsp)
movq (%rbp), %r14
movq %r14, %rdi
leaq 0xcfb(%rip), %rsi       # 0x2309
callq 0x10d0
testq %rax, %rax
je 0x176b
movq %rax, %r15
movq 0x2a7a(%rip), %rsi      # 0x40a0
movq %r13, %rdi
callq 0x1050
movq %r14, %rdi
leaq 0xcf2(%rip), %rsi       # 0x232a
pushq $0x5
popq %rdx
callq 0x1040
testl %eax, %eax
jne 0x169d
movq %r14, %rdi
callq 0x1070
cmpq $0xb, %rax
ja 0x166d
pushq $0x5
popq %rcx
cmpl %eax, %ecx
jae 0x168a
movb (%r14,%rcx), %dl
cmpb $0x26, %dl
je 0x166d
addb $-0x5f, %dl
incq %rcx
cmpb $-0x3f, %dl
jae 0x1655
movq 0x2984(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
leaq 0xcb2(%rip), %rsi       # 0x2330
movq %r14, %rdx
xorl %eax, %eax
callq 0x10c0
jmp 0x169d
leaq 0x5(%r14), %rsi
addq $-0x5, %rax
movq %r13, %rdi
movq %rax, %rdx
callq 0x1030
movq %rbp, 0x10(%rsp)
movq %rbx, %rdi
callq 0x121c
testl %eax, %eax
jne 0x1710
movq %r13, %rdi
movl %r12d, %esi
movq %r15, %rdx
callq 0x10b0
testq %rax, %rax
je 0x16db
movq %r13, %rdi
callq 0x1070
decl %eax
cltq
cmpb $0xa, (%rax,%r13)
jne 0x16a2
movb $0x0, (%rax,%r13)
jmp 0x16a2
movq 0x29c6(%rip), %rsi      # 0x40a8
movq %r13, %rdi
callq 0x1050
movq %rbx, %rdi
callq 0x121c
testl %eax, %eax
jne 0x1710
movq %r15, %rdi
callq 0x1060
movq 0x10(%rsp), %rbp
addq $0x8, %rbp
movl 0xc(%rsp), %esi
jmp 0x15f3
movl %eax, %ebp
cmpl $0x2, %eax
je 0x176b
movq 0x28da(%rip), %rax      # 0x3ff8
cmpl $0x3, %ebp
je 0x178a
cmpl $0x4, %ebp
jne 0x17a4
movq (%rax), %rdi
leaq 0xc5c(%rip), %rsi       # 0x238e
movq %r14, %rdx
xorl %eax, %eax
callq 0x10c0
pushq $0x4
jmp 0x17a1
movl 0x2b7e(%rip), %eax      # 0x42c4
movl $0x180, %ecx            # imm = 0x180
cltd
idivl %ecx
xorl %ebp, %ebp
testl %edx, %edx
je 0x17b7
movslq %edx, %rdx
leaq 0x2bd2(%rip), %rdi      # 0x4330
pushq $0x1
popq %rsi
movq %rbx, %rcx
callq 0x1110
jmp 0x17b7
movq 0x2886(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
leaq 0xbdf(%rip), %rsi       # 0x235b
movq %r14, %rdx
xorl %eax, %eax
callq 0x10c0
pushq $0x2
jmp 0x17a1
movq (%rax), %rcx
leaq 0xbe1(%rip), %rdi       # 0x2375
pushq $0x18
popq %rsi
pushq $0x1
popq %rdx
callq 0x1110
pushq $0x3
popq %rbp
jmp 0x17b7
movq (%rax), %rdi
leaq 0xc00(%rip), %rsi       # 0x23ae
movl %ebp, %edx
xorl %eax, %eax
callq 0x10c0
movq %rbx, %rdi
callq 0x1060
movl %ebp, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq