// File: /workspace/github_c_makefile_star2_O3/Zarh[P]regps3iso/main.c
// Function: read_region_info
void read_region_info(char *iso)
{
	FILE *f = fopen(iso, "rb");
	if( f == NULL ) {
		printf("Error : cannot fopen %s", iso);
		return;
	}
	
	char log[512];
	char str[1024]={0};
	sprintf(log, "%s.region.txt", iso);
	
	FILE *l = fopen(log, "wb");
	if( l == NULL ) {
		printf("Error : cannot fopen %s", log);
		fclose(f);
		return;
	}
	
	u32 plain_n;
	fread(&plain_n, 4, 1, f);
	
	plain_n = SWAP_BE(plain_n);
	
	u32 start[plain_n];
	u32 end[plain_n];
	u32 i, j;
	
	sprintf(str, "Plain number = %d\n", plain_n); fputs(str, l);
	fseek(f, 8, SEEK_SET);
	for(i=0; i < plain_n; i++) {
		fread(&start[i], 4, 1, f);
		fread(&end[i], 4, 1, f);
		
		start[i] = SWAP_BE(start[i]);
		end[i] = SWAP_BE(end[i]);
		
		if( i != 0 ) {
			sprintf(str, "0x%08X\n", start[i] - 1); 
			fputs(str, l);
		}
		
		sprintf(str, "Plain 0x%08X - 0x%08X\n", start[i], end[i]); fputs(str, l);
		if( i+1 < plain_n ) {
			sprintf(str, "**Enc 0x%08X - ", end[i] + 1); fputs(str, l);
		}
	}
	fclose(f);
	
	file_info_t *file_info=NULL;
	u32 file_number;
	
	GetFileInfo(iso, &file_info, &file_number);
	
	fputs("\n\n\n", l);
	
	fflush(l);
		
	for(i=0; i < plain_n; i++) {
			sprintf(str, "--- PLAIN               ---  %08X\n",  start[i]); fputs(str, l); fflush(l);
		if(i==0) fputs("\tsector 00000000 | Header\n", l); fflush(l);
				
		for(j=0; j<file_number; j++) {
			if( start[i] <= file_info[j].sector ) {
				if(end[i] <= file_info[j].sector ) break;
									
				sprintf(str, "\tsector %08X | %s\n", file_info[j].sector, file_info[j].path); fputs(str, l); fflush(l);
			}
		}
		if(i+1 == plain_n) {
			u64 footer = file_info[file_number-1].sector*0x800ULL  + file_info[file_number-1].size;
			footer = (footer + 2047)/2048;
			sprintf(str, "\tsector %08X | footer\n", footer); fputs(str, l); fflush(l);		
		}
			sprintf(str, "--- End of PLAIN        ---  %08X\n\n",  end[i]); fputs(str, l); fflush(l);
		if( i+1 < plain_n) {
			sprintf(str, "--- ENCRYPTED           ---  %08X\n",  end[i]+1); fputs(str, l); fflush(l);
			for(j=0; j<file_number; j++) {
				if( end[i]+1 <= file_info[j].sector ) {
					if(start[i+1] <= file_info[j].sector ) break;
										
					sprintf(str, "\tsector %08X | %s\n", file_info[j].sector, file_info[j].path); fputs(str, l); fflush(l);
				}
			}
			sprintf(str, "--- End of ENCRYPTED    ---  %08X\n\n",  start[i+1]-1); fputs(str, l); fflush(l);
		}
	}
	
	
	for(j=0; j < file_number; j++) FREE(file_info[j].path)
	FREE(file_info);
	
	fclose(l);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x668, %rsp            # imm = 0x668
movq %rdi, %r14
leaq 0xc07af(%rip), %rsi     # 0x4c3027
callq 0x41cbe0
testq %rax, %rax
je 0x402d3f
movq %rax, %r15
leaq -0x490(%rbp), %rdi
movl $0x400, %edx            # imm = 0x400
xorl %esi, %esi
callq 0x4010b8
leaq 0xc07f1(%rip), %rsi     # 0x4c3094
leaq -0x690(%rbp), %rbx
movq %rbx, %rdi
movq %r14, %rdx
xorl %eax, %eax
callq 0x40e1e0
leaq 0xc07e4(%rip), %rsi     # 0x4c30a2
movq %rbx, %rdi
callq 0x41cbe0
movq %rax, -0x30(%rbp)
testq %rax, %rax
je 0x402d5e
movq %r14, -0x70(%rbp)
leaq -0x8c(%rbp), %r14
movl $0x1, %eax
movq %rax, -0x50(%rbp)
movl $0x4, %esi
movl $0x1, %edx
movq %r14, %rdi
movq %r15, %rcx
callq 0x41ce10
movq %rsp, %r12
movl (%r14), %ebx
movl %ebx, %r13d
bswapl %r13d
movq %rsp, %rcx
leaq 0xf(,%r13,4), %rax
andq $-0x10, %rax
subq %rax, %rcx
movq %rcx, -0x58(%rbp)
movq %rcx, %rsp
movq %rsp, %rcx
subq %rax, %rcx
movq %rcx, -0x60(%rbp)
movq %rcx, %rsp
leaq 0xc0770(%rip), %rsi     # 0x4c30a5
leaq -0x490(%rbp), %r14
movq %r14, %rdi
movl %r13d, %edx
xorl %eax, %eax
callq 0x40e1e0
movq %r14, %rdi
movq -0x30(%rbp), %rsi
callq 0x41cce0
movl $0x8, %esi
movq %r15, %rdi
xorl %edx, %edx
callq 0x41f640
testl %ebx, %ebx
movq %r12, -0x80(%rbp)
je 0x402d80
movl %ebx, -0x64(%rbp)
movq %r15, -0x78(%rbp)
cmpl $0x2, %r13d
movl $0x1, %eax
movq %r13, -0x48(%rbp)
cmovael %r13d, %eax
shlq $0x2, %rax
movq %rax, -0x88(%rbp)
leaq -0x490(%rbp), %r12
xorl %r15d, %r15d
movq -0x60(%rbp), %rbx
movq -0x58(%rbp), %r13
leaq (%r15,%r13), %rdi
movl $0x4, %esi
movl $0x1, %edx
movq -0x78(%rbp), %r14
movq %r14, %rcx
callq 0x41ce10
leaq (%rbx,%r15), %rdi
movl $0x4, %esi
movl $0x1, %edx
movq %r14, %rcx
callq 0x41ce10
movl (%r13,%r15), %r14d
bswapl %r14d
movl %r14d, (%r13,%r15)
movl (%rbx,%r15), %r13d
bswapl %r13d
movl %r13d, (%rbx,%r15)
testq %r15, %r15
je 0x402a13
leal -0x1(%r14), %edx
movq %r12, %rdi
leaq 0xc06c7(%rip), %rsi     # 0x4c30c7
xorl %eax, %eax
callq 0x40e1e0
movq %r12, %rdi
movq -0x30(%rbp), %rsi
callq 0x41cce0
movq %r12, %rdi
leaq 0xc069b(%rip), %rsi     # 0x4c30b8
movl %r14d, %edx
movl %r13d, %ecx
xorl %eax, %eax
callq 0x40e1e0
movq %r12, %rdi
movq -0x30(%rbp), %rsi
callq 0x41cce0
movq -0x50(%rbp), %r14
cmpq -0x48(%rbp), %r14
jae 0x402a63
incl %r13d
movq %r12, %rdi
leaq 0xc0682(%rip), %rsi     # 0x4c30cf
movl %r13d, %edx
xorl %eax, %eax
callq 0x40e1e0
movq %r12, %rdi
movq -0x30(%rbp), %rsi
callq 0x41cce0
addq $0x4, %r15
incq %r14
movq %r14, -0x50(%rbp)
cmpq %r15, -0x88(%rbp)
jne 0x4029a1
movq -0x78(%rbp), %rdi
callq 0x41c640
leaq -0x40(%rbp), %rsi
movq $0x0, (%rsi)
leaq -0x34(%rbp), %rdx
movq -0x70(%rbp), %rdi
callq 0x40358c
leaq 0xc063c(%rip), %rdi     # 0x4c30df
movq -0x30(%rbp), %rbx
movq %rbx, %rsi
callq 0x41cce0
movq %rbx, %rdi
callq 0x41c830
cmpl $0x0, -0x64(%rbp)
movq -0x48(%rbp), %r12
je 0x402dbb
cmpl $0x2, %r12d
movl $0x1, %eax
cmovael %r12d, %eax
movq %rax, -0x50(%rbp)
xorl %r14d, %r14d
movq -0x58(%rbp), %rax
movl (%rax,%r14,4), %r13d
leaq -0x490(%rbp), %r15
movq %r15, %rdi
leaq 0xc05f1(%rip), %rsi     # 0x4c30e3
movl %r13d, %edx
xorl %eax, %eax
callq 0x40e1e0
movq %r15, %rdi
movq -0x30(%rbp), %rbx
movq %rbx, %rsi
callq 0x41cce0
movq %rbx, %rdi
callq 0x41c830
testq %r14, %r14
jne 0x402b28
leaq 0xc05e7(%rip), %rdi     # 0x4c3106
movq -0x30(%rbp), %rsi
callq 0x41cce0
movq -0x30(%rbp), %rdi
callq 0x41c830
movl -0x34(%rbp), %eax
testl %eax, %eax
je 0x402ba5
movl $0x8, %r15d
xorl %r12d, %r12d
movq -0x40(%rbp), %rcx
movl (%rcx,%r15), %edx
cmpl %edx, %r13d
ja 0x402b8f
movq -0x60(%rbp), %rsi
cmpl %edx, (%rsi,%r14,4)
jbe 0x402b9d
movq -0x8(%rcx,%r15), %rcx
leaq -0x490(%rbp), %rbx
movq %rbx, %rdi
leaq 0xc05b3(%rip), %rsi     # 0x4c3121
xorl %eax, %eax
callq 0x40e1e0
movq %rbx, %rdi
movq -0x30(%rbp), %rbx
movq %rbx, %rsi
callq 0x41cce0
movq %rbx, %rdi
callq 0x41c830
movl -0x34(%rbp), %eax
incq %r12
movl %eax, %ecx
addq $0x18, %r15
cmpq %rcx, %r12
jb 0x402b41
decl %eax
movq -0x48(%rbp), %r12
jmp 0x402baa
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
leaq 0x1(%r14), %r15
cmpq %r12, %r15
jne 0x402c0f
movq -0x40(%rbp), %rcx
leaq (%rax,%rax,2), %rax
movl 0x8(%rcx,%rax,8), %esi
movq 0x10(%rcx,%rax,8), %rdx
movl $0x7ff, %eax            # imm = 0x7FF
addq %rax, %rdx
shrq $0xb, %rdx
addq %rsi, %rdx
movabsq $0x1fffffffffffff, %rax # imm = 0x1FFFFFFFFFFFFF
andq %rax, %rdx
leaq -0x490(%rbp), %rbx
movq %rbx, %rdi
leaq 0xc0543(%rip), %rsi     # 0x4c3134
xorl %eax, %eax
callq 0x40e1e0
movq %rbx, %rdi
movq -0x30(%rbp), %rbx
movq %rbx, %rsi
callq 0x41cce0
movq %rbx, %rdi
callq 0x41c830
movq -0x60(%rbp), %rax
movl (%rax,%r14,4), %r13d
leaq -0x490(%rbp), %rbx
movq %rbx, %rdi
leaq 0xc0523(%rip), %rsi     # 0x4c314b
movl %r13d, %edx
xorl %eax, %eax
callq 0x40e1e0
movq %rbx, %rdi
movq -0x30(%rbp), %rbx
movq %rbx, %rsi
callq 0x41cce0
movq %rbx, %rdi
callq 0x41c830
cmpq %r12, %r15
jae 0x402d30
incl %r13d
leaq -0x490(%rbp), %rbx
movq %rbx, %rdi
leaq 0xc0509(%rip), %rsi     # 0x4c316f
movl %r13d, %edx
xorl %eax, %eax
callq 0x40e1e0
movq %rbx, %rdi
movq -0x30(%rbp), %rbx
movq %rbx, %rsi
callq 0x41cce0
movq %rbx, %rdi
callq 0x41c830
movl -0x34(%rbp), %eax
testl %eax, %eax
je 0x402cf3
movl $0x8, %r14d
xorl %r12d, %r12d
movq -0x40(%rbp), %rcx
movl (%rcx,%r14), %edx
cmpl %edx, %r13d
ja 0x402ce5
movq -0x58(%rbp), %rax
cmpl %edx, (%rax,%r15,4)
jbe 0x402cf3
movq -0x8(%rcx,%r14), %rcx
leaq -0x490(%rbp), %rbx
movq %rbx, %rdi
leaq 0xc045d(%rip), %rsi     # 0x4c3121
xorl %eax, %eax
callq 0x40e1e0
movq %rbx, %rdi
movq -0x30(%rbp), %rbx
movq %rbx, %rsi
callq 0x41cce0
movq %rbx, %rdi
callq 0x41c830
movl -0x34(%rbp), %eax
incq %r12
movl %eax, %ecx
addq $0x18, %r14
cmpq %rcx, %r12
jb 0x402c97
movq -0x58(%rbp), %rax
movl (%rax,%r15,4), %edx
decl %edx
leaq -0x490(%rbp), %rbx
movq %rbx, %rdi
leaq 0xc0484(%rip), %rsi     # 0x4c3192
xorl %eax, %eax
callq 0x40e1e0
movq %rbx, %rdi
movq -0x30(%rbp), %rbx
movq %rbx, %rsi
callq 0x41cce0
movq %rbx, %rdi
callq 0x41c830
movq -0x48(%rbp), %r12
movq %r15, %r14
cmpq -0x50(%rbp), %r15
jne 0x402ad9
jmp 0x402dbb
leaq 0xc0336(%rip), %rdi     # 0x4c307c
movq %r14, %rsi
xorl %eax, %eax
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x40e110
leaq 0xc0317(%rip), %rdi     # 0x4c307c
leaq -0x690(%rbp), %rsi
xorl %eax, %eax
callq 0x40e110
movq %r15, %rdi
callq 0x41c640
jmp 0x402e1d
movq %r15, %rdi
callq 0x41c640
leaq -0x40(%rbp), %rsi
movq $0x0, (%rsi)
leaq -0x34(%rbp), %rdx
movq -0x70(%rbp), %rdi
callq 0x40358c
leaq 0xc0338(%rip), %rdi     # 0x4c30df
movq -0x30(%rbp), %rbx
movq %rbx, %rsi
callq 0x41cce0
movq %rbx, %rdi
callq 0x41c830
movl -0x34(%rbp), %eax
testl %eax, %eax
movq -0x80(%rbp), %r12
je 0x402dfb
xorl %r14d, %r14d
xorl %r15d, %r15d
movq -0x40(%rbp), %rcx
movq (%rcx,%r14), %rdi
testq %rdi, %rdi
je 0x402ded
callq 0x42af80
movq -0x40(%rbp), %rax
movq $0x0, (%rax,%r14)
movl -0x34(%rbp), %eax
incq %r15
movl %eax, %ecx
addq $0x18, %r14
cmpq %rcx, %r15
jb 0x402dcc
movq -0x40(%rbp), %rdi
testq %rdi, %rdi
je 0x402e11
callq 0x42af80
movq $0x0, -0x40(%rbp)
movq -0x30(%rbp), %rdi
callq 0x41c640
movq %r12, %rsp
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq