// File: /workspace/github_c_makefile_star2_O3/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 0x401a44
movq 0x8(%rsi), %rbx
xorl %r15d, %r15d
movq %rbx, %rdi
xorl %esi, %esi
callq 0x45cc90
cmpl $-0x1, %eax
je 0x401a56
leaq 0xa168b(%rip), %rdi     # 0x4a300e
movq %rbx, %rsi
xorl %eax, %eax
callq 0x40c190
leaq 0x10(%rsp), %r14
movq %rbx, %rdi
movq %r14, %rsi
callq 0x401862
cmpb $0x0, 0x5(%r14)
je 0x401a6b
leaq 0xa1687(%rip), %rdi     # 0x4a3036
leaq 0x10(%rsp), %rsi
xorl %eax, %eax
callq 0x40c190
leaq 0xa168b(%rip), %rsi     # 0x4a304d
movq %rbx, %rdi
callq 0x412c80
testq %rax, %rax
je 0x401a74
movq %rax, %r14
movl $0x800, %esi            # imm = 0x800
movq %rax, %rdi
xorl %edx, %edx
callq 0x416cc0
leaq 0x2(%rsp), %r15
movq %r15, %rdi
movl $0xd, %esi
movq %r14, %rdx
callq 0x4129d0
movabsq $0x7461745379616c50, %rax # imm = 0x7461745379616C50
xorq (%r15), %rax
movabsq $0x336e6f69746174, %rcx # imm = 0x336E6F69746174
xorq 0x5(%r15), %rcx
orq %rax, %rcx
je 0x401a8e
leaq 0x2(%rsp), %rdi
callq 0x413450
movq %r14, %rdi
callq 0x4126e0
movq %r14, %rdi
callq 0x422600
leaq 0xa16aa(%rip), %rdi     # 0x4a30e5
xorl %eax, %eax
callq 0x40c190
jmp 0x401a50
leaq 0xa16a8(%rip), %rdi     # 0x4a30f3
callq 0x413450
movl $0x1, %r15d
movl %r15d, %eax
addq $0x1018, %rsp           # imm = 0x1018
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xa16a2(%rip), %rdi     # 0x4a3114
jmp 0x401a4b
callq 0x4046b0
movl (%rax), %edx
leaq 0xa15cf(%rip), %rdi     # 0x4a3051
movq %rbx, %rsi
xorl %eax, %eax
callq 0x40c190
jmp 0x401a50
leaq 0xa15f6(%rip), %rsi     # 0x4a308b
leaq 0x10(%rsp), %rdi
callq 0x412c80
testq %rax, %rax
je 0x401b4f
movq %rax, %r13
xorl %r15d, %r15d
movq %rax, %rdi
xorl %esi, %esi
movl $0x2, %edx
callq 0x416cc0
movq %r13, %rdi
callq 0x412e80
movq %rax, %r12
movq %r13, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x416cc0
leaq 0x1(%r12), %rdi
callq 0x4222c0
movq %rax, %rbp
movl $0x1, %esi
movq %rax, %rdi
movq %r12, %rdx
movq %r13, %rcx
callq 0x412d80
movq %r13, %rdi
callq 0x4126e0
movq %r13, %rdi
callq 0x422600
movl $0xf70, %esi            # imm = 0xF70
movq %r14, %rdi
xorl %edx, %edx
callq 0x416cc0
movl $0x1, %edx
movq %rbp, %rdi
movq %r12, %rsi
movq %r14, %rcx
callq 0x412fd0
leaq 0xa1592(%rip), %rdi     # 0x4a30bf
movq %r12, %rsi
movq %rbx, %rdx
xorl %eax, %eax
callq 0x40c190
movq %r14, %rdi
callq 0x4126e0
movq %r14, %rdi
callq 0x422600
jmp 0x401a56
callq 0x4046b0
movl (%rax), %edx
leaq 0xa1531(%rip), %rdi     # 0x4a308e
leaq 0x10(%rsp), %rsi
jmp 0x401a85
nopw (%rax,%rax)