// File: /workspace/github_c_makefile_star2_O1/atomlang[P]atomlang/cli/modules.c
// Function: fileOpen
static void _fileOpen(PKVM* vm) {

  int argc = pkGetArgc(vm);
  if (!pkCheckArgcRange(vm, argc, 1, 2)) return;

  const char* path;
  if (!pkGetArgString(vm, 1, &path, NULL)) return;

  const char* mode_str = "r";
  FileAccessMode mode = FMODE_READ;

  if (argc == 2) {
    if (!pkGetArgString(vm, 2, &mode_str, NULL)) return;

    // Check if the mode string is valid, and update the mode value.
    do {
      if (strcmp(mode_str, "r")  == 0) { mode = FMODE_READ;       break; }
      if (strcmp(mode_str, "w")  == 0) { mode = FMODE_WRITE;      break; }
      if (strcmp(mode_str, "a")  == 0) { mode = FMODE_APPEND;     break; }
      if (strcmp(mode_str, "r+") == 0) { mode = FMODE_READ_EXT;   break; }
      if (strcmp(mode_str, "w+") == 0) { mode = FMODE_WRITE_EXT;  break; }
      if (strcmp(mode_str, "a+") == 0) { mode = FMODE_APPEND_EXT; break; }

      // TODO: (fmt, ...) va_arg for runtime error public api.
      // If we reached here, that means it's an invalid mode string.
      pkSetRuntimeError(vm, "Invalid mode string.");
      return;
    } while (false);
  }

  FILE* fp = fopen(path, mode_str);

  if (fp != NULL) {
    File* file = NEW_OBJ(File);
    initObj(&file->_super, OBJ_FILE);
    file->fp = fp;
    file->mode = mode;
    file->closed = false;

    pkReturnInstNative(vm, (void*)file, OBJ_FILE);

  } else {
    pkReturnNull(vm);
  }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %rbx
subq $0x18, %rsp
movq %rdi, %rbx
callq 0x1345e
movl %eax, %ebp
movq %rbx, %rdi
movl %eax, %esi
movl $0x1, %edx
movl $0x2, %ecx
callq 0x1347f
testb %al, %al
je 0x1a707
movl $0x1, %r15d
leaq 0x10(%rsp), %rdx
movq %rbx, %rdi
movl $0x1, %esi
xorl %ecx, %ecx
callq 0x136ed
testb %al, %al
je 0x1a707
leaq 0x47dc(%rip), %rax      # 0x1edba
movq %rax, 0x8(%rsp)
cmpl $0x2, %ebp
jne 0x1a6ba
leaq 0x8(%rsp), %rdx
movq %rbx, %rdi
movl $0x2, %esi
xorl %ecx, %ecx
callq 0x136ed
testb %al, %al
je 0x1a707
movq 0x8(%rsp), %r14
leaq 0x47a6(%rip), %rsi      # 0x1edba
movq %r14, %rdi
callq 0x2230
testl %eax, %eax
je 0x1a6ba
leaq 0x45fe(%rip), %rsi      # 0x1ec29
movq %r14, %rdi
callq 0x2230
testl %eax, %eax
je 0x1a694
leaq 0x4490(%rip), %rsi      # 0x1eace
movq %r14, %rdi
callq 0x2230
testl %eax, %eax
je 0x1a69c
leaq 0x60e2(%rip), %rsi      # 0x20733
movq %r14, %rdi
callq 0x2230
testl %eax, %eax
je 0x1a6a4
leaq 0x60d2(%rip), %rsi      # 0x20736
movq %r14, %rdi
callq 0x2230
testl %eax, %eax
je 0x1a6ac
leaq 0x60c2(%rip), %rsi      # 0x20739
movq %r14, %rdi
callq 0x2230
testl %eax, %eax
je 0x1a6b4
leaq 0x60b2(%rip), %rsi      # 0x2073c
movq %rbx, %rdi
callq 0xe3b6
jmp 0x1a707
movl $0x2, %r15d
jmp 0x1a6ba
movl $0x4, %r15d
jmp 0x1a6ba
movl $0x9, %r15d
jmp 0x1a6ba
movl $0xa, %r15d
jmp 0x1a6ba
movl $0xc, %r15d
movq 0x10(%rsp), %rdi
movq 0x8(%rsp), %rsi
callq 0x2310
testq %rax, %rax
je 0x1a6ff
movq %rax, %r14
movl $0x18, %edi
callq 0x22b0
movl $0x1, (%rax)
movq %r14, 0x8(%rax)
movl %r15d, 0x10(%rax)
movb $0x0, 0x14(%rax)
movq %rbx, %rdi
movq %rax, %rsi
movl $0x1, %edx
callq 0x13b29
jmp 0x1a707
movq %rbx, %rdi
callq 0x13a3e
addq $0x18, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq