// File: /workspace/github_c_makefile_star2_O1/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
xorl %eax, %eax
movq %rax, 0x8(%rsp)
movq %rax, 0xe(%rsp)
leaq 0x1318(%rip), %rsi      # 0x34ba
callq 0x1190
movq %rax, 0x13082(%rip)     # 0x15230
testq %rax, %rax
je 0x2214
movq %r13, 0x48(%rsp)
movl %ebx, 0x1c(%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 0x12b6(%rip), %xmm0     # 0x34a0
movaps %xmm0, 0x30(%rsp)
movabsq $0x2020202020202020, %rax # imm = 0x2020202020202020
movq %rax, 0x28(%rsp)
movq $0x0, 0x20(%rsp)
testq %r14, %r14
je 0x221e
movq %r14, %r13
subq %rbx, %r13
jmp 0x2229
movl $0x1, %ebx
jmp 0x237d
movq %rbx, %rdi
callq 0x10c0
movq %rax, %r13
testl %r13d, %r13d
jle 0x2270
xorl %eax, %eax
testl %r12d, %r12d
sete %al
leal (%rax,%rax,2), %eax
addl $0x6, %eax
cmpl %r13d, %eax
cmovll %eax, %r13d
callq 0x1030
movq (%rax), %rax
cmpl $0x2, %r13d
movl $0x1, %ecx
cmovgel %r13d, %ecx
xorl %edx, %edx
movsbq (%rbx,%rdx), %rsi
movb (%rax,%rsi,4), %sil
movb %sil, 0x30(%rsp,%rdx)
incq %rdx
cmpq %rdx, %rcx
jne 0x225a
testq %r14, %r14
je 0x22b1
leaq 0x1(%r14), %rdi
callq 0x10c0
testl %eax, %eax
js 0x22b1
cmpl $0x3, %eax
movl $0x3, %ebx
cmovll %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 0x2299
leaq 0x30(%rsp), %rdi
callq 0x244d
movw %ax, 0x8(%rsp)
leaq 0x33(%rsp), %rdi
callq 0x244d
movw %ax, 0xa(%rsp)
testl %r12d, %r12d
movq 0x48(%rsp), %rbx
jne 0x22e8
leaq 0x36(%rsp), %rdi
callq 0x244d
movw %ax, 0x14(%rsp)
testq %r14, %r14
je 0x22fc
leaq 0x29(%rsp), %rdi
callq 0x244d
movw %ax, 0xc(%rsp)
xorl %r14d, %r14d
testl %r12d, %r12d
sete %r14b
leaq 0x8(%rsp), %r12
movb %r15b, 0x6(%r12)
movb %bpl, 0x7(%r12)
movw $0x9b, 0x8(%r12)
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(%r12)
leaq 0x20(%rsp), %rsi
movq %rbx, %rdi
callq 0x1200
movq %rax, %rbx
leal 0xc(,%r14,2), %esi
movq %r12, %rdi
callq 0x2018
testl %eax, %eax
je 0x238e
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
movq 0x12eb8(%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 0x1c(%rsp), %ebp
movl %ebp, %edi
callq 0x20fc
movslq %ebp, %r14
leaq 0x12e9d(%rip), %r15     # 0x15240
movq 0x12e86(%rip), %rcx     # 0x15230
movl $0x1, %esi
movq %r15, %rdi
movq %r14, %rdx
callq 0x10a0
testq %rax, %rax
je 0x2418
movq %rax, %r12
movq 0x12e67(%rip), %rdi     # 0x15230
callq 0x1090
testl %eax, %eax
jne 0x236c
testq %rbx, %rbx
jne 0x23e8
movq %r15, %rdi
movl %r12d, %esi
callq 0x2018
testl %eax, %eax
je 0x23a3
jmp 0x236c
cmpb $0xa, 0x12e51(%rip)     # 0x15240
jne 0x2403
movl $0xd, %edi
callq 0x2130
testl %eax, %eax
jne 0x236c
movsbl 0x12e36(%rip), %edi     # 0x15240
callq 0x2130
testl %eax, %eax
je 0x23e8
jmp 0x236c
callq 0x210d
testl %eax, %eax
jne 0x236c
xorl %ebx, %ebx
xorl %edi, %edi
callq 0x20a0
testl %eax, %eax
jne 0x236c
movl $0x1, %edi
callq 0x20a0
testl %eax, %eax
jne 0x236c
jmp 0x2371