// File: /workspace/github_c_makefile_star2_O3/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 $0x98, %rsp
movq %rcx, 0x10(%rsp)
movq %rdx, 0x38(%rsp)
movq %rsi, 0x8(%rsp)
movq %rdi, %rbx
xorl %r14d, %r14d
xorl %edi, %edi
callq 0x3200
leaq 0x58(%rsp), %rdi
movq %rax, (%rdi)
leaq 0x60(%rsp), %r13
movq %r13, %rsi
callq 0x30b0
movl 0x14(%r13), %r12d
movl 0x1c(%r13), %r15d
leal -0x1c(%r12), %eax
movl %eax, 0x14(%r13)
movq %rbx, %rdi
movl $0x1, %esi
xorl %edx, %edx
callq 0x7621
testl %eax, %eax
je 0x7598
imull $0x3e8, %r12d, %eax     # imm = 0x3E8
addl %r15d, %eax
addl $0x8131, %eax           # imm = 0x8131
movl %eax, 0x28(%rsp)
movzwl 0x554(%rbx), %eax
movzwl %ax, %ebp
xorl %r14d, %r14d
movq %rbx, %rdi
movl %ebp, %esi
xorl %edx, %edx
callq 0x7621
testl %eax, %eax
je 0x7598
movq 0x28(%rbx), %rax
shrq %rax
addq $-0x3, %rax
cmpq $0x2, %rax
movq 0x8(%rsp), %rdx
jb 0x7065
movl $0x1, %ecx
movl $0x5, %r12d
movzwl 0x554(%rbx,%rcx,2), %ecx
testw %cx, %cx
je 0x7059
cmpw %cx, 0x8(%rdx)
je 0x7076
movl %r12d, %ecx
addl $0x4, %r12d
cmpq %rcx, %rax
ja 0x7046
movzwl 0x554(%rbx), %eax
testw %ax, %ax
jne 0x700b
jmp 0x7598
leal -0x2(%r12), %eax
movzwl 0x554(%rbx,%rax,2), %r15d
addl $-0x3, %r12d
movzwl 0x554(%rbx,%r12,2), %r13d
testw %r13w, %r13w
jne 0x70cb
movq %rbx, %rdi
callq 0x76a8
testw %ax, %ax
je 0x75ad
movl %eax, %r13d
movw %ax, 0x554(%rbx,%r12,2)
xorl %r14d, %r14d
movq %rbx, %rdi
movl %ebp, %esi
xorl %edx, %edx
callq 0x7700
testl %eax, %eax
je 0x7598
movzwl %r13w, %r12d
xorl %r14d, %r14d
movq %rbx, %rdi
movl %r12d, %esi
xorl %edx, %edx
callq 0x7621
testl %eax, %eax
je 0x7598
leaq -0x1(%r15), %rbp
movq 0x28(%rbx), %rcx
shrq %rcx
subq %rbp, %rcx
cmpq $0x2, %rcx
jb 0x713b
movl $0x1, %edx
movl $0x9, %eax
cmpw $0x0, 0x554(%rbx,%rdx,2)
jne 0x712c
leal -0x7(%rax), %esi
cmpw $0x0, 0x554(%rbx,%rsi,2)
jne 0x712c
leal -0x6(%rax), %edi
cmpw $0x0, 0x554(%rbx,%rdi,2)
je 0x719c
leal (%r15,%rax), %edx
addl $-0x8, %edx
addl %r15d, %eax
cmpq %rdx, %rcx
ja 0x7105
movzwl 0x554(%rbx), %r13d
testw %r13w, %r13w
jne 0x717b
movq %rbx, %rdi
callq 0x76a8
testw %ax, %ax
je 0x75bf
movl %eax, %r13d
movw %ax, 0x554(%rbx)
xorl %r14d, %r14d
movq %rbx, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x7700
testl %eax, %eax
je 0x7598
movzwl %r13w, %r12d
xorl %r14d, %r14d
movq %rbx, %rdi
movl %r12d, %esi
xorl %edx, %edx
callq 0x7621
testl %eax, %eax
jne 0x70eb
jmp 0x7598
movq 0x8(%rsp), %r9
movzwl 0x2(%r9), %ecx
movq 0x38(%rsp), %r8
movw %cx, (%r8)
movw %cx, 0x554(%rbx,%rdx,2)
movzwl 0x4(%r9), %ecx
movw %cx, 0x2(%r8)
movw %cx, 0x554(%rbx,%rsi,2)
movzwl 0x6(%r9), %ecx
movw %cx, 0x4(%r8)
movw %cx, 0x554(%rbx,%rdi,2)
leaq 0x11df6(%rip), %rcx     # 0x18fd8
movl (%rcx), %ecx
andl $0x4, %ecx
movl %ecx, %edx
shll $0xd, %edx
orl 0x28(%rsp), %edx
movw %dx, 0x6(%r8)
leal -0x5(%rax), %esi
movw %dx, 0x554(%rbx,%rsi,2)
xorl %edx, %edx
movw %dx, 0x8(%r8)
leal -0x4(%rax), %esi
movw %dx, 0x554(%rbx,%rsi,2)
movw %dx, 0xa(%r8)
leal -0x3(%rax), %edi
movw %dx, 0x554(%rbx,%rdi,2)
movw %dx, 0xc(%r8)
leal -0x2(%rax), %r9d
movw %dx, 0x554(%rbx,%r9,2)
movw %dx, 0xe(%r8)
leal -0x1(%rax), %esi
movw %dx, 0x554(%rbx,%rsi,2)
movw $0x809b, %dx            # imm = 0x809B
movw %dx, 0x10(%r8)
movl %eax, %esi
movw %dx, 0x554(%rbx,%rsi,2)
movw %r13w, 0x12(%r8)
addl $-0x8, %eax
movw %ax, 0x14(%r8)
movq %rbx, 0x20(%r8)
testl %ecx, %ecx
je 0x7582
movq %r9, 0x48(%rsp)
movq %rdi, 0x50(%rsp)
movl %r12d, 0x34(%rsp)
movzwl 0x10(%rsp), %ecx
movl 0x48(%rbx), %eax
movq %rcx, 0x8(%rsp)
cmpl %ecx, %eax
jbe 0x7609
movzwl 0x150(%rbx), %edx
movzwl %dx, %ecx
notl %ecx
addl %ecx, %eax
cmpl 0x8(%rsp), %eax
jb 0x7609
movq 0x8(%rsp), %rax
decl %eax
cltq
movq %rax, 0x28(%rsp)
movl $0x11, %eax
subl 0x10(%rsp), %eax
movzwl %ax, %r13d
movb $0x1, %r14b
movzwl %dx, %eax
imull $0x8889, %eax, %r15d    # imm = 0x8889
shrl $0x19, %r15d
imull $0x3c0, %r15d, %eax     # imm = 0x3C0
subl %eax, %edx
movl %edx, %eax
movl %eax, 0x20(%rsp)
cmpw 0x4c(%rbx), %r15w
jae 0x7609
imull $0x3c0, %r15d, %ebp     # imm = 0x3C0
orl $0x10, %ebp
imull $0xfffffc40, %r15d, %eax # imm = 0xFFFFFC40
movl %eax, (%rsp)
movzwl %r15w, %esi
movq %rbx, %rdi
callq 0x9552
testl %eax, %eax
je 0x7609
movl 0x20(%rsp), %ecx
movzwl %cx, %edx
shrl $0x4, %edx
movl %r15d, 0x1c(%rsp)
imull $0x3c0, %r15d, %eax     # imm = 0x3C0
andl $-0x10, %ecx
leal (%rcx,%rbp), %r15d
movl (%rsp), %esi
subl %ecx, %esi
movzwl %si, %esi
xorl %r11d, %r11d
movzwl 0x15c(%rbx,%rdx,2), %edi
cmpl $0xffff, %edi           # imm = 0xFFFF
je 0x73a7
testb %r14b, %r14b
je 0x7365
movl %edx, %ecx
shll $0x4, %ecx
addl %eax, %ecx
movw %cx, 0x150(%rbx)
movq 0x10(%rsp), %r10
cmpw $0x10, %r10w
ja 0x739f
movq 0x28(%rsp), %rcx
leaq 0x11163(%rip), %r8      # 0x184e0
movzwl (%r8,%rcx,2), %r8d
xorl %r12d, %r12d
movl %r8d, %r9d
movl %r12d, %ecx
shll %cl, %r9d
testl %edi, %r9d
je 0x7530
incl %r12d
cmpl %r12d, %r13d
jne 0x7385
testw %di, %di
jns 0x73f4
xorl %r14d, %r14d
cmpq $0x3b, %rdx
leaq 0x1(%rdx), %rcx
setae %r11b
addl $0x10, %r15d
addl $-0x10, %esi
movq %rcx, %rdx
cmpq $0x3c, %rcx
jne 0x7342
movl 0x1c(%rsp), %r15d
incl %r15d
addl $0x3c0, %ebp            # imm = 0x3C0
addl $0xfffffc40, (%rsp)     # imm = 0xFFFFFC40
movl $0x0, 0x20(%rsp)
cmpw 0x4c(%rbx), %r15w
jb 0x7306
jmp 0x7609
shll $0x4, %edx
movl $0x1, %esi
leaq 0x110fd(%rip), %rcx     # 0x18500
testw %di, (%rcx,%rsi,2)
jne 0x7416
incq %rsi
cmpq $0x11, %rsi
jne 0x7403
movw $0x11, %si
movl %r10d, %ebp
subl %esi, %ebp
leal (%rdx,%rax), %r12d
addl $0x10, %r12d
cmpw $0x10, %bp
movl %r11d, 0x4(%rsp)
movq %rsi, 0x40(%rsp)
movl %r12d, %edx
jb 0x7492
movl %ebp, (%rsp)
movzwl %dx, %eax
imull $0x8889, %eax, %r14d    # imm = 0x8889
shrl $0x19, %r14d
movq %rbx, %rdi
movl %edx, %ebp
movl %r14d, %esi
callq 0x9552
testl %eax, %eax
je 0x7500
imull $0xfffffc40, %r14d, %eax # imm = 0xFFFFFC40
addl %ebp, %eax
movzwl %ax, %eax
shrl $0x4, %eax
addl $0x4, %eax
cmpw $0x0, 0x154(%rbx,%rax,2)
movl 0x4(%rsp), %eax
movl %ebp, %edx
jne 0x7506
addl $0x10, %edx
movl (%rsp), %ebp
addl $-0x10, %ebp
cmpw $0xf, %bp
ja 0x7436
testw %bp, %bp
je 0x75d1
movzwl %dx, %eax
imull $0x8889, %eax, %r14d    # imm = 0x8889
shrl $0x19, %r14d
movq %rbx, %rdi
movl %edx, (%rsp)
movl %r14d, %esi
callq 0x9552
testl %eax, %eax
je 0x74f7
movzwl %bp, %eax
imull $0x3c0, %r14d, %ecx     # imm = 0x3C0
movl (%rsp), %esi
movl %esi, %edx
subl %ecx, %edx
movzwl %dx, %ecx
shrl $0x4, %ecx
addl $0x4, %ecx
decl %eax
leaq 0x11002(%rip), %rdx     # 0x184e0
movzwl (%rdx,%rax,2), %eax
testw %ax, 0x154(%rbx,%rcx,2)
movl 0x4(%rsp), %eax
movl %esi, %edx
jne 0x7506
jmp 0x75d1
movl 0x4(%rsp), %eax
movl (%rsp), %edx
jmp 0x7506
movl 0x4(%rsp), %eax
movl %ebp, %edx
xorl %r14d, %r14d
testb $0x1, %al
movl 0x1c(%rsp), %r15d
jne 0x72e7
movzwl %dx, %eax
notl %eax
addl 0x48(%rbx), %eax
xorl %r14d, %r14d
cmpl 0x8(%rsp), %eax
jae 0x72ca
jmp 0x7609
orl %r9d, %edi
movw %di, 0x15c(%rbx,%rdx,2)
movb $0x1, 0x152(%rbx)
subl %esi, %r12d
testw %r12w, %r12w
je 0x7609
movq 0x38(%rsp), %rcx
movw %r12w, 0xa(%rcx)
movq 0x50(%rsp), %rax
movw %r12w, 0x554(%rbx,%rax,2)
movq 0x10(%rsp), %rax
movw %ax, 0xc(%rcx)
movq 0x48(%rsp), %rcx
movw %ax, 0x554(%rbx,%rcx,2)
movl 0x34(%rsp), %r12d
movq %rbx, %rdi
movl %r12d, %esi
xorl %edx, %edx
callq 0x7700
xorl %r14d, %r14d
testl %eax, %eax
setne %r14b
movl %r14d, %eax
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x24(%rsp), %r14
movq %rbx, %rdi
movq %r14, %rsi
callq 0x37da
jmp 0x7619
leaq 0x24(%rsp), %r14
movq %rbx, %rdi
movq %r14, %rsi
callq 0x371a
jmp 0x7619
movq 0x40(%rsp), %rcx
subl %ecx, %r12d
movq 0x10(%rsp), %rax
testw %ax, %ax
je 0x7545
subl %ecx, %r15d
movl %eax, %ebp
movzwl %r15w, %esi
movq %rbx, %rdi
callq 0x9620
testl %eax, %eax
je 0x7609
incl %r15d
decw %bp
jne 0x75ec
jmp 0x7545
leaq 0x24(%rsp), %r14
movq %rbx, %rdi
movq %r14, %rsi
callq 0x377a
movl (%r14), %r14d
jmp 0x7598