// File: /workspace/github_c_makefile_star2_O0/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:
subq $0x38, %rsp
movq %rdi, 0x30(%rsp)
movq 0x30(%rsp), %rdi
callq 0x1b110
movl %eax, 0x2c(%rsp)
movq 0x30(%rsp), %rdi
movl 0x2c(%rsp), %esi
movl $0x1, %edx
movl $0x2, %ecx
callq 0x1b190
testb $0x1, %al
jne 0x25a38
jmp 0x25bed
movq 0x30(%rsp), %rdi
movl $0x1, %esi
leaq 0x20(%rsp), %rdx
xorl %eax, %eax
movl %eax, %ecx
callq 0x1b8d0
testb $0x1, %al
jne 0x25a59
jmp 0x25bed
leaq 0x64ea(%rip), %rax      # 0x2bf4a
movq %rax, 0x18(%rsp)
movl $0x1, 0x14(%rsp)
cmpl $0x2, 0x2c(%rsp)
jne 0x25b76
movq 0x30(%rsp), %rdi
movl $0x2, %esi
leaq 0x18(%rsp), %rdx
xorl %eax, %eax
movl %eax, %ecx
callq 0x1b8d0
testb $0x1, %al
jne 0x25a99
jmp 0x25bed
jmp 0x25a9b
movq 0x18(%rsp), %rdi
leaq 0x64a3(%rip), %rsi      # 0x2bf4a
callq 0x2220
cmpl $0x0, %eax
jne 0x25abe
movl $0x1, 0x14(%rsp)
jmp 0x25b74
movq 0x18(%rsp), %rdi
leaq 0x62ef(%rip), %rsi      # 0x2bdb9
callq 0x2220
cmpl $0x0, %eax
jne 0x25ae1
movl $0x2, 0x14(%rsp)
jmp 0x25b74
movq 0x18(%rsp), %rdi
leaq 0x6171(%rip), %rsi      # 0x2bc5e
callq 0x2220
cmpl $0x0, %eax
jne 0x25b01
movl $0x4, 0x14(%rsp)
jmp 0x25b74
movq 0x18(%rsp), %rdi
leaq 0x7db7(%rip), %rsi      # 0x2d8c4
callq 0x2220
cmpl $0x0, %eax
jne 0x25b21
movl $0x9, 0x14(%rsp)
jmp 0x25b74
movq 0x18(%rsp), %rdi
leaq 0x7d9a(%rip), %rsi      # 0x2d8c7
callq 0x2220
cmpl $0x0, %eax
jne 0x25b41
movl $0xa, 0x14(%rsp)
jmp 0x25b74
movq 0x18(%rsp), %rdi
leaq 0x7d7d(%rip), %rsi      # 0x2d8ca
callq 0x2220
cmpl $0x0, %eax
jne 0x25b61
movl $0xc, 0x14(%rsp)
jmp 0x25b74
movq 0x30(%rsp), %rdi
leaq 0x7d60(%rip), %rsi      # 0x2d8cd
callq 0x11660
jmp 0x25bed
jmp 0x25b76
movq 0x20(%rsp), %rdi
movq 0x18(%rsp), %rsi
callq 0x2300
movq %rax, 0x8(%rsp)
cmpq $0x0, 0x8(%rsp)
je 0x25be3
movl $0x18, %edi
callq 0x22b0
movq %rax, (%rsp)
movq (%rsp), %rdi
movl $0x1, %esi
callq 0x24ea0
movq 0x8(%rsp), %rcx
movq (%rsp), %rax
movq %rcx, 0x8(%rax)
movl 0x14(%rsp), %ecx
movq (%rsp), %rax
movl %ecx, 0x10(%rax)
movq (%rsp), %rax
movb $0x0, 0x14(%rax)
movq 0x30(%rsp), %rdi
movq (%rsp), %rsi
movl $0x1, %edx
callq 0x1c350
jmp 0x25bed
movq 0x30(%rsp), %rdi
callq 0x1c160
addq $0x38, %rsp
retq
nopw %cs:(%rax,%rax)