// File: /workspace/github_c_makefile_star2_O1/simh[P]simtools/converters/fsio/dos11.c
// Function: dos11CreateFile
int dos11CreateFile(
  struct mountedFS *mount,
  struct dos11FileSpec *spec,
  struct dos11OpenFile *file,
  unsigned long contig
)
{
  struct DOS11data *data = &mount->dos11data;
  uint16_t mfdblk, ufdblk, ufdblk2;
  unsigned int i, j;
  struct tm tm;
  time_t now = time(NULL);
  uint16_t today;

  /*
   * Compute a suitable year for file creation date. This year will have
   * the same calendar as the current year but will be in the 20th century
   * so that DOS/BATCH-11 will be able to enter the date successfully (It is
   * not Y2K compliant!).
   */
  localtime_r(&now, &tm);
  tm.tm_year -= 28;

  today = ((tm.tm_year - 70) * 1000) + tm.tm_yday + 1;
  
  if (dos11ReadBlock(mount, MFD1_BLOCK, NULL) == 0)
    return 0;

  /*
   * Search the MFD for the specified UIC.
   */
  mfdblk = le16toh(data->buf[MFD1_MFD2BLOCK]);
  do {
    if (dos11ReadBlock(mount, mfdblk, NULL) == 0)
      return 0;

    for (i = MFD2_HEADER; i < EODIR(mount, MFD2_SIZE); i += MFD2_SIZE) {
      uint16_t uic = le16toh(data->buf[i + MFD2_UFDUIC]);
      uint16_t entrysz = le16toh(data->buf[i + MFD2_UFDSIZE]);

      if (uic != 0) {
        if (uic == ((spec->group << 8) | spec->user)) {
          ufdblk = le16toh(data->buf[i + MFD2_UFDSTART]);

          if (ufdblk == 0) {
            if ((ufdblk = dos11XtndDirectory(mount)) == 0) {
              ERROR("%s: Unable to create initial directory block\n",
                    mount->name);
              return 0;
            }

            data->buf[i + MFD2_UFDSTART] = htole16(ufdblk);
            if (dos11WriteBlock(mount, mfdblk, NULL) == 0)
              return 0;
          }
          for (;;) {
            if (dos11ReadBlock(mount, ufdblk, NULL) == 0)
              return 0;
            
            for (j = 1; j < EODIR(mount, entrysz); j += entrysz) {
              if ((le16toh(data->buf[j + UFD_FILENAME]) == 0) &&
                  (le16toh(data->buf[j + UFD_FILENAME + 1]) == 0) &&
                  (le16toh(data->buf[j + UFD_EXTENSION]) == 0)) {
                data->buf[j + UFD_FILENAME] =
                  file->name[0] = htole16(spec->name[0]);
                data->buf[j + UFD_FILENAME + 1] =
                  file->name[1] = htole16(spec->name[1]);
                data->buf[j + UFD_EXTENSION] = file->ext = htole16(spec->ext);
                data->buf[j + UFD_CREATION] =
                  file->creation =
                    htole16(today | (SWISSET('c') ? UFD_TYPECONTIGUOUS : 0));
                data->buf[j + UFD_NEXTFREEBYTE] = file->nfb = 0;
                data->buf[j + UFD_FILESTART] = file->start = 0;
                data->buf[j + UFD_FILELENGTH] = file->length = 0;
                data->buf[j + UFD_LASTBLOCKWRITTEN] = file->last = 0;
                data->buf[j + UFD_LUP] = file->lup = htole16(UFD_LOCK + 0233);

                file->ufdblk = ufdblk;
                file->ufdoffset = j;

                file-> mount = mount;

                if (SWISSET('c')) {
                  /*
                   * Allocate contiguous disk space for the file.
                   */
                  uint16_t base = bitmapAllocContiguous(mount, contig);

                  if (base == 0) {
                    ERROR("%s: Unable to allocate contiguous space\n",
                          mount->name);
                    return 0;
                  }

                  data->buf[j + UFD_FILESTART] = 
                    file->start = htole16(base);
                  data->buf[j + UFD_FILELENGTH] = 
                    file->length = htole16(contig);
                }

                /*
                 * Write the UFD block back to disk. The file will be marked
                 * as "locked" indicating that it is being written and the
                 * directory entry may not be accurate.
                 */
                if (dos11WriteBlock(mount, ufdblk, NULL) == 0)
                  return 0;

                return 1;
              }
            }

            if ((ufdblk = le16toh(data->buf[UFD_LINK])) == 0) {
              if ((ufdblk2 = dos11XtndDirectory(mount)) == 0) {
                ERROR("%s: Unable to extend UFD\n", mount->name);
                return 0;
              }

              data->buf[UFD_LINK] = htole16(ufdblk2);
              if (dos11WriteBlock(mount, ufdblk, NULL) == 0)
                return 0;
              ufdblk = ufdblk2;
            }
          }
        }
      }
    }
    mfdblk = le16toh(data->buf[MFD2_LINK]);
  } while (mfdblk != 0);

  return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xe8, %rsp
movq %rcx, %r12
movq %rdx, 0x70(%rsp)
movq %rsi, 0x78(%rsp)
movq %rdi, %r13
xorl %edi, %edi
callq 0x31f0
leaq 0xa8(%rsp), %rdi
movq %rax, (%rdi)
leaq 0xb0(%rsp), %r15
movq %r15, %rsi
callq 0x30b0
movl 0x14(%r15), %r14d
movl 0x1c(%r15), %ebx
leal -0x1c(%r14), %eax
movl %eax, 0x14(%r15)
movq %r13, %rdi
movl $0x1, %esi
xorl %edx, %edx
callq 0x78a2
testl %eax, %eax
movl $0x0, %eax
je 0x7890
imull $0x3e8, %r14d, %eax     # imm = 0x3E8
addl %ebx, %eax
addl $0x8131, %eax           # imm = 0x8131
movl %eax, 0x58(%rsp)
movzwl 0x554(%r13), %eax
leaq 0x8(%r13), %rcx
movq %rcx, 0x30(%rsp)
movzwl %r12w, %edx
leal -0x1(%rdx), %ecx
movslq %ecx, %rcx
movq %rcx, 0xa0(%rsp)
movl $0x11, %ecx
movq %rdx, 0x68(%rsp)
subl %edx, %ecx
movl %ecx, 0x20(%rsp)
cmpw $0x11, %r12w
setne 0x1f(%rsp)
movq %r13, %r10
movq %r13, 0x10(%rsp)
movzwl %ax, %esi
movq %r10, %rdi
movl %esi, 0x5c(%rsp)
xorl %edx, %edx
movq %r10, %rbx
callq 0x78a2
testl %eax, %eax
je 0x7888
movq 0x28(%rbx), %rax
shrq %rax
addq $-0x5, %rax
cmpq $-0x3, %rax
movq %rbx, %r10
ja 0x7877
movl $0x1, %ebp
movl $0x1, %eax
movzwl 0x554(%r10,%rax,2), %eax
movb $0x1, %bl
testl %eax, %eax
je 0x7859
movq 0x78(%rsp), %rcx
movzwl 0x8(%rcx), %ecx
cmpl %eax, %ecx
jne 0x7859
movl %ebp, %ebx
movzwl 0x558(%r10,%rbx,2), %r14d
movzwl 0x556(%r10,%rbx,2), %r15d
testw %r15w, %r15w
jne 0x7173
movq %r10, %rdi
movq %r10, %r13
callq 0x792b
testw %ax, %ax
je 0x7752
movl %eax, %r15d
incq %rbx
movw %ax, 0x554(%r13,%rbx,2)
xorl %ebx, %ebx
movq %r13, %rdi
movl 0x5c(%rsp), %esi
xorl %edx, %edx
callq 0x7983
testl %eax, %eax
movq %r13, %r10
je 0x77bb
movzwl %r15w, %esi
xorl %ebx, %ebx
movq %r10, %rdi
movl %esi, 0x4c(%rsp)
xorl %edx, %edx
callq 0x78a2
testl %eax, %eax
je 0x784c
movq %rbp, 0x28(%rsp)
leaq -0x1(%r14), %r13
movq 0x10(%rsp), %rbp
movq 0x28(%rbp), %rcx
shrq %rcx
subq %r13, %rcx
cmpq $0x2, %rcx
movq %rbp, %r10
jb 0x71f7
movl $0x1, %edx
movl $0x9, %eax
cmpw $0x0, 0x554(%r10,%rdx,2)
jne 0x71e8
leal -0x7(%rax), %esi
cmpw $0x0, 0x554(%r10,%rsi,2)
jne 0x71e8
leal -0x6(%rax), %edi
cmpw $0x0, 0x554(%r10,%rdi,2)
je 0x726b
leal (%r14,%rax), %edx
addl $-0x8, %edx
addl %r14d, %eax
cmpq %rdx, %rcx
ja 0x71ba
movzwl 0x554(%r10), %r15d
testw %r15w, %r15w
jne 0x723c
movq %r10, %rdi
movq %r10, %rbp
callq 0x792b
testw %ax, %ax
je 0x77f2
movl %eax, %r15d
movw %ax, 0x554(%rbp)
xorl %ebx, %ebx
movq %rbp, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x7983
testl %eax, %eax
movq %rbp, %r10
je 0x73f3
movzwl %r15w, %esi
xorl %ebx, %ebx
movq %r10, %rdi
movl %esi, 0x4c(%rsp)
xorl %edx, %edx
movq %r10, %rbp
callq 0x78a2
testl %eax, %eax
jne 0x719d
movl $0x0, 0xc(%rsp)
movq %rbp, %r10
jmp 0x73fb
movq 0x78(%rsp), %r8
movzwl 0x2(%r8), %ecx
movq 0x70(%rsp), %r9
movw %cx, (%r9)
movw %cx, 0x554(%r10,%rdx,2)
movzwl 0x4(%r8), %ecx
movw %cx, 0x2(%r9)
movw %cx, 0x554(%r10,%rsi,2)
movzwl 0x6(%r8), %ecx
movw %cx, 0x4(%r9)
movw %cx, 0x554(%r10,%rdi,2)
leaq 0x11d14(%rip), %rcx     # 0x18fc8
movl (%rcx), %ecx
andl $0x4, %ecx
movl %ecx, %edx
shll $0xd, %edx
orl 0x58(%rsp), %edx
movw %dx, 0x6(%r9)
leal -0x5(%rax), %esi
movw %dx, 0x554(%r10,%rsi,2)
xorl %esi, %esi
movw %si, 0x8(%r9)
leal -0x4(%rax), %edx
movw %si, 0x554(%r10,%rdx,2)
movw %si, 0xa(%r9)
leal -0x3(%rax), %edi
movw %si, 0x554(%r10,%rdi,2)
movw %si, 0xc(%r9)
leal -0x2(%rax), %r8d
movw %si, 0x554(%r10,%r8,2)
movw %si, 0xe(%r9)
leal -0x1(%rax), %edx
movw %si, 0x554(%r10,%rdx,2)
movw $0x809b, %si            # imm = 0x809B
movw %si, 0x10(%r9)
movl %eax, %edx
movw %si, 0x554(%r10,%rdx,2)
movw %r15w, 0x12(%r9)
addl $-0x8, %eax
movw %ax, 0x14(%r9)
movq %r10, 0x20(%r9)
testl %ecx, %ecx
movl $0xffff, %r14d          # imm = 0xFFFF
movq 0x28(%rsp), %rbp
je 0x73c7
movl 0x48(%r10), %eax
movq 0x68(%rsp), %rdx
cmpl %edx, %eax
movq %rdi, 0x90(%rsp)
movq %r8, 0x88(%rsp)
jbe 0x7382
movzwl 0x150(%r10), %r13d
movl %r13d, %ecx
notl %ecx
addl %ecx, %eax
cmpl %edx, %eax
jae 0x7405
xorl %ebx, %ebx
testw %bx, %bx
je 0x77d1
movq 0x70(%rsp), %rcx
movw %bx, 0xa(%rcx)
movq 0x90(%rsp), %rax
movw %bx, 0x554(%r10,%rax,2)
movw %r12w, 0xc(%rcx)
movq 0x88(%rsp), %rax
movw %r12w, 0x554(%r10,%rax,2)
movq 0x28(%rsp), %rbp
testw %bx, %bx
je 0x73ec
xorl %ebx, %ebx
movq %r10, %rdi
movl 0x4c(%rsp), %esi
xorl %edx, %edx
callq 0x7983
movq 0x10(%rsp), %r10
xorl %ecx, %ecx
testl %eax, %eax
setne %cl
movl %ecx, 0xc(%rsp)
jmp 0x7859
xorl %ebx, %ebx
jmp 0x7859
movl $0x0, 0xc(%rsp)
movq 0x28(%rsp), %rbp
jmp 0x7859
movb $0x1, %bpl
movl %eax, 0x24(%rsp)
movq %r12, 0x98(%rsp)
movzwl %r13w, %eax
imull $0x8889, %eax, %eax     # imm = 0x8889
shrl $0x19, %eax
movl %eax, 0x40(%rsp)
imull $0x3c0, %eax, %eax      # imm = 0x3C0
movl %r13d, %ecx
subl %eax, %ecx
movl %ecx, 0x3c(%rsp)
movl 0x20(%rsp), %ebx
movl 0x40(%rsp), %r15d
cmpw 0x4c(%r10), %r15w
jae 0x7382
movzwl %r15w, %esi
movq %r10, %rdi
callq 0x986f
testl %eax, %eax
je 0x7700
movzwl 0x3c(%rsp), %ecx
shrl $0x4, %ecx
imull $0x3c0, %r15d, %eax     # imm = 0x3C0
movl %eax, 0x48(%rsp)
orl $0x10, %eax
movq %rax, 0x80(%rsp)
movq 0x10(%rsp), %r10
cmpw $0x3b, %cx
ja 0x7715
leal 0x4(%rcx), %edx
andl %r14d, %edx
movzwl 0x154(%r10,%rdx,2), %eax
cmpl %r14d, %eax
je 0x7602
testb %bpl, %bpl
je 0x74bb
movq %rcx, %rsi
shll $0x4, %ecx
addl 0x48(%rsp), %ecx
movw %cx, 0x150(%r10)
movq %rsi, %rcx
cmpw $0x10, %r12w
ja 0x7549
movq %rcx, %r11
movq 0xa0(%rsp), %rcx
leaq 0x10ff8(%rip), %rsi     # 0x184d0
movzwl (%rsi,%rcx,2), %r8d
movzwl %r8w, %r9d
xorl %esi, %esi
testl %eax, %r9d
je 0x7502
incl %esi
movzwl %si, %ecx
cmpl %ecx, %ebx
seta %dil
jbe 0x7532
movl %r9d, %r8d
shll %cl, %r8d
testl %eax, %r8d
jne 0x74e8
jmp 0x7507
movb 0x1f(%rsp), %dil
orl %eax, %r8d
movw %r8w, 0x154(%r10,%rdx,2)
movb $0x1, 0x152(%r10)
movq %r11, %rcx
movl %ecx, %eax
shll $0x4, %eax
addl 0x48(%rsp), %eax
addl %eax, %esi
movl %r8d, %eax
movl %esi, 0x24(%rsp)
jmp 0x7535
movq %r11, %rcx
testb %dil, %dil
je 0x7549
movl $0x1, %r15d
xorl %ebp, %ebp
xorl %eax, %eax
jmp 0x76e0
testw %ax, %ax
js 0x7600
movq %rcx, 0x50(%rsp)
movl %ecx, %r14d
shll $0x4, %r14d
movl $0x1, %ebp
leaq 0x10f86(%rip), %rcx     # 0x184f0
movl %r13d, 0x44(%rsp)
testw %ax, (%rcx,%rbp,2)
jne 0x7582
incq %rbp
cmpq $0x11, %rbp
jne 0x756f
movw $0x11, %bp
movl %r12d, %r13d
subl %ebp, %r13d
movq 0x80(%rsp), %rax
leal (%r14,%rax), %ebx
cmpw $0x10, %r13w
movl %ebx, 0x60(%rsp)
jb 0x760e
movzwl %bx, %eax
imull $0x8889, %eax, %r15d    # imm = 0x8889
shrl $0x19, %r15d
movq %r10, %rdi
movl %r15d, %esi
callq 0x986f
movq 0x10(%rsp), %r10
testl %eax, %eax
je 0x75dc
imull $0xfffffc40, %r15d, %ecx # imm = 0xFFFFFC40
addl %ebx, %ecx
movzwl %cx, %ecx
shrl $0x4, %ecx
addl $0x4, %ecx
movzwl 0x154(%r10,%rcx,2), %r12d
movb $0x1, %cl
testl %eax, %eax
je 0x7675
testw %r12w, %r12w
jne 0x7675
addl $0x10, %ebx
addl $-0x10, %r13d
cmpw $0xf, %r13w
ja 0x759f
jmp 0x760e
xorl %ebp, %ebp
incl %ecx
xorl %r15d, %r15d
movb $0x1, %al
jmp 0x76e0
testw %r13w, %r13w
je 0x766f
movzwl %bx, %eax
imull $0x8889, %eax, %r15d    # imm = 0x8889
shrl $0x19, %r15d
movq %r10, %rdi
movl %r15d, %esi
callq 0x986f
movq 0x10(%rsp), %r10
testl %eax, %eax
je 0x7653
imull $0x3c0, %r15d, %ecx     # imm = 0x3C0
movl %ebx, %edx
subl %ecx, %edx
movzwl %dx, %ecx
shrl $0x4, %ecx
addl $0x4, %ecx
movzwl 0x154(%r10,%rcx,2), %r12d
testl %eax, %eax
je 0x766b
movzwl %r13w, %eax
decl %eax
leaq 0x10e6c(%rip), %rcx     # 0x184d0
testw %r12w, (%rcx,%rax,2)
je 0x766f
movb $0x1, %cl
jmp 0x7675
xorl %ecx, %ecx
movl 0x44(%rsp), %ebx
testb %cl, %cl
movq 0x98(%rsp), %r12
je 0x7689
movl $0x2, %r15d
jmp 0x76ca
movl 0x60(%rsp), %eax
subl %ebp, %eax
movl %eax, 0x24(%rsp)
movl $0x1, %r15d
testw %r12w, %r12w
je 0x76ca
addl 0x80(%rsp), %r14d
subl %ebp, %r14d
movl %r12d, %ebp
movzwl %r14w, %esi
movq %r10, %rdi
callq 0x993d
testl %eax, %eax
je 0x76ea
incl %r14d
decw %bp
movq 0x10(%rsp), %r10
jne 0x76ad
xorl %ebp, %ebp
movl %ebx, %r13d
xorl %eax, %eax
movl 0x20(%rsp), %ebx
movl $0xffff, %r14d          # imm = 0xFFFF
movq 0x50(%rsp), %rcx
testb %al, %al
jne 0x747f
jmp 0x7724
xorl %ebp, %ebp
movl %ebx, %r13d
xorl %eax, %eax
movl $0x0, 0x24(%rsp)
movq 0x10(%rsp), %r10
jmp 0x76d1
movl $0x1, %r15d
movl $0x0, 0x24(%rsp)
movq 0x10(%rsp), %r10
jmp 0x7724
incl 0x40(%rsp)
movl $0x0, 0x3c(%rsp)
xorl %r15d, %r15d
testl %r15d, %r15d
je 0x7438
cmpl $0x2, %r15d
jne 0x77c8
movzwl %r13w, %eax
notl %eax
addl 0x48(%r10), %eax
xorl %ebx, %ebx
cmpl 0x68(%rsp), %eax
jae 0x7414
jmp 0x7384
movq 0x10897(%rip), %rax     # 0x17ff0
movq (%rax), %rdi
xorl %ebx, %ebx
leaq 0xa132(%rip), %rsi      # 0x11897
movq 0x30(%rsp), %rdx
xorl %eax, %eax
callq 0x31d0
leaq 0x11840(%rip), %rax     # 0x18fb8
movq (%rax), %rdi
testq %rdi, %rdi
je 0x784c
movl $0x0, 0xc(%rsp)
movq 0x10825(%rip), %rax     # 0x17fb8
cmpq (%rax), %rdi
movq 0x10(%rsp), %r10
je 0x7859
xorl %ebx, %ebx
leaq 0xa0ed(%rip), %rsi      # 0x11897
movq 0x30(%rsp), %rdx
xorl %eax, %eax
callq 0x31d0
movq 0x10(%rsp), %r10
movl $0x0, 0xc(%rsp)
jmp 0x7859
movl 0x24(%rsp), %ebx
jmp 0x7384
movq 0x30(%rsp), %rdi
leaq 0x64(%rsp), %rsi
callq 0x370a
movq 0x10(%rsp), %r10
movl 0x64(%rsp), %eax
movl %eax, 0xc(%rsp)
jmp 0x73bd
movq 0x107f7(%rip), %rax     # 0x17ff0
movq (%rax), %rdi
xorl %ebx, %ebx
leaq 0xa0e9(%rip), %rsi      # 0x118ee
movq 0x30(%rsp), %rdx
xorl %eax, %eax
callq 0x31d0
leaq 0x117a0(%rip), %rax     # 0x18fb8
movq (%rax), %rdi
testq %rdi, %rdi
movq 0x28(%rsp), %rbp
je 0x784c
movl $0x0, 0xc(%rsp)
movq 0x10784(%rip), %rax     # 0x17fb8
cmpq (%rax), %rdi
movq 0x10(%rsp), %r10
je 0x7859
xorl %ebx, %ebx
leaq 0xa0a7(%rip), %rsi      # 0x118ee
jmp 0x77aa
movl $0x0, 0xc(%rsp)
movq 0x10(%rsp), %r10
testb %bl, %bl
je 0x788c
addl $0x4, %ebp
movq 0x28(%r10), %rcx
shrq %rcx
addq $-0x3, %rcx
movq %rbp, %rax
cmpq %rbp, %rcx
ja 0x70f7
movzwl 0x554(%r10), %eax
testw %ax, %ax
jne 0x70b9
xorl %eax, %eax
jmp 0x7890
movl 0xc(%rsp), %eax
addq $0xe8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq