// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x80, %rsp
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
cmpl $0x2, -0x8(%rbp)
jge 0x1340
movq -0x10(%rbp), %rax
movq (%rax), %rsi
leaq 0xd25(%rip), %rdi       # 0x2052
movb $0x0, %al
callq 0x1050
movl $0x1, -0x4(%rbp)
jmp 0x155e
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
leaq 0xd26(%rip), %rsi       # 0x2075
callq 0x1090
movq %rax, -0x70(%rbp)
cmpq $0x0, %rax
jne 0x1380
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rsi
leaq 0xd0b(%rip), %rdi       # 0x2078
movb $0x0, %al
callq 0x1050
movl $0x1, -0x4(%rbp)
jmp 0x155e
movq -0x70(%rbp), %rcx
leaq -0x60(%rbp), %rdi
movl $0x4, %esi
movl $0x1, %edx
callq 0x1030
movl -0x60(%rbp), %esi
leaq 0xced(%rip), %rdi       # 0x208e
movb $0x0, %al
callq 0x1050
movl $0x0, -0x64(%rbp)
movl -0x64(%rbp), %eax
cmpl -0x60(%rbp), %eax
jae 0x1540
movq -0x70(%rbp), %rdi
callq 0x1070
movl %eax, -0x5c(%rbp)
movl $0x0, -0x58(%rbp)
movq -0x70(%rbp), %rdi
callq 0x1060
cmpl $0x0, %eax
je 0x13e7
movl -0x58(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x58(%rbp)
jmp 0x13ce
movl -0x58(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x58(%rbp)
movq -0x70(%rbp), %rdi
movl -0x5c(%rbp), %eax
movl %eax, %esi
xorl %edx, %edx
callq 0x1080
movl -0x5c(%rbp), %eax
addl -0x58(%rbp), %eax
andl $0x3, %eax
cmpl $0x0, %eax
je 0x1424
movl -0x5c(%rbp), %ecx
addl -0x58(%rbp), %ecx
andl $0x3, %ecx
movl $0x4, %eax
subl %ecx, %eax
addl -0x58(%rbp), %eax
movl %eax, -0x58(%rbp)
cmpl $0x40, -0x58(%rbp)
jbe 0x1444
leaq 0xc72(%rip), %rdi       # 0x20a3
movb $0x0, %al
callq 0x1050
movl $0x1, -0x4(%rbp)
jmp 0x155e
leaq -0x50(%rbp), %rdi
movl -0x58(%rbp), %eax
movl %eax, %esi
movq -0x70(%rbp), %rcx
movl $0x1, %edx
callq 0x1030
movq -0x70(%rbp), %rcx
leaq -0x54(%rbp), %rdi
movl $0x4, %esi
movl $0x1, %edx
callq 0x1030
movq -0x70(%rbp), %rdi
callq 0x1070
movl $0x10, %ecx
cqto
idivq %rcx
cmpq $0x0, %rdx
je 0x14bc
movq -0x70(%rbp), %rax
movq %rax, -0x80(%rbp)
movq -0x70(%rbp), %rdi
callq 0x1070
movq -0x80(%rbp), %rdi
movl $0x10, %ecx
cqto
idivq %rcx
movl $0x10, %esi
subq %rdx, %rsi
movl $0x1, %edx
callq 0x1080
leaq -0x50(%rbp), %rsi
movl -0x54(%rbp), %edx
leaq 0xbe8(%rip), %rdi       # 0x20b2
movb $0x0, %al
callq 0x1050
leaq -0x50(%rbp), %rdi
leaq 0xbf4(%rip), %rsi       # 0x20d0
callq 0x1090
movq %rax, -0x78(%rbp)
cmpq $0x0, %rax
jne 0x1506
leaq -0x50(%rbp), %rsi
leaq 0xbdd(%rip), %rdi       # 0x20d3
movb $0x0, %al
callq 0x1050
movl $0x1, -0x4(%rbp)
jmp 0x155e
movq -0x70(%rbp), %rdi
movl -0x54(%rbp), %esi
movq -0x78(%rbp), %rdx
callq 0x11c0
movq -0x78(%rbp), %rdi
callq 0x1040
movq -0x70(%rbp), %rdi
movl $0x1, %esi
movl $0x1, %edx
callq 0x1080
movl -0x64(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x64(%rbp)
jmp 0x13af
leaq 0xba4(%rip), %rdi       # 0x20eb
movb $0x0, %al
callq 0x1050
movq -0x70(%rbp), %rdi
callq 0x1040
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x80, %rsp
popq %rbp
retq