// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0xb0, %rsp
movb %cl, %al
movb %dl, %cl
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movb %cl, -0x19(%rbp)
movb %al, -0x1a(%rbp)
movl %r8d, -0x20(%rbp)
movl %r9d, -0x24(%rbp)
movl -0x24(%rbp), %edx
movl $0xe, %eax
movl $0xc, %ecx
cmpl $0x0, %edx
cmovneq %rcx, %rax
movq %rax, -0x30(%rbp)
leaq -0x3e(%rbp), %rdi
xorl %esi, %esi
movl $0xe, %edx
callq 0x1100
movq -0x10(%rbp), %rdi
leaq 0x219b(%rip), %rsi      # 0x4573
callq 0x1180
movq %rax, 0x13e4c(%rip)     # 0x16230
cmpq $0x0, %rax
je 0x27b9
movq -0x10(%rbp), %rdi
movl $0x2f, %esi
callq 0x10f0
movq %rax, -0x48(%rbp)
cmpq $0x0, %rax
jne 0x240e
movq -0x10(%rbp), %rax
movq %rax, -0x48(%rbp)
movq -0x48(%rbp), %rdi
movl $0x2e, %esi
callq 0x10f0
movq %rax, -0x50(%rbp)
leaq -0x80(%rbp), %rdi
movl $0x20, %esi
movl $0x10, %edx
callq 0x1100
leaq -0x88(%rbp), %rdi
movl $0x20, %esi
movl $0x8, %edx
callq 0x1100
leaq -0x90(%rbp), %rdi
xorl %esi, %esi
movl $0x8, %edx
callq 0x1100
cmpq $0x0, -0x50(%rbp)
je 0x2476
movq -0x50(%rbp), %rax
movq -0x48(%rbp), %rcx
subq %rcx, %rax
movl %eax, -0x98(%rbp)
jmp 0x2485
movq -0x48(%rbp), %rdi
callq 0x10b0
movl %eax, -0x98(%rbp)
movl -0x98(%rbp), %eax
movl -0x24(%rbp), %esi
movl $0x9, %ecx
movl $0x6, %edx
cmpl $0x0, %esi
cmovnel %edx, %ecx
cmpl %ecx, %eax
jle 0x24bb
movl -0x24(%rbp), %edx
movl $0x9, %eax
movl $0x6, %ecx
cmpl $0x0, %edx
cmovnel %ecx, %eax
movl %eax, -0x98(%rbp)
movl $0x0, -0x94(%rbp)
movl -0x94(%rbp), %eax
cmpl -0x98(%rbp), %eax
jge 0x2505
movq -0x48(%rbp), %rax
movslq -0x94(%rbp), %rcx
movsbl (%rax,%rcx), %edi
callq 0x1070
movb %al, %cl
movslq -0x94(%rbp), %rax
movb %cl, -0x80(%rbp,%rax)
movl -0x94(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x94(%rbp)
jmp 0x24c5
cmpq $0x0, -0x50(%rbp)
je 0x25ae
movq -0x50(%rbp), %rdi
addq $0x1, %rdi
callq 0x10b0
movl %eax, -0x98(%rbp)
cmpl $0x3, -0x98(%rbp)
jle 0x2536
movl $0x3, -0x98(%rbp)
movl $0x0, -0x94(%rbp)
movl -0x94(%rbp), %eax
movl -0x98(%rbp), %ecx
addl $0x1, %ecx
cmpl %ecx, %eax
jge 0x25ac
movq -0x50(%rbp), %rax
movslq -0x94(%rbp), %rcx
movsbl (%rax,%rcx), %edi
callq 0x1070
movb %al, %cl
movslq -0x94(%rbp), %rax
movb %cl, -0x88(%rbp,%rax)
movq -0x50(%rbp), %rax
movslq -0x94(%rbp), %rcx
movsbl (%rax,%rcx), %edi
callq 0x1070
movb %al, %cl
movslq -0x94(%rbp), %rax
movb %cl, -0x90(%rbp,%rax)
movl -0x94(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x94(%rbp)
jmp 0x2540
jmp 0x25ae
leaq -0x80(%rbp), %rdi
callq 0x27d0
movw %ax, -0x3e(%rbp)
leaq -0x80(%rbp), %rdi
addq $0x3, %rdi
callq 0x27d0
movw %ax, -0x3c(%rbp)
cmpl $0x0, -0x24(%rbp)
jne 0x25e3
leaq -0x80(%rbp), %rdi
addq $0x6, %rdi
callq 0x27d0
movw %ax, -0x32(%rbp)
cmpq $0x0, -0x50(%rbp)
je 0x25fe
leaq -0x88(%rbp), %rdi
addq $0x1, %rdi
callq 0x27d0
movw %ax, -0x3a(%rbp)
movb -0x1a(%rbp), %al
movb %al, -0x38(%rbp)
movb -0x19(%rbp), %al
movb %al, -0x37(%rbp)
movw $0x9b, -0x36(%rbp)
xorl %eax, %eax
movl %eax, %edi
callq 0x1130
movq %rax, -0x60(%rbp)
leaq -0x60(%rbp), %rdi
callq 0x1030
movq %rax, -0x68(%rbp)
movq -0x68(%rbp), %rax
movl 0x14(%rax), %eax
subl $0x46, %eax
imull $0x3e8, %eax, %eax      # imm = 0x3E8
movq -0x68(%rbp), %rcx
addl 0x1c(%rcx), %eax
addl $0x1, %eax
movw %ax, -0x34(%rbp)
movq -0x18(%rbp), %rdi
leaq -0x90(%rbp), %rsi
callq 0x11f0
movq %rax, -0x58(%rbp)
movq -0x30(%rbp), %rax
movl %eax, %esi
leaq -0x3e(%rbp), %rdi
callq 0x2140
cmpl $0x0, %eax
jne 0x27a2
movl -0x20(%rbp), %edi
callq 0x22a0
movslq -0x20(%rbp), %rdx
movq 0x13ba9(%rip), %rcx     # 0x16230
leaq 0x13bb2(%rip), %rdi     # 0x16240
movl $0x1, %esi
callq 0x1090
movq %rax, -0xa0(%rbp)
cmpq $0x0, %rax
je 0x2764
movq 0x13b80(%rip), %rdi     # 0x16230
callq 0x1080
cmpl $0x0, %eax
je 0x26bf
jmp 0x27a4
cmpq $0x0, -0x58(%rbp)
je 0x2741
movq $0x0, -0xa8(%rbp)
movq -0xa8(%rbp), %rax
cmpq -0xa0(%rbp), %rax
jae 0x273f
movq -0xa8(%rbp), %rcx
leaq 0x13b51(%rip), %rax     # 0x16240
movsbl (%rax,%rcx), %eax
cmpl $0xa, %eax
jne 0x270e
movl $0xd, %edi
callq 0x2310
cmpl $0x0, %eax
je 0x270c
jmp 0x27a4
jmp 0x270e
movq -0xa8(%rbp), %rcx
leaq 0x13b24(%rip), %rax     # 0x16240
movsbl (%rax,%rcx), %edi
callq 0x2310
cmpl $0x0, %eax
je 0x272c
jmp 0x27a4
jmp 0x272e
movl -0x94(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x94(%rbp)
jmp 0x26d1
jmp 0x275f
movq -0xa0(%rbp), %rax
movl %eax, %esi
leaq 0x13aef(%rip), %rdi     # 0x16240
callq 0x2140
cmpl $0x0, %eax
je 0x275d
jmp 0x27a4
jmp 0x275f
jmp 0x267c
callq 0x22c0
cmpl $0x0, %eax
je 0x2770
jmp 0x27a4
xorl %edi, %edi
callq 0x2220
cmpl $0x0, %eax
jne 0x27a0
movl $0x1, %edi
callq 0x2220
cmpl $0x0, %eax
jne 0x27a0
movq 0x13a9e(%rip), %rdi     # 0x16230
callq 0x10a0
movl $0x0, -0x4(%rbp)
jmp 0x27c0
jmp 0x27a2
jmp 0x27a4
movq 0x13a85(%rip), %rdi     # 0x16230
callq 0x10a0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x27c0
movl $0x1, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0xb0, %rsp
popq %rbp
retq
nopl (%rax)