// File: /workspace/github_c_makefile_star2_O0/Hazematman[P]BSP-Renderer/pak.c
// Function: readPakFile
PakFile *readPakFile(const char *filename){
	PakFile *pak = malloc(sizeof(PakFile));
	FILE *fp = fopen(filename, "rb");
	if(fp == NULL){
		printf("Could not open file: %s\n", filename);
		return NULL;
	}

	PakHeader header;

	// Read in PAK header
	fread(&header, sizeof(PakHeader), 1, fp);

	// Verify that this is a valid pak file
	if(header.magic[0] != 'P' || header.magic[1] != 'A' ||
			header.magic[2] != 'C' || header.magic[3] != 'K'){
		printf("Invalid pak file: %s\n", filename);
		return NULL;
	}
	
	PakEntry *entries = malloc(header.size);
	int numEntries = header.size/sizeof(PakEntry);
	pak->numFiles = numEntries;
	pak->files = malloc(sizeof(FILE)*numEntries);
	
	// Go to first entry
	fseek(fp, header.offset, SEEK_SET);
	// Read in all entries
	fread(entries, sizeof(PakEntry), numEntries, fp);

	// Read all file entries
	for(int i=0; i < numEntries; i++){
		int filenameLength = strlen(entries[i].filename);
		pak->files[i].filename = malloc(filenameLength+1);
		strcpy(pak->files[i].filename, entries[i].filename);

		pak->files[i].size = entries[i].size;
		pak->files[i].data = malloc(pak->files[i].size);
		fseek(fp, entries[i].offset, SEEK_SET);
		fread(pak->files[i].data, 1, pak->files[i].size, fp);
	}


	fclose(fp);

	free(entries);
	return pak;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x10(%rbp)
movl $0x10, %edi
callq 0x1170
movq %rax, -0x18(%rbp)
movq -0x10(%rbp), %rdi
leaq 0x40fb(%rip), %rsi      # 0x9890
callq 0x1150
movq %rax, -0x20(%rbp)
cmpq $0x0, -0x20(%rbp)
jne 0x57c4
movq -0x10(%rbp), %rsi
leaq 0x4089(%rip), %rdi      # 0x9839
movb $0x0, %al
callq 0x1040
movq $0x0, -0x8(%rbp)
jmp 0x59d3
movq -0x20(%rbp), %rcx
leaq -0x2c(%rbp), %rdi
movl $0xc, %esi
movl $0x1, %edx
callq 0x11c0
movsbl -0x2c(%rbp), %eax
cmpl $0x50, %eax
jne 0x57ff
movsbl -0x2b(%rbp), %eax
cmpl $0x41, %eax
jne 0x57ff
movsbl -0x2a(%rbp), %eax
cmpl $0x43, %eax
jne 0x57ff
movsbl -0x29(%rbp), %eax
cmpl $0x4b, %eax
je 0x581e
movq -0x10(%rbp), %rsi
leaq 0x4089(%rip), %rdi      # 0x9893
movb $0x0, %al
callq 0x1040
movq $0x0, -0x8(%rbp)
jmp 0x59d3
movslq -0x24(%rbp), %rdi
callq 0x1170
movq %rax, -0x38(%rbp)
movslq -0x24(%rbp), %rax
shrq $0x6, %rax
movl %eax, -0x3c(%rbp)
movl -0x3c(%rbp), %ecx
movq -0x18(%rbp), %rax
movl %ecx, 0x8(%rax)
movslq -0x3c(%rbp), %rax
imulq $0xd8, %rax, %rdi
callq 0x1170
movq %rax, %rcx
movq -0x18(%rbp), %rax
movq %rcx, (%rax)
movq -0x20(%rbp), %rdi
movslq -0x28(%rbp), %rsi
xorl %edx, %edx
callq 0x1140
movq -0x38(%rbp), %rdi
movslq -0x3c(%rbp), %rdx
movq -0x20(%rbp), %rcx
movl $0x40, %esi
callq 0x11c0
movl $0x0, -0x40(%rbp)
movl -0x40(%rbp), %eax
cmpl -0x3c(%rbp), %eax
jge 0x59b9
movq -0x38(%rbp), %rdi
movslq -0x40(%rbp), %rax
shlq $0x6, %rax
addq %rax, %rdi
callq 0x1080
movl %eax, -0x44(%rbp)
movl -0x44(%rbp), %eax
addl $0x1, %eax
movslq %eax, %rdi
callq 0x1170
movq %rax, %rcx
movq -0x18(%rbp), %rax
movq (%rax), %rax
movslq -0x40(%rbp), %rdx
imulq $0x18, %rdx, %rdx
addq %rdx, %rax
movq %rcx, (%rax)
movq -0x18(%rbp), %rax
movq (%rax), %rax
movslq -0x40(%rbp), %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq (%rax), %rdi
movq -0x38(%rbp), %rsi
movslq -0x40(%rbp), %rax
shlq $0x6, %rax
addq %rax, %rsi
callq 0x1100
movq -0x38(%rbp), %rax
movslq -0x40(%rbp), %rcx
shlq $0x6, %rcx
addq %rcx, %rax
movl 0x3c(%rax), %ecx
movq -0x18(%rbp), %rax
movq (%rax), %rax
movslq -0x40(%rbp), %rdx
imulq $0x18, %rdx, %rdx
addq %rdx, %rax
movl %ecx, 0x8(%rax)
movq -0x18(%rbp), %rax
movq (%rax), %rax
movslq -0x40(%rbp), %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movslq 0x8(%rax), %rdi
callq 0x1170
movq %rax, %rcx
movq -0x18(%rbp), %rax
movq (%rax), %rax
movslq -0x40(%rbp), %rdx
imulq $0x18, %rdx, %rdx
addq %rdx, %rax
movq %rcx, 0x10(%rax)
movq -0x20(%rbp), %rdi
movq -0x38(%rbp), %rax
movslq -0x40(%rbp), %rcx
shlq $0x6, %rcx
addq %rcx, %rax
movslq 0x38(%rax), %rsi
xorl %edx, %edx
callq 0x1140
movq -0x18(%rbp), %rax
movq (%rax), %rax
movslq -0x40(%rbp), %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq 0x10(%rax), %rdi
movq -0x18(%rbp), %rax
movq (%rax), %rax
movslq -0x40(%rbp), %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movslq 0x8(%rax), %rdx
movq -0x20(%rbp), %rcx
movl $0x1, %esi
callq 0x11c0
movl -0x40(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x40(%rbp)
jmp 0x5886
movq -0x20(%rbp), %rdi
callq 0x1110
movq -0x38(%rbp), %rdi
callq 0x1160
movq -0x18(%rbp), %rax
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x50, %rsp
popq %rbp
retq
nopl (%rax)