// File: /workspace/github_c_makefile_star2_O1/Joy-Division[P]tools-mgs/dar-extract_psp.c
// Function: main
int main( int argc, char **argv ) {
	
	char name[64];
	uint32_t size, len;
	unsigned int remember;
	
	if( argc < 2 ) {
		printf("Not enough args!\nUse: %s PAK-file\n", argv[0]);
		return 1;
	}
	
	uint32_t numFiles, workfiles;
	FILE *f, *o;
	
	if( !(f = fopen( argv[1], "rb" ))) {
		printf("Couldnt open file %s\n", argv[1]);
		return 1;
	}
	
	fread( &numFiles, 4, 1, f );
	
	printf("Number of files: %u\n", numFiles );
	
	for( workfiles = 0; workfiles < numFiles; workfiles++ ) {
		remember = ftell(f);
		len = 0;
		while(fgetc(f)) len++;
		len++;
		fseek(f, remember, SEEK_SET);
		if((remember+len)%4) len+= 4-((remember+len)%4);
		if(len > 64) {
			printf("namelen > 32!\n");
			return 1;
		}
		fread(name, len, 1, f );
		fread( &size, 4, 1, f );
		if(ftell(f)%0x10) fseek(f, (0x10-(ftell(f)%0x10)), SEEK_CUR);
		printf("Current file: %s - Size %08x\n", name, size);
		
		if( !(o = fopen( name, "wb" ))) {
			printf("Couldnt open output %s\n", name);
			return 1;
		}
		
		writeFile( f, size, o );
		fclose(o);
		fseek(f, 1, SEEK_CUR);
	}
	printf("Done.\n");
	fclose(f);
	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %rsi, %r14
cmpl $0x1, %edi
jg 0x12c0
movq (%r14), %rsi
leaq 0xd97(%rip), %rdi       # 0x2052
jmp 0x1478
movq 0x8(%r14), %rdi
leaq 0xdaa(%rip), %rsi       # 0x2075
callq 0x10a0
testq %rax, %rax
je 0x1451
movq %rax, %rbx
leaq 0xc(%rsp), %r14
movl $0x4, %esi
movl $0x1, %edx
movq %r14, %rdi
movq %rax, %rcx
callq 0x1040
movl (%r14), %ebp
leaq 0xd8e(%rip), %rdi       # 0x208e
movl %ebp, %esi
xorl %eax, %eax
callq 0x1060
movl %ebp, 0x4(%rsp)
testl %ebp, %ebp
je 0x1439
xorl %r13d, %r13d
movq %rbx, %rdi
callq 0x1080
movq %rax, %r15
xorl %ebp, %ebp
movl $0x4, %r12d
movl %r15d, %r14d
movq %rbx, %rdi
callq 0x1070
incl %ebp
incl %r12d
incb %r14b
testl %eax, %eax
jne 0x132e
movl %r15d, %esi
movq %rbx, %rdi
xorl %edx, %edx
callq 0x1090
leal (%r15,%rbp), %eax
movzbl %r14b, %ecx
andl $0x3, %ecx
subl %ecx, %r12d
testb $0x3, %al
cmovel %ebp, %r12d
cmpl $0x41, %r12d
jae 0x145e
movl %r12d, %esi
movl $0x1, %edx
leaq 0x10(%rsp), %r14
movq %r14, %rdi
movq %rbx, %rcx
callq 0x1040
movl $0x4, %esi
movl $0x1, %edx
leaq 0x8(%rsp), %rdi
movq %rbx, %rcx
callq 0x1040
movq %rbx, %rdi
callq 0x1080
testb $0xf, %al
je 0x13d3
movq %rbx, %rdi
callq 0x1080
leaq 0xf(%rax), %rsi
testq %rax, %rax
cmovnsq %rax, %rsi
andq $-0x10, %rsi
subq %rax, %rsi
addq $0x10, %rsi
movq %rbx, %rdi
movl $0x1, %edx
callq 0x1090
movl 0x8(%rsp), %r15d
leaq 0xcc4(%rip), %rdi       # 0x20a3
movq %r14, %rsi
movl %r15d, %edx
xorl %eax, %eax
callq 0x1060
movq %r14, %rdi
leaq 0xccb(%rip), %rsi       # 0x20c1
callq 0x10a0
testq %rax, %rax
je 0x146c
movq %rax, %r12
movq %rbx, %rdi
movl %r15d, %esi
movq %rax, %rdx
callq 0x11cc
movq %r12, %rdi
callq 0x1050
movl $0x1, %esi
movq %rbx, %rdi
movl $0x1, %edx
callq 0x1090
incl %r13d
cmpl 0x4(%rsp), %r13d
jne 0x1318
leaq 0xc9c(%rip), %rdi       # 0x20dc
callq 0x1030
movq %rbx, %rdi
callq 0x1050
xorl %eax, %eax
jmp 0x1484
movq 0x8(%r14), %rsi
leaq 0xc1c(%rip), %rdi       # 0x2078
jmp 0x1478
leaq 0xc7d(%rip), %rdi       # 0x20e2
callq 0x1030
jmp 0x147f
leaq 0xc51(%rip), %rdi       # 0x20c4
leaq 0x10(%rsp), %rsi
xorl %eax, %eax
callq 0x1060
movl $0x1, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq