// File: /workspace/github_c_makefile_star2_O2/simh[P]simtools/converters/fsio/os8.c
// Function: os8OpenFileW
void *os8OpenFileW(
  struct mountedFS *mount,
  uint8_t unit,
  char *fname,
  off_t size
)
{
  struct os8OpenFile *file;
  struct os8FileSpec spec;
  uint16_t blocks = 0;

  if (size != 0)
    blocks = (size + ((OS8_BLOCKSIZE * 3) / 2) - 1) / (((OS8_BLOCKSIZE) * 3) / 2);

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

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

    if (os8LookupFile(mount, unit, &spec, file) == 0) {
      /*
       * Allocate local buffer space for the file.\
       */
      if ((file->buffer = malloc(mount->blocksz)) != NULL) {
        memset(file->buffer, 0, mount->blocksz);

        if (os8CreateFile(mount, unit, &spec, file, blocks) == 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 ERROR("Memory allocation failure\n");
  return file;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x20, %rsp
movq %rdx, %r14
movl %esi, %ebp
movq %rdi, %r15
testq %rcx, %rcx
je 0xd756
addq $0x17f, %rcx            # imm = 0x17F
movl $0x180, %esi            # imm = 0x180
movq %rcx, %rax
cqto
idivq %rsi
movq %rax, %r12
jmp 0xd759
xorl %r12d, %r12d
leaq 0xe(%rsp), %rsi
movq %r14, %rdi
xorl %edx, %edx
callq 0xc779
testl %eax, %eax
je 0xd7f3
pushq $0x1
popq %rdi
pushq $0x50
popq %rsi
callq 0x31c0
testq %rax, %rax
je 0xd812
movq %rax, %rbx
movzbl %bpl, %ebp
leaq 0xe(%rsp), %rdx
movq %r15, %rdi
movl %ebp, %esi
movq %rax, %rcx
callq 0xd129
testl %eax, %eax
jne 0xd862
movq 0x28(%r15), %rsi
pushq $0x1
popq %rdi
callq 0x31c0
movq %rax, 0x30(%rbx)
testq %rax, %rax
je 0xd8a9
movzwl %r12w, %r8d
leaq 0xe(%rsp), %rdx
movq %r15, %rdi
movl %ebp, %esi
movq %rbx, %rcx
callq 0xc953
testl %eax, %eax
je 0xd8f2
movl $0x1, 0x18(%rbx)
movq %rbx, %rax
addq $0x20, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
movq 0x97fe(%rip), %rax      # 0x16ff8
movq (%rax), %rdi
leaq 0x35d3(%rip), %rsi      # 0x10dd7
xorl %ebx, %ebx
movq %r14, %rdx
xorl %eax, %eax
callq 0x31e0
jmp 0xd7e3
movq 0x97df(%rip), %rax      # 0x16ff8
movq (%rax), %rcx
leaq 0x365f(%rip), %rdi      # 0x10e82
pushq $0x1a
popq %rsi
pushq $0x1
popq %rdx
callq 0x32d0
leaq 0xa793(%rip), %rax      # 0x17fc8
movq (%rax), %rcx
xorl %ebx, %ebx
testq %rcx, %rcx
je 0xd7e3
movq 0x977a(%rip), %rax      # 0x16fc0
cmpq (%rax), %rcx
je 0xd7e3
leaq 0x3630(%rip), %rdi      # 0x10e82
pushq $0x1a
popq %rsi
pushq $0x1
popq %rdx
callq 0x32d0
jmp 0xd94a
movq 0x978f(%rip), %rax      # 0x16ff8
movq (%rax), %rdi
leaq 0x35f5(%rip), %rsi      # 0x10e68
movq %r14, %rdx
xorl %eax, %eax
callq 0x31e0
leaq 0xa744(%rip), %rax      # 0x17fc8
movq (%rax), %rdi
testq %rdi, %rdi
je 0xd942
movq 0x9729(%rip), %rax      # 0x16fc0
cmpq (%rax), %rdi
je 0xd942
leaq 0x35c1(%rip), %rsi      # 0x10e68
jmp 0xd8e6
movq 0x9748(%rip), %rax      # 0x16ff8
movq (%rax), %rdi
leaq 0x358a(%rip), %rsi      # 0x10e44
movq %r14, %rdx
xorl %eax, %eax
callq 0x31e0
leaq 0xa6fd(%rip), %rax      # 0x17fc8
movq (%rax), %rdi
testq %rdi, %rdi
je 0xd942
movq 0x96e6(%rip), %rax      # 0x16fc0
cmpq (%rax), %rdi
je 0xd942
leaq 0x355e(%rip), %rsi      # 0x10e44
movq %r14, %rdx
xorl %eax, %eax
callq 0x31e0
jmp 0xd942
movq 0x96ff(%rip), %rax      # 0x16ff8
movq (%rax), %rdi
leaq 0x3525(%rip), %rsi      # 0x10e28
movq %r14, %rdx
xorl %eax, %eax
callq 0x31e0
leaq 0xa6b4(%rip), %rax      # 0x17fc8
movq (%rax), %rdi
testq %rdi, %rdi
je 0xd939
movq 0x969d(%rip), %rax      # 0x16fc0
cmpq (%rax), %rdi
je 0xd939
leaq 0x34f9(%rip), %rsi      # 0x10e28
movq %r14, %rdx
xorl %eax, %eax
callq 0x31e0
movq 0x30(%rbx), %rdi
callq 0x3050
movq %rbx, %rdi
callq 0x3050
xorl %ebx, %ebx
jmp 0xd7e3