// File: /workspace/github_c_makefile_star2_O0/simh[P]simtools/converters/fsio/dosmt.c
// Function: dosmtOpenFileW
static void *dosmtOpenFileW(
  struct mountedFS *mount,
  uint8_t UNUSED(unit),
  char *fname,
  off_t UNUSED(size)
)
{
  struct dosmtOpenFile *file;
  struct dosmtFileSpec spec;
  struct dosmthdr hdr;
  struct tm tm;
  time_t now = time(NULL);
  uint16_t today;

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

  if ((file = malloc(sizeof(struct dosmtOpenFile))) != NULL) {
    /*
     * 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 interpret it correctly.
     */
    localtime_r(&now, &tm);
    tm.tm_year -= 28;

    today = ((tm.tm_year - 70) * 1000) + tm.tm_yday + 1;

    hdr.fname[0] = htole16(spec.name[0]);
    hdr.fname[1] = htole16(spec.name[1]);
    if ((mount->flags & FS_DOSMTEXT) != 0)
      hdr.fname3 = htole16(spec.name[2]);
    else hdr.fname3 = 0;
    hdr.ext = htole16(spec.ext);
    hdr.prog = spec.prog;
    hdr.proj = spec.proj;
    hdr.prot = htole16(mount->dosmtdata.prot);
    hdr.date = htole16(today);

    if (tapeWriteRecord(mount->container, &hdr, sizeof(hdr)) == 0) {
      free(file);
      return NULL;
    }

    file->mode = M_WR;
    file->mount = mount;
    file->nextb = 0;
    file->tm = 0;
    file->error = 0;
  }
  return file;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xa0, %rsp
movb %sil, %al
movq %rdi, -0x10(%rbp)
movb %al, -0x11(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
xorl %eax, %eax
movl %eax, %edi
callq 0x31e0
movq %rax, -0x90(%rbp)
movq -0x10(%rbp), %rdi
movq -0x20(%rbp), %rsi
leaq -0x3c(%rbp), %rdx
xorl %ecx, %ecx
callq 0x10380
cmpl $0x0, %eax
jne 0x1196e
movq 0xc6ac(%rip), %rax      # 0x1dff8
movq (%rax), %rdi
movq -0x20(%rbp), %rdx
leaq 0x85a2(%rip), %rsi      # 0x19efc
movb $0x0, %al
callq 0x31c0
movq $0x0, -0x8(%rbp)
jmp 0x11aab
movl $0x218, %edi            # imm = 0x218
callq 0x3200
movq %rax, -0x30(%rbp)
cmpq $0x0, %rax
je 0x11aa3
leaq -0x90(%rbp), %rdi
leaq -0x88(%rbp), %rsi
callq 0x30a0
movl -0x74(%rbp), %eax
subl $0x1c, %eax
movl %eax, -0x74(%rbp)
movl -0x74(%rbp), %eax
subl $0x46, %eax
imull $0x3e8, %eax, %eax      # imm = 0x3E8
addl -0x6c(%rbp), %eax
addl $0x1, %eax
movw %ax, -0x92(%rbp)
movzwl -0x3a(%rbp), %edi
callq 0x10b80
movw %ax, -0x4a(%rbp)
movzwl -0x38(%rbp), %edi
callq 0x10b80
movw %ax, -0x48(%rbp)
movq -0x10(%rbp), %rax
movzwl 0x30(%rax), %eax
andl $0x100, %eax            # imm = 0x100
cmpl $0x0, %eax
je 0x119f6
movzwl -0x36(%rbp), %edi
callq 0x10b80
movw %ax, -0x3e(%rbp)
jmp 0x119fc
movw $0x0, -0x3e(%rbp)
movzwl -0x34(%rbp), %edi
callq 0x10b80
movw %ax, -0x46(%rbp)
movb -0x31(%rbp), %al
movb %al, -0x44(%rbp)
movb -0x32(%rbp), %al
movb %al, -0x43(%rbp)
movq -0x10(%rbp), %rax
movzbl 0x252(%rax), %eax
movzwl %ax, %edi
callq 0x10b80
movw %ax, -0x42(%rbp)
movzwl -0x92(%rbp), %edi
callq 0x10b80
movw %ax, -0x40(%rbp)
movq -0x10(%rbp), %rax
movq 0x38(%rax), %rdi
leaq -0x4a(%rbp), %rsi
movl $0xe, %edx
callq 0x65e0
cmpl $0x0, %eax
jne 0x11a6a
movq -0x30(%rbp), %rdi
callq 0x3040
movq $0x0, -0x8(%rbp)
jmp 0x11aab
movq -0x30(%rbp), %rax
movl $0x1, (%rax)
movq -0x10(%rbp), %rcx
movq -0x30(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x30(%rbp), %rax
movw $0x0, 0x210(%rax)
movq -0x30(%rbp), %rax
movb $0x0, 0x212(%rax)
movq -0x30(%rbp), %rax
movb $0x0, 0x213(%rax)
movq -0x30(%rbp), %rax
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0xa0, %rsp
popq %rbp
retq
nopl (%rax,%rax)