// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x90, %rsp
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq $0x0, -0x60(%rbp)
movq $0x0, -0x68(%rbp)
cmpl $0x2, -0x8(%rbp)
jge 0x1370
movq -0x10(%rbp), %rax
movq (%rax), %rsi
leaq 0xcf5(%rip), %rdi       # 0x2052
movb $0x0, %al
callq 0x1060
movl $0x1, -0x4(%rbp)
jmp 0x1657
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
leaq 0xcf6(%rip), %rsi       # 0x2075
callq 0x10b0
movq %rax, -0x80(%rbp)
cmpq $0x0, %rax
jne 0x13b0
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rsi
leaq 0xcdb(%rip), %rdi       # 0x2078
movb $0x0, %al
callq 0x1060
movl $0x1, -0x4(%rbp)
jmp 0x1657
movq -0x80(%rbp), %rdi
movq $-0x4, %rsi
movl $0x2, %edx
callq 0x10a0
movq -0x80(%rbp), %rcx
leaq -0x74(%rbp), %rdi
movl $0x4, %esi
movl $0x1, %edx
callq 0x1040
movl -0x74(%rbp), %esi
leaq 0xca8(%rip), %rdi       # 0x208e
movb $0x0, %al
callq 0x1060
movq -0x80(%rbp), %rdi
movl -0x74(%rbp), %eax
movl %eax, %esi
xorl %edx, %edx
callq 0x10a0
movq -0x80(%rbp), %rcx
leaq -0x6c(%rbp), %rdi
movl $0x4, %esi
movl $0x1, %edx
callq 0x1040
movl -0x6c(%rbp), %esi
leaq 0xc86(%rip), %rdi       # 0x20a4
movb $0x0, %al
callq 0x1060
movl -0x6c(%rbp), %eax
movl %eax, %edi
shlq $0x3, %rdi
callq 0x1090
movq %rax, -0x60(%rbp)
movl -0x6c(%rbp), %eax
movl %eax, %edi
shlq $0x3, %rdi
callq 0x1090
movq %rax, -0x68(%rbp)
movq -0x60(%rbp), %rdi
movl -0x6c(%rbp), %eax
movl %eax, %esi
shlq $0x3, %rsi
movq -0x80(%rbp), %rcx
movl $0x1, %edx
callq 0x1040
movl $0x0, -0x70(%rbp)
movl -0x70(%rbp), %eax
cmpl -0x6c(%rbp), %eax
jae 0x1500
movq -0x80(%rbp), %rdi
callq 0x1080
movl %eax, -0x58(%rbp)
movl $0x0, -0x54(%rbp)
movq -0x80(%rbp), %rdi
callq 0x1070
cmpl $0x0, %eax
je 0x14a3
movl -0x54(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x54(%rbp)
jmp 0x148a
movl -0x54(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x54(%rbp)
movq -0x80(%rbp), %rdi
movl -0x58(%rbp), %eax
movl %eax, %esi
xorl %edx, %edx
callq 0x10a0
movl -0x54(%rbp), %eax
movl %eax, %edi
callq 0x1090
movq %rax, %rdx
movq -0x68(%rbp), %rax
movl -0x70(%rbp), %ecx
movq %rdx, (%rax,%rcx,8)
movq -0x68(%rbp), %rax
movl -0x70(%rbp), %ecx
movq (%rax,%rcx,8), %rdi
movl -0x54(%rbp), %eax
movl %eax, %esi
movq -0x80(%rbp), %rcx
movl $0x1, %edx
callq 0x1040
movl -0x70(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x70(%rbp)
jmp 0x146b
movq -0x80(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
xorl %edx, %edx
callq 0x10a0
movl $0x0, -0x70(%rbp)
movl -0x70(%rbp), %eax
cmpl -0x6c(%rbp), %eax
jae 0x1627
movq -0x68(%rbp), %rax
movl -0x70(%rbp), %ecx
movq (%rax,%rcx,8), %rsi
movq -0x60(%rbp), %rax
movl -0x70(%rbp), %ecx
movl 0x4(%rax,%rcx,8), %edx
movq -0x60(%rbp), %rax
movl -0x70(%rbp), %ecx
movl (%rax,%rcx,8), %ecx
leaq 0xb70(%rip), %rdi       # 0x20b9
movb $0x0, %al
callq 0x1060
movq -0x68(%rbp), %rax
movl -0x70(%rbp), %ecx
movq (%rax,%rcx,8), %rdi
leaq 0xb81(%rip), %rsi       # 0x20e3
callq 0x10b0
movq %rax, -0x88(%rbp)
cmpq $0x0, %rax
jne 0x1592
leaq -0x50(%rbp), %rsi
leaq 0xb67(%rip), %rdi       # 0x20e6
movb $0x0, %al
callq 0x1060
movl $0x1, -0x4(%rbp)
jmp 0x1657
movq -0x80(%rbp), %rdi
movq -0x60(%rbp), %rax
movl -0x70(%rbp), %ecx
movl 0x4(%rax,%rcx,8), %esi
movq -0x88(%rbp), %rdx
callq 0x11e0
movq -0x88(%rbp), %rdi
callq 0x1050
movq -0x80(%rbp), %rdi
callq 0x1080
movl $0x80, %ecx
cqto
idivq %rcx
cmpq $0x0, %rdx
je 0x1609
movq -0x80(%rbp), %rax
movq %rax, -0x90(%rbp)
movq -0x80(%rbp), %rdi
callq 0x1080
movq -0x90(%rbp), %rdi
movl $0x80, %ecx
cqto
idivq %rcx
movl $0x80, %esi
subq %rdx, %rsi
movl $0x1, %edx
callq 0x10a0
movq -0x68(%rbp), %rax
movl -0x70(%rbp), %ecx
movq (%rax,%rcx,8), %rdi
callq 0x1030
movl -0x70(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x70(%rbp)
jmp 0x1516
movq -0x68(%rbp), %rdi
callq 0x1030
movq -0x60(%rbp), %rdi
callq 0x1030
leaq 0xabe(%rip), %rdi       # 0x20fe
movb $0x0, %al
callq 0x1060
movq -0x80(%rbp), %rdi
callq 0x1050
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x90, %rsp
popq %rbp
retq