// File: /workspace/github_c_makefile_star2_O1/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 0xc07d7(%rip), %rsi     # 0x4c3027
callq 0x41cb70
movq %rax, -0x50(%rbp)
testq %rax, %rax
je 0x402d5f
leaq -0x4a0(%rbp), %rdi
movl $0x400, %edx            # imm = 0x400
xorl %esi, %esi
callq 0x4010b8
leaq 0xc0818(%rip), %rsi     # 0x4c3094
leaq -0x6a0(%rbp), %rbx
movq %rbx, %rdi
movq %r14, %rdx
xorl %eax, %eax
callq 0x40e170
leaq 0xc080b(%rip), %rsi     # 0x4c30a2
movq %rbx, %rdi
callq 0x41cb70
testq %rax, %rax
je 0x402d7e
movq %rax, %rbx
movq %r14, -0x80(%rbp)
leaq -0x94(%rbp), %r14
movl $0x1, %eax
movq %rax, -0x78(%rbp)
movl $0x4, %esi
movl $0x1, %edx
movq %r14, %rdi
movq -0x50(%rbp), %r12
movq %r12, %rcx
callq 0x41cda0
movq %rsp, -0x88(%rbp)
movl (%r14), %r13d
movl %r13d, %r15d
bswapl %r15d
movl %r15d, (%r14)
movq %rsp, %rcx
leaq 0xf(,%r15,4), %rax
andq $-0x10, %rax
subq %rax, %rcx
movq %rcx, -0x58(%rbp)
movq %rcx, %rsp
movq %rsp, %rcx
subq %rax, %rcx
movq %rcx, -0x68(%rbp)
movq %rcx, %rsp
leaq 0xc078d(%rip), %rsi     # 0x4c30a5
leaq -0x4a0(%rbp), %r14
movq %r14, %rdi
movl %r15d, %edx
xorl %eax, %eax
callq 0x40e170
movq %r14, %rdi
movq %rbx, %rsi
callq 0x41cc70
movl $0x8, %esi
movq %r12, %rdi
xorl %edx, %edx
callq 0x41f5d0
movq %r15, -0x48(%rbp)
movl %r15d, %eax
movq %rax, -0x60(%rbp)
movl %r13d, -0x6c(%rbp)
testl %r13d, %r13d
je 0x402a5d
movq -0x48(%rbp), %rax
cmpl $0x1, %eax
adcl $0x0, %eax
shlq $0x2, %rax
movq %rax, -0x90(%rbp)
xorl %r15d, %r15d
movq -0x58(%rbp), %r14
leaq (%r14,%r15), %rdi
movl $0x4, %esi
movl $0x1, %edx
movq -0x50(%rbp), %r13
movq %r13, %rcx
callq 0x41cda0
movq -0x68(%rbp), %r12
leaq (%r12,%r15), %rdi
movl $0x4, %esi
movl $0x1, %edx
movq %r13, %rcx
callq 0x41cda0
movl (%r14,%r15), %r13d
bswapl %r13d
movl %r13d, (%r14,%r15)
movl (%r12,%r15), %r14d
bswapl %r14d
movl %r14d, (%r12,%r15)
testq %r15, %r15
leaq -0x4a0(%rbp), %r12
je 0x4029f0
leal -0x1(%r13), %edx
movq %r12, %rdi
leaq 0xc06e9(%rip), %rsi     # 0x4c30c7
xorl %eax, %eax
callq 0x40e170
movq %r12, %rdi
movq %rbx, %rsi
callq 0x41cc70
movq %r12, %rdi
leaq 0xc06be(%rip), %rsi     # 0x4c30b8
movl %r13d, %edx
movl %r14d, %ecx
xorl %eax, %eax
callq 0x40e170
movq %r12, %rdi
movq %rbx, %rsi
callq 0x41cc70
movq -0x78(%rbp), %r12
cmpq -0x60(%rbp), %r12
jae 0x402a45
incl %r14d
leaq -0x4a0(%rbp), %r13
movq %r13, %rdi
leaq 0xc069f(%rip), %rsi     # 0x4c30cf
movl %r14d, %edx
xorl %eax, %eax
callq 0x40e170
movq %r13, %rdi
movq %rbx, %rsi
callq 0x41cc70
addq $0x4, %r15
incq %r12
movq %r12, -0x78(%rbp)
cmpq %r15, -0x90(%rbp)
jne 0x402976
movq -0x50(%rbp), %rdi
callq 0x41c5d0
leaq -0x38(%rbp), %rsi
movq $0x0, (%rsi)
leaq -0x2c(%rbp), %rdx
movq -0x80(%rbp), %rdi
callq 0x40351b
leaq 0xc065a(%rip), %rdi     # 0x4c30df
movq %rbx, %rsi
callq 0x41cc70
movq %rbx, %rdi
callq 0x41c7c0
cmpl $0x0, -0x6c(%rbp)
je 0x402cff
movq -0x48(%rbp), %rax
cmpl $0x1, %eax
adcl $0x0, %eax
movq %rax, -0x48(%rbp)
xorl %r14d, %r14d
movq %rbx, -0x40(%rbp)
movq -0x58(%rbp), %rax
movl (%rax,%r14,4), %r13d
leaq -0x4a0(%rbp), %r15
movq %r15, %rdi
leaq 0xc0616(%rip), %rsi     # 0x4c30e3
movl %r13d, %edx
xorl %eax, %eax
callq 0x40e170
movq %r15, %rdi
movq %rbx, %rsi
callq 0x41cc70
movq %rbx, %rdi
callq 0x41c7c0
testq %r14, %r14
jne 0x402afe
leaq 0xc0610(%rip), %rdi     # 0x4c3106
movq %rbx, %rsi
callq 0x41cc70
movq %rbx, %rdi
callq 0x41c7c0
movl -0x2c(%rbp), %eax
testl %eax, %eax
je 0x402b70
movl $0x8, %r15d
xorl %r12d, %r12d
movq -0x38(%rbp), %rcx
movl (%rcx,%r15), %edx
cmpl %edx, %r13d
ja 0x402b61
movq -0x68(%rbp), %rsi
cmpl %edx, (%rsi,%r14,4)
jbe 0x402b70
movq -0x8(%rcx,%r15), %rcx
leaq -0x4a0(%rbp), %rbx
movq %rbx, %rdi
leaq 0xc05de(%rip), %rsi     # 0x4c3121
xorl %eax, %eax
callq 0x40e170
movq %rbx, %rdi
movq -0x40(%rbp), %rbx
movq %rbx, %rsi
callq 0x41cc70
movq %rbx, %rdi
callq 0x41c7c0
incq %r12
movl -0x2c(%rbp), %eax
addq $0x18, %r15
cmpq %rax, %r12
jb 0x402b16
leaq 0x1(%r14), %r15
cmpq -0x60(%rbp), %r15
jne 0x402bd8
movq -0x38(%rbp), %rcx
decl %eax
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 -0x4a0(%rbp), %rbx
movq %rbx, %rdi
leaq 0xc057a(%rip), %rsi     # 0x4c3134
xorl %eax, %eax
callq 0x40e170
movq %rbx, %rdi
movq -0x40(%rbp), %rbx
movq %rbx, %rsi
callq 0x41cc70
movq %rbx, %rdi
callq 0x41c7c0
movq -0x68(%rbp), %rax
movl (%rax,%r14,4), %r13d
leaq -0x4a0(%rbp), %rbx
movq %rbx, %rdi
leaq 0xc055a(%rip), %rsi     # 0x4c314b
movl %r13d, %edx
xorl %eax, %eax
callq 0x40e170
movq %rbx, %rdi
movq -0x40(%rbp), %rbx
movq %rbx, %rsi
callq 0x41cc70
movq %rbx, %rdi
callq 0x41c7c0
cmpq -0x60(%rbp), %r15
jae 0x402cf2
incl %r13d
movq %rbx, %r14
leaq -0x4a0(%rbp), %rbx
movq %rbx, %rdi
leaq 0xc053c(%rip), %rsi     # 0x4c316f
movl %r13d, %edx
xorl %eax, %eax
callq 0x40e170
movq %rbx, %rdi
movq %r14, %rsi
callq 0x41cc70
movq %r14, %rdi
callq 0x41c7c0
cmpl $0x0, -0x2c(%rbp)
je 0x402cb9
movl $0x8, %r14d
xorl %r12d, %r12d
movq -0x38(%rbp), %rax
movl (%rax,%r14), %edx
cmpl %edx, %r13d
ja 0x402caa
movq -0x58(%rbp), %rcx
cmpl %edx, (%rcx,%r15,4)
jbe 0x402cb9
movq -0x8(%rax,%r14), %rcx
leaq -0x4a0(%rbp), %rbx
movq %rbx, %rdi
leaq 0xc0495(%rip), %rsi     # 0x4c3121
xorl %eax, %eax
callq 0x40e170
movq %rbx, %rdi
movq -0x40(%rbp), %rbx
movq %rbx, %rsi
callq 0x41cc70
movq %rbx, %rdi
callq 0x41c7c0
incq %r12
movl -0x2c(%rbp), %eax
addq $0x18, %r14
cmpq %rax, %r12
jb 0x402c5f
movq -0x58(%rbp), %rax
movl (%rax,%r15,4), %edx
decl %edx
leaq -0x4a0(%rbp), %rbx
movq %rbx, %rdi
leaq 0xc04be(%rip), %rsi     # 0x4c3192
xorl %eax, %eax
callq 0x40e170
movq %rbx, %rdi
movq -0x40(%rbp), %rbx
movq %rbx, %rsi
callq 0x41cc70
movq %rbx, %rdi
callq 0x41c7c0
movq %r15, %r14
cmpq -0x48(%rbp), %r15
jne 0x402ab4
cmpl $0x0, -0x2c(%rbp)
je 0x402d38
xorl %r14d, %r14d
xorl %r15d, %r15d
movq -0x38(%rbp), %rax
movq (%rax,%r14), %rdi
testq %rdi, %rdi
je 0x402d29
callq 0x42af10
movq -0x38(%rbp), %rax
movq $0x0, (%rax,%r14)
incq %r15
movl -0x2c(%rbp), %eax
addq $0x18, %r14
cmpq %rax, %r15
jb 0x402d0b
movq -0x38(%rbp), %rdi
testq %rdi, %rdi
je 0x402d4e
callq 0x42af10
movq $0x0, -0x38(%rbp)
movq %rbx, %rdi
callq 0x41c5d0
movq -0x88(%rbp), %rsp
jmp 0x402d9c
leaq 0xc0316(%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 0x40e0a0
leaq 0xc02f7(%rip), %rdi     # 0x4c307c
leaq -0x6a0(%rbp), %rsi
xorl %eax, %eax
callq 0x40e0a0
movq -0x50(%rbp), %rdi
callq 0x41c5d0
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq