// File: /workspace/github_c_makefile_star2_O2/Joy-Division[P]tools-mgs/dar-extract_pc.c
// Function: main
int main( int argc, char **argv ) {
	
	char name[32];
	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 > 32) {
			printf("namelen > 32!\n");
			return 1;
		}
		fread(name, len, 1, f );
		fread( &size, 4, 1, f );
		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 $0x38, %rsp
movq %rsi, %r14
cmpl $0x1, %edi
jg 0x12a4
movq (%r14), %rsi
leaq 0xdb3(%rip), %rdi       # 0x2052
jmp 0x13ec
movq 0x8(%r14), %rdi
leaq 0xdc6(%rip), %rsi       # 0x2075
callq 0x10a0
testq %rax, %rax
je 0x13e1
movq %rax, %rbx
leaq 0xc(%rsp), %r14
pushq $0x4
popq %rsi
pushq $0x1
popq %rbp
movq %r14, %rdi
movq %rbp, %rdx
movq %rax, %rcx
callq 0x1040
movl (%r14), %esi
leaq 0xdab(%rip), %rdi       # 0x208e
movl %esi, (%rsp)
xorl %eax, %eax
callq 0x1060
xorl %eax, %eax
cmpl (%rsp), %eax
je 0x1409
movl %eax, 0x4(%rsp)
movq %rbx, %rdi
callq 0x1080
movq %rax, %r14
xorl %r15d, %r15d
movl %r14d, %r13d
pushq $0x4
popq %r12
movq %rbx, %rdi
callq 0x1070
incl %r15d
incl %r12d
incb %r13b
testl %eax, %eax
jne 0x1311
movl %r14d, %esi
movq %rbx, %rdi
xorl %edx, %edx
callq 0x1090
leal (%r14,%r15), %eax
movzbl %r13b, %ecx
andl $0x3, %ecx
subl %ecx, %r12d
testb $0x3, %al
cmovel %r15d, %r12d
cmpl $0x21, %r12d
jae 0x1422
movl %r12d, %esi
leaq 0x10(%rsp), %r15
movq %r15, %rdi
movq %rbp, %rdx
movq %rbx, %rcx
callq 0x1040
leaq 0x8(%rsp), %rdi
pushq $0x4
popq %rsi
movq %rbp, %rdx
movq %rbx, %rcx
callq 0x1040
movl 0x8(%rsp), %r14d
leaq 0xd1d(%rip), %rdi       # 0x20a3
movq %r15, %rsi
movl %r14d, %edx
xorl %eax, %eax
callq 0x1060
movq %r15, %rdi
leaq 0xd24(%rip), %rsi       # 0x20c1
callq 0x10a0
testq %rax, %rax
pushq $0x1
popq %r15
je 0x1430
movq %rax, %r12
movq %rbx, %rdi
movl %r14d, %esi
movq %rax, %rdx
callq 0x11cc
movq %r12, %rdi
callq 0x1050
movq %rbx, %rdi
movq %rbp, %rsi
movl %r15d, %edx
callq 0x1090
movl 0x4(%rsp), %eax
incl %eax
jmp 0x12ef
movq 0x8(%r14), %rsi
leaq 0xc8c(%rip), %rdi       # 0x2078
xorl %eax, %eax
callq 0x1060
pushq $0x1
popq %r15
movl %r15d, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xccc(%rip), %rdi       # 0x20dc
callq 0x1030
movq %rbx, %rdi
callq 0x1050
xorl %r15d, %r15d
jmp 0x13f7
leaq 0xcb9(%rip), %rdi       # 0x20e2
callq 0x1030
jmp 0x13f3
leaq 0xc8d(%rip), %rdi       # 0x20c4
leaq 0x10(%rsp), %rsi
xorl %eax, %eax
callq 0x1060
jmp 0x13f7