// File: /workspace/github_c_makefile_star2_O2/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
pushq $0xa
popq %rdx
movq %r14, %rsi
callq 0x664c
cmpb $0x0, (%r14)
je 0xb557
leaq 0x1(%rsp), %r15
leaq 0x3698(%rip), %rsi      # 0xead7
movq %r15, %rdi
callq 0x32b0
movq %rax, %r14
testq %rax, %rax
jne 0xb483
leaq 0x4089(%rip), %rsi      # 0xf4df
movq %rbx, %rdi
callq 0x68f1
movq %rbx, %rdi
movq %r15, %rsi
callq 0x68f1
leaq 0x2ef9(%rip), %rsi      # 0xe369
movq %rbx, %rdi
callq 0x68f1
pushq $-0x38
popq %rsi
movq %rbx, %rdi
callq 0x6517
movq 0x78(%rbx), %r15
movq %r14, 0x78(%rbx)
leaq 0x100(%rsp), %r12
xorl %r13d, %r13d
xorl %ebp, %ebp
movq %r12, %rdi
movl $0x100, %esi            # imm = 0x100
movq %r14, %rdx
callq 0x3170
testq %rax, %rax
je 0xb51d
movq %r12, %rdi
callq 0x30e0
leal -0x1(%rax), %ecx
incl %ebp
testl %ecx, %ecx
jle 0xb498
movl %ecx, %ecx
cmpb $0xa, 0x100(%rsp,%rcx)
jne 0xb4da
movb $0x0, 0x100(%rsp,%rcx)
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
addq %rcx, %rax
movl %eax, %esi
movq %rbx, %rdi
movq %r12, %rdx
callq 0x6970
movl %eax, %r13d
orl $0x2, %eax
cmpl $0xfffffeff, %eax       # imm = 0xFFFFFEFF
je 0xb498
movq %r15, 0x78(%rbx)
movq %r14, %rdi
callq 0x30c0
movq %rbx, %rdi
leaq 0x3fea(%rip), %rsi      # 0xf4f4
leaq 0x1(%rsp), %rdx
movl %ebp, %ecx
xorl %eax, %eax
callq 0x6527
jmp 0xb498
orq $-0x1, 0x78(%rbx)
leaq 0x2e41(%rip), %rdx      # 0xe36a
movq %rbx, %rdi
xorl %esi, %esi
callq 0x6970
movq %r15, 0x78(%rbx)
movq %r14, %rdi
callq 0x30c0
cmpl $0xfffffefd, %r13d      # imm = 0xFFFFFEFD
jne 0xb566
movq %rbx, %rdi
movl $0xfffffefd, %esi       # imm = 0xFFFFFEFD
callq 0x6517
jmp 0xb566
leaq 0x3f5f(%rip), %rsi      # 0xf4bd
movq %rbx, %rdi
callq 0x68f1
addq $0x208, %rsp            # imm = 0x208
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq