// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x50, %rsp
movb %sil, %al
movq %rdi, -0x10(%rbp)
movb %al, -0x11(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movw $0x0, -0x44(%rbp)
cmpq $0x0, -0x28(%rbp)
je 0x157e3
movq -0x28(%rbp), %rax
addq $0x180, %rax            # imm = 0x180
subq $0x1, %rax
movl $0x180, %ecx            # imm = 0x180
cqto
idivq %rcx
movw %ax, -0x44(%rbp)
movq -0x20(%rbp), %rdi
leaq -0x42(%rbp), %rsi
xorl %edx, %edx
callq 0x135b0
cmpl $0x0, %eax
jne 0x15820
movq 0x87fa(%rip), %rax      # 0x1dff8
movq (%rax), %rdi
movq -0x20(%rbp), %rdx
leaq 0x46f0(%rip), %rsi      # 0x19efc
movb $0x0, %al
callq 0x31c0
movq $0x0, -0x8(%rbp)
jmp 0x15a8a
movl $0x50, %edi
callq 0x3200
movq %rax, -0x30(%rbp)
cmpq $0x0, %rax
je 0x15a2d
movq -0x30(%rbp), %rdi
xorl %esi, %esi
movl $0x50, %edx
callq 0x3170
movq -0x10(%rbp), %rdi
movb -0x11(%rbp), %al
movq -0x30(%rbp), %rcx
leaq -0x42(%rbp), %rdx
movzbl %al, %esi
callq 0x14ad0
cmpl $0x0, %eax
jne 0x159bd
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rdi
callq 0x3200
movq -0x30(%rbp), %rcx
movq %rax, 0x30(%rcx)
cmpq $0x0, %rax
je 0x1594a
movq -0x30(%rbp), %rax
movq 0x30(%rax), %rdi
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rdx
xorl %esi, %esi
callq 0x3170
movq -0x10(%rbp), %rdi
movb -0x11(%rbp), %al
movq -0x30(%rbp), %rcx
leaq -0x42(%rbp), %rdx
movzbl %al, %esi
movzwl -0x44(%rbp), %r8d
callq 0x13920
cmpl $0x0, %eax
jne 0x1593d
movq 0x8732(%rip), %rax      # 0x1dff8
movq (%rax), %rdi
movq -0x20(%rbp), %rdx
leaq 0x4679(%rip), %rsi      # 0x19f4d
movb $0x0, %al
callq 0x31c0
leaq 0x9906(%rip), %rax      # 0x1f1e8
cmpq $0x0, (%rax)
je 0x1591a
leaq 0x98f9(%rip), %rax      # 0x1f1e8
movq (%rax), %rax
movq 0x86c7(%rip), %rcx      # 0x1dfc0
cmpq (%rcx), %rax
je 0x1591a
leaq 0x98e3(%rip), %rax      # 0x1f1e8
movq (%rax), %rdi
movq -0x20(%rbp), %rdx
leaq 0x463a(%rip), %rsi      # 0x19f4d
movb $0x0, %al
callq 0x31c0
movq -0x30(%rbp), %rax
movq 0x30(%rax), %rdi
callq 0x3040
movq -0x30(%rbp), %rdi
callq 0x3040
movq $0x0, -0x8(%rbp)
jmp 0x15a8a
movq -0x30(%rbp), %rax
movl $0x1, 0x18(%rax)
jmp 0x159bb
movq 0x86a7(%rip), %rax      # 0x1dff8
movq (%rax), %rdi
movq -0x20(%rbp), %rdx
leaq 0x460a(%rip), %rsi      # 0x19f69
movb $0x0, %al
callq 0x31c0
leaq 0x987b(%rip), %rax      # 0x1f1e8
cmpq $0x0, (%rax)
je 0x159a5
leaq 0x986e(%rip), %rax      # 0x1f1e8
movq (%rax), %rax
movq 0x863c(%rip), %rcx      # 0x1dfc0
cmpq (%rcx), %rax
je 0x159a5
leaq 0x9858(%rip), %rax      # 0x1f1e8
movq (%rax), %rdi
movq -0x20(%rbp), %rdx
leaq 0x45cb(%rip), %rsi      # 0x19f69
movb $0x0, %al
callq 0x31c0
movq -0x30(%rbp), %rdi
callq 0x3040
movq $0x0, -0x8(%rbp)
jmp 0x15a8a
jmp 0x15a2b
movq 0x8634(%rip), %rax      # 0x1dff8
movq (%rax), %rdi
movq -0x20(%rbp), %rdx
leaq 0x45bb(%rip), %rsi      # 0x19f8d
movb $0x0, %al
callq 0x31c0
leaq 0x9808(%rip), %rax      # 0x1f1e8
cmpq $0x0, (%rax)
je 0x15a18
leaq 0x97fb(%rip), %rax      # 0x1f1e8
movq (%rax), %rax
movq 0x85c9(%rip), %rcx      # 0x1dfc0
cmpq (%rcx), %rax
je 0x15a18
leaq 0x97e5(%rip), %rax      # 0x1f1e8
movq (%rax), %rdi
movq -0x20(%rbp), %rdx
leaq 0x457c(%rip), %rsi      # 0x19f8d
movb $0x0, %al
callq 0x31c0
movq -0x30(%rbp), %rdi
callq 0x3040
movq $0x0, -0x8(%rbp)
jmp 0x15a8a
jmp 0x15a82
movq 0x85c4(%rip), %rax      # 0x1dff8
movq (%rax), %rdi
leaq 0x4569(%rip), %rsi      # 0x19fa7
movb $0x0, %al
callq 0x31c0
leaq 0x979c(%rip), %rax      # 0x1f1e8
cmpq $0x0, (%rax)
je 0x15a80
leaq 0x978f(%rip), %rax      # 0x1f1e8
movq (%rax), %rax
movq 0x855d(%rip), %rcx      # 0x1dfc0
cmpq (%rcx), %rax
je 0x15a80
leaq 0x9779(%rip), %rax      # 0x1f1e8
movq (%rax), %rdi
leaq 0x452e(%rip), %rsi      # 0x19fa7
movb $0x0, %al
callq 0x31c0
jmp 0x15a82
movq -0x30(%rbp), %rax
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x50, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)