// File: /workspace/github_c_makefile_star2_O2/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 0x12a4
movq (%r14), %rsi
leaq 0xdb3(%rip), %rdi       # 0x2052
jmp 0x1418
movq 0x8(%r14), %rdi
leaq 0xdc6(%rip), %rsi       # 0x2075
callq 0x10a0
testq %rax, %rax
je 0x140d
movq %rax, %rbx
leaq 0xc(%rsp), %r14
pushq $0x4
popq %rsi
pushq $0x1
popq %r13
movq %r14, %rdi
movq %r13, %rdx
movq %rax, %rcx
callq 0x1040
movl (%r14), %esi
leaq 0xdaa(%rip), %rdi       # 0x208e
movl %esi, (%rsp)
xorl %eax, %eax
callq 0x1060
xorl %eax, %eax
cmpl (%rsp), %eax
je 0x1433
movl %eax, 0x4(%rsp)
movq %r13, %r15
movq %rbx, %rdi
callq 0x1080
movq %rax, %r14
xorl %r13d, %r13d
movl %r14d, %ebp
pushq $0x4
popq %r12
movq %rbx, %rdi
callq 0x1070
incl %r13d
incl %r12d
incb %bpl
testl %eax, %eax
jne 0x1315
movl %r14d, %esi
movq %rbx, %rdi
xorl %edx, %edx
callq 0x1090
leal (%r14,%r13), %eax
movzbl %bpl, %ecx
andl $0x3, %ecx
subl %ecx, %r12d
testb $0x3, %al
cmovel %r13d, %r12d
cmpl $0x41, %r12d
jae 0x144b
movl %r12d, %esi
leaq 0x10(%rsp), %rdi
movq %r15, %r13
movq %r15, %rdx
movq %rbx, %rcx
callq 0x1040
leaq 0x8(%rsp), %rdi
pushq $0x4
popq %rsi
movq %r15, %rdx
movq %rbx, %rcx
callq 0x1040
movq %rbx, %rdi
callq 0x1080
testb $0xf, %al
pushq $0x1
popq %rbp
je 0x13aa
movq %rbx, %rdi
callq 0x1080
cqto
pushq $0x10
popq %rsi
idivq %rsi
subq %rdx, %rsi
movq %rbx, %rdi
movl %ebp, %edx
callq 0x1090
movl 0x8(%rsp), %r14d
leaq 0xced(%rip), %rdi       # 0x20a3
leaq 0x10(%rsp), %r15
movq %r15, %rsi
movl %r14d, %edx
xorl %eax, %eax
callq 0x1060
movq %r15, %rdi
leaq 0xcef(%rip), %rsi       # 0x20c1
callq 0x10a0
testq %rax, %rax
je 0x1459
movq %rax, %r12
movq %rbx, %rdi
movl %r14d, %esi
movq %rax, %rdx
callq 0x11cc
movq %r12, %rdi
callq 0x1050
movq %rbx, %rdi
movq %r13, %rsi
movl %ebp, %edx
callq 0x1090
movl 0x4(%rsp), %eax
incl %eax
jmp 0x12f0
movq 0x8(%r14), %rsi
leaq 0xc60(%rip), %rdi       # 0x2078
xorl %eax, %eax
callq 0x1060
pushq $0x1
popq %rbp
movl %ebp, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xca2(%rip), %rdi       # 0x20dc
callq 0x1030
movq %rbx, %rdi
callq 0x1050
xorl %ebp, %ebp
jmp 0x1422
leaq 0xc90(%rip), %rdi       # 0x20e2
callq 0x1030
jmp 0x141f
leaq 0xc64(%rip), %rdi       # 0x20c4
leaq 0x10(%rsp), %rsi
xorl %eax, %eax
callq 0x1060
jmp 0x1422