// File: /workspace/github_c_makefile_star2_O2/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 0x401a01
movq 0x8(%rsi), %rbx
movq %rbx, %rdi
callq 0x4017d9
testl %eax, %eax
je 0x401a26
leaq 0xa16c2(%rip), %rdi     # 0x4a300e
movq %rbx, %rsi
xorl %eax, %eax
callq 0x40c150
leaq 0x10(%rsp), %r14
movq %rbx, %rdi
movq %r14, %rsi
callq 0x40185e
cmpb $0x0, 0x5(%r14)
je 0x401a2b
leaq 0xa16be(%rip), %rdi     # 0x4a3036
leaq 0x10(%rsp), %rsi
xorl %eax, %eax
callq 0x40c150
leaq 0xa16c2(%rip), %rsi     # 0x4a304d
movq %rbx, %rdi
callq 0x412c40
testq %rax, %rax
je 0x401a34
movq %rax, %r14
movl $0x800, %esi            # imm = 0x800
movq %rax, %rdi
xorl %edx, %edx
callq 0x416c80
leaq 0x2(%rsp), %r15
pushq $0xd
popq %rsi
movq %r15, %rdi
movq %r14, %rdx
callq 0x412990
leaq 0xa16b6(%rip), %rsi     # 0x4a307e
pushq $0xd
popq %rdx
movq %r15, %rdi
callq 0x401028
testl %eax, %eax
je 0x401a4e
leaq 0x2(%rsp), %rdi
callq 0x413410
movq %r14, %rdi
callq 0x4126a0
movq %r14, %rdi
callq 0x4225c0
leaq 0xa16ed(%rip), %rdi     # 0x4a30e5
xorl %eax, %eax
callq 0x40c150
jmp 0x401a0d
leaq 0xa16eb(%rip), %rdi     # 0x4a30f3
callq 0x413410
pushq $0x1
popq %r15
movl %r15d, %eax
addq $0x1018, %rsp           # imm = 0x1018
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %r15d, %r15d
jmp 0x401a11
leaq 0xa16e2(%rip), %rdi     # 0x4a3114
jmp 0x401a08
callq 0x404670
movl (%rax), %edx
leaq 0xa160f(%rip), %rdi     # 0x4a3051
movq %rbx, %rsi
xorl %eax, %eax
callq 0x40c150
jmp 0x401a0d
leaq 0xa1636(%rip), %rsi     # 0x4a308b
leaq 0x10(%rsp), %rdi
callq 0x412c40
testq %rax, %rax
je 0x401b09
movq %rax, %r13
xorl %r15d, %r15d
pushq $0x2
popq %rdx
movq %rax, %rdi
xorl %esi, %esi
callq 0x416c80
movq %r13, %rdi
callq 0x412e40
movq %rax, %r12
movq %r13, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x416c80
leaq 0x1(%r12), %rdi
callq 0x422280
movq %rax, %rbp
pushq $0x1
popq %rsi
movq %rax, %rdi
movq %r12, %rdx
movq %r13, %rcx
callq 0x412d40
movq %r13, %rdi
callq 0x4126a0
movq %r13, %rdi
callq 0x4225c0
movl $0xf70, %esi            # imm = 0xF70
movq %r14, %rdi
xorl %edx, %edx
callq 0x416c80
movq %rbp, %rdi
movq %r12, %rsi
pushq $0x1
popq %rdx
movq %r14, %rcx
callq 0x412f90
leaq 0xa15d8(%rip), %rdi     # 0x4a30bf
movq %r12, %rsi
movq %rbx, %rdx
xorl %eax, %eax
callq 0x40c150
movq %r14, %rdi
callq 0x4126a0
movq %r14, %rdi
callq 0x4225c0
jmp 0x401a11
callq 0x404670
movl (%rax), %edx
leaq 0xa1577(%rip), %rdi     # 0x4a308e
leaq 0x10(%rsp), %rsi
jmp 0x401a45
nopw %cs:(%rax,%rax)
nopl (%rax,%rax)