// File: /workspace/github_c_makefile_star2_O3/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 $0xb8, %rsp
movq %rdi, %r15
leaq 0x28(%rsp), %rsi
callq 0x4650b0
testl %eax, %eax
je 0x402e70
leaq 0xc0361(%rip), %rdi     # 0x4c31b6
movq %r15, %rsi
xorl %eax, %eax
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x40e110
leaq 0x10(%rsp), %rsi
movq $0x0, (%rsi)
leaq 0x4(%rsp), %r12
movq %r15, %rdi
movq %r12, %rdx
callq 0x40358c
movl $0x1, (%rsp)
movl $0x4, %edi
callq 0x42ac40
movq %rax, %rbx
movl $0x4, %edi
callq 0x42ac40
movq %rax, %r14
movl $0x0, (%rbx)
cmpl $0x0, (%r12)
je 0x40308e
movq %rbx, 0x8(%rsp)
movq %r15, 0x18(%rsp)
movb $0x1, %bpl
movq 0x10(%rsp), %rdx
xorl %r13d, %r13d
leaq (,%r13,2), %rax
addq %r13, %rax
leaq 0x8(,%rax,8), %r15
movq %rdx, %rbx
movq -0x8(%rdx,%r15), %r12
movq %r12, %rdi
callq 0x4010f8
movq (%r12), %rcx
movabsq $0x4d41475f3353502f, %rdx # imm = 0x4D41475F3353502F
xorq %rdx, %rcx
movzwl 0x8(%r12), %edx
xorq $0x2f45, %rdx           # imm = 0x2F45
orq %rcx, %rdx
je 0x402f42
movl (%r12), %ecx
movl $0x3353502f, %edx       # imm = 0x3353502F
xorl %edx, %ecx
movl 0x3(%r12), %edx
movl $0x4d475f33, %esi       # imm = 0x4D475F33
xorl %esi, %edx
orl %ecx, %edx
jne 0x402fcf
cmpl $0xa, %eax
ja 0x402f4b
jmp 0x402fcf
cmpl $0xb, %eax
jb 0x402fcf
movl 0xa(%r12), %eax
movl $0x504f5254, %ecx       # imm = 0x504F5254
xorl %ecx, %eax
movl 0xd(%r12), %ecx
movl $0x52494450, %edx       # imm = 0x52494450
xorl %edx, %ecx
orl %eax, %ecx
je 0x402f9f
movl 0xa(%r12), %eax
movl $0x44525355, %ecx       # imm = 0x44525355
xorl %ecx, %eax
movzwl 0xe(%r12), %ecx
xorl $0x5249, %ecx           # imm = 0x5249
orl %eax, %ecx
je 0x402f9f
movl 0xa(%r12), %eax
movl $0x4443494c, %ecx       # imm = 0x4443494C
xorl %ecx, %eax
movzwl 0xe(%r12), %ecx
xorl $0x5249, %ecx           # imm = 0x5249
orl %eax, %ecx
jne 0x402fcf
cmpb $0x1, %bpl
jne 0x402fd4
movq %rbx, %rdx
movl (%rbx,%r15), %eax
decl %eax
movl (%rsp), %ecx
decl %ecx
movl %eax, (%r14,%rcx,4)
incq %r13
movl 0x4(%rsp), %eax
addq $0x18, %r15
xorl %ebp, %ebp
cmpq %rax, %r13
jb 0x402ee6
jmp 0x403032
testb %bpl, %bpl
je 0x402fec
movq %rbx, %rdx
incq %r13
movl 0x4(%rsp), %eax
cmpq %rax, %r13
jb 0x402ed3
jmp 0x40322f
movl (%rsp), %esi
incl %esi
movl %esi, (%rsp)
shlq $0x2, %rsi
movq 0x8(%rsp), %rdi
callq 0x42b2d0
movq %rax, %rbx
movl (%rsp), %esi
shlq $0x2, %rsi
movq %r14, %rdi
callq 0x42b2d0
movq %rax, %r14
movq 0x10(%rsp), %rdx
movl (%rdx,%r15), %eax
movl (%rsp), %ecx
decl %ecx
movq %rbx, 0x8(%rsp)
movl %eax, (%rbx,%rcx,4)
movb $0x1, %bpl
jmp 0x402fd7
movq 0x18(%rsp), %r15
movq 0x8(%rsp), %rbx
movl (%rsp), %esi
incl %esi
movl %esi, (%rsp)
shlq $0x2, %rsi
movq %rbx, %rdi
callq 0x42b2d0
movq %rax, %rbx
movl (%rsp), %esi
shlq $0x2, %rsi
movq %r14, %rdi
callq 0x42b2d0
movq 0x10(%rsp), %rcx
movl 0x4(%rsp), %edx
decl %edx
leaq (%rdx,%rdx,2), %rdx
movl $0x7ff, %esi            # imm = 0x7FF
addq 0x10(%rcx,%rdx,8), %rsi
shrq $0xb, %rsi
addl 0x8(%rcx,%rdx,8), %esi
movq %rax, %r14
movl (%rsp), %eax
decl %eax
movl %esi, (%rbx,%rax,4)
movq %r15, %rdi
callq 0x401b4c
movq %rax, %r12
leaq 0xbff7b(%rip), %rsi     # 0x4c301b
movq %rax, %rdi
callq 0x401098
testl %eax, %eax
je 0x4030f1
leaq 0xc01ad(%rip), %rsi     # 0x4c3260
movq %r12, %rdi
callq 0x401098
testl %eax, %eax
jne 0x4030d1
movq 0x58(%rsp), %rax
shrq $0xb, %rax
movl (%rsp), %ecx
decl %ecx
movl %eax, (%r14,%rcx,4)
leaq 0xc0116(%rip), %rsi     # 0x4c31ee
movq %r15, %rdi
callq 0x41cbe0
movq %rax, %r12
testq %rax, %rax
jne 0x403177
jmp 0x40320c
leaq 0xc00f6(%rip), %rsi     # 0x4c31ee
movq %r15, %rdi
callq 0x41cbe0
testq %rax, %rax
je 0x40320c
movq %rax, %r12
leaq 0x24(%rsp), %r15
movl $0x4, %esi
movl $0x1, %edx
movq %r15, %rdi
movq %rax, %rcx
callq 0x41ce10
movl (%r15), %eax
bswapl %eax
leal 0x4(,%rax,8), %esi
movq %r12, %rdi
xorl %edx, %edx
callq 0x41f640
leaq 0x20(%rsp), %r15
movl $0x0, (%r15)
movl $0x4, %esi
movl $0x1, %edx
movq %r15, %rdi
movq %r12, %rcx
callq 0x41ce10
movl (%r15), %eax
bswapl %eax
movl (%rsp), %ecx
decl %ecx
movl %eax, (%r14,%rcx,4)
movq %r12, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x41f640
movq %rsp, %r15
movl (%r15), %eax
bswapl %eax
movl %eax, (%r15)
movl $0x4, %esi
movl $0x1, %edx
movq %r15, %rdi
movq %r12, %rcx
callq 0x41cf10
movl (%r15), %ebp
movl %ebp, %eax
bswapl %eax
movl %eax, (%r15)
movl $0x8, %esi
movq %r12, %rdi
xorl %edx, %edx
callq 0x41f640
testl %ebp, %ebp
je 0x403202
xorl %r15d, %r15d
movl (%rbx), %eax
bswapl %eax
movl %eax, (%rbx)
movl (%r14), %eax
bswapl %eax
movl %eax, (%r14)
movl $0x4, %esi
movl $0x1, %edx
movq %rbx, %rdi
movq %r12, %rcx
callq 0x41cf10
movl $0x4, %esi
movl $0x1, %edx
movq %r14, %rdi
movq %r12, %rcx
callq 0x41cf10
incq %r15
movl (%rsp), %eax
addq $0x4, %rbx
addq $0x4, %r14
cmpq %rax, %r15
jb 0x4031b7
movq %r12, %rdi
callq 0x41c640
jmp 0x40321d
leaq 0xbfe69(%rip), %rdi     # 0x4c307c
movq %r15, %rsi
xorl %eax, %eax
callq 0x40e110
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
testb %bpl, %bpl
movq 0x18(%rsp), %r15
movq 0x8(%rsp), %rbx
jne 0x40308e
jmp 0x40303c