// File: /workspace/github_c_makefile_star2_O2/Zarh[P]regps3iso/main.c
// Function: fixps3iso_region
void fixps3iso_region(char *iso)
{
	struct stat s;
	FILE *f = NULL;
	
	if(stat(iso, &s) != 0) {
		printf("Error : file doesn't exist %s", iso);
		return;
	} 
	
	file_info_t *file_info=NULL;
	u32 file_number;
	
	GetFileInfo(iso, &file_info, &file_number);
	
	u32 i;
	u32 plain_n=1;
	u32 *start = (u32 *) malloc(plain_n * sizeof(u32) );
	u32 *end = malloc(plain_n * sizeof(u32) );
	start[plain_n-1] = 0;
	
	u8 is_plain=YES;
	for(i=0; i<file_number; i++) {	
		
		u32 len = strlen(file_info[i].path);
		if( (memcmp(file_info[i].path, "/PS3_GAME/", 10) == 0 || memcmp(file_info[i].path, "/PS3_GM", 7) == 0) && 10 < len ) {
			
			if( memcmp(file_info[i].path + 10, "TROPDIR", 7) == 0 
			||  memcmp(file_info[i].path + 10, "USRDIR" , 6) == 0 
			||  memcmp(file_info[i].path + 10, "LICDIR" , 6) == 0 ) {
				if( is_plain == YES ) {
					is_plain = NO;
					end[plain_n - 1] = file_info[i].sector - 1;
				}
			} else {
				if( is_plain == NO ) {
					is_plain = YES;
					plain_n++;
					start = realloc(start, plain_n * sizeof(u32));
					end = realloc(end, plain_n * sizeof(u32));
					start[plain_n-1] = file_info[i].sector;
				}
			}
		} else {
			if( is_plain == NO ) {
				is_plain = YES;
				plain_n++;
				start = realloc(start, plain_n * sizeof(u32));
				end = realloc(end, plain_n * sizeof(u32));
				start[plain_n-1] = file_info[i].sector;
			}
		}
	}
	if( is_plain == NO) { // PUP is supposed to be the last file, but just in case, there is no pup or if the iso isn't 'proper'
		is_plain = YES;
		plain_n++;
		start = realloc(start, plain_n * sizeof(u32));
		end = realloc(end, plain_n * sizeof(u32));
		u64 footer = file_info[file_number-1].sector*0x800ULL  + file_info[file_number-1].size;
		footer = (footer + 2047)/2048;	
		start[plain_n-1] = (u32) footer; 
	}
	char *ext = GetExtension(iso);
	if( strcasecmp(ext, ".header.bin") == 0 ) {
		u32 current_plain_n;
		f = fopen(iso, "rb+");
		if(f==NULL ) {
			printf("Error : cannot fopen %s", iso);
			return;
		}
		fread(&current_plain_n, 4, 1, f);
		current_plain_n = SWAP_BE(current_plain_n);
		fseek(f, 8 + current_plain_n*8 - 4, SEEK_SET);
		u32 tot_sec=0;
		fread(&tot_sec, 4, 1, f);
		end[plain_n-1] = SWAP_BE(tot_sec);
		fseek(f, 0, SEEK_SET);
	} else 
	if( strcasecmp(ext, ".iso") == 0) {
		end[plain_n-1] = (u32) (s.st_size / 0x800ULL);
	}
	
	if(f==NULL) f = fopen(iso, "rb+");
	if(f==NULL ) {
		printf("Error : cannot fopen %s", iso);
		return;
	}
	plain_n = SWAP_BE(plain_n);
	fwrite(&plain_n, 4, 1, f);
	plain_n = SWAP_BE(plain_n);
	fseek(f, 8, SEEK_SET);
	for(i=0; i<plain_n; i++) {
		start[i] = SWAP_BE(start[i]);
		end[i] = SWAP_BE(end[i]);
		
		fwrite(&start[i], 4, 1, f);
		fwrite(&end[i], 4, 1, f);
	}
	fclose(f);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xc8, %rsp
movq %rdi, %rbx
leaq 0x38(%rsp), %rsi
callq 0x464cb0
testl %eax, %eax
je 0x402bfc
leaq 0xbf5d5(%rip), %rdi     # 0x4c21b6
movq %rbx, %rsi
xorl %eax, %eax
addq $0xc8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x40dcf0
leaq 0x20(%rsp), %rsi
andq $0x0, (%rsi)
leaq 0x1c(%rsp), %rdx
movq %rbx, 0x28(%rsp)
movq %rbx, %rdi
callq 0x40323a
movl $0x1, 0x8(%rsp)
pushq $0x4
popq %r14
movq %r14, %rdi
callq 0x42a820
movq %rax, %rbx
movq %r14, %rdi
callq 0x42a820
movq %rbx, 0x10(%rsp)
andl $0x0, (%rbx)
movq %rax, %r14
movb $0x1, %al
movl %eax, 0xc(%rsp)
pushq $0x8
popq %r13
xorl %r15d, %r15d
movl 0x1c(%rsp), %eax
cmpq %rax, %r15
jae 0x402d72
movq 0x20(%rsp), %rbx
movq -0x8(%rbx,%r13), %rbp
movq %rbp, %rdi
callq 0x4010f8
movq %rax, %r12
movq %rbp, %rdi
leaq 0xbf55a(%rip), %rsi     # 0x4c21d4
pushq $0xa
popq %rdx
callq 0x401028
testl %eax, %eax
je 0x402caa
movq %rbp, %rdi
leaq 0xbf54f(%rip), %rsi     # 0x4c21df
pushq $0x7
popq %rdx
callq 0x401028
testl %eax, %eax
sete %al
cmpl $0xb, %r12d
setae %cl
testb %cl, %al
jne 0x402cb0
jmp 0x402cf6
cmpl $0xb, %r12d
jb 0x402cf6
addq $0xa, %rbp
movq %rbp, %rdi
leaq 0xbf3af(%rip), %rsi     # 0x4c206d
pushq $0x7
popq %rdx
callq 0x401028
testl %eax, %eax
je 0x402d41
movq %rbp, %rdi
leaq 0xbf3a1(%rip), %rsi     # 0x4c2075
pushq $0x6
popq %rdx
callq 0x401028
testl %eax, %eax
je 0x402d41
movq %rbp, %rdi
leaq 0xbf4fd(%rip), %rsi     # 0x4c21e7
pushq $0x6
popq %rdx
callq 0x401028
testl %eax, %eax
je 0x402d41
cmpb $0x0, 0xc(%rsp)
jne 0x402d66
movl 0x8(%rsp), %esi
incl %esi
movl %esi, 0x8(%rsp)
shlq $0x2, %rsi
movq 0x10(%rsp), %rdi
callq 0x42aeb0
movq %rax, %rbx
movl 0x8(%rsp), %esi
shlq $0x2, %rsi
movq %r14, %rdi
callq 0x42aeb0
movq 0x20(%rsp), %rcx
movl (%rcx,%r13), %ecx
movb $0x1, %dl
movl %edx, 0xc(%rsp)
movq %rbx, 0x10(%rsp)
movq %rbx, %r14
jmp 0x402d59
cmpb $0x1, 0xc(%rsp)
jne 0x402d66
movl (%rbx,%r13), %ecx
decl %ecx
movl $0x0, 0xc(%rsp)
movq %r14, %rax
movl 0x8(%rsp), %edx
decl %edx
movl %ecx, (%r14,%rdx,4)
movq %rax, %r14
incq %r15
addq $0x18, %r13
jmp 0x402c4e
cmpb $0x0, 0xc(%rsp)
jne 0x402dd6
movl 0x8(%rsp), %esi
incl %esi
movl %esi, 0x8(%rsp)
shlq $0x2, %rsi
movq 0x10(%rsp), %rdi
callq 0x42aeb0
movq %rax, %rbx
movl 0x8(%rsp), %esi
shlq $0x2, %rsi
movq %r14, %rdi
callq 0x42aeb0
movq 0x20(%rsp), %rcx
movl 0x1c(%rsp), %edx
decl %edx
imulq $0x18, %rdx, %rdx
movl $0x7ff, %esi            # imm = 0x7FF
addq 0x10(%rcx,%rdx), %rsi
shrq $0xb, %rsi
addl 0x8(%rcx,%rdx), %esi
movq %rax, %r14
movl 0x8(%rsp), %eax
decl %eax
movq %rbx, 0x10(%rsp)
movl %esi, (%rbx,%rax,4)
movq 0x28(%rsp), %rbx
movq %rbx, %rdi
callq 0x401b4c
movq %rax, %r15
leaq 0xbf22e(%rip), %rsi     # 0x4c201b
movq %rax, %rdi
callq 0x401098
testl %eax, %eax
je 0x402e3f
leaq 0xbf460(%rip), %rsi     # 0x4c2260
movq %r15, %rdi
callq 0x401098
testl %eax, %eax
jne 0x402e1f
movq 0x68(%rsp), %rax
shrq $0xb, %rax
movl 0x8(%rsp), %ecx
decl %ecx
movl %eax, (%r14,%rcx,4)
leaq 0xbf3c8(%rip), %rsi     # 0x4c21ee
movq %rbx, %rdi
callq 0x41c7c0
movq %rax, %r15
testq %rax, %rax
jne 0x402ec5
jmp 0x402f5d
leaq 0xbf3a8(%rip), %rsi     # 0x4c21ee
movq %rbx, %rdi
callq 0x41c7c0
testq %rax, %rax
je 0x402f5d
movq %rax, %r15
leaq 0x34(%rsp), %r12
pushq $0x4
popq %r13
pushq $0x1
popq %rbp
movq %r12, %rdi
movq %r13, %rsi
movq %rbp, %rdx
movq %rax, %rcx
callq 0x41c9f0
movl (%r12), %eax
bswapl %eax
leal 0x4(,%rax,8), %esi
movq %r15, %rdi
xorl %edx, %edx
callq 0x41f220
leaq 0x30(%rsp), %r12
andl $0x0, (%r12)
movq %r12, %rdi
movq %r13, %rsi
movq %rbp, %rdx
movq %r15, %rcx
callq 0x41c9f0
movl (%r12), %eax
bswapl %eax
movl 0x8(%rsp), %ecx
decl %ecx
movl %eax, (%r14,%rcx,4)
movq %r15, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x41f220
leaq 0x8(%rsp), %rbx
movl (%rbx), %eax
bswapl %eax
movl %eax, (%rbx)
pushq $0x4
popq %rsi
pushq $0x1
popq %r13
movq %rbx, %rdi
movq %r13, %rdx
movq %r15, %rcx
callq 0x41caf0
movl (%rbx), %ebp
bswapl %ebp
movl %ebp, (%rbx)
pushq $0x8
popq %rsi
xorl %ebx, %ebx
movq %r15, %rdi
xorl %edx, %edx
callq 0x41f220
movl %ebp, %eax
cmpq %rax, %rbx
jae 0x402f53
movq 0x10(%rsp), %r12
movl (%r12), %eax
bswapl %eax
movl %eax, (%r12)
movl (%r14), %eax
bswapl %eax
movl %eax, (%r14)
movq %r12, %rdi
pushq $0x4
popq %rbp
movq %rbp, %rsi
movq %r13, %rdx
movq %r15, %rcx
callq 0x41caf0
movq %r14, %rdi
movq %rbp, %rsi
movq %r13, %rdx
movq %r15, %rcx
callq 0x41caf0
incq %rbx
movl 0x8(%rsp), %ebp
addq $0x4, %r12
movq %r12, 0x10(%rsp)
addq $0x4, %r14
jmp 0x402efa
movq %r15, %rdi
callq 0x41c220
jmp 0x402f6e
leaq 0xbf118(%rip), %rdi     # 0x4c207c
movq %rbx, %rsi
xorl %eax, %eax
callq 0x40dcf0
addq $0xc8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq