// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x130, %rsp            # imm = 0x130
movq %rdi, -0x8(%rbp)
movq $0x0, -0xa0(%rbp)
movq -0x8(%rbp), %rdi
leaq -0x98(%rbp), %rsi
callq 0x466d70
cmpl $0x0, %eax
je 0x4038a6
movq -0x8(%rbp), %rsi
leaq 0xc0936(%rip), %rdi     # 0x4c41d0
movb $0x0, %al
callq 0x40ff80
jmp 0x40423b
movq $0x0, -0xa8(%rbp)
movq -0x8(%rbp), %rdi
leaq -0xa8(%rbp), %rsi
leaq -0xac(%rbp), %rdx
callq 0x4046b0
movl $0x1, -0xb4(%rbp)
movl -0xb4(%rbp), %eax
movl %eax, %edi
shlq $0x2, %rdi
callq 0x42c910
movq %rax, -0xc0(%rbp)
movl -0xb4(%rbp), %eax
movl %eax, %edi
shlq $0x2, %rdi
callq 0x42c910
movq %rax, -0xc8(%rbp)
movq -0xc0(%rbp), %rax
movl -0xb4(%rbp), %ecx
subl $0x1, %ecx
movl %ecx, %ecx
movl $0x0, (%rax,%rcx,4)
movb $0x1, -0xc9(%rbp)
movl $0x0, -0xb0(%rbp)
movl -0xb0(%rbp), %eax
cmpl -0xac(%rbp), %eax
jae 0x403be8
movq -0xa8(%rbp), %rax
movl -0xb0(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq (%rax), %rdi
callq 0x4010f0
movl %eax, -0xd0(%rbp)
movq -0xa8(%rbp), %rax
movl -0xb0(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq (%rax), %rdi
leaq 0xc0870(%rip), %rsi     # 0x4c41ee
movl $0xa, %edx
callq 0x401098
cmpl $0x0, %eax
je 0x4039be
movq -0xa8(%rbp), %rax
movl -0xb0(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq (%rax), %rdi
leaq 0xc084e(%rip), %rsi     # 0x4c41f9
movl $0x7, %edx
callq 0x401098
cmpl $0x0, %eax
jne 0x403b40
movl $0xa, %eax
cmpl -0xd0(%rbp), %eax
jae 0x403b40
movq -0xa8(%rbp), %rax
movl -0xb0(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq (%rax), %rdi
addq $0xa, %rdi
leaq 0xc0696(%rip), %rsi     # 0x4c4087
movl $0x7, %edx
callq 0x401098
cmpl $0x0, %eax
je 0x403a62
movq -0xa8(%rbp), %rax
movl -0xb0(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq (%rax), %rdi
addq $0xa, %rdi
leaq 0xc066d(%rip), %rsi     # 0x4c408f
movl $0x6, %edx
callq 0x401098
cmpl $0x0, %eax
je 0x403a62
movq -0xa8(%rbp), %rax
movl -0xb0(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq (%rax), %rdi
addq $0xa, %rdi
leaq 0xc07ae(%rip), %rsi     # 0x4c4201
movl $0x6, %edx
callq 0x401098
cmpl $0x0, %eax
jne 0x403aa9
movzbl -0xc9(%rbp), %eax
cmpl $0x1, %eax
jne 0x403aa4
movb $0x0, -0xc9(%rbp)
movq -0xa8(%rbp), %rax
movl -0xb0(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0x8(%rax), %edx
subl $0x1, %edx
movq -0xc8(%rbp), %rax
movl -0xb4(%rbp), %ecx
subl $0x1, %ecx
movl %ecx, %ecx
movl %edx, (%rax,%rcx,4)
jmp 0x403b3b
movzbl -0xc9(%rbp), %eax
cmpl $0x0, %eax
jne 0x403b39
movb $0x1, -0xc9(%rbp)
movl -0xb4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xb4(%rbp)
movq -0xc0(%rbp), %rdi
movl -0xb4(%rbp), %eax
movl %eax, %esi
shlq $0x2, %rsi
callq 0x42cfa0
movq %rax, -0xc0(%rbp)
movq -0xc8(%rbp), %rdi
movl -0xb4(%rbp), %eax
movl %eax, %esi
shlq $0x2, %rsi
callq 0x42cfa0
movq %rax, -0xc8(%rbp)
movq -0xa8(%rbp), %rax
movl -0xb0(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0x8(%rax), %edx
movq -0xc0(%rbp), %rax
movl -0xb4(%rbp), %ecx
subl $0x1, %ecx
movl %ecx, %ecx
movl %edx, (%rax,%rcx,4)
jmp 0x403b3b
jmp 0x403bd2
movzbl -0xc9(%rbp), %eax
cmpl $0x0, %eax
jne 0x403bd0
movb $0x1, -0xc9(%rbp)
movl -0xb4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xb4(%rbp)
movq -0xc0(%rbp), %rdi
movl -0xb4(%rbp), %eax
movl %eax, %esi
shlq $0x2, %rsi
callq 0x42cfa0
movq %rax, -0xc0(%rbp)
movq -0xc8(%rbp), %rdi
movl -0xb4(%rbp), %eax
movl %eax, %esi
shlq $0x2, %rsi
callq 0x42cfa0
movq %rax, -0xc8(%rbp)
movq -0xa8(%rbp), %rax
movl -0xb0(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0x8(%rax), %edx
movq -0xc0(%rbp), %rax
movl -0xb4(%rbp), %ecx
subl $0x1, %ecx
movl %ecx, %ecx
movl %edx, (%rax,%rcx,4)
jmp 0x403bd2
jmp 0x403bd4
movl -0xb0(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xb0(%rbp)
jmp 0x40392c
movzbl -0xc9(%rbp), %eax
cmpl $0x0, %eax
jne 0x403cc6
movb $0x1, -0xc9(%rbp)
movl -0xb4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xb4(%rbp)
movq -0xc0(%rbp), %rdi
movl -0xb4(%rbp), %eax
movl %eax, %esi
shlq $0x2, %rsi
callq 0x42cfa0
movq %rax, -0xc0(%rbp)
movq -0xc8(%rbp), %rdi
movl -0xb4(%rbp), %eax
movl %eax, %esi
shlq $0x2, %rsi
callq 0x42cfa0
movq %rax, -0xc8(%rbp)
movq -0xa8(%rbp), %rax
movl -0xac(%rbp), %ecx
subl $0x1, %ecx
movl %ecx, %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0x8(%rax), %eax
shlq $0xb, %rax
movq -0xa8(%rbp), %rcx
movl -0xac(%rbp), %edx
subl $0x1, %edx
movl %edx, %edx
imulq $0x18, %rdx, %rdx
addq %rdx, %rcx
addq 0x10(%rcx), %rax
movq %rax, -0xd8(%rbp)
movq -0xd8(%rbp), %rax
addq $0x7ff, %rax            # imm = 0x7FF
shrq $0xb, %rax
movq %rax, -0xd8(%rbp)
movq -0xd8(%rbp), %rax
movl %eax, %edx
movq -0xc0(%rbp), %rax
movl -0xb4(%rbp), %ecx
subl $0x1, %ecx
movl %ecx, %ecx
movl %edx, (%rax,%rcx,4)
movq -0x8(%rbp), %rdi
callq 0x401af0
movq %rax, -0xe0(%rbp)
movq -0xe0(%rbp), %rdi
leaq 0xc0351(%rip), %rsi     # 0x4c4035
callq 0x401090
cmpl $0x0, %eax
jne 0x403ea4
movq -0x8(%rbp), %rdi
leaq 0xc050b(%rip), %rsi     # 0x4c4208
callq 0x41ea50
movq %rax, -0xa0(%rbp)
cmpq $0x0, -0xa0(%rbp)
jne 0x403d2a
movq -0x8(%rbp), %rsi
leaq 0xc0378(%rip), %rdi     # 0x4c4096
movb $0x0, %al
callq 0x40ff80
jmp 0x40423b
movq -0xa0(%rbp), %rcx
leaq -0xe4(%rbp), %rdi
movl $0x4, %esi
movl $0x1, %edx
callq 0x41ec80
movw $0x1, -0xe6(%rbp)
cmpb $0x0, -0xe6(%rbp)
jne 0x403d68
movl -0xe4(%rbp), %eax
movq %rax, -0x100(%rbp)
jmp 0x403db2
movl -0xe4(%rbp), %eax
andl $0xff, %eax
shll $0x18, %eax
movl -0xe4(%rbp), %ecx
andl $0xff00, %ecx           # imm = 0xFF00
shll $0x8, %ecx
orl %ecx, %eax
movl -0xe4(%rbp), %ecx
andl $0xff0000, %ecx         # imm = 0xFF0000
shrl $0x8, %ecx
orl %ecx, %eax
movl -0xe4(%rbp), %ecx
andl $0xff000000, %ecx       # imm = 0xFF000000
shrl $0x18, %ecx
orl %ecx, %eax
movl %eax, %eax
movq %rax, -0x100(%rbp)
movq -0x100(%rbp), %rax
movl %eax, -0xe4(%rbp)
movq -0xa0(%rbp), %rdi
movl -0xe4(%rbp), %eax
shll $0x3, %eax
addl $0x8, %eax
subl $0x4, %eax
movl %eax, %eax
movl %eax, %esi
xorl %edx, %edx
callq 0x421310
movl $0x0, -0xec(%rbp)
movq -0xa0(%rbp), %rcx
leaq -0xec(%rbp), %rdi
movl $0x4, %esi
movl $0x1, %edx
callq 0x41ec80
movw $0x1, -0xee(%rbp)
cmpb $0x0, -0xee(%rbp)
jne 0x403e28
movl -0xec(%rbp), %eax
movq %rax, -0x108(%rbp)
jmp 0x403e72
movl -0xec(%rbp), %eax
andl $0xff, %eax
shll $0x18, %eax
movl -0xec(%rbp), %ecx
andl $0xff00, %ecx           # imm = 0xFF00
shll $0x8, %ecx
orl %ecx, %eax
movl -0xec(%rbp), %ecx
andl $0xff0000, %ecx         # imm = 0xFF0000
shrl $0x8, %ecx
orl %ecx, %eax
movl -0xec(%rbp), %ecx
andl $0xff000000, %ecx       # imm = 0xFF000000
shrl $0x18, %ecx
orl %ecx, %eax
movl %eax, %eax
movq %rax, -0x108(%rbp)
movq -0x108(%rbp), %rax
movl %eax, %edx
movq -0xc8(%rbp), %rax
movl -0xb4(%rbp), %ecx
subl $0x1, %ecx
movl %ecx, %ecx
movl %edx, (%rax,%rcx,4)
movq -0xa0(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
xorl %edx, %edx
callq 0x421310
jmp 0x403edd
movq -0xe0(%rbp), %rdi
leaq 0xc035a(%rip), %rsi     # 0x4c420c
callq 0x401090
cmpl $0x0, %eax
jne 0x403edb
movq -0x68(%rbp), %rax
shrq $0xb, %rax
movl %eax, %edx
movq -0xc8(%rbp), %rax
movl -0xb4(%rbp), %ecx
subl $0x1, %ecx
movl %ecx, %ecx
movl %edx, (%rax,%rcx,4)
jmp 0x403edd
cmpq $0x0, -0xa0(%rbp)
jne 0x403efe
movq -0x8(%rbp), %rdi
leaq 0xc0316(%rip), %rsi     # 0x4c4208
callq 0x41ea50
movq %rax, -0xa0(%rbp)
cmpq $0x0, -0xa0(%rbp)
jne 0x403f1f
movq -0x8(%rbp), %rsi
leaq 0xc0183(%rip), %rdi     # 0x4c4096
movb $0x0, %al
callq 0x40ff80
jmp 0x40423b
movw $0x1, -0xf0(%rbp)
cmpb $0x0, -0xf0(%rbp)
jne 0x403f40
movl -0xb4(%rbp), %eax
movq %rax, -0x110(%rbp)
jmp 0x403f8a
movl -0xb4(%rbp), %eax
andl $0xff, %eax
shll $0x18, %eax
movl -0xb4(%rbp), %ecx
andl $0xff00, %ecx           # imm = 0xFF00
shll $0x8, %ecx
orl %ecx, %eax
movl -0xb4(%rbp), %ecx
andl $0xff0000, %ecx         # imm = 0xFF0000
shrl $0x8, %ecx
orl %ecx, %eax
movl -0xb4(%rbp), %ecx
andl $0xff000000, %ecx       # imm = 0xFF000000
shrl $0x18, %ecx
orl %ecx, %eax
movl %eax, %eax
movq %rax, -0x110(%rbp)
movq -0x110(%rbp), %rax
movl %eax, -0xb4(%rbp)
movq -0xa0(%rbp), %rcx
leaq -0xb4(%rbp), %rdi
movl $0x4, %esi
movl $0x1, %edx
callq 0x41ed80
movw $0x1, -0xf2(%rbp)
cmpb $0x0, -0xf2(%rbp)
jne 0x403fd5
movl -0xb4(%rbp), %eax
movq %rax, -0x118(%rbp)
jmp 0x40401f
movl -0xb4(%rbp), %eax
andl $0xff, %eax
shll $0x18, %eax
movl -0xb4(%rbp), %ecx
andl $0xff00, %ecx           # imm = 0xFF00
shll $0x8, %ecx
orl %ecx, %eax
movl -0xb4(%rbp), %ecx
andl $0xff0000, %ecx         # imm = 0xFF0000
shrl $0x8, %ecx
orl %ecx, %eax
movl -0xb4(%rbp), %ecx
andl $0xff000000, %ecx       # imm = 0xFF000000
shrl $0x18, %ecx
orl %ecx, %eax
movl %eax, %eax
movq %rax, -0x118(%rbp)
movq -0x118(%rbp), %rax
movl %eax, -0xb4(%rbp)
movq -0xa0(%rbp), %rdi
movl $0x8, %esi
xorl %edx, %edx
callq 0x421310
movl $0x0, -0xb0(%rbp)
movl -0xb0(%rbp), %eax
cmpl -0xb4(%rbp), %eax
jae 0x40422f
movw $0x1, -0xf4(%rbp)
cmpb $0x0, -0xf4(%rbp)
jne 0x404086
movq -0xc0(%rbp), %rax
movl -0xb0(%rbp), %ecx
movl (%rax,%rcx,4), %eax
movq %rax, -0x120(%rbp)
jmp 0x4040f8
movq -0xc0(%rbp), %rax
movl -0xb0(%rbp), %ecx
movl (%rax,%rcx,4), %eax
andl $0xff, %eax
shll $0x18, %eax
movq -0xc0(%rbp), %rcx
movl -0xb0(%rbp), %edx
movl (%rcx,%rdx,4), %ecx
andl $0xff00, %ecx           # imm = 0xFF00
shll $0x8, %ecx
orl %ecx, %eax
movq -0xc0(%rbp), %rcx
movl -0xb0(%rbp), %edx
movl (%rcx,%rdx,4), %ecx
andl $0xff0000, %ecx         # imm = 0xFF0000
shrl $0x8, %ecx
orl %ecx, %eax
movq -0xc0(%rbp), %rcx
movl -0xb0(%rbp), %edx
movl (%rcx,%rdx,4), %ecx
andl $0xff000000, %ecx       # imm = 0xFF000000
shrl $0x18, %ecx
orl %ecx, %eax
movl %eax, %eax
movq %rax, -0x120(%rbp)
movq -0x120(%rbp), %rax
movl %eax, %edx
movq -0xc0(%rbp), %rax
movl -0xb0(%rbp), %ecx
movl %edx, (%rax,%rcx,4)
movw $0x1, -0xf6(%rbp)
cmpb $0x0, -0xf6(%rbp)
jne 0x40413c
movq -0xc8(%rbp), %rax
movl -0xb0(%rbp), %ecx
movl (%rax,%rcx,4), %eax
movq %rax, -0x128(%rbp)
jmp 0x4041ae
movq -0xc8(%rbp), %rax
movl -0xb0(%rbp), %ecx
movl (%rax,%rcx,4), %eax
andl $0xff, %eax
shll $0x18, %eax
movq -0xc8(%rbp), %rcx
movl -0xb0(%rbp), %edx
movl (%rcx,%rdx,4), %ecx
andl $0xff00, %ecx           # imm = 0xFF00
shll $0x8, %ecx
orl %ecx, %eax
movq -0xc8(%rbp), %rcx
movl -0xb0(%rbp), %edx
movl (%rcx,%rdx,4), %ecx
andl $0xff0000, %ecx         # imm = 0xFF0000
shrl $0x8, %ecx
orl %ecx, %eax
movq -0xc8(%rbp), %rcx
movl -0xb0(%rbp), %edx
movl (%rcx,%rdx,4), %ecx
andl $0xff000000, %ecx       # imm = 0xFF000000
shrl $0x18, %ecx
orl %ecx, %eax
movl %eax, %eax
movq %rax, -0x128(%rbp)
movq -0x128(%rbp), %rax
movl %eax, %edx
movq -0xc8(%rbp), %rax
movl -0xb0(%rbp), %ecx
movl %edx, (%rax,%rcx,4)
movq -0xc0(%rbp), %rdi
movl -0xb0(%rbp), %eax
shlq $0x2, %rax
addq %rax, %rdi
movq -0xa0(%rbp), %rcx
movl $0x4, %esi
movl $0x1, %edx
callq 0x41ed80
movq -0xc8(%rbp), %rdi
movl -0xb0(%rbp), %eax
shlq $0x2, %rax
addq %rax, %rdi
movq -0xa0(%rbp), %rcx
movl $0x4, %esi
movl $0x1, %edx
callq 0x41ed80
movl -0xb0(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xb0(%rbp)
jmp 0x404049
movq -0xa0(%rbp), %rdi
callq 0x41e4b0
addq $0x130, %rsp            # imm = 0x130
popq %rbp
retq
nopw %cs:(%rax,%rax)