// File: /workspace/github_c_makefile_star2_O1/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 0x69a3
cmpb $0x0, (%r14)
je 0xbdc9
leaq 0x1(%rsp), %rdi
leaq 0x2e46(%rip), %rsi      # 0xead7
callq 0x32b0
movq %rax, %r14
testq %rax, %rax
jne 0xbcd6
leaq 0x383a(%rip), %rsi      # 0xf4df
movq %rbx, %rdi
callq 0x6c86
movq %rbx, %rdi
leaq 0x1(%rsp), %rsi
callq 0x6c86
leaq 0x26a8(%rip), %rsi      # 0xe369
movq %rbx, %rdi
callq 0x6c86
movq %rbx, %rdi
movl $0xffffffc8, %esi       # imm = 0xFFFFFFC8
callq 0x6869
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 0xbdda
movl $0x1, %ebp
leaq 0x100(%rsp), %r12
xorl %r15d, %r15d
movq %r12, %rdi
callq 0x30e0
leal -0x1(%rax), %ecx
testl %ecx, %ecx
jle 0xbd75
movl %ecx, %ecx
cmpb $0xa, 0x100(%rsp,%rcx)
jne 0xbd37
movb $0x0, 0x100(%rsp,%rcx)
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
addq %rcx, %rax
movl %eax, %esi
movq %rbx, %rdi
movq %r12, %rdx
callq 0x6d08
movl %eax, %r15d
orl $0x2, %eax
cmpl $0xfffffeff, %eax       # imm = 0xFFFFFEFF
je 0xbd75
movq %r13, 0x78(%rbx)
movq %r14, %rdi
callq 0x30c0
movq %rbx, %rdi
leaq 0x378d(%rip), %rsi      # 0xf4f4
leaq 0x1(%rsp), %rdx
movl %ebp, %ecx
xorl %eax, %eax
callq 0x6879
movq %r12, %rdi
movl $0x100, %esi            # imm = 0x100
movq %r14, %rdx
callq 0x3170
incl %ebp
testq %rax, %rax
jne 0xbd0c
movq $-0x1, 0x78(%rbx)
leaq 0x25cf(%rip), %rdx      # 0xe36a
movq %rbx, %rdi
xorl %esi, %esi
callq 0x6d08
movq %r13, 0x78(%rbx)
movq %r14, %rdi
callq 0x30c0
cmpl $0xfffffefd, %r15d      # imm = 0xFFFFFEFD
jne 0xbdff
movq %rbx, %rdi
movl $0xfffffefd, %esi       # imm = 0xFFFFFEFD
callq 0x6869
jmp 0xbdff
leaq 0x36ed(%rip), %rsi      # 0xf4bd
movq %rbx, %rdi
callq 0x6c86
jmp 0xbdff
movq $-0x1, 0x78(%rbx)
leaq 0x2581(%rip), %rdx      # 0xe36a
movq %rbx, %rdi
xorl %esi, %esi
callq 0x6d08
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