// File: /workspace/github_c_makefile_star2_O3/hoobaa[P]ficl/extras.c
// Function: ficlPrimitiveLoad
static void ficlPrimitiveLoad(ficlVm *vm)
{
    char    buffer[BUFFER_SIZE];
    char    filename[BUFFER_SIZE];
    ficlCountedString *counted = (ficlCountedString *)filename;
    int     line = 0;
    FILE   *f;
    int     result = 0;
    ficlCell    oldSourceId;
	ficlString s;

    ficlVmGetString(vm, counted, '\n');

    if (FICL_COUNTED_STRING_GET_LENGTH(*counted) <= 0)
    {
        ficlVmTextOut(vm, "Warning (load): nothing happened\n");
        return;
    }

    /*
    ** get the file's size and make sure it exists 
    */

    f = fopen(FICL_COUNTED_STRING_GET_POINTER(*counted), "r");
    if (!f)
    {
        ficlVmTextOut(vm, "Unable to open file ");
        ficlVmTextOut(vm, FICL_COUNTED_STRING_GET_POINTER(*counted));
        ficlVmTextOut(vm, "\n");
        ficlVmThrow(vm, FICL_VM_STATUS_QUIT);
    }

    oldSourceId = vm->sourceId;
    vm->sourceId.p = (void *)f;

    /* feed each line to ficlExec */
    while (fgets(buffer, BUFFER_SIZE, f))
    {
        int length = strlen(buffer) - 1;

        line++;
        if (length <= 0)
            continue;

        if (buffer[length] == '\n')
            buffer[length--] = '\0';

		FICL_STRING_SET_POINTER(s, buffer);
		FICL_STRING_SET_LENGTH(s, length + 1);
        result = ficlVmExecuteString(vm, s);
        /* handle "bye" in loaded files. --lch */
        switch (result)
        {
            case FICL_VM_STATUS_OUT_OF_TEXT:
            case FICL_VM_STATUS_USER_EXIT:
                break;

            default:
                vm->sourceId = oldSourceId;
                fclose(f);
                ficlVmThrowError(vm, "Error loading file <%s> line %d", FICL_COUNTED_STRING_GET_POINTER(*counted), line);
                break; 
        }
    }
    /*
    ** Pass an empty line with SOURCE-ID == -1 to flush
    ** any pending REFILLs (as required by FILE wordset)
    */
    vm->sourceId.i = -1;
	FICL_STRING_SET_FROM_CSTRING(s, "");
    ficlVmExecuteString(vm, s);

    vm->sourceId = oldSourceId;
    fclose(f);

    /* handle "bye" in loaded files. --lch */
    if (result == FICL_VM_STATUS_USER_EXIT)
        ficlVmThrow(vm, FICL_VM_STATUS_USER_EXIT);
    return;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x208, %rsp            # imm = 0x208
movq %rdi, %rbx
movq %rsp, %r14
movq %r14, %rsi
movl $0xa, %edx
callq 0x68fc
cmpb $0x0, (%r14)
je 0xbc61
leaq 0x1(%rsp), %rdi
leaq 0x2fae(%rip), %rsi      # 0xead7
callq 0x32b0
movq %rax, %r14
testq %rax, %rax
jne 0xbb6e
leaq 0x39a2(%rip), %rsi      # 0xf4df
movq %rbx, %rdi
callq 0x6b89
movq %rbx, %rdi
leaq 0x1(%rsp), %rsi
callq 0x6b89
leaq 0x2810(%rip), %rsi      # 0xe369
movq %rbx, %rdi
callq 0x6b89
movq %rbx, %rdi
movl $0xffffffc8, %esi       # imm = 0xFFFFFFC8
callq 0x67c2
movq 0x78(%rbx), %r13
movq %r14, 0x78(%rbx)
leaq 0x100(%rsp), %rdi
movl $0x100, %esi            # imm = 0x100
movq %r14, %rdx
callq 0x3170
testq %rax, %rax
je 0xbc72
movl $0x1, %ebp
leaq 0x100(%rsp), %r12
xorl %r15d, %r15d
movq %r12, %rdi
callq 0x30e0
leal -0x1(%rax), %ecx
testl %ecx, %ecx
jle 0xbc0d
movl %ecx, %ecx
cmpb $0xa, 0x100(%rsp,%rcx)
jne 0xbbcf
movb $0x0, 0x100(%rsp,%rcx)
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
addq %rcx, %rax
movl %eax, %esi
movq %rbx, %rdi
movq %r12, %rdx
callq 0x6c0b
movl %eax, %r15d
orl $0x2, %eax
cmpl $0xfffffeff, %eax       # imm = 0xFFFFFEFF
je 0xbc0d
movq %r13, 0x78(%rbx)
movq %r14, %rdi
callq 0x30c0
movq %rbx, %rdi
leaq 0x38f5(%rip), %rsi      # 0xf4f4
leaq 0x1(%rsp), %rdx
movl %ebp, %ecx
xorl %eax, %eax
callq 0x67d2
movq %r12, %rdi
movl $0x100, %esi            # imm = 0x100
movq %r14, %rdx
callq 0x3170
incl %ebp
testq %rax, %rax
jne 0xbba4
movq $-0x1, 0x78(%rbx)
leaq 0x2737(%rip), %rdx      # 0xe36a
movq %rbx, %rdi
xorl %esi, %esi
callq 0x6c0b
movq %r13, 0x78(%rbx)
movq %r14, %rdi
callq 0x30c0
cmpl $0xfffffefd, %r15d      # imm = 0xFFFFFEFD
jne 0xbc97
movq %rbx, %rdi
movl $0xfffffefd, %esi       # imm = 0xFFFFFEFD
callq 0x67c2
jmp 0xbc97
leaq 0x3855(%rip), %rsi      # 0xf4bd
movq %rbx, %rdi
callq 0x6b89
jmp 0xbc97
movq $-0x1, 0x78(%rbx)
leaq 0x26e9(%rip), %rdx      # 0xe36a
movq %rbx, %rdi
xorl %esi, %esi
callq 0x6c0b
movq %r13, 0x78(%rbx)
movq %r14, %rdi
callq 0x30c0
addq $0x208, %rsp            # imm = 0x208
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq