// File: /workspace/github_c_makefile_star2_O0/simh[P]simtools/converters/fsio/dos11.c
// Function: dos11CreateUFD
static int dos11CreateUFD(
  struct mountedFS *mount,
  uint8_t group,
  uint8_t user
)
{
  struct DOS11data *data = &mount->dos11data;
  uint16_t mfdblk, mfdsav, uic = (group << 8) | user;
  unsigned int i;

  if (dos11ReadBlock(mount, MFD1_BLOCK, NULL) == 0)
    return 0;

  /*
   * Search the MFD to see if the UFD already exists
   */
  mfdblk = mfdsav = 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) {
      if (le16toh(data->buf[i + MFD2_UFDUIC]) != 0)
        if (le16toh(data->buf[i + MFD2_UFDUIC] == uic))
          return 1;
    }
    mfdblk = le16toh(data->buf[MFD2_LINK]);
  } while (mfdblk != 0);

  /*
   * The UFD does not currently exist, create a new one without a data
   * block assigned. The first file creation operation will allocate a
   * data block.
   */
  mfdblk = mfdsav;
  for (;;) {
    if (dos11ReadBlock(mount, mfdblk, NULL) == 0)
      return 0;

    for (i = MFD2_HEADER; i < EODIR(mount, MFD2_SIZE); i += MFD2_SIZE) {
      if (le16toh(data->buf[i + MFD2_UFDUIC]) == 0) {
        data->buf[i + MFD2_UFDUIC] = htole16(uic);
        data->buf[i + MFD2_UFDSTART] = 0;
        data->buf[i + MFD2_UFDSIZE] = htole16(UFD_LEN);
        data->buf[i + MFD2_UFDZERO] = 0;

        dos11WriteBlock(mount, mfdblk, NULL);
        return 1;
      }
    }

    if (le16toh(data->buf[MFD2_LINK]) == 0) {
      /*
       * We have reached the end of the MFD, append a new block if available.
       */
      uint16_t buf[512], newblk;

      if ((newblk = bitmapAllocBlock(mount)) == 0) {
        ERROR("No space available to extend MFD on \"%s:\"\n", mount->name);
        return 0;
      }

      /*
       * The newly created block consists only of unused entries.
       */
      memset(buf, 0, sizeof(buf));
      if (dos11WriteBlock(mount, newblk, buf) == 0)
        return 0;

      data->buf[MFD2_LINK] = htole16(newblk);

      if (dos11WriteBlock(mount, mfdblk, NULL) == 0)
        return 0;
    }
    mfdblk = le16toh(data->buf[MFD2_LINK]);
  }
  return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x440, %rsp            # imm = 0x440
