// File: /workspace/github_c_makefile_star2_O1/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, %rbx
cmpl $0x1, %edi
jg 0x17b7
movq (%rbx), %rsi
leaq 0x967(%rip), %rdi       # 0x2119
jmp 0x1926
cmpl $0x3, %edi
jne 0x1825
movq (%rbx), %r14
movq %r14, %rdi
callq 0x1090
cmpq $0x1001, %rax           # imm = 0x1001
jb 0x17e0
leaq 0x995(%rip), %rdi       # 0x216b
callq 0x1060
jmp 0x192d
leaq 0x10(%rsp), %r15
movq %r15, %rdi
movq %r14, %rsi
callq 0x1050
movq %r15, %rdi
movl $0x2f, %esi
callq 0x10c0
movb $0x0, (%rax)
movq 0x10(%rbx), %rdx
leaq 0x28cd(%rip), %rdi      # 0x40d8
leaq -0x596(%rip), %r8       # 0x127c
movq %r15, %rsi
movl $0x2, %ecx
callq 0x1318
movl %eax, 0x28c3(%rip)      # 0x40e8
movq 0x8(%rbx), %rdi
leaq 0x919(%rip), %rsi       # 0x2149
callq 0x1130
testq %rax, %rax
je 0x190f
movq %rax, %r14
xorl %ebx, %ebx
movq %rax, %rdi
xorl %esi, %esi
movl $0x2, %edx
callq 0x1120
movq %r14, %rdi
callq 0x10f0
movq %rax, %r15
movq %r14, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x1120
movq %r14, %rdi
callq 0x10f0
movl %r15d, %r15d
cmpq %r15, %rax
jge 0x18ed
leaq 0x8e2(%rip), %r12       # 0x2162
movl $0x8, %esi
movl $0x1, %edx
leaq 0x8(%rsp), %rdi
movq %r14, %rcx
callq 0x1070
movl 0x8(%rsp), %eax
movl %eax, %ecx
shrl $0x10, %ecx
movzwl %ax, %edi
movzbl %cl, %esi
callq 0x16fa
movq %rax, %r13
movq %rax, %rdi
movq %r12, %rsi
callq 0x1130
testq %rax, %rax
je 0x191c
movq %rax, %rbp
movl 0xc(%rsp), %esi
movq %r14, %rdi
movq %rax, %rdx
callq 0x15fc
movq %rbp, %rdi
callq 0x1080
movq %r13, %rdi
callq 0x1030
movq %r14, %rdi
callq 0x10f0
cmpq %r15, %rax
jl 0x1880
movq 0x27e4(%rip), %rdi      # 0x40d8
callq 0x1030
leaq 0x865(%rip), %rdi       # 0x2165
callq 0x1060
movq %r14, %rdi
callq 0x1080
jmp 0x1932
movq 0x8(%rbx), %rsi
leaq 0x832(%rip), %rdi       # 0x214c
jmp 0x1926
leaq 0x829(%rip), %rdi       # 0x214c
movq %r13, %rsi
xorl %eax, %eax
callq 0x10a0
movl $0x1, %ebx
movl %ebx, %eax
addq $0x1018, %rsp           # imm = 0x1018
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq