// File: /workspace/github_c_makefile_star2_O1/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, %r14
leaq 0x28(%rsp), %rsi
callq 0x465030
testl %eax, %eax
je 0x402def
leaq 0xc03e2(%rip), %rdi     # 0x4c31b6
movq %r14, %rsi
xorl %eax, %eax
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x40e0a0
leaq 0x10(%rsp), %rsi
movq $0x0, (%rsi)
leaq 0xc(%rsp), %r12
movq %r14, %rdi
movq %r12, %rdx
callq 0x40351b
movl $0x1, 0x4(%rsp)
movl $0x4, %edi
callq 0x42abd0
movq %rax, %rbx
movl $0x4, %edi
callq 0x42abd0
movq %rax, %r15
movl $0x0, (%rbx)
cmpl $0x0, (%r12)
je 0x402f8d
movq %r14, 0x18(%rsp)
movb $0x1, %al
movl %eax, 0x8(%rsp)
movl $0x8, %r13d
xorl %ebp, %ebp
movq 0x10(%rsp), %r14
movq -0x8(%r14,%r13), %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 0x402ea8
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 0x402f01
cmpl $0xa, %eax
ja 0x402ead
jmp 0x402f01
cmpl $0xb, %eax
jb 0x402f01
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 0x402f45
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 0x402f45
movl 0xa(%r12), %eax
movl $0x4443494c, %ecx       # imm = 0x4443494C
xorl %ecx, %eax
movzwl 0xe(%r12), %ecx
xorl $0x5249, %ecx           # imm = 0x5249
orl %eax, %ecx
je 0x402f45
cmpb $0x0, 0x8(%rsp)
jne 0x402f6a
movl 0x4(%rsp), %esi
incl %esi
movl %esi, 0x4(%rsp)
shlq $0x2, %rsi
movq %rbx, %rdi
callq 0x42b260
movq %rax, %rbx
movl 0x4(%rsp), %esi
shlq $0x2, %rsi
movq %r15, %rdi
callq 0x42b260
movq 0x10(%rsp), %rcx
movl (%rcx,%r13), %ecx
movb $0x1, %dl
movl %edx, 0x8(%rsp)
movq %rbx, %r15
jmp 0x402f5d
cmpb $0x1, 0x8(%rsp)
jne 0x402f6a
movl (%r14,%r13), %ecx
decl %ecx
movl $0x0, 0x8(%rsp)
movq %r15, %rax
movl 0x4(%rsp), %edx
decl %edx
movl %ecx, (%r15,%rdx,4)
movq %rax, %r15
incq %rbp
movl 0xc(%rsp), %eax
addq $0x18, %r13
cmpq %rax, %rbp
jb 0x402e51
cmpb $0x0, 0x8(%rsp)
sete %al
movq 0x18(%rsp), %r14
jmp 0x402f8f
xorl %eax, %eax
testb %al, %al
je 0x402fe9
movl 0x4(%rsp), %esi
incl %esi
movl %esi, 0x4(%rsp)
shlq $0x2, %rsi
movq %rbx, %rdi
callq 0x42b260
movq %rax, %rbx
movl 0x4(%rsp), %esi
shlq $0x2, %rsi
movq %r15, %rdi
callq 0x42b260
movq 0x10(%rsp), %rcx
movl 0xc(%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, %r15
movl 0x4(%rsp), %eax
decl %eax
movl %esi, (%rbx,%rax,4)
movq %r14, %rdi
callq 0x401b4c
movq %rax, %r12
leaq 0xc0020(%rip), %rsi     # 0x4c301b
movq %rax, %rdi
callq 0x401098
testl %eax, %eax
je 0x403035
leaq 0xc0255(%rip), %rsi     # 0x4c3263
movq %r12, %rdi
callq 0x401098
testl %eax, %eax
jne 0x40302d
movq 0x58(%rsp), %rax
shrq $0xb, %rax
movl 0x4(%rsp), %ecx
decl %ecx
movl %eax, (%r15,%rcx,4)
xorl %r12d, %r12d
jmp 0x4030db
leaq 0xc01b2(%rip), %rsi     # 0x4c31ee
movq %r14, %rdi
callq 0x41cb70
movq %rax, %r12
testq %rax, %rax
je 0x4030c1
leaq 0x24(%rsp), %r13
movl $0x4, %esi
movl $0x1, %edx
movq %r13, %rdi
movq %r12, %rcx
callq 0x41cda0
movl (%r13), %eax
bswapl %eax
movl %eax, (%r13)
leal 0x4(,%rax,8), %esi
movq %r12, %rdi
xorl %edx, %edx
callq 0x41f5d0
leaq 0x20(%rsp), %r13
movl $0x0, (%r13)
movl $0x4, %esi
movl $0x1, %edx
movq %r13, %rdi
movq %r12, %rcx
callq 0x41cda0
movl (%r13), %eax
bswapl %eax
movl 0x4(%rsp), %ecx
decl %ecx
movl %eax, (%r15,%rcx,4)
movq %r12, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x41f5d0
jmp 0x4030d2
leaq 0xbffb4(%rip), %rdi     # 0x4c307c
movq %r14, %rsi
xorl %eax, %eax
callq 0x40e0a0
testq %r12, %r12
je 0x4031a4
testq %r12, %r12
jne 0x4030f2
leaq 0xc0107(%rip), %rsi     # 0x4c31ee
movq %r14, %rdi
callq 0x41cb70
movq %rax, %r12
testq %r12, %r12
je 0x403193
leaq 0x4(%rsp), %r14
movl (%r14), %eax
bswapl %eax
movl %eax, (%r14)
movl $0x4, %esi
movl $0x1, %edx
movq %r14, %rdi
movq %r12, %rcx
callq 0x41cea0
movl (%r14), %eax
bswapl %eax
movl %eax, (%r14)
movl $0x8, %esi
movq %r12, %rdi
xorl %edx, %edx
callq 0x41f5d0
cmpl $0x0, (%r14)
je 0x403189
xorl %r14d, %r14d
movl (%rbx), %eax
bswapl %eax
movl %eax, (%rbx)
movl (%r15), %eax
bswapl %eax
movl %eax, (%r15)
movl $0x4, %esi
movl $0x1, %edx
movq %rbx, %rdi
movq %r12, %rcx
callq 0x41cea0
movl $0x4, %esi
movl $0x1, %edx
movq %r15, %rdi
movq %r12, %rcx
callq 0x41cea0
incq %r14
movl 0x4(%rsp), %eax
addq $0x4, %rbx
addq $0x4, %r15
cmpq %rax, %r14
jb 0x40313d
movq %r12, %rdi
callq 0x41c5d0
jmp 0x4031a4
leaq 0xbfee2(%rip), %rdi     # 0x4c307c
movq %r14, %rsi
xorl %eax, %eax
callq 0x40e0a0
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq