// File: akdess[P]BAFExtract/src/utils.cpp
// Function: load_file(char*)
t_file_buffer* load_file(char* fp)
{
	FILE* f = NULL;

	// If the file is "stdin", load the standard input.
	if(strcmp(fp, "stdin") == 0)
	{
		fprintf(stderr, "Cannot buffer stdin.\n");
		return(NULL);
	}
	else
	{
		// Open the file if it is not the stdin.
		f = fopen(fp, "rb");
		if (f == NULL) 
		{ 
			fprintf(stderr, "Could not open %s\n", fp);
			return(NULL);
		} 
	}

	fseek(f, 0, SEEK_END);
	long int file_size = ftell(f);
if(__DUMP_UTIL_MESSAGES__)
{
	fprintf(stderr, "Loading %s of %ld bytes.\n", fp, file_size);
}

	fseek(f, 0, SEEK_SET);
	char* file_buff = new char[file_size + 1];

if(__DUMP_UTIL_MESSAGES__)
{
	fprintf(stderr, "Allocated buffer memory.\n");
}

	if (file_size != (long int)fread(file_buff, sizeof(char), file_size, f)) 
	{ 
		delete [] file_buff;
		return NULL;
	}

if(__DUMP_UTIL_MESSAGES__)
{
	fprintf(stderr, "Read successfully.\n");
}

	fclose(f);
	file_buff[file_size] = 0;

	t_file_buffer* file_buffer = new t_file_buffer();
	file_buffer->file_buffer = file_buff;
	file_buffer->f_ptr = 0;
	file_buffer->l_file = file_size;

	return file_buffer;
}

// Assembly:
subq $0x48, %rsp
movq %rdi, 0x38(%rsp)
movq $0x0, 0x30(%rsp)
movq 0x38(%rsp), %rdi
leaq 0xdab5(%rip), %rsi      # 0x14333
callq 0x2220
cmpl $0x0, %eax
jne 0x68ae
movq 0x14751(%rip), %rax     # 0x1afe0
movq (%rax), %rdi
leaq 0xe1c5(%rip), %rsi      # 0x14a5e
movb $0x0, %al
callq 0x2240
movq $0x0, 0x40(%rsp)
jmp 0x6a73
movq 0x38(%rsp), %rdi
leaq 0xd788(%rip), %rsi      # 0x14042
callq 0x21b0
movq %rax, 0x30(%rsp)
cmpq $0x0, 0x30(%rsp)
jne 0x68f7
movq 0x1470d(%rip), %rax     # 0x1afe0
movq (%rax), %rdi
movq 0x38(%rsp), %rdx
leaq 0xd76f(%rip), %rsi      # 0x14051
movb $0x0, %al
callq 0x2240
movq $0x0, 0x40(%rsp)
jmp 0x6a73
jmp 0x68f9
movq 0x30(%rsp), %rdi
xorl %eax, %eax
movl %eax, %esi
movl $0x2, %edx
callq 0x21a0
movq 0x30(%rsp), %rdi
callq 0x2030
movq %rax, 0x28(%rsp)
testb $0x1, 0x14888(%rip)     # 0x1b1aa
je 0x6946
movq 0x146b5(%rip), %rax     # 0x1afe0
movq (%rax), %rdi
movq 0x38(%rsp), %rdx
movq 0x28(%rsp), %rcx
leaq 0xe135(%rip), %rsi      # 0x14a74
movb $0x0, %al
callq 0x2240
movq 0x30(%rsp), %rdi
xorl %eax, %eax
movl %eax, %esi
xorl %edx, %edx
callq 0x21a0
movq 0x28(%rsp), %rdi
addq $0x1, %rdi
callq 0x2040
movq %rax, 0x20(%rsp)
testb $0x1, 0x1483a(%rip)     # 0x1b1aa
je 0x698a
movq 0x14667(%rip), %rax     # 0x1afe0
movq (%rax), %rdi
leaq 0xe10b(%rip), %rsi      # 0x14a8e
movb $0x0, %al
callq 0x2240
movq 0x28(%rsp), %rax
movq %rax, 0x10(%rsp)
movq 0x20(%rsp), %rdi
movq 0x28(%rsp), %rdx
movq 0x30(%rsp), %rcx
movl $0x1, %esi
callq 0x2260
movq %rax, %rcx
movq 0x10(%rsp), %rax
cmpq %rcx, %rax
je 0x69e2
movq 0x20(%rsp), %rax
movq %rax, 0x8(%rsp)
cmpq $0x0, %rax
je 0x69d4
movq 0x8(%rsp), %rdi
callq 0x2200
movq $0x0, 0x40(%rsp)
jmp 0x6a73
testb $0x1, 0x147c1(%rip)     # 0x1b1aa
je 0x6a03
movq 0x145ee(%rip), %rax     # 0x1afe0
movq (%rax), %rdi
leaq 0xe0ac(%rip), %rsi      # 0x14aa8
movb $0x0, %al
callq 0x2240
movq 0x30(%rsp), %rdi
callq 0x2170
movq 0x20(%rsp), %rax
movq 0x28(%rsp), %rcx
movb $0x0, (%rax,%rcx)
movl $0x18, %edi
callq 0x2180
movq %rax, %rdi
movq %rdi, (%rsp)
xorl %esi, %esi
movl $0x18, %edx
callq 0x2110
movq (%rsp), %rax
movq %rax, 0x18(%rsp)
movq 0x20(%rsp), %rcx
movq 0x18(%rsp), %rax
movq %rcx, (%rax)
movq 0x18(%rsp), %rax
movq $0x0, 0x8(%rax)
movq 0x28(%rsp), %rcx
movq 0x18(%rsp), %rax
movq %rcx, 0x10(%rax)
movq 0x18(%rsp), %rax
movq %rax, 0x40(%rsp)
movq 0x40(%rsp), %rax
addq $0x48, %rsp
retq
nopl (%rax)