// File: /workspace/github_c_makefile_star2_O0/Joy-Division[P]tools-zoe/pak-extract.c
// Function: main
int main( int argc, char **argv ) {
	
	fileEntry workfile;
	
	unsigned char temp[4];
	
	memset( workfile.name, 0, FILENAMELENGTH );
	
	if( argc < 2 ) {
		printf("Not enough args!\nUse: %s PAK-file\n", argv[0]);
		return 1;
	}
	
	unsigned int numFiles, workfiles;
	FILE *f, *o;
	
	if( !(f = fopen( argv[1], "rb" ))) {
		printf("Couldnt open file %s\n", argv[1]);
		return 1;
	}
	
	fread( temp, 4, 1, f );
	numFiles  = temp[3] << 24;
	numFiles |= temp[2] << 16;
	numFiles |= temp[1] << 8;
	numFiles |= temp[0];
	
	fseek( f, 16, SEEK_SET );
	
	printf("Number of files: %u\n", numFiles );
	
	for( workfiles = 0; workfiles < numFiles; workfiles++ ) {
		fread( &workfile, sizeof(workfile), 1, f );
		printf("Current file: %s\n", workfile.name);
		
		if( !(o = fopen( workfile.name, "wb" ))) {
			printf("Couldnt open output %s\n", workfile.name);
			return 1;
		}
		
		writeFile( f, workfile.size, o );
		fclose(o);
		memset( workfile.name, 0, FILENAMELENGTH );
	}
	printf("Done.\n");
	fclose(f);
	return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x60, %rsp
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
leaq -0x40(%rbp), %rdi
xorl %esi, %esi
movl $0x20, %edx
callq 0x1060
cmpl $0x2, -0x8(%rbp)
jge 0x133d
movq -0x10(%rbp), %rax
movq (%rax), %rsi
leaq 0xd28(%rip), %rdi       # 0x2052
movb $0x0, %al
callq 0x1050
movl $0x1, -0x4(%rbp)
jmp 0x14a9
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
leaq 0xd29(%rip), %rsi       # 0x2075
callq 0x1080
movq %rax, -0x58(%rbp)
cmpq $0x0, %rax
jne 0x137d
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rsi
leaq 0xd0e(%rip), %rdi       # 0x2078
movb $0x0, %al
callq 0x1050
movl $0x1, -0x4(%rbp)
jmp 0x14a9
leaq -0x44(%rbp), %rdi
movq -0x58(%rbp), %rcx
movl $0x4, %esi
movl $0x1, %edx
callq 0x1030
movzbl -0x41(%rbp), %eax
shll $0x18, %eax
movl %eax, -0x48(%rbp)
movzbl -0x42(%rbp), %eax
shll $0x10, %eax
orl -0x48(%rbp), %eax
movl %eax, -0x48(%rbp)
movzbl -0x43(%rbp), %eax
shll $0x8, %eax
orl -0x48(%rbp), %eax
movl %eax, -0x48(%rbp)
movzbl -0x44(%rbp), %eax
orl -0x48(%rbp), %eax
movl %eax, -0x48(%rbp)
movq -0x58(%rbp), %rdi
movl $0x10, %esi
xorl %edx, %edx
callq 0x1070
movl -0x48(%rbp), %esi
leaq 0xcb2(%rip), %rdi       # 0x208e
movb $0x0, %al
callq 0x1050
movl $0x0, -0x4c(%rbp)
movl -0x4c(%rbp), %eax
cmpl -0x48(%rbp), %eax
jae 0x148b
movq -0x58(%rbp), %rcx
leaq -0x40(%rbp), %rdi
movl $0x30, %esi
movl $0x1, %edx
callq 0x1030
leaq -0x40(%rbp), %rsi
leaq 0xc8b(%rip), %rdi       # 0x20a3
movb $0x0, %al
callq 0x1050
leaq -0x40(%rbp), %rdi
leaq 0xc8b(%rip), %rsi       # 0x20b5
callq 0x1080
movq %rax, -0x60(%rbp)
cmpq $0x0, %rax
jne 0x1454
leaq -0x40(%rbp), %rsi
leaq 0xc74(%rip), %rdi       # 0x20b8
movb $0x0, %al
callq 0x1050
movl $0x1, -0x4(%rbp)
jmp 0x14a9
movq -0x58(%rbp), %rdi
movl -0x14(%rbp), %esi
movq -0x60(%rbp), %rdx
callq 0x11b0
movq -0x60(%rbp), %rdi
callq 0x1040
leaq -0x40(%rbp), %rdi
xorl %esi, %esi
movl $0x20, %edx
callq 0x1060
movl -0x4c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x4c(%rbp)
jmp 0x13ea
leaq 0xc3e(%rip), %rdi       # 0x20d0
movb $0x0, %al
callq 0x1050
movq -0x58(%rbp), %rdi
callq 0x1040
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x60, %rsp
popq %rbp
retq