// File: /workspace/github_c_makefile_star2_O3/Joy-Division[P]tools-mgs/qar-extract_psp.c
// Function: main
int main( int argc, char **argv ) {
	
	char name[64];
	uint32_t len;
	unsigned int remember;
	
	entry *filelist = NULL;
	char **namelist = NULL;
	
	if( argc < 2 ) {
		printf("Not enough args!\nUse: %s QAR-file\n", argv[0]);
		return 1;
	}
	
	uint32_t numFiles, workfiles, listoff;
	FILE *f, *o;
	
	if( !(f = fopen( argv[1], "rb" ))) {
		printf("Couldnt open file %s\n", argv[1]);
		return 1;
	}
	
	fseek(f, -4, SEEK_END);
	
	fread( &listoff, 4, 1, f );
	
	printf("Offset of list: %08x\n", listoff);
	
	fseek(f, listoff, SEEK_SET);
	
	fread( &numFiles, 4, 1, f );
	
	printf("Number of files: %u\n", numFiles );
	
	filelist = malloc(numFiles*sizeof(entry));
	namelist = malloc(numFiles*sizeof(char *));
	
	fread(filelist, sizeof(filelist)*numFiles, 1, f);
	
	for( workfiles = 0; workfiles < numFiles; workfiles++ ) {
		remember = ftell(f);
		len = 0;
		while(fgetc(f)) len++;
		len++;
		fseek(f, remember, SEEK_SET);
		namelist[workfiles] = malloc(len);
		
		fread(namelist[workfiles], len, 1, f );
	}
	
	fseek(f, 0, SEEK_SET);
	
	for( workfiles = 0; workfiles < numFiles; workfiles++ ) {
		printf("Current file: %s - Size %08x - Hash %08x\n", namelist[workfiles], filelist[workfiles].size, filelist[workfiles].hash);
		
		if( !(o = fopen( namelist[workfiles], "wb" ))) {
			printf("Couldnt open output %s\n", name);
			return 1;
		}
		
		writeFile( f, filelist[workfiles].size, o );
		fclose(o);
		if(ftell(f)%0x80) fseek(f, 0x80-(ftell(f)%0x80), SEEK_CUR);
		free(namelist[workfiles]);
	}
	free(namelist);
	free(filelist);
	
	printf("Done.\n");
	fclose(f);
	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movq %rsi, %r14
cmpl $0x1, %edi
jg 0x12ce
movq (%r14), %rsi
leaq 0xd89(%rip), %rdi       # 0x2052
jmp 0x1512
movq 0x8(%r14), %rdi
leaq 0xd9c(%rip), %rsi       # 0x2075
callq 0x10c0
testq %rax, %rax
je 0x14c1
movq %rax, %rbx
movq %rax, %rdi
movq $-0x4, %rsi
movl $0x2, %edx
callq 0x10b0
leaq 0x18(%rsp), %r14
movl $0x4, %esi
movl $0x1, %edx
movq %r14, %rdi
movq %rbx, %rcx
callq 0x1050
movl (%r14), %r14d
leaq 0xd6c(%rip), %rdi       # 0x208e
movl %r14d, %esi
xorl %eax, %eax
callq 0x1070
movq %rbx, %rdi
movq %r14, %rsi
xorl %edx, %edx
callq 0x10b0
leaq 0x1c(%rsp), %r14
movl $0x4, %esi
movl $0x1, %edx
movq %r14, %rdi
movq %rbx, %rcx
callq 0x1050
movl (%r14), %r15d
leaq 0xd47(%rip), %rdi       # 0x20a4
movl %r15d, %esi
xorl %eax, %eax
callq 0x1070
leaq (,%r15,8), %r14
movq %r14, %rdi
callq 0x10a0
movq %rax, %r12
movq %r14, %rdi
callq 0x10a0
movq %rax, 0x8(%rsp)
movl $0x1, %edx
movq %r12, 0x10(%rsp)
movq %r12, %rdi
movq %r14, %rsi
movq %rbx, %rcx
callq 0x1050
testq %r15, %r15
je 0x14ce
xorl %r12d, %r12d
movq %rbx, %rdi
callq 0x1090
movq %rax, %r13
xorl %ebp, %ebp
movq %rbx, %rdi
callq 0x1080
incl %ebp
testl %eax, %eax
jne 0x13b8
movl %r13d, %esi
movq %rbx, %rdi
xorl %edx, %edx
callq 0x10b0
movl %ebp, %r14d
movq %r14, %rdi
callq 0x10a0
movq 0x8(%rsp), %rcx
movq %rax, (%rcx,%r12,8)
movl $0x1, %edx
movq %rax, %rdi
movq %r14, %rsi
movq %rbx, %rcx
callq 0x1050
incq %r12
cmpq %r15, %r12
jne 0x13ab
movq %rbx, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x10b0
testq %r15, %r15
je 0x14da
xorl %r12d, %r12d
movq 0x8(%rsp), %rax
movq (%rax,%r12,8), %r13
movq 0x10(%rsp), %rax
movl (%rax,%r12,8), %ecx
movl 0x4(%rax,%r12,8), %ebp
leaq 0xc81(%rip), %rdi       # 0x20b9
movq %r13, %rsi
movl %ebp, %edx
xorl %eax, %eax
callq 0x1070
movq %r13, %rdi
leaq 0xc95(%rip), %rsi       # 0x20e3
callq 0x10c0
testq %rax, %rax
je 0x1506
movq %rax, %r14
movq %rbx, %rdi
movl %ebp, %esi
movq %rax, %rdx
callq 0x11ec
movq %r14, %rdi
callq 0x1060
movq %rbx, %rdi
callq 0x1090
testb $0x7f, %al
je 0x14ab
movq %rbx, %rdi
callq 0x1090
leaq 0x7f(%rax), %rsi
testq %rax, %rax
cmovnsq %rax, %rsi
andq $-0x80, %rsi
subq %rax, %rsi
subq $-0x80, %rsi
movq %rbx, %rdi
movl $0x1, %edx
callq 0x10b0
movq %r13, %rdi
callq 0x1030
incq %r12
cmpq %r12, %r15
jne 0x141a
jmp 0x14da
movq 0x8(%r14), %rsi
leaq 0xbac(%rip), %rdi       # 0x2078
jmp 0x1512
movq %rbx, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x10b0
movq 0x8(%rsp), %rdi
callq 0x1030
movq 0x10(%rsp), %rdi
callq 0x1030
leaq 0xc09(%rip), %rdi       # 0x20fe
callq 0x1040
movq %rbx, %rdi
callq 0x1060
xorl %eax, %eax
jmp 0x151e
leaq 0xbd9(%rip), %rdi       # 0x20e6
leaq 0x20(%rsp), %rsi
xorl %eax, %eax
callq 0x1070
movl $0x1, %eax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq