// File: /workspace/github_c_makefile_star2_O1/zefie[P]zefie_3k_iso_patcher/zefie_3k_iso_patcher.c
// Function: main
int main(int argc, char *argv[], char *envp[])
{
	char *filename;	
#ifdef DEBUG
	filename = "Minecraft Story Mode.ISO";
#else
	if (argc > 1) {
		filename = argv[1];
	}
	else {
		printf("You did not specify an iso file.\n");
		return 1;
	}
#endif
	
	if (file_exists(filename)) {
		printf("Found %s, searching for 3K Key File...\n", filename);
		
		char keyfilename[PATH_MAX];
		findKeyfile(filename, keyfilename);
		
		if (keyfilename[5] == 0) {
			printf("Could not find zefie 3K key dump (disc.key)\n");
			return 1;
		}
		printf("Found %s, patching...\n", keyfilename);
		
		FILE *file = fopen(filename, "rb+");
		if (!file) {
			printf("Error opening ISO %s for patching (Err %i).\n", filename, errno);
			return 1;
		}
		
		char test[14];
		fseek(file, 2048, SEEK_SET);
		fgets(test, 13, file);
		if (!strcmp(test, "PlayStation3")) {
			FILE *keyfile = fopen(keyfilename, "rb");
			if (!keyfile) {
				printf("Error opening key file %s for reading (Err %i).\n", keyfilename, errno);
				return 1;
			}
			fseek(keyfile, 0, SEEK_END);
			long keylen = ftell(keyfile);
			fseek(keyfile, 0, SEEK_SET);
			char *keybin = malloc(keylen + 1);
			fread(keybin, 1, keylen, keyfile);
			fclose(keyfile);
			free(keyfile);
			
			
			int header_threekey = 3952;

			fseek(file, header_threekey, SEEK_SET);
			fwrite(keybin, keylen, 1, file);
			printf("Patched %i bytes of %s successfully.\n", keylen, filename);
			fclose(file);
			free(file);
		}
		else {
			printf("%s\n", test);
			fclose(file);
			free(file);
			printf("Not a PS3 ISO");
			return 1;
		}
	}
	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1018, %rsp           # imm = 0x1018
cmpl $0x2, %edi
jl 0x401a45
movq 0x8(%rsi), %rbx
movq %rbx, %rdi
xorl %esi, %esi
callq 0x45cc90
cmpl $-0x1, %eax
je 0x401a8a
leaq 0xa168e(%rip), %rdi     # 0x4a300e
movq %rbx, %rsi
xorl %eax, %eax
callq 0x40c1a0
leaq 0x10(%rsp), %r14
movq %rbx, %rdi
movq %r14, %rsi
callq 0x401862
cmpb $0x0, 0x5(%r14)
je 0x401a59
leaq 0xa168a(%rip), %rdi     # 0x4a3036
leaq 0x10(%rsp), %rsi
xorl %eax, %eax
callq 0x40c1a0
leaq 0xa168e(%rip), %rsi     # 0x4a304d
movq %rbx, %rdi
callq 0x412c90
testq %rax, %rax
je 0x401a67
movq %rax, %r14
movl $0x800, %esi            # imm = 0x800
movq %rax, %rdi
xorl %edx, %edx
callq 0x416cd0
leaq 0x2(%rsp), %r15
movq %r15, %rdi
movl $0xd, %esi
movq %r14, %rdx
callq 0x4129e0
movabsq $0x7461745379616c50, %rax # imm = 0x7461745379616C50
xorq (%r15), %rax
movabsq $0x336e6f69746174, %rcx # imm = 0x336E6F69746174
xorq 0x5(%r15), %rcx
orq %rax, %rcx
je 0x401aa2
leaq 0x2(%rsp), %rdi
callq 0x413460
movq %r14, %rdi
callq 0x4126f0
movq %r14, %rdi
callq 0x422610
leaq 0xa16a9(%rip), %rdi     # 0x4a30e5
xorl %eax, %eax
callq 0x40c1a0
jmp 0x401a7f
leaq 0xa16a7(%rip), %rdi     # 0x4a30f3
callq 0x413460
movl $0x1, %r15d
jmp 0x401a8d
leaq 0xa16b4(%rip), %rdi     # 0x4a3114
callq 0x413460
jmp 0x401a7f
callq 0x4046c0
movl (%rax), %edx
leaq 0xa15dc(%rip), %rdi     # 0x4a3051
movq %rbx, %rsi
xorl %eax, %eax
callq 0x40c1a0
movl $0x1, %r15d
testl %r15d, %r15d
jne 0x401a8d
xorl %r15d, %r15d
movl %r15d, %eax
addq $0x1018, %rsp           # imm = 0x1018
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xa15e2(%rip), %rsi     # 0x4a308b
leaq 0x10(%rsp), %rdi
callq 0x412c90
testq %rax, %rax
je 0x401b63
movq %rax, %r13
xorl %r15d, %r15d
movq %rax, %rdi
xorl %esi, %esi
movl $0x2, %edx
callq 0x416cd0
movq %r13, %rdi
callq 0x412e90
movq %rax, %r12
movq %r13, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x416cd0
leaq 0x1(%r12), %rdi
callq 0x4222d0
movq %rax, %rbp
movl $0x1, %esi
movq %rax, %rdi
movq %r12, %rdx
movq %r13, %rcx
callq 0x412d90
movq %r13, %rdi
callq 0x4126f0
movq %r13, %rdi
callq 0x422610
movl $0xf70, %esi            # imm = 0xF70
movq %r14, %rdi
xorl %edx, %edx
callq 0x416cd0
movl $0x1, %edx
movq %rbp, %rdi
movq %r12, %rsi
movq %r14, %rcx
callq 0x412fe0
leaq 0xa157e(%rip), %rdi     # 0x4a30bf
movq %r12, %rsi
movq %rbx, %rdx
xorl %eax, %eax
callq 0x40c1a0
movq %r14, %rdi
callq 0x4126f0
movq %r14, %rdi
callq 0x422610
jmp 0x401a85
callq 0x4046c0
movl (%rax), %edx
leaq 0xa151d(%rip), %rdi     # 0x4a308e
leaq 0x10(%rsp), %rsi
jmp 0x401a78
nopl (%rax,%rax)