// File: /workspace/github_c_makefile_star2_O3/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 0xc0e92(%rip), %rsi     # 0x4c3027
callq 0x41cbe0
testq %rax, %rax
je 0x4023e4
movq %rax, %r14
movq %rbx, -0x30(%rbp)
leaq -0x6c(%rbp), %r15
movl $0x4, %esi
movl $0x1, %edx
movq %r15, %rdi
movq %rax, %rcx
callq 0x41ce10
movq %rsp, -0x80(%rbp)
movl (%r15), %ebx
movl %ebx, %r13d
bswapl %r13d
movl %r13d, (%r15)
movq %rsp, %rcx
leaq 0xf(,%r13,4), %rax
andq $-0x10, %rax
subq %rax, %rcx
movq %rcx, -0x88(%rbp)
movq %rcx, %rsp
movq %rsp, %rcx
subq %rax, %rcx
movq %rcx, -0x48(%rbp)
movq %rcx, %rsp
movq $0x0, -0x38(%rbp)
movl $0x8, %esi
movq %r14, %rdi
xorl %edx, %edx
callq 0x41f640
movl %ebx, -0x78(%rbp)
testl %ebx, %ebx
je 0x402278
cmpl $0x1, %r13d
movl %r13d, %ebx
adcl $0x0, %ebx
movq -0x48(%rbp), %r15
movq -0x88(%rbp), %r12
movl $0x4, %esi
movl $0x1, %edx
movq %r12, %rdi
movq %r14, %rcx
callq 0x41ce10
movl $0x4, %esi
movl $0x1, %edx
movq %r15, %rdi
movq %r14, %rcx
callq 0x41ce10
movl (%r12), %eax
bswapl %eax
movl %eax, (%r12)
movl (%r15), %eax
bswapl %eax
movl %eax, (%r15)
addq $0x4, %r12
addq $0x4, %r15
decq %rbx
jne 0x40222f
movq %r14, %rdi
callq 0x41c640
leaq -0x68(%rbp), %rsi
movq $0x0, (%rsi)
leaq -0x4c(%rbp), %rdx
movq -0x30(%rbp), %rdi
callq 0x40358c
leaq 0xc0dc0(%rip), %rdi     # 0x4c305f
leaq 0xc0dad(%rip), %rsi     # 0x4c3053
callq 0x41cbe0
testq %rax, %rax
je 0x402409
movq %rax, %r15
leaq -0x290(%rbp), %rdi
movl $0x200, %esi            # imm = 0x200
movq %rax, %rdx
callq 0x41c930
testq %rax, %rax
je 0x402416
leaq -0x290(%rbp), %r12
movq $0x0, -0x40(%rbp)
movq $0x0, -0x30(%rbp)
xorl %ebx, %ebx
movzbl -0x290(%rbp), %eax
cmpl $0xd, %eax
je 0x4022fe
cmpl $0xa, %eax
jne 0x402318
movq %r12, %rdi
movl $0x200, %esi            # imm = 0x200
movq %r15, %rdx
callq 0x41c930
testq %rax, %rax
jne 0x4022ed
jmp 0x402403
movq %r12, %rdi
movl $0xd, %esi
callq 0x401100
testq %rax, %rax
je 0x402339
movq %r12, %rdi
leaq 0xc0d22(%rip), %rsi     # 0x4c3056
callq 0x42ddf0
movq %r12, %rdi
movl $0xa, %esi
callq 0x401100
testq %rax, %rax
je 0x40235a
movq %r12, %rdi
leaq 0xc1775(%rip), %rsi     # 0x4c3aca
callq 0x42ddf0
leal 0x1(%rbx), %r14d
leaq (,%r14,8), %rsi
movq -0x30(%rbp), %rdi
callq 0x42b2d0
movq %rax, -0x30(%rbp)
leaq (,%r14,4), %rsi
movq -0x40(%rbp), %rdi
callq 0x42b2d0
movl %ebx, %ebx
movq %rax, -0x40(%rbp)
leaq (%rax,%rbx,4), %rdx
movq %r12, %rdi
leaq 0xc0cc0(%rip), %rsi     # 0x4c3058
xorl %eax, %eax
callq 0x40e370
leaq -0x285(%rbp), %rdi
callq 0x403544
movq -0x30(%rbp), %rcx
movq %rax, (%rcx,%rbx,8)
movl $0x200, %edx            # imm = 0x200
movq %r12, %rdi
xorl %esi, %esi
callq 0x4010b8
movq %r12, %rdi
movl $0x200, %esi            # imm = 0x200
movq %r15, %rdx
callq 0x41c930
movq %r14, -0x38(%rbp)
movl %r14d, %ebx
testq %rax, %rax
jne 0x4022ed
jmp 0x40242e
leaq 0xc0c3f(%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 0x40e110
movq %rbx, -0x38(%rbp)
jmp 0x40242e
movq $0x0, -0x30(%rbp)
xorl %r8d, %r8d
jmp 0x40243a
movq $0x0, -0x38(%rbp)
movq $0x0, -0x30(%rbp)
movq $0x0, -0x40(%rbp)
movq %r15, %rdi
callq 0x41c640
movq -0x40(%rbp), %r8
movl -0x78(%rbp), %eax
testl %eax, %eax
movq -0x48(%rbp), %rdi
movq -0x38(%rbp), %rbx
je 0x4026f5
movl -0x4c(%rbp), %eax
xorl %r9d, %r9d
movq %r9, %r10
incq %r9
testl %eax, %eax
je 0x4026e7
xorl %r15d, %r15d
movq %r9, -0x60(%rbp)
movq %r10, -0x58(%rbp)
movl -0x6c(%rbp), %r13d
cmpq %r13, %r9
jae 0x4026d2
movl (%rdi,%r10,4), %esi
incl %esi
movq -0x68(%rbp), %rcx
leaq (%r15,%r15,2), %r11
movl 0x8(%rcx,%r11,8), %edx
cmpl %edx, %esi
ja 0x4026d2
movq -0x88(%rbp), %rsi
cmpl %edx, (%rsi,%r9,4)
jbe 0x4026e9
movq %r8, -0x40(%rbp)
movq %r11, -0x78(%rbp)
leaq (%rcx,%r11,8), %rax
movq (%rax), %r13
movq %r13, %rdi
callq 0x4010f8
movq %rax, %r12
movq %rbx, -0x38(%rbp)
testl %ebx, %ebx
je 0x402597
movl -0x38(%rbp), %ebx
xorl %r14d, %r14d
cmpl $0xb, %r12d
jb 0x402534
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 0x40254d
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 -0x30(%rbp), %rax
movq (%rax,%r14,8), %rdi
jne 0x40253c
movdqu (%r13), %xmm0
movdqu (%rdi), %xmm1
pcmpeqb %xmm0, %xmm1
movzbl 0x10(%r13), %eax
movzbl 0x10(%rdi), %ecx
movd %eax, %xmm0
movd %ecx, %xmm2
jmp 0x402574
movq -0x30(%rbp), %rax
movq (%rax,%r14,8), %rdi
movq %r13, %rsi
callq 0x4010c8
testl %eax, %eax
jne 0x40258b
jmp 0x40266a
movq -0x30(%rbp), %rax
movq (%rax,%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 0x40266a
incq %r14
cmpq %r14, %rbx
jne 0x4024d1
movq -0x38(%rbp), %r14
leal 0x1(%r14), %r13d
leaq (,%r13,8), %rsi
movq -0x30(%rbp), %rdi
callq 0x42b2d0
movq %rax, %rbx
leaq (,%r13,4), %rsi
movq -0x40(%rbp), %rdi
callq 0x42b2d0
movq -0x68(%rbp), %rcx
movq -0x78(%rbp), %rdx
movq (%rcx,%rdx,8), %rdi
cmpl $0xb, %r12d
movq %rbx, -0x30(%rbp)
jb 0x402653
leaq (%rcx,%rdx,8), %rbx
movl 0xa(%rdi), %edx
movl $0x504f5254, %ecx       # imm = 0x504F5254
xorl %ecx, %edx
movl 0xd(%rdi), %ecx
movl $0x52494450, %esi       # imm = 0x52494450
xorl %esi, %ecx
orl %edx, %ecx
je 0x40267c
movq %rax, %r14
movl 0xa(%rdi), %eax
movl $0x44525355, %ecx       # imm = 0x44525355
xorl %ecx, %eax
movzwl 0xe(%rdi), %ecx
xorl $0x5249, %ecx           # imm = 0x5249
orl %eax, %ecx
jne 0x40263e
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 0x403544
movl -0x38(%rbp), %ecx
movq -0x30(%rbp), %rdx
movq %rax, (%rdx,%rcx,8)
movq %r14, %r8
jmp 0x4026bb
movq %r14, %r12
movq %rax, %r14
callq 0x403544
movq %r14, %r8
movl %r12d, %ecx
movq %rax, (%rbx,%rcx,8)
jmp 0x4026bb
movq -0x40(%rbp), %r8
incl (%r8,%r14,4)
movq -0x48(%rbp), %rdi
movq -0x38(%rbp), %rbx
jmp 0x4026ca
movl $0x200, %edx            # imm = 0x200
leaq -0x490(%rbp), %r12
movq %r12, %rdi
xorl %esi, %esi
movq %rax, %r14
callq 0x4010b8
movq (%rbx), %rsi
movl $0x12, %edx
movq %r12, %rdi
callq 0x401090
movq %r12, %rdi
callq 0x403544
movq %r14, %r8
movl -0x38(%rbp), %ecx
movq -0x30(%rbp), %rdx
movq %rax, (%rdx,%rcx,8)
movl $0x1, (%r14,%rcx,4)
movl %r13d, %ebx
movq -0x48(%rbp), %rdi
movq -0x60(%rbp), %r9
movq -0x58(%rbp), %r10
incq %r15
movl -0x4c(%rbp), %eax
cmpq %rax, %r15
jb 0x40246c
movl -0x6c(%rbp), %r13d
jmp 0x4026e9
xorl %eax, %eax
movl %r13d, %ecx
cmpq %rcx, %r9
jb 0x402453
movq %r8, %r13
leaq 0xc0960(%rip), %rdi     # 0x4c305f
leaq 0xc0957(%rip), %rsi     # 0x4c305d
callq 0x41cbe0
testl %ebx, %ebx
movq %rax, -0x40(%rbp)
je 0x4027c1
movq %rbx, -0x38(%rbp)
movl %ebx, %ebx
leaq -0x490(%rbp), %r12
xorl %r14d, %r14d
movq %r13, %rdi
xorl %eax, %eax
xorl %ecx, %ecx
movl %ecx, %edx
movl (%rdi,%rdx,4), %esi
movl %eax, %edx
cmpl (%rdi,%rax,4), %esi
jb 0x40273c
movl %ecx, %edx
incq %rax
movl %edx, %ecx
cmpq %rax, %rbx
jne 0x40272e
movl %edx, %r15d
movl (%rdi,%r15,4), %edx
movq -0x30(%rbp), %rax
movq (%rax,%r15,8), %rcx
movq %r12, %rdi
leaq 0xc09ca(%rip), %rsi     # 0x4c3129
xorl %eax, %eax
callq 0x40e1e0
movq %r12, %rdi
movq -0x40(%rbp), %rsi
callq 0x41cce0
movq %r13, %rdi
movl $0x0, (%r13,%r15,4)
incl %r14d
cmpl -0x38(%rbp), %r14d
jne 0x40272a
movq -0x40(%rbp), %rdi
callq 0x41c640
movq %r13, %rax
xorl %r14d, %r14d
movq -0x80(%rbp), %r15
movq -0x30(%rbp), %r12
movq (%r12,%r14,8), %rdi
testq %rdi, %rdi
je 0x4027b7
callq 0x42af80
movq %r13, %rax
movq $0x0, (%r12,%r14,8)
incq %r14
cmpq %r14, %rbx
jne 0x40279e
jmp 0x4027d4
movq %rax, %rdi
callq 0x41c640
movq -0x80(%rbp), %r15
movq -0x30(%rbp), %r12
movq %r13, %rax
testq %rax, %rax
je 0x4027e1
movq %rax, %rdi
callq 0x42af80
testq %r12, %r12
je 0x4027ee
movq %r12, %rdi
callq 0x42af80
movl -0x4c(%rbp), %eax
testl %eax, %eax
je 0x402829
xorl %ebx, %ebx
xorl %r14d, %r14d
movq -0x68(%rbp), %rcx
movq (%rcx,%rbx), %rdi
testq %rdi, %rdi
je 0x40281b
callq 0x42af80
movq -0x68(%rbp), %rax
movq $0x0, (%rax,%rbx)
movl -0x4c(%rbp), %eax
incq %r14
movl %eax, %ecx
addq $0x18, %rbx
cmpq %rcx, %r14
jb 0x4027fa
movq -0x68(%rbp), %rdi
testq %rdi, %rdi
je 0x40283f
callq 0x42af80
movq $0x0, -0x68(%rbp)
movq -0x40(%rbp), %rdi
callq 0x41c640
movq %r15, %rsp
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq