// File: /workspace/github_c_makefile_star2_O2/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 $0x48, %rsp
movl %r9d, %r12d
movl %r8d, %ebx
movl %ecx, %r13d
movl %edx, %ebp
movq %rsi, %r15
andq $0x0, 0x16(%rsp)
movq %rdi, %r14
andw $0x0, 0x1e(%rsp)
leaq 0x1644(%rip), %rsi      # 0x34ba
callq 0x1190
movq %rax, 0x133ae(%rip)     # 0x15230
testq %rax, %rax
je 0x1ee8
movl %r13d, 0x8(%rsp)
movl %ebp, 0xc(%rsp)
movl %ebx, 0x4(%rsp)
pushq $0x2f
popq %rsi
movq %r14, %rdi
callq 0x1100
movq %rax, %rbx
testq %rax, %rax
cmoveq %r14, %rbx
pushq $0x2e
popq %rsi
movq %rbx, %rdi
callq 0x1100
movq %rax, %r14
movaps 0x15e2(%rip), %xmm0     # 0x34a0
movaps %xmm0, 0x30(%rsp)
movabsq $0x2020202020202020, %rax # imm = 0x2020202020202020
movq %rax, 0x28(%rsp)
andq $0x0, 0x20(%rsp)
movq %r15, %rbp
testq %r14, %r14
je 0x1ef0
movq %r14, %r13
subq %rbx, %r13
jmp 0x1efb
pushq $0x1
popq %rbx
jmp 0x204c
movq %rbx, %rdi
callq 0x10c0
movq %rax, %r13
xorl %eax, %eax
testl %r12d, %r12d
sete %al
leal (%rax,%rax,2), %eax
addl $0x6, %eax
cmpl %r13d, %eax
cmovll %eax, %r13d
xorl %r15d, %r15d
testl %r13d, %r13d
cmovlel %r15d, %r13d
cmpq %r15, %r13
je 0x1f39
callq 0x1030
movq (%rax), %rax
movsbq (%rbx,%r15), %rcx
movb (%rax,%rcx,4), %al
movb %al, 0x30(%rsp,%r15)
incq %r15
jmp 0x1f1a
pushq $-0x1
popq %r15
testq %r14, %r14
je 0x1f83
leaq 0x1(%r14), %rdi
callq 0x10c0
cmpl $0x3, %eax
pushq $0x3
popq %rbx
cmovll %eax, %ebx
testl %ebx, %ebx
cmovsl %r15d, %ebx
incl %ebx
xorl %r15d, %r15d
cmpq %r15, %rbx
je 0x1f83
callq 0x1030
movq (%rax), %rax
movsbq (%r14,%r15), %rcx
movb (%rax,%rcx,4), %al
movb %al, 0x28(%rsp,%r15)
movb %al, 0x20(%rsp,%r15)
incq %r15
jmp 0x1f5f
leaq 0x30(%rsp), %rdi
callq 0x211f
movw %ax, 0x12(%rsp)
leaq 0x33(%rsp), %rdi
callq 0x211f
movw %ax, 0x14(%rsp)
testl %r12d, %r12d
jne 0x1fb5
leaq 0x36(%rsp), %rdi
callq 0x211f
movw %ax, 0x1e(%rsp)
testq %r14, %r14
je 0x1fc9
leaq 0x29(%rsp), %rdi
callq 0x211f
movw %ax, 0x16(%rsp)
xorl %r13d, %r13d
testl %r12d, %r12d
sete %r13b
leaq 0x12(%rsp), %r14
movl 0x8(%rsp), %eax
movb %al, 0x6(%r14)
movl 0xc(%rsp), %eax
movb %al, 0x7(%r14)
movw $0x9b, 0x8(%r14)
xorl %edi, %edi
callq 0x1140
leaq 0x40(%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 %rbp, %rdi
callq 0x1200
movq %rax, %rbx
leal 0xc(,%r13,2), %esi
movq %r14, %rdi
callq 0x1d11
testl %eax, %eax
je 0x205d
pushq $-0x1
popq %rbx
movq 0x131e9(%rip), %rdi     # 0x15230
callq 0x10b0
movl %ebx, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl 0x4(%rsp), %ebp
movl %ebp, %edi
callq 0x1dda
movslq %ebp, %r14
leaq 0x131ce(%rip), %r15     # 0x15240
pushq $0x1
popq %r12
movq 0x131b3(%rip), %rcx     # 0x15230
movq %r15, %rdi
movq %r12, %rsi
movq %r14, %rdx
callq 0x10a0
testq %rax, %rax
je 0x20b9
movq %rax, %r13
movq 0x13196(%rip), %rdi     # 0x15230
callq 0x1090
testl %eax, %eax
jne 0x203d
testq %rbx, %rbx
jne 0x20ec
movq %r15, %rdi
movl %r13d, %esi
callq 0x1d11
testl %eax, %eax
je 0x2076
jmp 0x203d
callq 0x1de8
testl %eax, %eax
jne 0x203d
xorl %ebx, %ebx
xorl %edi, %edi
callq 0x1d8d
testl %eax, %eax
jne 0x203d
pushq $0x1
popq %rdi
callq 0x1d8d
testl %eax, %eax
jne 0x203d
jmp 0x2040
pushq $0xd
popq %rbx
movb 0x1314b(%rip), %al      # 0x15240
cmpb $0xa, %al
jne 0x210e
movl %ebx, %edi
callq 0x1e08
testl %eax, %eax
jne 0x203d
movb 0x13132(%rip), %al      # 0x15240
movsbl %al, %edi
callq 0x1e08
testl %eax, %eax
je 0x20ef
jmp 0x203d