// File: /workspace/github_c_makefile_star2_O2/simh[P]simtools/converters/fsio/dos11.c
// Function: dos11OpenFileW
static void *dos11OpenFileW(
  struct mountedFS *mount,
  uint8_t UNUSED(unit),
  char *fname,
  off_t size
)
{
  struct DOS11data *data = &mount->dos11data;
  struct dos11OpenFile *file;
  struct dos11FileSpec spec;
  unsigned long contig = (size + mount->blocksz - 1) / mount->blocksz;
  uint8_t user = data->user;
  uint8_t group = data->group;

  if (SWISSET('c') && (contig == 0)) {
      fprintf(stderr,"DOS-11 contiguous files must be at least 1 block\n");
      return NULL;
  }

  if (dos11ParseFilespec(fname, &spec, user, group, DOS11_M_NONE) == 0) {
    fprintf(stderr, "Failed to parse filename \"%s\"\n", fname);
    return NULL;
  }

  if ((file = malloc(sizeof(struct dos11OpenFile))) != NULL) {
    memset(file, 0, sizeof(struct dos11OpenFile));

    if (dos11LookupFile(mount, &spec, file) == 0) {
      /*
       * Allocate local bufferspace for the file.
       */
      if ((file->buffer = malloc(mount->blocksz)) != NULL) {
        if (dos11CreateFile(mount, &spec, file, contig) == 0) {
          ERROR("Failed to create file \"%s\"\n", fname);
          free(file->buffer);
          free(file);
          return NULL;
        } else file->mode = M_WR;
      } else {
        ERROR("Buffer allocation failure for \"%s\"\n", fname);
        free(file);
        return NULL;
      }
    } else {
      ERROR("File \"%s\" already exists\n", fname);
      free(file);
      return NULL;
    }
  } else fprintf(stderr, "Memory allocation failure\n");
  return file;
}

// Assembly:
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rdx, %r14
movq %rdi, %r15
movq 0x28(%rdi), %rsi
decq %rcx
leaq (%rcx,%rsi), %rax
xorl %edx, %edx
divq %rsi
movq %rax, %r12
addq %rsi, %rcx
jae 0x7d80
leaq 0x10263(%rip), %rax     # 0x17fd8
movl (%rax), %eax
andl $0x4, %eax
jne 0x7e14
movzbl 0x954(%r15), %ecx
movzbl 0x955(%r15), %edx
leaq 0xe(%rsp), %rsi
movq %r14, %rdi
xorl %r8d, %r8d
callq 0x66c1
testl %eax, %eax
je 0x7e29
pushq $0x1
popq %rdi
pushq $0x38
popq %rsi
callq 0x31c0
testq %rax, %rax
je 0x7e4b
movq %rax, %rbx
leaq 0xe(%rsp), %rsi
movq %r15, %rdi
movq %rax, %rdx
callq 0x6284
testl %eax, %eax
jne 0x7e6c
movq 0x28(%r15), %rdi
callq 0x3220
movq %rax, 0x28(%rbx)
testq %rax, %rax
je 0x7eb3
leaq 0xe(%rsp), %rsi
movq %r15, %rdi
movq %rbx, %rdx
movq %r12, %rcx
callq 0x599f
testl %eax, %eax
je 0x7efc
movl $0x1, 0x18(%rbx)
jmp 0x7f56
movq 0xf1dd(%rip), %rax      # 0x16ff8
movq (%rax), %rcx
leaq 0x8fd1(%rip), %rdi      # 0x10df6
pushq $0x31
jmp 0x7e5e
movq 0xf1c8(%rip), %rax      # 0x16ff8
movq (%rax), %rdi
leaq 0x8f9d(%rip), %rsi      # 0x10dd7
xorl %ebx, %ebx
movq %r14, %rdx
xorl %eax, %eax
callq 0x31e0
jmp 0x7f56
movq 0xf1a6(%rip), %rax      # 0x16ff8
movq (%rax), %rcx
leaq 0x9026(%rip), %rdi      # 0x10e82
pushq $0x1a
popq %rsi
pushq $0x1
popq %rdx
callq 0x32d0
jmp 0x7f54
movq 0xf185(%rip), %rax      # 0x16ff8
movq (%rax), %rdi
leaq 0x8feb(%rip), %rsi      # 0x10e68
movq %r14, %rdx
xorl %eax, %eax
callq 0x31e0
leaq 0x1013a(%rip), %rax     # 0x17fc8
movq (%rax), %rdi
testq %rdi, %rdi
je 0x7f4c
movq 0xf11f(%rip), %rax      # 0x16fc0
cmpq (%rax), %rdi
je 0x7f4c
leaq 0x8fb7(%rip), %rsi      # 0x10e68
jmp 0x7ef0
movq 0xf13e(%rip), %rax      # 0x16ff8
movq (%rax), %rdi
leaq 0x8f80(%rip), %rsi      # 0x10e44
movq %r14, %rdx
xorl %eax, %eax
callq 0x31e0
leaq 0x100f3(%rip), %rax     # 0x17fc8
movq (%rax), %rdi
testq %rdi, %rdi
je 0x7f4c
movq 0xf0dc(%rip), %rax      # 0x16fc0
cmpq (%rax), %rdi
je 0x7f4c
leaq 0x8f54(%rip), %rsi      # 0x10e44
movq %r14, %rdx
xorl %eax, %eax
callq 0x31e0
jmp 0x7f4c
movq 0xf0f5(%rip), %rax      # 0x16ff8
movq (%rax), %rdi
leaq 0x8f1b(%rip), %rsi      # 0x10e28
movq %r14, %rdx
xorl %eax, %eax
callq 0x31e0
leaq 0x100aa(%rip), %rax     # 0x17fc8
movq (%rax), %rdi
testq %rdi, %rdi
je 0x7f43
movq 0xf093(%rip), %rax      # 0x16fc0
cmpq (%rax), %rdi
je 0x7f43
leaq 0x8eef(%rip), %rsi      # 0x10e28
movq %r14, %rdx
xorl %eax, %eax
callq 0x31e0
movq 0x28(%rbx), %rdi
callq 0x3050
movq %rbx, %rdi
callq 0x3050
xorl %ebx, %ebx
movq %rbx, %rax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
retq