// File: /workspace/github_c_makefile_star2_O1/Zarh[P]regps3iso/main.c
// Function: get_enc_files
void get_enc_files(char *iso)
{
	FILE *f = fopen(iso, "rb");
	if( f == NULL ) {
		printf("Error : cannot fopen %s\n", iso);
		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, k;
	
	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]);
	}
	fclose(f);
	
	file_info_t *file_info=NULL;
	u32 file_number;
	
	GetFileInfo(iso, &file_info, &file_number);
	
	u32 files_n = 0;
	char **files=NULL;
	u32 *amount=NULL;
	
	char line[512];
	
	// read db
	FILE *l = fopen("enc_files.txt", "r+");
	if( l != NULL ) {
	
		while(fgets(line, 512, l) != NULL) {
			if(line[0]=='\r' || line[0]=='\n') continue;
			
			if(strstr(line, "\r") != NULL) strtok(line, "\r");
			if(strstr(line, "\n") != NULL) strtok(line, "\n");
			
			files = (char **) realloc(files, (files_n+1) * sizeof(char *));
			amount = (u32 *) realloc(amount, (files_n+1) * sizeof(u32));
			
			sscanf(line, "%08X", &amount[files_n]);
			files[files_n] = strcpy_malloc(line + 11);
			
			files_n++;
			
			memset(line, 0, 512);
		}
		
		
		fclose(l);
	}
	
	
	for(i=0; i < plain_n; i++) {	
		for(j=0; j<file_number; j++) {
			if( i+1 < plain_n) {
				if( end[i]+1 <= file_info[j].sector ) {
					if(start[i+1] <= file_info[j].sector ) break;
					u8 found = NO;
					u32 len = strlen(file_info[j].path);
					for(k=0; k<files_n; k++) {
						
						if( 10 < len ) {
							if( memcmp(file_info[j].path + 10, "TROPDIR", 7) == 0 ) {
								if( memcmp(file_info[j].path, files[k], 18) == 0 ){
									found = YES;
									amount[k]++;;
									break;
								}
							} else
							if( memcmp(file_info[j].path + 10, "USRDIR", 6) == 0 ) {
								if( memcmp(file_info[j].path, files[k], 17) == 0 ){
									found = YES;
									amount[k]++;;
									break;
								}
							} else 
							if( strcmp(files[k], file_info[j].path) == 0 ) {
								found = YES;
								amount[k]++;;
								break;
							}
						} else
						if( strcmp(files[k], file_info[j].path) == 0 ) {
							found = YES;
							amount[k]++;;
							break;
						}
					}
					if( found == NO ) {
						files = (char **) realloc(files, (files_n+1) * sizeof(char *));
						amount = (u32 *) realloc(amount, (files_n+1) * sizeof(u32));
						if( 10 < len ) {
							if( memcmp(file_info[j].path + 10, "TROPDIR", 7) == 0 ) {
								char temp[512]={0};
								memset(temp, 0, 512);
								strncpy(temp, file_info[j].path, 18);
								files[files_n] = strcpy_malloc(temp);
								amount[files_n]=1;
							} else
							if( memcmp(file_info[j].path + 10, "USRDIR", 6) == 0 ) {
								char temp[512]={0};
								memset(temp, 0, 512);
								strncpy(temp, file_info[j].path, 17);
								files[files_n] = strcpy_malloc(temp);
								amount[files_n]=1;
							} else {
								files[files_n] = strcpy_malloc(file_info[j].path);
								amount[files_n]=1;
							}
						} else {
							files[files_n] = strcpy_malloc(file_info[j].path);
							amount[files_n]=1;
						}
						files_n++;
					}
				}
			}
		}
	}
	
	// write db
	char str[512];
	l = fopen("enc_files.txt", "w");
	
	for(i=0; i<files_n; i++) {
	
		u32 max_amount = 0;	
		// search_max
		for( j = 0; j<files_n; j++) {
			if(amount[max_amount] < amount[j]) max_amount=j;
		}
		sprintf(str, "%08X | %s\n", amount[max_amount], files[max_amount]);
		fputs(str, l);
		
		amount[max_amount] = 0;
	}
	fclose(l);
	
	for(j=0; j < files_n; j++) {
		FREE(files[j]);
	}
	FREE(amount);
	FREE(files);
	
	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 $0x468, %rsp            # imm = 0x468
