// File: /workspace/github_c_makefile_star2_O1/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 0xc13cc(%rip), %rsi     # 0x4c3027
callq 0x41cb70
testq %rax, %rax
je 0x401eac
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, -0x60(%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 0x41f5d0
testl %r12d, %r12d
je 0x401d30
cmpl $0x1, %r13d
adcl $0x0, %r13d
movq -0x70(%rbp), %r15
movq -0x60(%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 0x401ce7
movq %r14, %rdi
callq 0x41c5d0
leaq -0x58(%rbp), %rsi
movq $0x0, (%rsi)
leaq -0x3c(%rbp), %rdx
movq %rbx, %rdi
callq 0x40351b
leaq 0xc12ed(%rip), %rdi     # 0x4c3043
leaq 0xc12f6(%rip), %rsi     # 0x4c3053
callq 0x41cb70
testq %rax, %rax
je 0x401ed1
movq %rax, %r15
leaq -0x280(%rbp), %rdi
movl $0x200, %esi            # imm = 0x200
movq %rax, %rdx
callq 0x41c8c0
testq %rax, %rax
movq -0x60(%rbp), %r14
je 0x401ee7
leaq -0x280(%rbp), %r12
leaq 0xc1d2d(%rip), %rbx     # 0x4c3aca
movq $0x0, -0x30(%rbp)
movq $0x0, -0x38(%rbp)
xorl %r13d, %r13d
movzbl -0x280(%rbp), %eax
cmpl $0xd, %eax
je 0x401dc1
cmpl $0xa, %eax
jne 0x401ddb
movq %r12, %rdi
movl $0x200, %esi            # imm = 0x200
movq %r15, %rdx
callq 0x41c8c0
testq %rax, %rax
jne 0x401db0
jmp 0x401ecb
movq %r12, %rdi
movl $0xd, %esi
callq 0x401100
testq %rax, %rax
je 0x401dfc
movq %r12, %rdi
leaq 0xc125f(%rip), %rsi     # 0x4c3056
callq 0x42dd80
movq %r12, %rdi
movl $0xa, %esi
callq 0x401100
testq %rax, %rax
je 0x401e19
movq %r12, %rdi
movq %rbx, %rsi
callq 0x42dd80
leal 0x1(%r13), %ebx
movq %rbx, -0x48(%rbp)
leaq (,%rbx,8), %rsi
movq -0x38(%rbp), %rdi
callq 0x42b260
movq %rax, -0x38(%rbp)
leaq (,%rbx,4), %rsi
movq -0x30(%rbp), %rdi
callq 0x42b260
movl %r13d, %ebx
movq %rax, -0x30(%rbp)
leaq (%rax,%rbx,4), %rdx
movq %r12, %rdi
leaq 0xc11fc(%rip), %rsi     # 0x4c3058
xorl %eax, %eax
callq 0x40e300
leaq -0x275(%rbp), %rdi
callq 0x4034b8
movq -0x38(%rbp), %rcx
movq %rax, (%rcx,%rbx,8)
leaq 0xc1c4c(%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 0x41c8c0
movq -0x48(%rbp), %r13
testq %rax, %rax
jne 0x401db0
jmp 0x401eff
leaq 0xc1177(%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 %r13, -0x48(%rbp)
jmp 0x401eff
movq $0x0, -0x38(%rbp)
movq $0x0, -0x30(%rbp)
movq -0x60(%rbp), %r14
jmp 0x401f07
movq $0x0, -0x48(%rbp)
movq $0x0, -0x38(%rbp)
movq $0x0, -0x30(%rbp)
movq %r15, %rdi
callq 0x41c5d0
cmpl $0x0, -0x64(%rbp)
movq -0x48(%rbp), %r12
je 0x402021
xorl %esi, %esi
cmpl $0x0, -0x3c(%rbp)
je 0x402012
xorl %r13d, %r13d
movq %rsi, -0x78(%rbp)
movq -0x58(%rbp), %rax
leaq (,%r13,2), %rdi
addq %r13, %rdi
movl 0x8(%rax,%rdi,8), %ecx
cmpl %ecx, (%r14,%rsi,4)
ja 0x402003
movq -0x70(%rbp), %rdx
cmpl %ecx, (%rdx,%rsi,4)
jbe 0x402012
movq %rdi, -0x50(%rbp)
movl %r12d, %r14d
movq %r12, -0x48(%rbp)
testl %r12d, %r12d
je 0x401f96
movq -0x50(%rbp), %rcx
movq (%rax,%rcx,8), %r15
movq %r15, %rdi
callq 0x4010f8
movq %rax, %r12
xorl %ebx, %ebx
movq -0x38(%rbp), %rax
movq (%rax,%rbx,8), %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0x401028
testl %eax, %eax
je 0x401ff0
incq %rbx
cmpq %rbx, %r14
jne 0x401f77
movq -0x48(%rbp), %r12
incl %r12d
leaq (,%r12,8), %rsi
movq -0x38(%rbp), %rdi
callq 0x42b260
movq %rax, %rbx
leaq (,%r12,4), %rsi
movq -0x30(%rbp), %rdi
callq 0x42b260
movq %rax, %r15
movq -0x58(%rbp), %rax
movq -0x50(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
callq 0x4034b8
movq %rbx, -0x38(%rbp)
movq %rax, (%rbx,%r14,8)
movq %r15, -0x30(%rbp)
movl $0x1, (%r15,%r14,4)
movq -0x60(%rbp), %r14
jmp 0x401fff
movq -0x30(%rbp), %rax
incl (%rax,%rbx,4)
movq -0x60(%rbp), %r14
movq -0x48(%rbp), %r12
movq -0x78(%rbp), %rsi
incq %r13
movl -0x3c(%rbp), %eax
cmpq %rax, %r13
jb 0x401f28
incq %rsi
movl -0x64(%rbp), %eax
cmpq %rax, %rsi
jb 0x401f17
movl %r12d, %r15d
leaq 0xc1018(%rip), %rdi     # 0x4c3043
leaq 0xc102b(%rip), %rsi     # 0x4c305d
callq 0x41cb70
movq %rax, -0x50(%rbp)
testl %r12d, %r12d
movq -0x30(%rbp), %rdi
je 0x4020b3
leaq 0xc10de(%rip), %r12     # 0x4c3129
leaq -0x480(%rbp), %r13
xorl %ebx, %ebx
xorl %eax, %eax
xorl %ecx, %ecx
movl %ecx, %edx
movl (%rdi,%rdx,4), %esi
movl %eax, %edx
cmpl (%rdi,%rax,4), %esi
jb 0x402066
movl %ecx, %edx
incq %rax
movl %edx, %ecx
cmpq %rax, %r15
jne 0x402058
movl %edx, %r14d
movq -0x30(%rbp), %rax
movl (%rax,%r14,4), %edx
movq -0x38(%rbp), %rax
movq (%rax,%r14,8), %rcx
movq %r13, %rdi
movq %r12, %rsi
xorl %eax, %eax
callq 0x40e170
movq %r13, %rdi
movq -0x50(%rbp), %rsi
callq 0x41cc70
movq -0x30(%rbp), %rax
movl $0x0, (%rax,%r14,4)
movq -0x30(%rbp), %rdi
incl %ebx
cmpl %r15d, %ebx
jne 0x402054
movq -0x50(%rbp), %rdi
callq 0x41c5d0
testq %r15, %r15
movq -0x38(%rbp), %r14
je 0x4020e5
xorl %ebx, %ebx
movq (%r14,%rbx,8), %rdi
testq %rdi, %rdi
je 0x4020dd
callq 0x42af10
movq $0x0, (%r14,%rbx,8)
incq %rbx
cmpq %rbx, %r15
jne 0x4020c7
movq -0x30(%rbp), %rdi
testq %rdi, %rdi
je 0x4020f3
callq 0x42af10
testq %r14, %r14
je 0x402100
movq %r14, %rdi
callq 0x42af10
cmpl $0x0, -0x3c(%rbp)
je 0x402138
xorl %ebx, %ebx
xorl %r14d, %r14d
movq -0x58(%rbp), %rax
movq (%rax,%rbx), %rdi
testq %rdi, %rdi
je 0x402129
callq 0x42af10
movq -0x58(%rbp), %rax
movq $0x0, (%rax,%rbx)
incq %r14
movl -0x3c(%rbp), %eax
addq $0x18, %rbx
cmpq %rax, %r14
jb 0x40210b
movq -0x58(%rbp), %rdi
testq %rdi, %rdi
je 0x40214e
callq 0x42af10
movq $0x0, -0x58(%rbp)
movq -0x50(%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