// File: /workspace/github_c_makefile_star2_O2/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 0xbff45(%rip), %rsi     # 0x4c2027
callq 0x41c7c0
testq %rax, %rax
je 0x4022f0
movq %rax, %r14
movq %rbx, -0x30(%rbp)
leaq -0x70(%rbp), %rbx
pushq $0x4
popq %rsi
pushq $0x1
popq %rdx
movq %rbx, %rdi
movq %rax, %rcx
callq 0x41c9f0
movq %rsp, -0x78(%rbp)
movl (%rbx), %r15d
bswapl %r15d
movl %r15d, (%rbx)
movq %rsp, %rbx
leaq 0xf(,%r15,4), %rax
andq $-0x10, %rax
subq %rax, %rbx
movq %rbx, %rsp
movq %rsp, %r13
subq %rax, %r13
movq %r13, %rsp
pushq $0x8
popq %rsi
movq %r14, -0x38(%rbp)
movq %r14, %rdi
xorl %edx, %edx
callq 0x41f220
movq %r13, -0x60(%rbp)
movq %rbx, -0x80(%rbp)
movq %r15, %r12
movq -0x38(%rbp), %r14
subq $0x1, %r12
jb 0x402199
movq %rbx, %rdi
pushq $0x4
popq %rsi
pushq $0x1
popq %rdx
movq %r14, %rcx
callq 0x41c9f0
movq %r13, %rdi
pushq $0x4
popq %rsi
pushq $0x1
popq %rdx
movq %r14, %rcx
callq 0x41c9f0
movl (%rbx), %eax
bswapl %eax
movl %eax, (%rbx)
movl (%r13), %eax
bswapl %eax
movl %eax, (%r13)
addq $0x4, %rbx
addq $0x4, %r13
jmp 0x402157
movq -0x38(%rbp), %rdi
callq 0x41c220
leaq -0x68(%rbp), %rsi
andq $0x0, (%rsi)
leaq -0x6c(%rbp), %rdx
movq -0x30(%rbp), %rdi
callq 0x40323a
leaq 0xbfea1(%rip), %rdi     # 0x4c205f
leaq 0xbfe8e(%rip), %rsi     # 0x4c2053
callq 0x41c7c0
testq %rax, %rax
je 0x40230f
movq %rax, %r12
leaq -0x490(%rbp), %r13
pushq $0xd
popq %rbx
pushq $0xa
popq %r14
movq $0x0, -0x58(%rbp)
movq $0x0, -0x38(%rbp)
movq $0x0, -0x30(%rbp)
movq %r13, %rdi
movl $0x200, %esi            # imm = 0x200
movq %r12, %rdx
callq 0x41c510
testq %rax, %rax
je 0x4022e2
movzbl -0x490(%rbp), %eax
cmpl $0xa, %eax
je 0x4021fc
cmpl $0xd, %eax
je 0x4021fc
movq %r13, %rdi
movl %ebx, %esi
callq 0x401100
testq %rax, %rax
je 0x402244
movq %r13, %rdi
leaq 0xbfe17(%rip), %rsi     # 0x4c2056
callq 0x42d9d0
movq %r13, %rdi
movl %r14d, %esi
callq 0x401100
testq %rax, %rax
je 0x402263
movq %r13, %rdi
leaq 0xc086c(%rip), %rsi     # 0x4c2aca
callq 0x42d9d0
movq -0x58(%rbp), %r14
leal 0x1(%r14), %ebx
leaq (,%rbx,8), %rsi
movq -0x38(%rbp), %rdi
callq 0x42aeb0
movq %rax, -0x38(%rbp)
leaq (,%rbx,4), %rsi
movq -0x30(%rbp), %rdi
callq 0x42aeb0
movl %r14d, %r14d
movq %rax, -0x30(%rbp)
leaq (%rax,%r14,4), %rdx
movq %r13, %rdi
leaq 0xbfdb2(%rip), %rsi     # 0x4c2058
xorl %eax, %eax
callq 0x40df50
leaq -0x485(%rbp), %rdi
callq 0x4031f4
movq -0x38(%rbp), %rcx
movq %rax, (%rcx,%r14,8)
movl $0x200, %edx            # imm = 0x200
movq %r13, %rdi
xorl %esi, %esi
callq 0x4010b8
movl %ebx, %eax
movq %rax, -0x58(%rbp)
pushq $0xd
popq %rbx
pushq $0xa
popq %r14
jmp 0x4021fc
movq %r12, %rdi
callq 0x41c220
movq -0x38(%rbp), %r12
jmp 0x402322
leaq 0xbfd33(%rip), %rdi     # 0x4c202a
movq %rbx, %rsi
xorl %eax, %eax
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x40dcf0
movq $0x0, -0x58(%rbp)
xorl %r12d, %r12d
movq $0x0, -0x30(%rbp)
xorl %edi, %edi
movq -0x60(%rbp), %r13
movl %r15d, %eax
cmpq %rax, %rdi
jae 0x402568
leaq 0x1(%rdi), %r8
xorl %r9d, %r9d
movq -0x30(%rbp), %rsi
movq %rdi, -0x48(%rbp)
movq %r8, -0x40(%rbp)
movl -0x6c(%rbp), %eax
cmpq %rax, %r9
jae 0x40255c
movl %r15d, %eax
cmpq %rax, %r8
jae 0x402550
movl (%r13,%rdi,4), %edx
incl %edx
movq -0x68(%rbp), %rax
imulq $0x18, %r9, %r10
movl 0x8(%rax,%r10), %ecx
cmpl %ecx, %edx
ja 0x402550
movq %rsi, -0x30(%rbp)
addq %r10, %rax
movq -0x80(%rbp), %rdx
cmpl %ecx, 0x4(%rdx,%rdi,4)
jbe 0x402560
movq %r10, -0x88(%rbp)
movq %r9, -0x50(%rbp)
movq %r12, %rbx
movq (%rax), %r15
movq %r15, %rdi
callq 0x4010f8
movq %rax, %r13
leaq 0xa(%r15), %r12
movl -0x58(%rbp), %eax
movq %rax, -0x38(%rbp)
xorl %r14d, %r14d
cmpq %r14, -0x38(%rbp)
je 0x40242c
cmpl $0xb, %r13d
jb 0x4023fb
movq %r12, %rdi
leaq 0xbfc9d(%rip), %rsi     # 0x4c206d
pushq $0x7
popq %rdx
callq 0x401028
testl %eax, %eax
je 0x402409
movq %r12, %rdi
leaq 0xbfc8f(%rip), %rsi     # 0x4c2075
pushq $0x6
popq %rdx
callq 0x401028
movq (%rbx,%r14,8), %rsi
testl %eax, %eax
je 0x402414
movq %rsi, %rdi
jmp 0x4023ff
movq (%rbx,%r14,8), %rdi
movq %r15, %rsi
callq 0x4010c8
jmp 0x40241f
movq (%rbx,%r14,8), %rsi
movq %r15, %rdi
pushq $0x12
jmp 0x402419
movq %r15, %rdi
pushq $0x11
popq %rdx
callq 0x401028
testl %eax, %eax
je 0x4024d1
incq %r14
jmp 0x4023ba
movq -0x58(%rbp), %r14
incl %r14d
leaq (,%r14,8), %rsi
movq %rbx, %rdi
callq 0x42aeb0
movq %rax, %r12
movq %r14, -0x58(%rbp)
leaq (,%r14,4), %rsi
movq -0x30(%rbp), %rdi
callq 0x42aeb0
movq %rax, %rbx
movq -0x68(%rbp), %r14
movq -0x88(%rbp), %rax
movq (%r14,%rax), %r15
cmpl $0xb, %r13d
jb 0x4024e2
movq %rbx, -0x30(%rbp)
movq %r12, %rbx
addq %rax, %r14
leaq 0xa(%r15), %r12
movq %r12, %rdi
leaq 0xbfbe2(%rip), %rsi     # 0x4c206d
pushq $0x7
popq %rdx
callq 0x401028
testl %eax, %eax
movq -0x60(%rbp), %r13
je 0x402502
movq %r12, %rdi
leaq 0xbfbd0(%rip), %rsi     # 0x4c2075
pushq $0x6
popq %rdx
callq 0x401028
testl %eax, %eax
jne 0x402526
movl $0x200, %edx            # imm = 0x200
leaq -0x290(%rbp), %r15
movq %r15, %rdi
xorl %esi, %esi
callq 0x4010b8
movq (%r14), %rsi
movq %r15, %rdi
pushq $0x11
jmp 0x402520
movq -0x30(%rbp), %rsi
incl (%rsi,%r14,4)
movq %rbx, %r12
movq -0x60(%rbp), %r13
jmp 0x402544
movq %r15, %rdi
callq 0x4031f4
movq -0x38(%rbp), %rcx
movq %rax, (%r12,%rcx,8)
movl $0x1, (%rbx,%rcx,4)
movq -0x60(%rbp), %r13
movq %rbx, %rsi
jmp 0x402544
movl $0x200, %edx            # imm = 0x200
leaq -0x290(%rbp), %r15
movq %r15, %rdi
xorl %esi, %esi
callq 0x4010b8
movq (%r14), %rsi
movq %r15, %rdi
pushq $0x12
popq %rdx
callq 0x401090
movq %r15, %rdi
callq 0x4031f4
movq %rbx, %r12
movq -0x38(%rbp), %rcx
movq %rax, (%rbx,%rcx,8)
movq -0x30(%rbp), %rsi
movl $0x1, (%rsi,%rcx,4)
movq -0x48(%rbp), %rdi
movq -0x40(%rbp), %r8
movq -0x50(%rbp), %r9
incq %r9
movl -0x70(%rbp), %r15d
jmp 0x402347
movq %rsi, -0x30(%rbp)
movq %r8, %rdi
jmp 0x402328
movq %r12, %r15
leaq 0xbfaed(%rip), %rdi     # 0x4c205f
leaq 0xbfae4(%rip), %rsi     # 0x4c205d
callq 0x41c7c0
movq %rax, -0x38(%rbp)
movl -0x58(%rbp), %ebx
leaq -0x290(%rbp), %r13
xorl %r14d, %r14d
movq -0x30(%rbp), %rsi
cmpl -0x58(%rbp), %r14d
je 0x4025e7
xorl %eax, %eax
xorl %ecx, %ecx
movl %ecx, %r12d
movl (%rsi,%r12,4), %edx
cmpq %rax, %rbx
je 0x4025b8
movl %eax, %ecx
cmpl (%rsi,%rax,4), %edx
jb 0x4025b3
movl %r12d, %ecx
incq %rax
jmp 0x40259d
movq (%r15,%r12,8), %rcx
movq %r13, %rdi
leaq 0xbfb63(%rip), %rsi     # 0x4c2129
xorl %eax, %eax
callq 0x40ddc0
movq %r13, %rdi
movq -0x38(%rbp), %rsi
callq 0x41c8c0
movq -0x30(%rbp), %rsi
andl $0x0, (%rsi,%r12,4)
incl %r14d
jmp 0x402593
movq -0x38(%rbp), %rdi
callq 0x41c220
xorl %r14d, %r14d
cmpq %r14, %rbx
je 0x402610
movq (%r15,%r14,8), %rdi
testq %rdi, %rdi
je 0x40260b
callq 0x42ab60
andq $0x0, (%r15,%r14,8)
incq %r14
jmp 0x4025f3
movq -0x30(%rbp), %rdi
callq 0x42ab60
movq %r15, %rdi
callq 0x42ab60
xorl %ebx, %ebx
xorl %r14d, %r14d
movl -0x6c(%rbp), %eax
movq -0x68(%rbp), %rdi
cmpq %rax, %r14
jae 0x402652
movq (%rdi,%rbx), %rdi
testq %rdi, %rdi
je 0x402649
callq 0x42ab60
movq -0x68(%rbp), %rax
andq $0x0, (%rax,%rbx)
incq %r14
addq $0x18, %rbx
jmp 0x402626
testq %rdi, %rdi
je 0x402661
callq 0x42ab60
andq $0x0, -0x68(%rbp)
movq -0x38(%rbp), %rdi
callq 0x41c220
movq -0x78(%rbp), %rsp
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq