// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x60, %rsp
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
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 0x1511
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
leaq 0xd29(%rip), %rsi       # 0x2075
callq 0x1090
movq %rax, -0x50(%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 0x1511
movq -0x50(%rbp), %rcx
leaq -0x40(%rbp), %rdi
movl $0x4, %esi
movl $0x1, %edx
callq 0x1030
movl -0x40(%rbp), %esi
leaq 0xcf0(%rip), %rdi       # 0x208e
movb $0x0, %al
callq 0x1050
movl $0x0, -0x44(%rbp)
movl -0x44(%rbp), %eax
cmpl -0x40(%rbp), %eax
jae 0x14f3
movq -0x50(%rbp), %rdi
callq 0x1070
movl %eax, -0x3c(%rbp)
movl $0x0, -0x38(%rbp)
movq -0x50(%rbp), %rdi
callq 0x1060
cmpl $0x0, %eax
je 0x13e4
movl -0x38(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x38(%rbp)
jmp 0x13cb
movl -0x38(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x38(%rbp)
movq -0x50(%rbp), %rdi
movl -0x3c(%rbp), %eax
movl %eax, %esi
xorl %edx, %edx
callq 0x1080
movl -0x3c(%rbp), %eax
addl -0x38(%rbp), %eax
andl $0x3, %eax
cmpl $0x0, %eax
je 0x1421
movl -0x3c(%rbp), %ecx
addl -0x38(%rbp), %ecx
andl $0x3, %ecx
movl $0x4, %eax
subl %ecx, %eax
addl -0x38(%rbp), %eax
movl %eax, -0x38(%rbp)
cmpl $0x20, -0x38(%rbp)
jbe 0x1441
leaq 0xc75(%rip), %rdi       # 0x20a3
movb $0x0, %al
callq 0x1050
movl $0x1, -0x4(%rbp)
jmp 0x1511
leaq -0x30(%rbp), %rdi
movl -0x38(%rbp), %eax
movl %eax, %esi
movq -0x50(%rbp), %rcx
movl $0x1, %edx
callq 0x1030
movq -0x50(%rbp), %rcx
leaq -0x34(%rbp), %rdi
movl $0x4, %esi
movl $0x1, %edx
callq 0x1030
leaq -0x30(%rbp), %rsi
movl -0x34(%rbp), %edx
leaq 0xc35(%rip), %rdi       # 0x20b2
movb $0x0, %al
callq 0x1050
leaq -0x30(%rbp), %rdi
leaq 0xc41(%rip), %rsi       # 0x20d0
callq 0x1090
movq %rax, -0x58(%rbp)
cmpq $0x0, %rax
jne 0x14b9
leaq -0x30(%rbp), %rsi
leaq 0xc2a(%rip), %rdi       # 0x20d3
movb $0x0, %al
callq 0x1050
movl $0x1, -0x4(%rbp)
jmp 0x1511
movq -0x50(%rbp), %rdi
movl -0x34(%rbp), %esi
movq -0x58(%rbp), %rdx
callq 0x11c0
movq -0x58(%rbp), %rdi
callq 0x1040
movq -0x50(%rbp), %rdi
movl $0x1, %esi
movl $0x1, %edx
callq 0x1080
movl -0x44(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x44(%rbp)
jmp 0x13ac
leaq 0xbf1(%rip), %rdi       # 0x20eb
movb $0x0, %al
callq 0x1050
movq -0x50(%rbp), %rdi
callq 0x1040
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x60, %rsp
popq %rbp
retq