// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x90, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movq -0x10(%rbp), %rax
addq $0x48, %rax
movq %rax, -0x30(%rbp)
xorl %eax, %eax
movl %eax, %edi
callq 0x31e0
movq %rax, -0x80(%rbp)
leaq -0x80(%rbp), %rdi
leaq -0x78(%rbp), %rsi
callq 0x30a0
movl -0x64(%rbp), %eax
subl $0x1c, %eax
movl %eax, -0x64(%rbp)
movl -0x64(%rbp), %eax
subl $0x46, %eax
imull $0x3e8, %eax, %eax      # imm = 0x3E8
addl -0x5c(%rbp), %eax
addl $0x1, %eax
movw %ax, -0x82(%rbp)
movq -0x10(%rbp), %rdi
movl $0x1, %esi
xorl %eax, %eax
movl %eax, %edx
callq 0x7380
cmpl $0x0, %eax
jne 0x6cf6
movl $0x0, -0x4(%rbp)
jmp 0x7373
movq -0x30(%rbp), %rax
movzwl 0x50c(%rax), %edi
callq 0x6c50
movw %ax, -0x32(%rbp)
movq -0x10(%rbp), %rdi
movzwl -0x32(%rbp), %esi
xorl %eax, %eax
movl %eax, %edx
callq 0x7380
cmpl $0x0, %eax
jne 0x6d2c
movl $0x0, -0x4(%rbp)
jmp 0x7373
movl $0x1, -0x3c(%rbp)
movl -0x3c(%rbp), %eax
movq -0x10(%rbp), %rcx
movq 0x28(%rcx), %rcx
shrq %rcx
subq $0x3, %rcx
cmpq %rcx, %rax
jae 0x734b
movq -0x30(%rbp), %rax
movl -0x3c(%rbp), %ecx
addl $0x0, %ecx
movl %ecx, %ecx
movzwl 0x50c(%rax,%rcx,2), %edi
callq 0x6c50
movw %ax, -0x84(%rbp)
movq -0x30(%rbp), %rax
movl -0x3c(%rbp), %ecx
addl $0x2, %ecx
movl %ecx, %ecx
movzwl 0x50c(%rax,%rcx,2), %edi
callq 0x6c50
movw %ax, -0x86(%rbp)
movzwl -0x84(%rbp), %eax
cmpl $0x0, %eax
je 0x733b
movzwl -0x84(%rbp), %eax
movq -0x18(%rbp), %rcx
movzbl 0x9(%rcx), %ecx
shll $0x8, %ecx
movq -0x18(%rbp), %rdx
movzbl 0x8(%rdx), %edx
orl %edx, %ecx
cmpl %ecx, %eax
jne 0x7339
movq -0x30(%rbp), %rax
movl -0x3c(%rbp), %ecx
addl $0x1, %ecx
movl %ecx, %ecx
movzwl 0x50c(%rax,%rcx,2), %edi
callq 0x6c50
movw %ax, -0x34(%rbp)
movzwl -0x34(%rbp), %eax
cmpl $0x0, %eax
jne 0x6eb4
movq -0x10(%rbp), %rdi
callq 0x7510
movw %ax, -0x34(%rbp)
movzwl %ax, %eax
cmpl $0x0, %eax
jne 0x6e70
movq 0x171f0(%rip), %rax     # 0x1dff8
movq (%rax), %rdi
movq -0x10(%rbp), %rdx
addq $0x8, %rdx
leaq 0x12ac7(%rip), %rsi     # 0x198e1
movb $0x0, %al
callq 0x31c0
leaq 0x183c0(%rip), %rax     # 0x1f1e8
cmpq $0x0, (%rax)
je 0x6e64
leaq 0x183b3(%rip), %rax     # 0x1f1e8
movq (%rax), %rax
movq 0x17181(%rip), %rcx     # 0x1dfc0
cmpq (%rcx), %rax
je 0x6e64
leaq 0x1839d(%rip), %rax     # 0x1f1e8
movq (%rax), %rdi
movq -0x10(%rbp), %rdx
addq $0x8, %rdx
leaq 0x12a84(%rip), %rsi     # 0x198e1
movb $0x0, %al
callq 0x31c0
movl $0x0, -0x4(%rbp)
jmp 0x7373
movzwl -0x34(%rbp), %edi
callq 0x6c50
movw %ax, %dx
movq -0x30(%rbp), %rax
movl -0x3c(%rbp), %ecx
addl $0x1, %ecx
movl %ecx, %ecx
movw %dx, 0x50c(%rax,%rcx,2)
movq -0x10(%rbp), %rdi
movzwl -0x32(%rbp), %esi
xorl %eax, %eax
movl %eax, %edx
callq 0x75a0
cmpl $0x0, %eax
jne 0x6eb2
movl $0x0, -0x4(%rbp)
jmp 0x7373
jmp 0x6eb4
jmp 0x6eb6
movq -0x10(%rbp), %rdi
movzwl -0x34(%rbp), %esi
xorl %eax, %eax
movl %eax, %edx
callq 0x7380
cmpl $0x0, %eax
jne 0x6ed8
movl $0x0, -0x4(%rbp)
jmp 0x7373
movl $0x1, -0x40(%rbp)
movl -0x40(%rbp), %eax
movq -0x10(%rbp), %rcx
movq 0x28(%rcx), %rcx
shrq %rcx
movzwl -0x86(%rbp), %edx
subl $0x1, %edx
movslq %edx, %rdx
subq %rdx, %rcx
cmpq %rcx, %rax
jae 0x7255
movq -0x30(%rbp), %rax
movl -0x40(%rbp), %ecx
addl $0x0, %ecx
movl %ecx, %ecx
movzwl 0x50c(%rax,%rcx,2), %edi
callq 0x6c50
movzwl %ax, %eax
cmpl $0x0, %eax
jne 0x7241
movq -0x30(%rbp), %rax
movl -0x40(%rbp), %ecx
addl $0x0, %ecx
addl $0x1, %ecx
movl %ecx, %ecx
movzwl 0x50c(%rax,%rcx,2), %edi
callq 0x6c50
movzwl %ax, %eax
cmpl $0x0, %eax
jne 0x7241
movq -0x30(%rbp), %rax
movl -0x40(%rbp), %ecx
addl $0x2, %ecx
movl %ecx, %ecx
movzwl 0x50c(%rax,%rcx,2), %edi
callq 0x6c50
movzwl %ax, %eax
cmpl $0x0, %eax
jne 0x7241
movq -0x18(%rbp), %rax
movzwl 0x2(%rax), %edi
callq 0x6c50
movw %ax, %dx
movq -0x20(%rbp), %rax
movw %dx, (%rax)
movq -0x30(%rbp), %rax
movl -0x40(%rbp), %ecx
addl $0x0, %ecx
movl %ecx, %ecx
movw %dx, 0x50c(%rax,%rcx,2)
movq -0x18(%rbp), %rax
movzwl 0x4(%rax), %edi
callq 0x6c50
movw %ax, %dx
movq -0x20(%rbp), %rax
movw %dx, 0x2(%rax)
movq -0x30(%rbp), %rax
movl -0x40(%rbp), %ecx
addl $0x0, %ecx
addl $0x1, %ecx
movl %ecx, %ecx
movw %dx, 0x50c(%rax,%rcx,2)
movq -0x18(%rbp), %rax
movzwl 0x6(%rax), %edi
callq 0x6c50
movw %ax, %dx
movq -0x20(%rbp), %rax
movw %dx, 0x4(%rax)
movq -0x30(%rbp), %rax
movl -0x40(%rbp), %ecx
addl $0x2, %ecx
movl %ecx, %ecx
movw %dx, 0x50c(%rax,%rcx,2)
movzwl -0x82(%rbp), %eax
leaq 0x181ec(%rip), %rcx     # 0x1f1f8
movl (%rcx), %esi
andl $0x4, %esi
xorl %ecx, %ecx
movl $0x8000, %edx           # imm = 0x8000
cmpl $0x0, %esi
cmovnel %edx, %ecx
orl %ecx, %eax
movzwl %ax, %edi
callq 0x6c50
movw %ax, %dx
movq -0x20(%rbp), %rax
movw %dx, 0x6(%rax)
movq -0x30(%rbp), %rax
movl -0x40(%rbp), %ecx
addl $0x3, %ecx
movl %ecx, %ecx
movw %dx, 0x50c(%rax,%rcx,2)
movq -0x20(%rbp), %rax
movw $0x0, 0x8(%rax)
movq -0x30(%rbp), %rax
movl -0x40(%rbp), %ecx
addl $0x4, %ecx
movl %ecx, %ecx
movw $0x0, 0x50c(%rax,%rcx,2)
movq -0x20(%rbp), %rax
movw $0x0, 0xa(%rax)
movq -0x30(%rbp), %rax
movl -0x40(%rbp), %ecx
addl $0x5, %ecx
movl %ecx, %ecx
movw $0x0, 0x50c(%rax,%rcx,2)
movq -0x20(%rbp), %rax
movw $0x0, 0xc(%rax)
movq -0x30(%rbp), %rax
movl -0x40(%rbp), %ecx
addl $0x6, %ecx
movl %ecx, %ecx
movw $0x0, 0x50c(%rax,%rcx,2)
movq -0x20(%rbp), %rax
movw $0x0, 0xe(%rax)
movq -0x30(%rbp), %rax
movl -0x40(%rbp), %ecx
addl $0x7, %ecx
movl %ecx, %ecx
movw $0x0, 0x50c(%rax,%rcx,2)
movl $0x809b, %edi           # imm = 0x809B
callq 0x6c50
movw %ax, %dx
movq -0x20(%rbp), %rax
movw %dx, 0x10(%rax)
movq -0x30(%rbp), %rax
movl -0x40(%rbp), %ecx
addl $0x8, %ecx
movl %ecx, %ecx
movw %dx, 0x50c(%rax,%rcx,2)
movw -0x34(%rbp), %cx
movq -0x20(%rbp), %rax
movw %cx, 0x12(%rax)
movl -0x40(%rbp), %eax
movw %ax, %cx
movq -0x20(%rbp), %rax
movw %cx, 0x14(%rax)
movq -0x10(%rbp), %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x20(%rax)
leaq 0x180db(%rip), %rax     # 0x1f1f8
movl (%rax), %eax
andl $0x4, %eax
cmpl $0x0, %eax
je 0x7213
movq -0x10(%rbp), %rdi
movq -0x28(%rbp), %rax
movzwl %ax, %esi
callq 0x7730
movw %ax, -0x88(%rbp)
movzwl -0x88(%rbp), %eax
cmpl $0x0, %eax
jne 0x71bd
movq 0x16ea3(%rip), %rax     # 0x1dff8
movq (%rax), %rdi
movq -0x10(%rbp), %rdx
addq $0x8, %rdx
leaq 0x127a8(%rip), %rsi     # 0x1990f
movb $0x0, %al
callq 0x31c0
leaq 0x18073(%rip), %rax     # 0x1f1e8
cmpq $0x0, (%rax)
je 0x71b1
leaq 0x18066(%rip), %rax     # 0x1f1e8
movq (%rax), %rax
movq 0x16e34(%rip), %rcx     # 0x1dfc0
cmpq (%rcx), %rax
je 0x71b1
leaq 0x18050(%rip), %rax     # 0x1f1e8
movq (%rax), %rdi
movq -0x10(%rbp), %rdx
addq $0x8, %rdx
leaq 0x12765(%rip), %rsi     # 0x1990f
movb $0x0, %al
callq 0x31c0
movl $0x0, -0x4(%rbp)
jmp 0x7373
movzwl -0x88(%rbp), %edi
callq 0x6c50
movw %ax, %dx
movq -0x20(%rbp), %rax
movw %dx, 0xa(%rax)
movq -0x30(%rbp), %rax
movl -0x40(%rbp), %ecx
addl $0x5, %ecx
movl %ecx, %ecx
movw %dx, 0x50c(%rax,%rcx,2)
movq -0x28(%rbp), %rax
movzwl %ax, %edi
callq 0x6c50
movw %ax, %dx
movq -0x20(%rbp), %rax
movw %dx, 0xc(%rax)
movq -0x30(%rbp), %rax
movl -0x40(%rbp), %ecx
addl $0x6, %ecx
movl %ecx, %ecx
movw %dx, 0x50c(%rax,%rcx,2)
movq -0x10(%rbp), %rdi
movzwl -0x34(%rbp), %esi
xorl %eax, %eax
movl %eax, %edx
callq 0x75a0
cmpl $0x0, %eax
jne 0x7235
movl $0x0, -0x4(%rbp)
jmp 0x7373
movl $0x1, -0x4(%rbp)
jmp 0x7373
jmp 0x7243
movzwl -0x86(%rbp), %eax
addl -0x40(%rbp), %eax
movl %eax, -0x40(%rbp)
jmp 0x6edf
movq -0x30(%rbp), %rax
movzwl 0x50c(%rax), %edi
callq 0x6c50
movw %ax, -0x34(%rbp)
movzwl %ax, %eax
cmpl $0x0, %eax
jne 0x7334
movq -0x10(%rbp), %rdi
callq 0x7510
movw %ax, -0x36(%rbp)
movzwl %ax, %eax
cmpl $0x0, %eax
jne 0x72f6
movq 0x16d67(%rip), %rax     # 0x1dff8
movq (%rax), %rdi
movq -0x10(%rbp), %rdx
addq $0x8, %rdx
leaq 0x12695(%rip), %rsi     # 0x19938
movb $0x0, %al
callq 0x31c0
leaq 0x17f37(%rip), %rax     # 0x1f1e8
cmpq $0x0, (%rax)
je 0x72ed
leaq 0x17f2a(%rip), %rax     # 0x1f1e8
movq (%rax), %rax
movq 0x16cf8(%rip), %rcx     # 0x1dfc0
cmpq (%rcx), %rax
je 0x72ed
leaq 0x17f14(%rip), %rax     # 0x1f1e8
movq (%rax), %rdi
movq -0x10(%rbp), %rdx
addq $0x8, %rdx
leaq 0x12652(%rip), %rsi     # 0x19938
movb $0x0, %al
callq 0x31c0
movl $0x0, -0x4(%rbp)
jmp 0x7373
movzwl -0x36(%rbp), %edi
callq 0x6c50
movw %ax, %cx
movq -0x30(%rbp), %rax
movw %cx, 0x50c(%rax)
movq -0x10(%rbp), %rdi
movzwl -0x34(%rbp), %esi
xorl %eax, %eax
movl %eax, %edx
callq 0x75a0
cmpl $0x0, %eax
jne 0x732c
movl $0x0, -0x4(%rbp)
jmp 0x7373
movw -0x36(%rbp), %ax
movw %ax, -0x34(%rbp)
jmp 0x6eb6
jmp 0x733b
jmp 0x733d
movl -0x3c(%rbp), %eax
addl $0x4, %eax
movl %eax, -0x3c(%rbp)
jmp 0x6d33
movq -0x30(%rbp), %rax
movzwl 0x50c(%rax), %edi
callq 0x6c50
movw %ax, -0x32(%rbp)
movzwl -0x32(%rbp), %eax
cmpl $0x0, %eax
jne 0x6d0a
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x90, %rsp
popq %rbp
retq
nop