// File: /workspace/github_c_makefile_star2_O3/simh[P]simtools/extracters/dbtap/dos11.c
// Function: appendFile
int appendFile(
  char *name,
  char *ascii,
  uint8 prog,
  uint8 proj,
  int reclen,
  int strict
)
{
  size_t hdrSz = strict ? sizeof(struct dos11hdr1) : sizeof(struct dos11hdr2);
  struct dos11hdr2 hdr;
  char *fname, *ext, *useAscii;
  time_t now;
  struct tm *tm;

  memset(&hdr, 0, sizeof(hdr));

  if ((file = fopen(name, "r")) != NULL) {
    char filename[16], extension[8], exten[8];
    int i, len;
    size_t datalen;

    /*
     * We now need to convert the supplied filename into something that
     * fits into the DOS/BATCH-11 file header structure(s).
     */
    if ((fname = strrchr(name, '/')) == NULL)
      fname = name;

    ext = strrchr(fname, '.');

    /*
     * Fill int the first 'n' bytes of the filename and extension with the
     * remaining bytes set to ' '.
     */
    memset(&filename, ' ', sizeof(filename));
    memset(&extension, ' ', sizeof(extension));
    memset(&exten, 0, sizeof(exten));

    if (ext != NULL)
      len = ext - fname;
    else len = strlen(fname);

    if (len > (strict ? 6 : 9))
      len = strict ? 6 : 9;

    for (i = 0; i < len; i++)
      filename[i] = toupper(fname[i]);

    if (ext != NULL) {
      len = strlen(&ext[1]);
      if (len > 3)
        len = 3;

      for (i = 0; i < len + 1; i++) {
        extension[i] = toupper(ext[i]);
        exten[i] = toupper(ext[i]);
      }
    }

    /*
     * Construct the directory entry
     */
    hdr.fname[0] = AscR50(&filename[0]);
    hdr.fname[1] = AscR50(&filename[3]);
    if (!strict)
      hdr.fname3 = AscR50(&filename[6]);
    if (ext != NULL)
      hdr.ext = AscR50(&extension[1]);

    hdr.proj = proj;
    hdr.prog = prog;
    hdr.prot = 0233;

    now = time(NULL);
    tm = localtime(&now);

    hdr.date = ((tm->tm_year - 70) * 1000) + tm->tm_yday + 1;

    useAscii = strstr(ascii, exten);

    if (WriteTapeRecord(&hdr, hdrSz) == 0) {
      initTapeBuffering(reclen);
      while ((datalen = fread(record, sizeof(char), reclen, file)) != 0) {
        if (ferror(file))
          goto failed;

        if (useAscii != NULL) {
          size_t j;

          for (j = 0; j < datalen; i++) {
            if (record[j] == '\n')
              if (writeTapeBuffering('\r') != 0)
                goto failed;
            if (writeTapeBuffering(record[j]) != 0)
              goto failed;
          }
        } else {
          if (WriteTapeRecord(record, datalen) != 0)
            goto failed;
        }
      }
      if (flushTapeBuffering() != 0)
        goto failed;

      if ((WriteTapeMark(0) == 0) && (WriteTapeMark(1) == 0)) {
        fclose(file);
        return 0;
      }
    }
  failed:
    fclose(file);
    return -1;
  }
  return 1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movl %r9d, %r12d
movl %r8d, %ebx
movl %ecx, %r15d
movl %edx, %ebp
movq %rsi, %r13
movq %rdi, %r14
movq $0x0, 0x16(%rsp)
movw $0x0, 0x1e(%rsp)
leaq 0x135c(%rip), %rsi      # 0x34ba
callq 0x1190
movq %rax, 0x130c6(%rip)     # 0x15230
testq %rax, %rax
je 0x21d0
movq %r13, 0x48(%rsp)
movl %ebx, 0xc(%rsp)
movq %r14, %rdi
movl $0x2f, %esi
callq 0x1100
movq %rax, %rbx
testq %rax, %rax
cmoveq %r14, %rbx
movq %rbx, %rdi
movl $0x2e, %esi
callq 0x1100
movq %rax, %r14
movaps 0x12fa(%rip), %xmm0     # 0x34a0
movaps %xmm0, 0x30(%rsp)
movabsq $0x2020202020202020, %rax # imm = 0x2020202020202020
movq %rax, 0x28(%rsp)
movq $0x0, 0x20(%rsp)
testq %r14, %r14
je 0x21da
movq %r14, %r13
subq %rbx, %r13
jmp 0x21e5
movl $0x1, %ebx
jmp 0x2323
movq %rbx, %rdi
callq 0x10c0
movq %rax, %r13
testl %r13d, %r13d
jle 0x221d
xorl %eax, %eax
testl %r12d, %r12d
sete %al
leal (%rax,%rax,2), %eax
addl $0x6, %eax
cmpl %r13d, %eax
cmovbl %eax, %r13d
callq 0x1030
movq (%rax), %rax
xorl %ecx, %ecx
movsbq (%rbx,%rcx), %rdx
movb (%rax,%rdx,4), %dl
movb %dl, 0x30(%rsp,%rcx)
incq %rcx
cmpq %rcx, %r13
jne 0x2209
testq %r14, %r14
je 0x225e
leaq 0x1(%r14), %rdi
callq 0x10c0
testl %eax, %eax
js 0x225e
cmpl $0x3, %eax
movl $0x3, %ebx
cmovbl %eax, %ebx
callq 0x1030
movq (%rax), %rax
incl %ebx
xorl %ecx, %ecx
movsbq (%r14,%rcx), %rdx
movb (%rax,%rdx,4), %dl
movb %dl, 0x28(%rsp,%rcx)
movb %dl, 0x20(%rsp,%rcx)
incq %rcx
cmpq %rcx, %rbx
jne 0x2246
leaq 0x30(%rsp), %rdi
callq 0x23f6
movw %ax, 0x12(%rsp)
leaq 0x33(%rsp), %rdi
callq 0x23f6
movw %ax, 0x14(%rsp)
testl %r12d, %r12d
jne 0x2290
leaq 0x36(%rsp), %rdi
callq 0x23f6
movw %ax, 0x1e(%rsp)
testq %r14, %r14
je 0x22a4
leaq 0x29(%rsp), %rdi
callq 0x23f6
movw %ax, 0x16(%rsp)
xorl %r13d, %r13d
testl %r12d, %r12d
sete %r13b
leaq 0x12(%rsp), %r14
movb %r15b, 0x6(%r14)
movb %bpl, 0x7(%r14)
movw $0x9b, 0x8(%r14)
xorl %edi, %edi
callq 0x1140
leaq 0x50(%rsp), %rdi
movq %rax, (%rdi)
callq 0x1050
imull $0x3e8, 0x14(%rax), %ecx # imm = 0x3E8
movl 0x1c(%rax), %eax
addl %ecx, %eax
addl $0xffffee91, %eax       # imm = 0xFFFFEE91
movw %ax, 0xa(%r14)
leaq 0x20(%rsp), %rsi
movq 0x48(%rsp), %rdi
callq 0x1200
movq %rax, %rbx
leal 0xc(,%r13,2), %esi
movq %r14, %rdi
callq 0x1fd1
testl %eax, %eax
je 0x2334
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
movq 0x12f12(%rip), %rdi     # 0x15230
callq 0x10b0
movl %ebx, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl 0xc(%rsp), %ebp
movl %ebp, %edi
callq 0x20b5
movslq %ebp, %r14
leaq 0x12ef7(%rip), %r15     # 0x15240
movq 0x12ee0(%rip), %rcx     # 0x15230
movl $0x1, %esi
movq %r15, %rdi
movq %r14, %rdx
callq 0x10a0
testq %rax, %rax
je 0x23c1
movq %rax, %r12
movq 0x12ec1(%rip), %rdi     # 0x15230
callq 0x1090
testl %eax, %eax
jne 0x2312
testq %rbx, %rbx
jne 0x238e
movq %r15, %rdi
movl %r12d, %esi
callq 0x1fd1
testl %eax, %eax
je 0x2349
jmp 0x2312
movb 0x12eac(%rip), %al      # 0x15240
cmpb $0xa, %al
jne 0x23b0
movl $0xd, %edi
callq 0x20e9
testl %eax, %eax
jne 0x2312
movb 0x12e90(%rip), %al      # 0x15240
movsbl %al, %edi
callq 0x20e9
testl %eax, %eax
je 0x238e
jmp 0x2312
callq 0x20c6
testl %eax, %eax
jne 0x2312
xorl %ebx, %ebx
xorl %edi, %edi
callq 0x2059
testl %eax, %eax
jne 0x2312
movl $0x1, %edi
callq 0x2059
testl %eax, %eax
jne 0x2312
jmp 0x2317