movq %rdi, %rbx
leaq 0xc0e9f(%rip), %rsi     # 0x4c3027
callq 0x41cb70
testq %rax, %rax
je 0x4023c7
movq %rax, %r14
leaq -0x64(%rbp), %r15
movl $0x4, %esi
movl $0x1, %edx
movq %r15, %rdi
movq %rax, %rcx
callq 0x41cda0
movq %rsp, -0x80(%rbp)
movl (%r15), %r12d
movl %r12d, %r13d
bswapl %r13d
movl %r13d, (%r15)
movq %rsp, %rcx
leaq 0xf(,%r13,4), %rax
andq $-0x10, %rax
subq %rax, %rcx
movq %rcx, -0x78(%rbp)
movq %rcx, %rsp
movq %rsp, %rcx
subq %rax, %rcx
movq %rcx, -0x40(%rbp)
movq %rcx, %rsp
movq $0x0, -0x30(%rbp)
movl $0x8, %esi
movq %r14, %rdi
xorl %edx, %edx
callq 0x41f5d0
testl %r12d, %r12d
je 0x40225d
cmpl $0x1, %r13d
adcl $0x0, %r13d
movq -0x40(%rbp), %r15
movq -0x78(%rbp), %r12
movl $0x4, %esi
movl $0x1, %edx
movq %r12, %rdi
movq %r14, %rcx
callq 0x41cda0
movl $0x4, %esi
movl $0x1, %edx
movq %r15, %rdi
movq %r14, %rcx
callq 0x41cda0
movl (%r12), %eax
bswapl %eax
movl %eax, (%r12)
movl (%r15), %eax
bswapl %eax
movl %eax, (%r15)
addq $0x4, %r12
addq $0x4, %r15
decq %r13
jne 0x402214
movq %r14, %rdi
callq 0x41c5d0
leaq -0x70(%rbp), %rsi
movq $0x0, (%rsi)
leaq -0x4c(%rbp), %rdx
movq %rbx, %rdi
callq 0x40351b
leaq 0xc0ddc(%rip), %rdi     # 0x4c305f
leaq 0xc0dc9(%rip), %rsi     # 0x4c3053
callq 0x41cb70
testq %rax, %rax
je 0x4023ec
movq %rax, %r15
leaq -0x290(%rbp), %rdi
movl $0x200, %esi            # imm = 0x200
movq %rax, %rdx
callq 0x41c8c0
testq %rax, %rax
je 0x4023f8
leaq -0x290(%rbp), %r13
leaq 0xc1804(%rip), %r12     # 0x4c3aca
movq $0x0, -0x38(%rbp)
xorl %r14d, %r14d
xorl %ebx, %ebx
movzbl -0x290(%rbp), %eax
cmpl $0xd, %eax
je 0x4022e4
cmpl $0xa, %eax
jne 0x4022fe
movq %r13, %rdi
movl $0x200, %esi            # imm = 0x200
movq %r15, %rdx
callq 0x41c8c0
testq %rax, %rax
jne 0x4022d3
jmp 0x4023e6
movq %r13, %rdi
movl $0xd, %esi
callq 0x401100
testq %rax, %rax
je 0x40231f
movq %r13, %rdi
leaq 0xc0d3c(%rip), %rsi     # 0x4c3056
callq 0x42dd80
movq %r13, %rdi
movl $0xa, %esi
callq 0x401100
testq %rax, %rax
je 0x40233c
movq %r13, %rdi
movq %r12, %rsi
callq 0x42dd80
leal 0x1(%rbx), %r12d
leaq (,%r12,8), %rsi
movq %r14, %rdi
callq 0x42b260
movq %rax, %r14
leaq (,%r12,4), %rsi
movq -0x38(%rbp), %rdi
callq 0x42b260
movl %ebx, %ebx
movq %rax, -0x38(%rbp)
leaq (%rax,%rbx,4), %rdx
movq %r13, %rdi
leaq 0xc0ce0(%rip), %rsi     # 0x4c3058
xorl %eax, %eax
callq 0x40e300
leaq -0x285(%rbp), %rdi
callq 0x4034b8
movq %rax, (%r14,%rbx,8)
movl $0x200, %edx            # imm = 0x200
movq %r13, %rdi
xorl %esi, %esi
callq 0x4010b8
movq %r13, %rdi
movl $0x200, %esi            # imm = 0x200
movq %r15, %rdx
callq 0x41c8c0
movq %r12, -0x30(%rbp)
movl %r12d, %ebx
leaq 0xc170e(%rip), %r12     # 0x4c3aca
testq %rax, %rax
jne 0x4022d3
jmp 0x40240b
leaq 0xc0c5c(%rip), %rdi     # 0x4c302a
movq %rbx, %rsi
xorl %eax, %eax
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x40e0a0
movq %rbx, -0x30(%rbp)
jmp 0x40240b
xorl %esi, %esi
movq $0x0, -0x38(%rbp)
jmp 0x402416
movq $0x0, -0x30(%rbp)
xorl %r14d, %r14d
movq $0x0, -0x38(%rbp)
movq %r15, %rdi
callq 0x41c5d0
movq %r14, %rsi
movq -0x40(%rbp), %rdi
cmpl $0x0, -0x64(%rbp)
movq -0x30(%rbp), %r14
je 0x4026de
xorl %r8d, %r8d
movq %r8, %r9
incq %r8
cmpl $0x0, -0x4c(%rbp)
je 0x4026d2
xorl %r15d, %r15d
movq %r8, -0x60(%rbp)
movq %r9, -0x58(%rbp)
movl -0x64(%rbp), %eax
cmpq %rax, %r8
jae 0x4026c3
movl (%rdi,%r9,4), %edx
incl %edx
movq -0x70(%rbp), %rax
leaq (%r15,%r15,2), %r10
movl 0x8(%rax,%r10,8), %ecx
cmpl %ecx, %edx
ja 0x4026c3
movq -0x78(%rbp), %rdx
cmpl %ecx, (%rdx,%r8,4)
jbe 0x4026d2
movq %r10, -0x88(%rbp)
leaq (%rax,%r10,8), %rax
movq (%rax), %r13
movq %r13, %rdi
movq %rsi, -0x48(%rbp)
callq 0x4010f8
movq -0x48(%rbp), %rsi
movq %rax, %r12
movq %r14, -0x30(%rbp)
testl %r14d, %r14d
je 0x40256d
movl -0x30(%rbp), %ebx
xorl %r14d, %r14d
cmpl $0xb, %r12d
jb 0x40250e
movl 0xa(%r13), %eax
movl $0x504f5254, %ecx       # imm = 0x504F5254
xorl %ecx, %eax
movl 0xd(%r13), %ecx
movl $0x52494450, %edx       # imm = 0x52494450
xorl %edx, %ecx
orl %eax, %ecx
je 0x402527
movl 0xa(%r13), %eax
movl $0x44525355, %ecx       # imm = 0x44525355
xorl %ecx, %eax
movzwl 0xe(%r13), %ecx
xorl $0x5249, %ecx           # imm = 0x5249
orl %eax, %ecx
movq (%rsi,%r14,8), %rdi
jne 0x402512
movdqu (%r13), %xmm0
movdqu (%rdi), %xmm1
pcmpeqb %xmm0, %xmm1
movzbl 0x10(%r13), %eax
movzbl 0x10(%rdi), %ecx
movd %eax, %xmm0
movd %ecx, %xmm2
jmp 0x40254a
movq (%rsi,%r14,8), %rdi
movq %r13, %rsi
callq 0x4010c8
movq -0x48(%rbp), %rsi
testl %eax, %eax
jne 0x402561
jmp 0x40265e
movq (%rsi,%r14,8), %rax
movdqu (%r13), %xmm0
movdqu (%rax), %xmm1
pcmpeqb %xmm0, %xmm1
movzwl 0x10(%r13), %ecx
movzwl 0x10(%rax), %eax
movd %ecx, %xmm0
movd %eax, %xmm2
pcmpeqb %xmm0, %xmm2
pand %xmm1, %xmm2
pmovmskb %xmm2, %eax
cmpl $0xffff, %eax           # imm = 0xFFFF
je 0x40265e
incq %r14
cmpq %r14, %rbx
jne 0x4024af
movq -0x30(%rbp), %rax
leal 0x1(%rax), %r14d
movq %rsi, %rdi
leaq (,%r14,8), %rsi
callq 0x42b260
movq %rax, %rbx
leaq (,%r14,4), %rsi
movq -0x38(%rbp), %rdi
callq 0x42b260
movq %rax, %r13
movq -0x70(%rbp), %rax
movq -0x88(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
cmpl $0xb, %r12d
movq %r13, -0x38(%rbp)
jb 0x402644
movq %r14, -0x48(%rbp)
movq %rbx, %r14
leaq (%rax,%rcx,8), %rbx
movl 0xa(%rdi), %eax
movl $0x504f5254, %ecx       # imm = 0x504F5254
xorl %ecx, %eax
movl 0xd(%rdi), %ecx
movl $0x52494450, %edx       # imm = 0x52494450
xorl %edx, %ecx
orl %eax, %ecx
je 0x402670
movl 0xa(%rdi), %eax
movl $0x44525355, %ecx       # imm = 0x44525355
xorl %ecx, %eax
movzwl 0xe(%rdi), %ecx
xorl $0x5249, %ecx           # imm = 0x5249
orl %eax, %ecx
movq -0x48(%rbp), %r13
jne 0x402625
movl $0x200, %edx            # imm = 0x200
leaq -0x490(%rbp), %r12
movq %r12, %rdi
xorl %esi, %esi
callq 0x4010b8
movq (%rbx), %rsi
movl $0x11, %edx
movq %r12, %rdi
callq 0x401090
movq %r12, %rdi
callq 0x4034b8
movl -0x30(%rbp), %ecx
movq %r14, %rsi
movq %rax, (%r14,%rcx,8)
movq -0x38(%rbp), %rax
movl $0x1, (%rax,%rcx,4)
movl %r13d, %r14d
jmp 0x4026b7
callq 0x4034b8
movl -0x30(%rbp), %ecx
movq %rax, (%rbx,%rcx,8)
movl $0x1, (%r13,%rcx,4)
movq %rbx, %rsi
jmp 0x4026b7
movq -0x38(%rbp), %rax
incl (%rax,%r14,4)
movq -0x40(%rbp), %rdi
movq -0x30(%rbp), %r14
jmp 0x4026bb
movl $0x200, %edx            # imm = 0x200
leaq -0x490(%rbp), %r12
movq %r12, %rdi
xorl %esi, %esi
callq 0x4010b8
movq (%rbx), %rsi
movl $0x12, %edx
movq %r12, %rdi
callq 0x401090
movq %r12, %rdi
callq 0x4034b8
movl -0x30(%rbp), %ecx
movq %r14, %rsi
movq %rax, (%r14,%rcx,8)
movq -0x38(%rbp), %rax
movl $0x1, (%rax,%rcx,4)
movq -0x48(%rbp), %r14
movq -0x40(%rbp), %rdi
movq -0x60(%rbp), %r8
movq -0x58(%rbp), %r9
incq %r15
movl -0x4c(%rbp), %eax
cmpq %rax, %r15
jb 0x402446
movl -0x64(%rbp), %eax
cmpq %rax, %r8
jb 0x40242b
movq %rsi, %r12
leaq 0xc0977(%rip), %rdi     # 0x4c305f
leaq 0xc096e(%rip), %rsi     # 0x4c305d
callq 0x41cb70
movq %rax, -0x48(%rbp)
movl %r14d, %ebx
movq %r14, -0x30(%rbp)
testl %r14d, %r14d
movq -0x38(%rbp), %r15
je 0x402775
leaq -0x490(%rbp), %r13
xorl %r14d, %r14d
xorl %eax, %eax
xorl %ecx, %ecx
movl %ecx, %edx
movl (%r15,%rdx,4), %esi
movl %eax, %edx
cmpl (%r15,%rax,4), %esi
jb 0x402726
movl %ecx, %edx
incq %rax
movl %edx, %ecx
cmpq %rax, %rbx
jne 0x402716
movl %edx, %r15d
movq -0x38(%rbp), %rax
movl (%rax,%r15,4), %edx
movq (%r12,%r15,8), %rcx
movq %r13, %rdi
leaq 0xc09e0(%rip), %rsi     # 0x4c3129
xorl %eax, %eax
callq 0x40e170
movq %r13, %rdi
movq -0x48(%rbp), %rsi
callq 0x41cc70
movq -0x38(%rbp), %rax
movl $0x0, (%rax,%r15,4)
movq -0x38(%rbp), %r15
incl %r14d
cmpl -0x30(%rbp), %r14d
jne 0x402712
movq -0x48(%rbp), %rdi
callq 0x41c5d0
cmpl $0x0, -0x30(%rbp)
movq %r12, %rax
je 0x4027ab
xorl %r14d, %r14d
movq (%rax,%r14,8), %rdi
testq %rdi, %rdi
je 0x4027a3
callq 0x42af10
movq %r12, %rax
movq $0x0, (%r12,%r14,8)
incq %r14
cmpq %r14, %rbx
jne 0x40278a
testq %r15, %r15
je 0x4027bb
movq %r15, %rdi
callq 0x42af10
movq %r12, %rax
testq %rax, %rax
je 0x4027c8
movq %rax, %rdi
callq 0x42af10
cmpl $0x0, -0x4c(%rbp)
je 0x402800
xorl %ebx, %ebx
xorl %r14d, %r14d
movq -0x70(%rbp), %rax
movq (%rax,%rbx), %rdi
testq %rdi, %rdi
je 0x4027f1
callq 0x42af10
movq -0x70(%rbp), %rax
movq $0x0, (%rax,%rbx)
incq %r14
movl -0x4c(%rbp), %eax
addq $0x18, %rbx
cmpq %rax, %r14
jb 0x4027d3
movq -0x70(%rbp), %rdi
testq %rdi, %rdi
je 0x402816
callq 0x42af10
movq $0x0, -0x70(%rbp)
movq -0x48(%rbp), %rdi
callq 0x41c5d0
movq -0x80(%rbp), %rsp
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq