// File: /workspace/github_c_makefile_star2_O2/Joy-Division[P]tools-mgs/dar-extract_psx.c
// Function: main
int main( int argc, char **argv ) {
	
	char execpath[4096];
	char *namestring = NULL;
	unsigned int darsize;
	fileentry thing;
	uint16_t type, hash;
	
	if( argc < 2 ) {
		printf("Not enough args!\nUse: %s DAR-file [dictionary]\n", argv[0]);
		return 1;
	}
	
	if( argc == 3 ) {
		if(strlen(argv[0]) > 4096) BREAK("Path to executable exceeds 4096 chars!\n")
		strcpy(execpath, argv[0]);
		*strrchr(execpath, '/') = 0;
		
		numcommondic = loaddic(&commondic, execpath, argv[2], DIC_HASH_SINGLE_EXT, StrCode16);
	}
	
	FILE *f, *o;
	
	if( !(f = fopen( argv[1], "rb" ))) {
		printf("Couldnt open file %s\n", argv[1]);
		return 1;
	}
	
	fseek(f, 0, SEEK_END);
	darsize = ftell(f);
	fseek(f, 0, SEEK_SET);
	
	while(ftell(f)<darsize) {
		fread(&thing, 8, 1, f);
		type = (thing.hash >> 16) & 0xFF;
		hash = thing.hash & 0xFFFF;
		namestring = getNameFromDetails(hash, type);
		if( !(o = fopen( namestring, "wb" ))) {
			printf("Couldnt open file %s\n", namestring);
			return 1;
		}
		writeFile(f, thing.size, o);
		fclose(o);
		free(namestring);
	}
	
	free(commondic);
	
	
	printf("Done.\n");
	fclose(f);
	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1018, %rsp           # imm = 0x1018
movq %rsi, %r14
cmpl $0x1, %edi
jg 0x174a
movq (%r14), %rsi
leaq 0x9d4(%rip), %rdi       # 0x2119
jmp 0x18a8
cmpl $0x3, %edi
jne 0x17b4
movq (%r14), %rbx
movq %rbx, %rdi
callq 0x1090
cmpq $0x1001, %rax           # imm = 0x1001
jb 0x1773
leaq 0xa02(%rip), %rdi       # 0x216b
callq 0x1060
jmp 0x18af
leaq 0x10(%rsp), %r15
movq %r15, %rdi
movq %rbx, %rsi
callq 0x1050
pushq $0x2f
popq %rsi
movq %r15, %rdi
callq 0x10c0
movb $0x0, (%rax)
movq 0x10(%r14), %rdx
leaq 0x293c(%rip), %rdi      # 0x40d8
leaq -0x527(%rip), %r8       # 0x127c
pushq $0x2
popq %rcx
movq %r15, %rsi
callq 0x12f8
movl %eax, 0x2934(%rip)      # 0x40e8
movq 0x8(%r14), %rdi
leaq 0x98a(%rip), %rsi       # 0x2149
callq 0x1130
testq %rax, %rax
je 0x186d
movq %rax, %rbx
pushq $0x2
popq %rdx
movq %rax, %rdi
xorl %esi, %esi
callq 0x1120
movq %rbx, %rdi
callq 0x10f0
movq %rax, %r14
movq %rbx, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x1120
movl %r14d, %r15d
pushq $0x1
popq %r12
leaq 0x960(%rip), %r13       # 0x2162
movq %rbx, %rdi
callq 0x10f0
cmpq %r15, %rax
jge 0x187a
leaq 0x8(%rsp), %rdi
pushq $0x8
popq %rsi
movq %r12, %rdx
movq %rbx, %rcx
callq 0x1070
movl 0x8(%rsp), %eax
movl %eax, %ecx
shrl $0x10, %ecx
movzwl %ax, %edi
movzbl %cl, %esi
callq 0x16a5
movq %rax, %rbp
movq %rax, %rdi
movq %r13, %rsi
callq 0x1130
testq %rax, %rax
je 0x189e
movq %rax, %r14
movl 0xc(%rsp), %esi
movq %rbx, %rdi
movq %rax, %rdx
callq 0x15c4
movq %r14, %rdi
callq 0x1080
movq %rbp, %rdi
callq 0x1030
jmp 0x1802
movq 0x8(%r14), %rsi
leaq 0x8d4(%rip), %rdi       # 0x214c
jmp 0x18a8
movq 0x2857(%rip), %rdi      # 0x40d8
callq 0x1030
leaq 0x8d8(%rip), %rdi       # 0x2165
callq 0x1060
movq %rbx, %rdi
callq 0x1080
xorl %eax, %eax
jmp 0x18b2
leaq 0x8a7(%rip), %rdi       # 0x214c
movq %rbp, %rsi
xorl %eax, %eax
callq 0x10a0
pushq $0x1
popq %rax
addq $0x1018, %rsp           # imm = 0x1018
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq