// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x1040, %rsp           # imm = 0x1040
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq $0x0, -0x1018(%rbp)
cmpl $0x2, -0x8(%rbp)
jge 0x1a7b
movq -0x10(%rbp), %rax
movq (%rax), %rsi
leaq 0x6bb(%rip), %rdi       # 0x2123
movb $0x0, %al
callq 0x10a0
movl $0x1, -0x4(%rbp)
jmp 0x1ca0
cmpl $0x3, -0x8(%rbp)
jne 0x1b07
movq -0x10(%rbp), %rax
movq (%rax), %rdi
callq 0x1090
cmpq $0x1000, %rax           # imm = 0x1000
jbe 0x1ab3
leaq 0x6b3(%rip), %rdi       # 0x2153
movb $0x0, %al
callq 0x10a0
movl $0x1, -0x4(%rbp)
jmp 0x1ca0
leaq -0x1010(%rbp), %rdi
movq -0x10(%rbp), %rax
movq (%rax), %rsi
callq 0x1060
leaq -0x1010(%rbp), %rdi
movl $0x2f, %esi
callq 0x10c0
movb $0x0, (%rax)
leaq -0x1010(%rbp), %rsi
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rdx
leaq 0x25e8(%rip), %rdi      # 0x40d8
movl $0x2, %ecx
leaq -0x87c(%rip), %r8       # 0x1280
callq 0x13d0
movl %eax, 0x25e1(%rip)      # 0x40e8
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
leaq 0x665(%rip), %rsi       # 0x217b
callq 0x1130
movq %rax, -0x1030(%rbp)
cmpq $0x0, %rax
jne 0x1b4a
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rsi
leaq 0x647(%rip), %rdi       # 0x217e
movb $0x0, %al
callq 0x10a0
movl $0x1, -0x4(%rbp)
jmp 0x1ca0
movq -0x1030(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
movl $0x2, %edx
callq 0x1120
movq -0x1030(%rbp), %rdi
callq 0x10f0
movl %eax, -0x101c(%rbp)
movq -0x1030(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
xorl %edx, %edx
callq 0x1120
movq -0x1030(%rbp), %rdi
callq 0x10f0
movl -0x101c(%rbp), %ecx
cmpq %rcx, %rax
jge 0x1c73
movq -0x1030(%rbp), %rcx
leaq -0x1024(%rbp), %rdi
movl $0x8, %esi
movl $0x1, %edx
callq 0x1070
movl -0x1024(%rbp), %eax
shrl $0x10, %eax
andl $0xff, %eax
movw %ax, -0x1026(%rbp)
movl -0x1024(%rbp), %eax
andl $0xffff, %eax           # imm = 0xFFFF
movw %ax, -0x1028(%rbp)
movw -0x1028(%rbp), %ax
movzwl %ax, %edi
movzwl -0x1026(%rbp), %esi
callq 0x1990
movq %rax, -0x1018(%rbp)
movq -0x1018(%rbp), %rdi
leaq 0x587(%rip), %rsi       # 0x2194
callq 0x1130
movq %rax, -0x1038(%rbp)
cmpq $0x0, %rax
jne 0x1c3d
movq -0x1018(%rbp), %rsi
leaq 0x551(%rip), %rdi       # 0x217e
movb $0x0, %al
callq 0x10a0
movl $0x1, -0x4(%rbp)
jmp 0x1ca0
movq -0x1030(%rbp), %rdi
movl -0x1020(%rbp), %esi
movq -0x1038(%rbp), %rdx
callq 0x17d0
movq -0x1038(%rbp), %rdi
callq 0x1080
movq -0x1018(%rbp), %rdi
callq 0x1030
jmp 0x1b83
movq 0x245e(%rip), %rdi      # 0x40d8
callq 0x1030
leaq 0x511(%rip), %rdi       # 0x2197
movb $0x0, %al
callq 0x10a0
movq -0x1030(%rbp), %rdi
callq 0x1080
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x1040, %rsp           # imm = 0x1040
popq %rbp
retq