// File: /workspace/github_c_makefile_star2_O3/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 $0x458, %rsp            # imm = 0x458
movq %rdi, %rbx
leaq 0xc13cb(%rip), %rsi     # 0x4c3027
callq 0x41cbe0
testq %rax, %rax
je 0x401ea9
movq %rax, %r14
leaq -0x64(%rbp), %r15
movl $0x4, %esi
movl $0x1, %edx
movq %r15, %rdi
movq %rax, %rcx
callq 0x41ce10
movq %rsp, -0x80(%rbp)
movl (%r15), %r12d
movl %r12d, %eax
bswapl %eax
movl %eax, (%r15)
movq %rsp, %rcx
movq %rax, -0x38(%rbp)
leaq 0xf(,%rax,4), %rax
andq $-0x10, %rax
subq %rax, %rcx
movq %rcx, -0x58(%rbp)
movq %rcx, %rsp
movq %rsp, %rcx
subq %rax, %rcx
movq %rcx, -0x70(%rbp)
movq %rcx, %rsp
movq $0x0, -0x48(%rbp)
movl $0x8, %esi
movq %r14, %rdi
xorl %edx, %edx
callq 0x41f640
movl %r12d, -0x60(%rbp)
testl %r12d, %r12d
je 0x401d3c
movq -0x38(%rbp), %r13
cmpl $0x1, %r13d
adcl $0x0, %r13d
movq -0x70(%rbp), %r15
movq -0x58(%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 %r13
jne 0x401cf3
movq %r14, %rdi
callq 0x41c640
leaq -0x50(%rbp), %rsi
movq $0x0, (%rsi)
leaq -0x3c(%rbp), %rdx
movq %rbx, %rdi
callq 0x40358c
leaq 0xc12e1(%rip), %rdi     # 0x4c3043
leaq 0xc12ea(%rip), %rsi     # 0x4c3053
callq 0x41cbe0
testq %rax, %rax
je 0x401ece
movq %rax, %r15
leaq -0x280(%rbp), %rdi
movl $0x200, %esi            # imm = 0x200
movq %rax, %rdx
callq 0x41c930
testq %rax, %rax
je 0x401ed6
leaq -0x280(%rbp), %r12
leaq 0xc1d25(%rip), %rbx     # 0x4c3aca
movq $0x0, -0x30(%rbp)
xorl %r14d, %r14d
xorl %r13d, %r13d
movzbl -0x280(%rbp), %eax
cmpl $0xd, %eax
je 0x401dc4
cmpl $0xa, %eax
jne 0x401dde
movq %r12, %rdi
movl $0x200, %esi            # imm = 0x200
movq %r15, %rdx
callq 0x41c930
testq %rax, %rax
jne 0x401db3
jmp 0x401ec8
movq %r12, %rdi
movl $0xd, %esi
callq 0x401100
testq %rax, %rax
je 0x401dff
movq %r12, %rdi
leaq 0xc125c(%rip), %rsi     # 0x4c3056
callq 0x42ddf0
movq %r12, %rdi
movl $0xa, %esi
callq 0x401100
testq %rax, %rax
je 0x401e1c
movq %r12, %rdi
movq %rbx, %rsi
callq 0x42ddf0
leal 0x1(%r13), %ebx
movq %rbx, -0x48(%rbp)
leaq (,%rbx,8), %rsi
movq %r14, %rdi
callq 0x42b2d0
movq %rax, %r14
leaq (,%rbx,4), %rsi
movq -0x30(%rbp), %rdi
callq 0x42b2d0
movl %r13d, %ebx
movq %rax, -0x30(%rbp)
leaq (%rax,%rbx,4), %rdx
movq %r12, %rdi
leaq 0xc11fb(%rip), %rsi     # 0x4c3058
xorl %eax, %eax
callq 0x40e370
leaq -0x275(%rbp), %rdi
callq 0x403544
movq %rax, (%r14,%rbx,8)
leaq 0xc1c4f(%rip), %rbx     # 0x4c3aca
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 -0x48(%rbp), %r13
testq %rax, %rax
jne 0x401db3
jmp 0x401ee9
leaq 0xc117a(%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 %r13, -0x48(%rbp)
jmp 0x401ee9
xorl %r12d, %r12d
xorl %r15d, %r15d
jmp 0x401ef8
movq $0x0, -0x48(%rbp)
xorl %r14d, %r14d
movq $0x0, -0x30(%rbp)
movq %r15, %rdi
callq 0x41c640
movq %r14, %r12
movq -0x30(%rbp), %r15
movq -0x38(%rbp), %rdx
movl -0x60(%rbp), %eax
testl %eax, %eax
movq -0x58(%rbp), %rbx
movq -0x48(%rbp), %r13
je 0x402029
movl -0x3c(%rbp), %eax
xorl %edi, %edi
testl %eax, %eax
je 0x402019
xorl %r8d, %r8d
movq %rdi, -0x78(%rbp)
movq -0x50(%rbp), %rcx
leaq (%r8,%r8,2), %r9
movl 0x8(%rcx,%r9,8), %edx
cmpl %edx, (%rbx,%rdi,4)
ja 0x402005
movq -0x70(%rbp), %rsi
cmpl %edx, (%rsi,%rdi,4)
jbe 0x402014
movq %r8, -0x38(%rbp)
movq %r15, -0x30(%rbp)
testl %r13d, %r13d
movq %r9, -0x60(%rbp)
je 0x401f9e
movq %r13, %rbx
movq %r12, %r13
movq (%rcx,%r9,8), %r15
movq %r15, %rdi
callq 0x4010f8
movq %rax, %r12
movq %rbx, -0x48(%rbp)
movl %ebx, %r14d
xorl %ebx, %ebx
movq (%r13,%rbx,8), %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0x401028
testl %eax, %eax
je 0x401fea
incq %rbx
cmpq %rbx, %r14
jne 0x401f75
movq -0x58(%rbp), %rbx
movq %r13, %r12
movq -0x48(%rbp), %r13
jmp 0x401fa1
xorl %r14d, %r14d
incl %r13d
leaq (,%r13,8), %rsi
movq %r12, %rdi
callq 0x42b2d0
movq %rax, %r12
leaq (,%r13,4), %rsi
movq -0x30(%rbp), %rdi
callq 0x42b2d0
movq %rax, %r15
movq -0x50(%rbp), %rax
movq -0x60(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
callq 0x403544
movq %rax, (%r12,%r14,8)
movl $0x1, (%r15,%r14,4)
jmp 0x401ffd
movq -0x30(%rbp), %r15
incl (%r15,%rbx,4)
movq -0x58(%rbp), %rbx
movq %r13, %r12
movq -0x48(%rbp), %r13
movq -0x78(%rbp), %rdi
movq -0x38(%rbp), %r8
incq %r8
movl -0x3c(%rbp), %eax
cmpq %rax, %r8
jb 0x401f23
movl -0x64(%rbp), %edx
jmp 0x40201b
xorl %eax, %eax
incq %rdi
movl %edx, %ecx
cmpq %rcx, %rdi
jb 0x401f14
movq %r15, -0x30(%rbp)
leaq 0xc100f(%rip), %rdi     # 0x4c3043
leaq 0xc1022(%rip), %rsi     # 0x4c305d
callq 0x41cbe0
movq %rax, %rbx
testl %r13d, %r13d
je 0x4020e5
movq %rbx, -0x38(%rbp)
movl %r13d, %r15d
leaq -0x480(%rbp), %r13
xorl %ebx, %ebx
movq -0x30(%rbp), %rdi
xorl %eax, %eax
xorl %ecx, %ecx
movl %ecx, %edx
movl (%rdi,%rdx,4), %esi
movl %eax, %edx
cmpl (%rdi,%rax,4), %esi
jb 0x402072
movl %ecx, %edx
incq %rax
movl %edx, %ecx
cmpq %rax, %r15
jne 0x402064
movl %edx, %r14d
movl (%rdi,%r14,4), %edx
movq (%r12,%r14,8), %rcx
movq %r13, %rdi
leaq 0xc1098(%rip), %rsi     # 0x4c3129
xorl %eax, %eax
callq 0x40e1e0
movq %r13, %rdi
movq -0x38(%rbp), %rsi
callq 0x41cce0
movq -0x30(%rbp), %rdi
movl $0x0, (%rdi,%r14,4)
incl %ebx
cmpl %r15d, %ebx
jne 0x402060
movq -0x38(%rbp), %r13
movq %r13, %rdi
callq 0x41c640
xorl %ebx, %ebx
movq (%r12,%rbx,8), %rdi
testq %rdi, %rdi
je 0x4020db
callq 0x42af80
movq $0x0, (%r12,%rbx,8)
incq %rbx
cmpq %rbx, %r15
jne 0x4020c5
jmp 0x4020f0
movq %rbx, %rdi
callq 0x41c640
movq %rbx, %r13
movq -0x30(%rbp), %rdi
testq %rdi, %rdi
je 0x4020fe
callq 0x42af80
testq %r12, %r12
je 0x40210b
movq %r12, %rdi
callq 0x42af80
movl -0x3c(%rbp), %eax
testl %eax, %eax
je 0x402146
xorl %ebx, %ebx
xorl %r14d, %r14d
movq -0x50(%rbp), %rcx
movq (%rcx,%rbx), %rdi
testq %rdi, %rdi
je 0x402138
callq 0x42af80
movq -0x50(%rbp), %rax
movq $0x0, (%rax,%rbx)
movl -0x3c(%rbp), %eax
incq %r14
movl %eax, %ecx
addq $0x18, %rbx
cmpq %rcx, %r14
jb 0x402117
movq -0x50(%rbp), %rdi
testq %rdi, %rdi
je 0x40215c
callq 0x42af80
movq $0x0, -0x50(%rbp)
movq %r13, %rdi
callq 0x41c640
movq -0x80(%rbp), %rsp
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq