// File: Fohlen[P]cubescript-standalone/stream.cpp
// Function: loadfile(char const*, unsigned long*, bool)
char *loadfile(const char *fn, size_t *size, bool utf8)
{
    stream *f = openfile(fn, "rb");
    if(!f) return NULL;
    size_t len = f->size();
    if(len <= 0) { delete f; return NULL; }
    char *buf = new char[len+1];
    if(!buf) { delete f; return NULL; }
    size_t offset = 0;
    if(utf8 && len >= 3)
    {
        if(f->read(buf, 3) != 3) { delete f; delete[] buf; return NULL; }
        if(((uchar *)buf)[0] == 0xEF && ((uchar *)buf)[1] == 0xBB && ((uchar *)buf)[2] == 0xBF) len -= 3;
        else offset += 3;
    } 
    size_t rlen = f->read(&buf[offset], len-offset);
    delete f;
    if(rlen != len-offset) { delete[] buf; return NULL; }
    if(utf8) len = decodeutf8((uchar *)buf, len, (uchar *)buf, len);
    buf[len] = '\0';
    if(size!=NULL) *size = len;
    return buf;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x80, %rsp
movb %dl, %al
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
andb $0x1, %al
movb %al, -0x19(%rbp)
movq -0x10(%rbp), %rdi
leaq 0x5457(%rip), %rsi      # 0x1ee3c
callq 0x197b0
movq %rax, -0x28(%rbp)
cmpq $0x0, -0x28(%rbp)
jne 0x19a02
movq $0x0, -0x8(%rbp)
jmp 0x19be5
movq -0x28(%rbp), %rdi
movq (%rdi), %rax
callq *0x38(%rax)
movq %rax, -0x30(%rbp)
cmpq $0x0, -0x30(%rbp)
ja 0x19a3c
movq -0x28(%rbp), %rax
movq %rax, -0x50(%rbp)
cmpq $0x0, %rax
je 0x19a2f
movq -0x50(%rbp), %rdi
movq (%rdi), %rax
callq *0x8(%rax)
movq $0x0, -0x8(%rbp)
jmp 0x19be5
movq -0x30(%rbp), %rdi
addq $0x1, %rdi
callq 0x1b0b0
movq %rax, -0x38(%rbp)
cmpq $0x0, -0x38(%rbp)
jne 0x19a79
movq -0x28(%rbp), %rax
movq %rax, -0x58(%rbp)
cmpq $0x0, %rax
je 0x19a6c
movq -0x58(%rbp), %rdi
movq (%rdi), %rax
callq *0x8(%rax)
movq $0x0, -0x8(%rbp)
jmp 0x19be5
movq $0x0, -0x40(%rbp)
testb $0x1, -0x19(%rbp)
je 0x19b33
cmpq $0x3, -0x30(%rbp)
jb 0x19b33
movq -0x28(%rbp), %rdi
movq -0x38(%rbp), %rsi
movq (%rdi), %rax
movl $0x3, %edx
callq *0x48(%rax)
cmpq $0x3, %rax
je 0x19aeb
movq -0x28(%rbp), %rax
movq %rax, -0x60(%rbp)
cmpq $0x0, %rax
je 0x19ac7
movq -0x60(%rbp), %rdi
movq (%rdi), %rax
callq *0x8(%rax)
movq -0x38(%rbp), %rax
movq %rax, -0x68(%rbp)
cmpq $0x0, %rax
je 0x19ade
movq -0x68(%rbp), %rdi
callq 0x1b110
movq $0x0, -0x8(%rbp)
jmp 0x19be5
movq -0x38(%rbp), %rax
movzbl (%rax), %eax
cmpl $0xef, %eax
jne 0x19b25
movq -0x38(%rbp), %rax
movzbl 0x1(%rax), %eax
cmpl $0xbb, %eax
jne 0x19b25
movq -0x38(%rbp), %rax
movzbl 0x2(%rax), %eax
cmpl $0xbf, %eax
jne 0x19b25
movq -0x30(%rbp), %rax
subq $0x3, %rax
movq %rax, -0x30(%rbp)
jmp 0x19b31
movq -0x40(%rbp), %rax
addq $0x3, %rax
movq %rax, -0x40(%rbp)
jmp 0x19b33
movq -0x28(%rbp), %rdi
movq -0x38(%rbp), %rsi
addq -0x40(%rbp), %rsi
movq -0x30(%rbp), %rdx
subq -0x40(%rbp), %rdx
movq (%rdi), %rax
callq *0x48(%rax)
movq %rax, -0x48(%rbp)
movq -0x28(%rbp), %rax
movq %rax, -0x70(%rbp)
cmpq $0x0, %rax
je 0x19b69
movq -0x70(%rbp), %rdi
movq (%rdi), %rax
callq *0x8(%rax)
movq -0x48(%rbp), %rax
movq -0x30(%rbp), %rcx
subq -0x40(%rbp), %rcx
cmpq %rcx, %rax
je 0x19b9b
movq -0x38(%rbp), %rax
movq %rax, -0x78(%rbp)
cmpq $0x0, %rax
je 0x19b91
movq -0x78(%rbp), %rdi
callq 0x1b110
movq $0x0, -0x8(%rbp)
jmp 0x19be5
testb $0x1, -0x19(%rbp)
je 0x19bbf
movq -0x38(%rbp), %rdi
movq -0x30(%rbp), %rsi
movq -0x38(%rbp), %rdx
movq -0x30(%rbp), %rcx
xorl %eax, %eax
movl %eax, %r8d
callq 0x17a30
movq %rax, -0x30(%rbp)
movq -0x38(%rbp), %rax
movq -0x30(%rbp), %rcx
movb $0x0, (%rax,%rcx)
cmpq $0x0, -0x18(%rbp)
je 0x19bdd
movq -0x30(%rbp), %rcx
movq -0x18(%rbp), %rax
movq %rcx, (%rax)
movq -0x38(%rbp), %rax
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x80, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)