// File: /workspace/github_c_makefile_star2_O2/Zarh[P]regps3iso/main.c
// Function: get_plain_files
void get_plain_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("plain_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( start[i] <= file_info[j].sector ) {
				if(end[i] <= file_info[j].sector ) break;
				
				u8 found = NO;
				for(k=0; k<files_n; k++) {
					if( memcmp(files[k], file_info[j].path, strlen(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));
					files[files_n] = strcpy_malloc(file_info[j].path);
					amount[files_n]=1;
					
					files_n++;
				}
			}
		}
	}
	
	// write db
	char str[512];
	l = fopen("plain_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 0xc03d7(%rip), %rsi     # 0x4c2027
callq 0x41c7c0
testq %rax, %rax
je 0x401e6b
movq %rax, %r14
movq %rbx, -0x38(%rbp)
leaq -0x64(%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, -0x30(%rbp)
movq %r14, %rdi
xorl %edx, %edx
callq 0x41f220
movq %r13, -0x80(%rbp)
movq %rbx, -0x60(%rbp)
movq %r15, -0x58(%rbp)
pushq $0x4
popq %r12
movq -0x30(%rbp), %r14
subq $0x1, %r15
jb 0x401d0c
movq %rbx, %rdi
movq %r12, %rsi
pushq $0x1
popq %rdx
movq %r14, %rcx
callq 0x41c9f0
movq %r13, %rdi
movq %r12, %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 0x401cca
movq -0x30(%rbp), %rdi
callq 0x41c220
leaq -0x48(%rbp), %rsi
andq $0x0, (%rsi)
leaq -0x4c(%rbp), %rdx
movq -0x38(%rbp), %rdi
callq 0x40323a
leaq 0xc0312(%rip), %rdi     # 0x4c2043
leaq 0xc031b(%rip), %rsi     # 0x4c2053
callq 0x41c7c0
testq %rax, %rax
je 0x401e8a
movq %rax, %r15
leaq -0x290(%rbp), %r12
pushq $0xd
popq %rbx
pushq $0xa
popq %r14
leaq 0xc0d6c(%rip), %r13     # 0x4c2aca
movq $0x0, -0x38(%rbp)
movq $0x0, -0x30(%rbp)
movq $0x0, -0x40(%rbp)
movq %r12, %rdi
movl $0x200, %esi            # imm = 0x200
movq %r15, %rdx
callq 0x41c510
testq %rax, %rax
je 0x401e5d
movzbl -0x290(%rbp), %eax
cmpl $0xa, %eax
je 0x401d76
cmpl $0xd, %eax
je 0x401d76
movq %r12, %rdi
movl %ebx, %esi
callq 0x401100
testq %rax, %rax
je 0x401dbe
movq %r12, %rdi
leaq 0xc029d(%rip), %rsi     # 0x4c2056
callq 0x42d9d0
movq %r12, %rdi
movl %r14d, %esi
callq 0x401100
testq %rax, %rax
je 0x401dd9
movq %r12, %rdi
movq %r13, %rsi
callq 0x42d9d0
movq -0x38(%rbp), %r14
leal 0x1(%r14), %ebx
leaq (,%rbx,8), %rsi
movq -0x30(%rbp), %rdi
callq 0x42aeb0
movq %rax, %r13
leaq (,%rbx,4), %rsi
movq -0x40(%rbp), %rdi
callq 0x42aeb0
movl %r14d, %r14d
movq %rax, -0x40(%rbp)
leaq (%rax,%r14,4), %rdx
movq %r12, %rdi
leaq 0xc023d(%rip), %rsi     # 0x4c2058
xorl %eax, %eax
callq 0x40df50
leaq -0x285(%rbp), %rdi
callq 0x4031f4
movq %r13, -0x30(%rbp)
movq %rax, (%r13,%r14,8)
leaq 0xc0c8c(%rip), %r13     # 0x4c2aca
movl $0x200, %edx            # imm = 0x200
movq %r12, %rdi
xorl %esi, %esi
callq 0x4010b8
movq %rbx, -0x38(%rbp)
pushq $0xd
popq %rbx
pushq $0xa
popq %r14
jmp 0x401d76
movq %r15, %rdi
callq 0x41c220
movq -0x30(%rbp), %r12
jmp 0x401e9d
leaq 0xc01b8(%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, -0x38(%rbp)
xorl %r12d, %r12d
movq $0x0, -0x40(%rbp)
xorl %esi, %esi
movq -0x60(%rbp), %rcx
movq -0x58(%rbp), %rax
movl %eax, %eax
cmpq %rax, %rsi
jae 0x401fb2
xorl %edi, %edi
movq %rsi, -0x70(%rbp)
movl -0x4c(%rbp), %eax
cmpq %rax, %rdi
jae 0x401fa7
movq -0x48(%rbp), %r13
imulq $0x18, %rdi, %r8
movl 0x8(%r13,%r8), %eax
cmpl %eax, (%rcx,%rsi,4)
ja 0x401f9f
movq -0x80(%rbp), %rdx
cmpl %eax, (%rdx,%rsi,4)
jbe 0x401fa7
movq %rdi, -0x58(%rbp)
movq %r8, -0x88(%rbp)
addq %r8, %r13
movl -0x38(%rbp), %eax
movq %rax, -0x30(%rbp)
xorl %r14d, %r14d
cmpq %r14, -0x30(%rbp)
je 0x401f3b
movq (%r12,%r14,8), %r15
movq %r12, %rbx
movq (%r13), %r12
movq %r12, %rdi
callq 0x4010f8
movq %r15, %rdi
movq %r12, %rsi
movq %rbx, %r12
movq %rax, %rdx
callq 0x401028
incq %r14
testl %eax, %eax
jne 0x401eff
movq -0x40(%rbp), %rax
incl -0x4(%rax,%r14,4)
jmp 0x401f93
movq -0x38(%rbp), %rbx
incl %ebx
leaq (,%rbx,8), %rsi
movq %r12, %rdi
callq 0x42aeb0
movq %rax, %r12
movq %rbx, -0x38(%rbp)
leaq (,%rbx,4), %rsi
movq -0x40(%rbp), %rdi
callq 0x42aeb0
movq %rax, %rbx
movq -0x48(%rbp), %rax
movq -0x88(%rbp), %rcx
movq (%rax,%rcx), %rdi
callq 0x4031f4
movq -0x30(%rbp), %rcx
movq %rax, (%r12,%rcx,8)
movq %rbx, -0x40(%rbp)
movl $0x1, (%rbx,%rcx,4)
movq -0x60(%rbp), %rcx
movq -0x70(%rbp), %rsi
movq -0x58(%rbp), %rdi
incq %rdi
jmp 0x401eb8
incq %rsi
movl -0x64(%rbp), %eax
jmp 0x401ea7
movq %r12, %r15
leaq 0xc0087(%rip), %rdi     # 0x4c2043
leaq 0xc009a(%rip), %rsi     # 0x4c205d
callq 0x41c7c0
movq %rax, -0x30(%rbp)
movl -0x38(%rbp), %r13d
leaq -0x490(%rbp), %r12
xorl %ebx, %ebx
movq -0x40(%rbp), %rsi
cmpl -0x38(%rbp), %ebx
je 0x40202f
xorl %eax, %eax
xorl %ecx, %ecx
movl %ecx, %r14d
movl (%rsi,%r14,4), %edx
cmpq %rax, %r13
je 0x402001
movl %eax, %ecx
cmpl (%rsi,%rax,4), %edx
jb 0x401ffc
movl %r14d, %ecx
incq %rax
jmp 0x401fe6
movq (%r15,%r14,8), %rcx
movq %r12, %rdi
leaq 0xc011a(%rip), %rsi     # 0x4c2129
xorl %eax, %eax
callq 0x40ddc0
movq %r12, %rdi
movq -0x30(%rbp), %rsi
callq 0x41c8c0
movq -0x40(%rbp), %rsi
andl $0x0, (%rsi,%r14,4)
incl %ebx
jmp 0x401fdd
movq -0x30(%rbp), %rdi
callq 0x41c220
xorl %ebx, %ebx
cmpq %rbx, %r13
je 0x402057
movq (%r15,%rbx,8), %rdi
testq %rdi, %rdi
je 0x402052
callq 0x42ab60
andq $0x0, (%r15,%rbx,8)
incq %rbx
jmp 0x40203a
movq -0x40(%rbp), %rdi
callq 0x42ab60
movq %r15, %rdi
callq 0x42ab60
xorl %ebx, %ebx
xorl %r14d, %r14d
movl -0x4c(%rbp), %eax
movq -0x48(%rbp), %rdi
cmpq %rax, %r14
jae 0x402099
movq (%rdi,%rbx), %rdi
testq %rdi, %rdi
je 0x402090
callq 0x42ab60
movq -0x48(%rbp), %rax
andq $0x0, (%rax,%rbx)
incq %r14
addq $0x18, %rbx
jmp 0x40206d
testq %rdi, %rdi
je 0x4020a8
callq 0x42ab60
andq $0x0, -0x48(%rbp)
movq -0x30(%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