// File: /workspace/github_c_makefile_star2_O0/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:
subq $0x1078, %rsp           # imm = 0x1078
movl $0x0, 0x1074(%rsp)
movl %edi, 0x1070(%rsp)
movq %rsi, 0x1068(%rsp)
movq %rdx, 0x1060(%rsp)
cmpl $0x1, 0x1070(%rsp)
jle 0x401b39
movq 0x1068(%rsp), %rax
movq 0x8(%rax), %rax
movq %rax, 0x1058(%rsp)
jmp 0x401b57
leaq 0xa75a1(%rip), %rdi     # 0x4a90e1
movb $0x0, %al
callq 0x40c410
movl $0x1, 0x1074(%rsp)
jmp 0x401ddd
movq 0x1058(%rsp), %rdi
callq 0x4017c0
cmpl $0x0, %eax
je 0x401dd2
movq 0x1058(%rsp), %rsi
leaq 0xa7587(%rip), %rdi     # 0x4a9103
movb $0x0, %al
callq 0x40c410
movq 0x1058(%rsp), %rdi
leaq 0x50(%rsp), %rsi
callq 0x4019a0
movsbl 0x55(%rsp), %eax
cmpl $0x0, %eax
jne 0x401bbd
leaq 0xa7585(%rip), %rdi     # 0x4a912b
movb $0x0, %al
callq 0x40c410
movl $0x1, 0x1074(%rsp)
jmp 0x401ddd
leaq 0x50(%rsp), %rsi
leaq 0xa758f(%rip), %rdi     # 0x4a9158
movb $0x0, %al
callq 0x40c410
movq 0x1058(%rsp), %rdi
leaq 0xa7590(%rip), %rsi     # 0x4a916f
callq 0x412f00
movq %rax, 0x48(%rsp)
cmpq $0x0, 0x48(%rsp)
jne 0x401c28
movq 0x1058(%rsp), %rax
movq %rax, 0x10(%rsp)
callq 0x404930
movq 0x10(%rsp), %rsi
movl (%rax), %edx
leaq 0xa7562(%rip), %rdi     # 0x4a9173
movb $0x0, %al
callq 0x40c410
movl $0x1, 0x1074(%rsp)
jmp 0x401ddd
movq 0x48(%rsp), %rdi
movl $0x800, %esi            # imm = 0x800
xorl %edx, %edx
callq 0x416da0
leaq 0x3a(%rsp), %rdi
movq 0x48(%rsp), %rdx
movl $0xd, %esi
callq 0x412c50
leaq 0x3a(%rsp), %rdi
leaq 0xa7547(%rip), %rsi     # 0x4a91a0
callq 0x4010c0
cmpl $0x0, %eax
jne 0x401d8e
leaq 0x50(%rsp), %rdi
leaq 0xa753a(%rip), %rsi     # 0x4a91ad
callq 0x412f00
movq %rax, 0x30(%rsp)
cmpq $0x0, 0x30(%rsp)
jne 0x401cb9
leaq 0x50(%rsp), %rax
movq %rax, 0x8(%rsp)
callq 0x404930
movq 0x8(%rsp), %rsi
movl (%rax), %edx
leaq 0xa750e(%rip), %rdi     # 0x4a91b0
movb $0x0, %al
callq 0x40c410
movl $0x1, 0x1074(%rsp)
jmp 0x401ddd
movq 0x30(%rsp), %rdi
xorl %eax, %eax
movl %eax, %esi
movl $0x2, %edx
callq 0x416da0
movq 0x30(%rsp), %rdi
callq 0x413100
movq %rax, 0x28(%rsp)
movq 0x30(%rsp), %rdi
xorl %eax, %eax
movl %eax, %esi
xorl %edx, %edx
callq 0x416da0
movq 0x28(%rsp), %rdi
addq $0x1, %rdi
callq 0x4223a0
movq %rax, 0x20(%rsp)
movq 0x20(%rsp), %rdi
movq 0x28(%rsp), %rdx
movq 0x30(%rsp), %rcx
movl $0x1, %esi
callq 0x413000
movq 0x30(%rsp), %rdi
callq 0x412960
movq 0x30(%rsp), %rdi
callq 0x4226e0
movl $0xf70, 0x1c(%rsp)      # imm = 0xF70
movq 0x48(%rsp), %rdi
movslq 0x1c(%rsp), %rsi
xorl %edx, %edx
callq 0x416da0
movq 0x20(%rsp), %rdi
movq 0x28(%rsp), %rsi
movq 0x48(%rsp), %rcx
movl $0x1, %edx
callq 0x413250
movq 0x28(%rsp), %rsi
movq 0x1058(%rsp), %rdx
leaq 0xa7470(%rip), %rdi     # 0x4a91e1
movb $0x0, %al
callq 0x40c410
movq 0x48(%rsp), %rdi
callq 0x412960
movq 0x48(%rsp), %rdi
callq 0x4226e0
jmp 0x401dd0
leaq 0x3a(%rsp), %rsi
leaq 0xbe4ae(%rip), %rdi     # 0x4c0248
movb $0x0, %al
callq 0x40c410
movq 0x48(%rsp), %rdi
callq 0x412960
movq 0x48(%rsp), %rdi
callq 0x4226e0
leaq 0xa744b(%rip), %rdi     # 0x4a9207
movb $0x0, %al
callq 0x40c410
movl $0x1, 0x1074(%rsp)
jmp 0x401ddd
jmp 0x401dd2
movl $0x0, 0x1074(%rsp)
movl 0x1074(%rsp), %eax
addq $0x1078, %rsp           # imm = 0x1078
retq
nopl (%rax)