movb %dl, %al
movb %sil, %cl
movq %rdi, -0x10(%rbp)
movb %cl, -0x11(%rbp)
movb %al, -0x12(%rbp)
movq -0x10(%rbp), %rax
addq $0x48, %rax
movq %rax, -0x20(%rbp)
movzbl -0x11(%rbp), %eax
shll $0x8, %eax
movzbl -0x12(%rbp), %ecx
orl %ecx, %eax
movw %ax, -0x26(%rbp)
movq -0x10(%rbp), %rdi
movl $0x1, %esi
xorl %eax, %eax
movl %eax, %edx
callq 0x7380
cmpl $0x0, %eax
jne 0xb4da
movl $0x0, -0x4(%rbp)
jmp 0xb7fc
movq -0x20(%rbp), %rax
movzwl 0x50c(%rax), %edi
callq 0x6c50
movw %ax, -0x24(%rbp)
movw %ax, -0x22(%rbp)
movq -0x10(%rbp), %rdi
movzwl -0x22(%rbp), %esi
xorl %eax, %eax
movl %eax, %edx
callq 0x7380
cmpl $0x0, %eax
jne 0xb514
movl $0x0, -0x4(%rbp)
jmp 0xb7fc
movl $0x1, -0x2c(%rbp)
movl -0x2c(%rbp), %eax
movq -0x10(%rbp), %rcx
movq 0x28(%rcx), %rcx
shrq %rcx
subq $0x3, %rcx
cmpq %rcx, %rax
jae 0xb5a1
movq -0x20(%rbp), %rax
movl -0x2c(%rbp), %ecx
addl $0x0, %ecx
movl %ecx, %ecx
movzwl 0x50c(%rax,%rcx,2), %edi
callq 0x6c50
movzwl %ax, %eax
cmpl $0x0, %eax
je 0xb591
movq -0x20(%rbp), %rax
movl -0x2c(%rbp), %ecx
addl $0x0, %ecx
movl %ecx, %ecx
movzwl 0x50c(%rax,%rcx,2), %eax
movzwl -0x26(%rbp), %ecx
cmpl %ecx, %eax
sete %al
andb $0x1, %al
movzbl %al, %eax
movzwl %ax, %edi
callq 0x6c50
cmpw $0x0, %ax
je 0xb58f
movl $0x1, -0x4(%rbp)
jmp 0xb7fc
jmp 0xb591
jmp 0xb593
movl -0x2c(%rbp), %eax
addl $0x4, %eax
movl %eax, -0x2c(%rbp)
jmp 0xb51b
movq -0x20(%rbp), %rax
movzwl 0x50c(%rax), %edi
callq 0x6c50
movw %ax, -0x22(%rbp)
movzwl -0x22(%rbp), %eax
cmpl $0x0, %eax
jne 0xb4f2
movw -0x24(%rbp), %ax
movw %ax, -0x22(%rbp)
movq -0x10(%rbp), %rdi
movzwl -0x22(%rbp), %esi
xorl %eax, %eax
movl %eax, %edx
callq 0x7380
cmpl $0x0, %eax
jne 0xb5ec
movl $0x0, -0x4(%rbp)
jmp 0xb7fc
movl $0x1, -0x2c(%rbp)
movl -0x2c(%rbp), %eax
movq -0x10(%rbp), %rcx
movq 0x28(%rcx), %rcx
shrq %rcx
subq $0x3, %rcx
cmpq %rcx, %rax
jae 0xb6cd
movq -0x20(%rbp), %rax
movl -0x2c(%rbp), %ecx
addl $0x0, %ecx
movl %ecx, %ecx
movzwl 0x50c(%rax,%rcx,2), %edi
callq 0x6c50
movzwl %ax, %eax
cmpl $0x0, %eax
jne 0xb6bd
movzwl -0x26(%rbp), %edi
callq 0x6c50
movw %ax, %dx
movq -0x20(%rbp), %rax
movl -0x2c(%rbp), %ecx
addl $0x0, %ecx
movl %ecx, %ecx
movw %dx, 0x50c(%rax,%rcx,2)
movq -0x20(%rbp), %rax
movl -0x2c(%rbp), %ecx
addl $0x1, %ecx
movl %ecx, %ecx
movw $0x0, 0x50c(%rax,%rcx,2)
movl $0x9, %edi
callq 0x6c50
movw %ax, %dx
movq -0x20(%rbp), %rax
movl -0x2c(%rbp), %ecx
addl $0x2, %ecx
movl %ecx, %ecx
movw %dx, 0x50c(%rax,%rcx,2)
movq -0x20(%rbp), %rax
movl -0x2c(%rbp), %ecx
addl $0x3, %ecx
movl %ecx, %ecx
movw $0x0, 0x50c(%rax,%rcx,2)
movq -0x10(%rbp), %rdi
movzwl -0x22(%rbp), %esi
xorl %eax, %eax
movl %eax, %edx
callq 0x75a0
movl $0x1, -0x4(%rbp)
jmp 0xb7fc
jmp 0xb6bf
movl -0x2c(%rbp), %eax
addl $0x4, %eax
movl %eax, -0x2c(%rbp)
jmp 0xb5f3
movq -0x20(%rbp), %rax
movzwl 0x50c(%rax), %edi
callq 0x6c50
movzwl %ax, %eax
cmpl $0x0, %eax
jne 0xb7e3
movq -0x10(%rbp), %rdi
callq 0x80a0
movw %ax, -0x432(%rbp)
movzwl %ax, %eax
cmpl $0x0, %eax
jne 0xb770
movq 0x128f0(%rip), %rax     # 0x1dff8
movq (%rax), %rdi
movq -0x10(%rbp), %rdx
addq $0x8, %rdx
leaq 0xe55a(%rip), %rsi      # 0x19c74
movb $0x0, %al
callq 0x31c0
leaq 0x13ac0(%rip), %rax     # 0x1f1e8
cmpq $0x0, (%rax)
je 0xb764
leaq 0x13ab3(%rip), %rax     # 0x1f1e8
movq (%rax), %rax
movq 0x12881(%rip), %rcx     # 0x1dfc0
cmpq (%rcx), %rax
je 0xb764
leaq 0x13a9d(%rip), %rax     # 0x1f1e8
movq (%rax), %rdi
movq -0x10(%rbp), %rdx
addq $0x8, %rdx
leaq 0xe517(%rip), %rsi      # 0x19c74
movb $0x0, %al
callq 0x31c0
movl $0x0, -0x4(%rbp)
jmp 0xb7fc
leaq -0x430(%rbp), %rdi
xorl %esi, %esi
movl $0x400, %edx            # imm = 0x400
callq 0x3170
movq -0x10(%rbp), %rdi
movzwl -0x432(%rbp), %esi
leaq -0x430(%rbp), %rdx
callq 0x75a0
cmpl $0x0, %eax
jne 0xb7a8
movl $0x0, -0x4(%rbp)
jmp 0xb7fc
movzwl -0x432(%rbp), %edi
callq 0x6c50
movw %ax, %cx
movq -0x20(%rbp), %rax
movw %cx, 0x50c(%rax)
movq -0x10(%rbp), %rdi
movzwl -0x22(%rbp), %esi
xorl %eax, %eax
movl %eax, %edx
callq 0x75a0
cmpl $0x0, %eax
jne 0xb7e1
movl $0x0, -0x4(%rbp)
jmp 0xb7fc
jmp 0xb7e3
movq -0x20(%rbp), %rax
movzwl 0x50c(%rax), %edi
callq 0x6c50
movw %ax, -0x22(%rbp)
jmp 0xb5ca
movl -0x4(%rbp), %eax
addq $0x440, %rsp            # imm = 0x440
popq %rbp
retq
nopl (%rax,%rax)