// File: /workspace/github_c_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rsi, %r12
movq %rdi, %rbx
movq 0x60(%rdi), %rdi
callq 0x422e
movq %rax, %r15
movq 0x60(%rbx), %rdi
callq 0x422e
movq %rax, %r13
movq 0x60(%rbx), %rdi
callq 0x420e
movq %rax, %rbp
leal 0x1(%r13), %eax
movslq %eax, %rdi
callq 0x3230
movq %rax, %r14
movslq %r13d, %r13
movq %rax, %rdi
movq %rbp, %rsi
movq %r13, %rdx
callq 0x3200
xorl %eax, %eax
movb %al, (%r14,%r13)
movb %al, 0x4(%rsp)
movl %r15d, %eax
andl $0x7, %eax
cmpl $0x3, %eax
ja 0xcb4f
leaq 0x2cbf(%rip), %rcx      # 0xf7a8
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq 0x60(%rbx), %rdi
xorl %esi, %esi
callq 0x4287
movq 0x60(%rbx), %rdi
pushq $0x16
popq %rsi
callq 0x4298
jmp 0xcbd4
leaq 0x4(%rsp), %rdi
movq %r12, %rsi
callq 0x32e0
jmp 0xcb4f
leaq 0x4(%rsp), %r13
movq %r13, %rdi
movq %r12, %rsi
callq 0x32e0
movq %r13, %rdi
callq 0x30e0
movw $0x2b, 0x4(%rsp,%rax)
jmp 0xcb4f
leaq 0x4(%rsp), %rdi
callq 0x30e0
movw $0x72, 0x4(%rsp,%rax)
leaq 0x4(%rsp), %r12
movq %r12, %rdi
callq 0x30e0
testb $0x8, %r15b
pushq $0x74
popq %rcx
pushq $0x62
popq %rdx
cmovel %ecx, %edx
movw %dx, 0x4(%rsp,%rax)
movq %r14, %rdi
movq %r12, %rsi
callq 0x32b0
movq %rax, %r15
testq %rax, %rax
je 0xcbb8
movl $0x108, %edi            # imm = 0x108
callq 0x3230
movq %rax, %r12
leaq 0x8(%rax), %rdi
movq %r14, %rsi
callq 0x3070
movq %r15, (%r12)
movq 0x60(%rbx), %rdi
movq %r12, %rsi
callq 0x4287
movq %r15, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x3260
jmp 0xcbc3
movq 0x60(%rbx), %rdi
xorl %esi, %esi
callq 0x4287
xorl %esi, %esi
testq %r15, %r15
setne %sil
movq %rbx, %rdi
callq 0xcbeb
movq %r14, %rdi
callq 0x3030
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq