// File: /workspace/github_c_makefile_star2_O2/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 $0x678, %rsp            # imm = 0x678
movq %rdi, %r14
leaq 0xbf98c(%rip), %rsi     # 0x4c2027
callq 0x41c7c0
movq %rax, -0x50(%rbp)
testq %rax, %rax
je 0x402b6c
leaq -0x4a0(%rbp), %rdi
movl $0x400, %edx            # imm = 0x400
xorl %esi, %esi
callq 0x4010b8
leaq 0xbf9cd(%rip), %rsi     # 0x4c2094
leaq -0x6a0(%rbp), %rbx
movq %rbx, %rdi
movq %r14, %rdx
xorl %eax, %eax
callq 0x40ddc0
leaq 0xbf9c0(%rip), %rsi     # 0x4c20a2
movq %rbx, %rdi
callq 0x41c7c0
testq %rax, %rax
je 0x402b8b
movq %rax, %rbx
movq %r14, -0x78(%rbp)
leaq -0x94(%rbp), %r14
pushq $0x4
popq %rsi
pushq $0x1
popq %rdx
movq %r14, %rdi
movq -0x50(%rbp), %r13
movq %r13, %rcx
callq 0x41c9f0
movq %rsp, -0x80(%rbp)
movl (%r14), %r12d
bswapl %r12d
movq %rsp, %r14
leaq 0xf(,%r12,4), %rax
andq $-0x10, %rax
subq %rax, %r14
movq %r14, %rsp
movq %rsp, %r15
subq %rax, %r15
movq %r15, %rsp
leaq 0xbf960(%rip), %rsi     # 0x4c20a5
movq %rbx, -0x30(%rbp)
leaq -0x4a0(%rbp), %rbx
movq %rbx, %rdi
movl %r12d, %edx
xorl %eax, %eax
callq 0x40ddc0
movq %rbx, %rdi
movq -0x30(%rbp), %rbx
movq %rbx, %rsi
callq 0x41c8c0
pushq $0x8
popq %rsi
movq %r13, %rdi
xorl %edx, %edx
callq 0x41f220
pushq $0x1
popq %rax
movq %rax, -0x40(%rbp)
movq %r15, -0x70(%rbp)
movq %r15, -0x68(%rbp)
movq %r14, -0x58(%rbp)
movq %r14, %rdi
movq %r12, -0x60(%rbp)
subq $0x1, %r12
jb 0x402898
movq %r12, -0x88(%rbp)
movq %rdi, %r12
pushq $0x4
popq %r15
movq %r15, %rsi
pushq $0x1
popq %r13
movq %r13, %rdx
movq -0x50(%rbp), %r14
movq %r14, %rcx
callq 0x41c9f0
movq -0x68(%rbp), %rbx
movq %rbx, %rdi
movq %r15, %rsi
movq %r13, %rdx
movq %r14, %rcx
callq 0x41c9f0
movl (%r12), %r15d
bswapl %r15d
movq %r12, -0x90(%rbp)
movl %r15d, (%r12)
movl (%rbx), %r13d
bswapl %r13d
movl %r13d, (%rbx)
movq -0x30(%rbp), %rbx
cmpq $0x1, -0x40(%rbp)
je 0x402823
leal -0x1(%r15), %edx
leaq -0x4a0(%rbp), %r14
movq %r14, %rdi
leaq 0xbf8b6(%rip), %rsi     # 0x4c20c7
xorl %eax, %eax
callq 0x40ddc0
movq %r14, %rdi
movq %rbx, %rsi
callq 0x41c8c0
leaq -0x4a0(%rbp), %r14
movq %r14, %rdi
leaq 0xbf884(%rip), %rsi     # 0x4c20b8
movl %r15d, %edx
movl %r13d, %ecx
xorl %eax, %eax
callq 0x40ddc0
movq %r14, %rdi
movq %rbx, %rsi
callq 0x41c8c0
movq -0x40(%rbp), %rax
cmpq -0x60(%rbp), %rax
jae 0x402878
incl %r13d
movq %r14, %rdi
leaq 0xbf86c(%rip), %rsi     # 0x4c20cf
movl %r13d, %edx
xorl %eax, %eax
callq 0x40ddc0
movq %r14, %rdi
movq %rbx, %rsi
callq 0x41c8c0
movq -0x90(%rbp), %rdi
addq $0x4, %rdi
addq $0x4, -0x68(%rbp)
incq -0x40(%rbp)
movq -0x88(%rbp), %r12
jmp 0x402793
movq -0x50(%rbp), %rdi
callq 0x41c220
leaq -0x48(%rbp), %rsi
andq $0x0, (%rsi)
leaq -0x34(%rbp), %rdx
movq -0x78(%rbp), %rdi
callq 0x40323a
leaq 0xbf822(%rip), %rdi     # 0x4c20df
xorl %r14d, %r14d
movq %rbx, %rsi
callq 0x41c8c0
movq %rbx, %rdi
callq 0x41c410
movq %r14, %r15
movq -0x60(%rbp), %r12
cmpq %r12, %r15
je 0x402b1d
movq -0x58(%rbp), %rax
movl (%rax,%r15,4), %r14d
leaq -0x4a0(%rbp), %r12
movq %r12, %rdi
leaq 0xbf7ea(%rip), %rsi     # 0x4c20e3
movl %r14d, %edx
xorl %eax, %eax
callq 0x40ddc0
movq %r12, %rdi
movq %rbx, %rsi
callq 0x41c8c0
movq %rbx, %rdi
callq 0x41c410
testq %r15, %r15
jne 0x40292a
leaq 0xbf7e4(%rip), %rdi     # 0x4c2106
movq %rbx, %rsi
callq 0x41c8c0
movq %rbx, %rdi
callq 0x41c410
pushq $0x8
popq %r12
xorl %r13d, %r13d
movl -0x34(%rbp), %eax
cmpq %rax, %r13
jae 0x402995
movq -0x48(%rbp), %rcx
movl (%rcx,%r12), %edx
cmpl %edx, %r14d
ja 0x40298c
movq -0x70(%rbp), %rsi
cmpl %edx, (%rsi,%r15,4)
jbe 0x402995
movq -0x8(%rcx,%r12), %rcx
leaq -0x4a0(%rbp), %rbx
movq %rbx, %rdi
leaq 0xbf7b3(%rip), %rsi     # 0x4c2121
xorl %eax, %eax
callq 0x40ddc0
movq %rbx, %rdi
movq -0x30(%rbp), %rbx
movq %rbx, %rsi
callq 0x41c8c0
movq %rbx, %rdi
callq 0x41c410
incq %r13
addq $0x18, %r12
jmp 0x402939
leaq 0x1(%r15), %r14
movq -0x60(%rbp), %r12
cmpl %r14d, %r12d
jne 0x402a00
movq -0x48(%rbp), %rcx
decl %eax
imulq $0x18, %rax, %rax
movl 0x8(%rcx,%rax), %esi
movq 0x10(%rcx,%rax), %rdx
movl $0x7ff, %eax            # imm = 0x7FF
addq %rax, %rdx
shrq $0xb, %rdx
addq %rsi, %rdx
movabsq $0x1fffffffffffff, %rax # imm = 0x1FFFFFFFFFFFFF
andq %rax, %rdx
leaq -0x4a0(%rbp), %rbx
movq %rbx, %rdi
leaq 0xbf752(%rip), %rsi     # 0x4c2134
xorl %eax, %eax
callq 0x40ddc0
movq %rbx, %rdi
movq -0x30(%rbp), %rbx
movq %rbx, %rsi
callq 0x41c8c0
movq %rbx, %rdi
callq 0x41c410
movq -0x70(%rbp), %rax
movl (%rax,%r15,4), %r13d
leaq -0x4a0(%rbp), %rbx
movq %rbx, %rdi
leaq 0xbf732(%rip), %rsi     # 0x4c214b
movl %r13d, %edx
xorl %eax, %eax
callq 0x40ddc0
movq %rbx, %rdi
movq -0x30(%rbp), %rbx
movq %rbx, %rsi
callq 0x41c8c0
movq %rbx, %rdi
callq 0x41c410
movq %r14, %r15
cmpl %r14d, %r12d
jbe 0x4028d7
incl %r13d
leaq -0x4a0(%rbp), %rbx
movq %rbx, %rdi
leaq 0xbf715(%rip), %rsi     # 0x4c216f
movl %r13d, %edx
xorl %eax, %eax
callq 0x40ddc0
movq %rbx, %rdi
movq -0x30(%rbp), %rbx
movq %rbx, %rsi
callq 0x41c8c0
movq %rbx, %rdi
callq 0x41c410
movl %r14d, %eax
movq %rax, -0x40(%rbp)
pushq $0x8
popq %r15
xorl %r12d, %r12d
movl -0x34(%rbp), %eax
cmpq %rax, %r12
jae 0x402aea
movq -0x48(%rbp), %rcx
movl (%rcx,%r15), %edx
cmpl %edx, %r13d
ja 0x402ae1
movq -0x58(%rbp), %rax
movq -0x40(%rbp), %rsi
movl (%rax,%rsi,4), %eax
cmpl %edx, %eax
jbe 0x402af5
movq -0x8(%rcx,%r15), %rcx
leaq -0x4a0(%rbp), %rbx
movq %rbx, %rdi
leaq 0xbf65e(%rip), %rsi     # 0x4c2121
xorl %eax, %eax
callq 0x40ddc0
movq %rbx, %rdi
movq -0x30(%rbp), %rbx
movq %rbx, %rsi
callq 0x41c8c0
movq %rbx, %rdi
callq 0x41c410
incq %r12
addq $0x18, %r15
jmp 0x402a89
movq -0x58(%rbp), %rax
movq -0x40(%rbp), %rcx
movl (%rax,%rcx,4), %eax
decl %eax
leaq -0x4a0(%rbp), %rbx
movq %rbx, %rdi
leaq 0xbf68a(%rip), %rsi     # 0x4c2192
movl %eax, %edx
xorl %eax, %eax
callq 0x40ddc0
movq %rbx, %rdi
movq -0x30(%rbp), %rbx
jmp 0x4028c0
xorl %r14d, %r14d
xorl %r15d, %r15d
movl -0x34(%rbp), %eax
movq -0x48(%rbp), %rdi
cmpq %rax, %r15
jae 0x402b4f
movq (%rdi,%r14), %rdi
testq %rdi, %rdi
je 0x402b46
callq 0x42ab60
movq -0x48(%rbp), %rax
andq $0x0, (%rax,%r14)
incq %r15
addq $0x18, %r14
jmp 0x402b23
testq %rdi, %rdi
je 0x402b5e
callq 0x42ab60
andq $0x0, -0x48(%rbp)
movq %rbx, %rdi
callq 0x41c220
movq -0x80(%rbp), %rsp
jmp 0x402ba9
leaq 0xbf509(%rip), %rdi     # 0x4c207c
movq %r14, %rsi
xorl %eax, %eax
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x40dcf0
leaq 0xbf4ea(%rip), %rdi     # 0x4c207c
leaq -0x6a0(%rbp), %rsi
xorl %eax, %eax
callq 0x40dcf0
movq -0x50(%rbp), %rdi
callq 0x41c220
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq