// File: /workspace/github_c_makefile_star2_O0/hoobaa[P]ficl/fileaccess.c
// Function: ficlFileOpen
static void ficlFileOpen(ficlVm *vm, char *writeMode) /* ( c-addr u fam -- fileid ior ) */
{
    int fam = ficlStackPopInteger(vm->dataStack);
    int length = ficlStackPopInteger(vm->dataStack);
    void *address = (void *)ficlStackPopPointer(vm->dataStack);
    char mode[4];
    FILE *f;
    char *filename = (char *)malloc(length + 1);
    memcpy(filename, address, length);
    filename[length] = 0;

    *mode = 0;

    switch (FICL_FAM_OPEN_MODE(fam))
        {
        case 0:
            ficlStackPushPointer(vm->dataStack, NULL);
            ficlStackPushInteger(vm->dataStack, EINVAL);
            goto EXIT;
        case FICL_FAM_READ:
            strcat(mode, "r");
            break;
        case FICL_FAM_WRITE:
            strcat(mode, writeMode);
            break;
        case FICL_FAM_READ | FICL_FAM_WRITE:
            strcat(mode, writeMode);
            strcat(mode, "+");
            break;
        }

    strcat(mode, (fam & FICL_FAM_BINARY) ? "b" : "t");

    f = fopen(filename, mode);
    if (f == NULL)
        ficlStackPushPointer(vm->dataStack, NULL);
    else
        {
        ficlFile *ff = (ficlFile *)malloc(sizeof(ficlFile));
        strcpy(ff->filename, filename);
        ff->f = f;
        ficlStackPushPointer(vm->dataStack, ff);

        fseek(f, 0, SEEK_SET);
        }
    pushIor(vm, f != NULL);
	
EXIT:
	free(filename);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x8(%rbp), %rax
movq 0x60(%rax), %rdi
callq 0x4b70
movl %eax, -0x14(%rbp)
movq -0x8(%rbp), %rax
movq 0x60(%rax), %rdi
callq 0x4b70
movl %eax, -0x18(%rbp)
movq -0x8(%rbp), %rax
movq 0x60(%rax), %rdi
callq 0x4b30
movq %rax, -0x20(%rbp)
movl -0x18(%rbp), %eax
incl %eax
movslq %eax, %rdi
callq 0x3210
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rdi
movq -0x20(%rbp), %rsi
movslq -0x18(%rbp), %rdx
callq 0x31e0
movq -0x38(%rbp), %rax
movslq -0x18(%rbp), %rcx
movb $0x0, (%rax,%rcx)
movb $0x0, -0x24(%rbp)
movl -0x14(%rbp), %eax
andl $0x7, %eax
movl %eax, %ecx
movq %rcx, -0x48(%rbp)
subl $0x3, %eax
ja 0x130ee
movq -0x48(%rbp), %rax
leaq 0x3839(%rip), %rcx      # 0x168b8
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq -0x8(%rbp), %rax
movq 0x60(%rax), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x4c60
movq -0x8(%rbp), %rax
movq 0x60(%rax), %rdi
movl $0x16, %esi
callq 0x4c90
jmp 0x1319d
leaq -0x24(%rbp), %rdi
leaq 0x2b93(%rip), %rsi      # 0x15c4e
callq 0x32c0
jmp 0x130ee
leaq -0x24(%rbp), %rdi
movq -0x10(%rbp), %rsi
callq 0x32c0
jmp 0x130ee
leaq -0x24(%rbp), %rdi
movq -0x10(%rbp), %rsi
callq 0x32c0
leaq -0x24(%rbp), %rdi
leaq 0x2a77(%rip), %rsi      # 0x15b60
callq 0x32c0
leaq -0x24(%rbp), %rdi
movl -0x14(%rbp), %ecx
andl $0x8, %ecx
leaq 0x2dad(%rip), %rsi      # 0x15eac
leaq 0x3886(%rip), %rax      # 0x1698c
cmpl $0x0, %ecx
cmovneq %rax, %rsi
callq 0x32c0
movq -0x38(%rbp), %rdi
leaq -0x24(%rbp), %rsi
callq 0x3290
movq %rax, -0x30(%rbp)
cmpq $0x0, -0x30(%rbp)
jne 0x1313d
movq -0x8(%rbp), %rax
movq 0x60(%rax), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x4c60
jmp 0x13187
movl $0x108, %edi            # imm = 0x108
callq 0x3210
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rdi
addq $0x8, %rdi
movq -0x38(%rbp), %rsi
callq 0x3070
movq -0x30(%rbp), %rcx
movq -0x40(%rbp), %rax
movq %rcx, (%rax)
movq -0x8(%rbp), %rax
movq 0x60(%rax), %rdi
movq -0x40(%rbp), %rsi
callq 0x4c60
movq -0x30(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
xorl %edx, %edx
callq 0x3240
movq -0x8(%rbp), %rdi
cmpq $0x0, -0x30(%rbp)
setne %al
andb $0x1, %al
movzbl %al, %esi
callq 0x131b0
movq -0x38(%rbp), %rdi
callq 0x3030
addq $0x50, %rsp
popq %rbp
retq
nopl (%rax)