// File: /workspace/github_c_makefile_star2_O2/j-hoppe[P]tu58fs/filesort.c
// Function: filename_sort
void filename_sort(char **name, int name_count, char **group, int group_count) {
	sort_name_entry_t *_name;
	sort_group_regex_t *_group;
	int i, j;
	int err;
	char errbuff[1024];
	int match;

	// if name/group_size undefined: search for terminating NULL
	if (name_count < 0)
		for (name_count = 0; name[name_count]; name_count++)
			;
	if (group_count < 0)
		for (group_count = 0; group[group_count]; group_count++)
			;

	// allocate internal lists. internals have prefix "_"
	_name = calloc(name_count, sizeof(sort_name_entry_t));
	_group = calloc(group_count, sizeof(sort_group_regex_t));

	// init lists with params
	for (i = 0; i < name_count; i++) {
		_name[i].name = name[i];
		_name[i].group = NOGROUP;
	}

	for (i = 0; i < group_count; i++) {
		_group[i].pattern = group[i];
		_group[i].group = i; // enumerate
	}

	// compile all regex. case is ignored
	for (i = 0; i < group_count; i++) {
		err = regcomp(&_group[i].reg, _group[i].pattern, REG_ICASE | REG_NOSUB);
		if (err) {
			regerror(err, &_group[i].reg, errbuff, sizeof(errbuff));
			fprintf(ferr, "Error compiling regex: %s", errbuff);
		}
	}

	// 1) match each name against the exact patten, no regex
	for (i = 0; i < name_count; i++)
		for (match = j = 0; !match && j < group_count; j++)
			if (!strcasecmp(_group[j].pattern, _name[i].name)) {
				_name[i].group = j;
				match = 1;
			}

	// 2) match each name against the regexes, first match defines group
	for (i = 0; i < name_count; i++)
		if (_name[i].group == NOGROUP) // refine, not reDEfine
			for (match = j = 0; !match && j < group_count; j++) {
				if (regexec(&_group[j].reg, _name[i].name, 0, NULL, 0) == 0) {
					_name[i].group = j;
					match = 1;
				}
			}

	// sort by group and name
	qsort(_name, name_count, sizeof(sort_name_entry_t), name_compare);

	// result back into input name list
	for (i = 0; i < name_count; i++)
		name[i] = _name[i].name;

	/// cleanup
	for (i = 0; i < group_count; i++)
		regfree(&_group[i].reg);
	free(_name);
	free(_group);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x438, %rsp            # imm = 0x438
movl %ecx, %r15d
movq %rdx, %rbp
movq %rdi, %rbx
testl %esi, %esi
jns 0xa9fc
pushq $-0x1
popq %rsi
movq %rbx, %rax
incl %esi
cmpq $0x0, (%rax)
leaq 0x8(%rax), %rax
jne 0xa9f0
testl %r15d, %r15d
jns 0xaa15
pushq $-0x1
popq %r15
movq %rbp, %rax
incl %r15d
cmpq $0x0, (%rax)
leaq 0x8(%rax), %rax
jne 0xaa08
movl %esi, %r14d
pushq $0x10
popq %rsi
movq %r14, %rdi
callq 0x32d0
movq %rax, (%rsp)
movl %r15d, %r15d
pushq $0x50
popq %rsi
movq %r15, %rdi
callq 0x32d0
movq %rax, 0x8(%rsp)
movq %r14, 0x18(%rsp)
leaq (,%r14,8), %rsi
xorl %eax, %eax
movq (%rsp), %rdx
cmpq %rax, %rsi
je 0xaa68
movq (%rbx,%rax), %rcx
movq %rcx, (%rdx,%rax,2)
movl $0xffffff, 0x8(%rdx,%rax,2) # imm = 0xFFFFFF
addq $0x8, %rax
jmp 0xaa4d
movq 0x8(%rsp), %rax
addq $0x8, %rax
xorl %ecx, %ecx
cmpq %rcx, %r15
je 0xaa8c
movq (%rbp,%rcx,8), %rdx
movq %rdx, (%rax)
movl %ecx, -0x8(%rax)
incq %rcx
addq $0x50, %rax
jmp 0xaa73
movq %rsi, 0x20(%rsp)
movq %rbx, 0x28(%rsp)
movq 0x8(%rsp), %rax
leaq 0x10(%rax), %rbp
pushq $0xa
popq %r14
leaq 0x30(%rsp), %rbx
leaq 0x13781(%rip), %r12     # 0x1e230
movq %r15, %r13
subq $0x1, %r13
jb 0xaaf8
movq -0x8(%rbp), %rsi
movq %rbp, %rdi
movl %r14d, %edx
callq 0x3280
testl %eax, %eax
je 0xaaf2
movl $0x400, %ecx            # imm = 0x400
movl %eax, %edi
movq %rbp, %rsi
movq %rbx, %rdx
callq 0x3350
movq (%r12), %rdi
leaq 0x9ef9(%rip), %rsi      # 0x149e1
movq %rbx, %rdx
xorl %eax, %eax
callq 0x3310
addq $0x50, %rbp
jmp 0xaab2
movq 0x8(%rsp), %rax
addq $0x8, %rax
movq %rax, 0x10(%rsp)
xorl %r12d, %r12d
pushq $0x1
popq %rbp
cmpq 0x18(%rsp), %r12
je 0xab57
movq %r12, %r13
shlq $0x4, %r13
addq (%rsp), %r13
xorl %r14d, %r14d
movq 0x10(%rsp), %rbx
xorl %ecx, %ecx
testl %ecx, %ecx
jne 0xab52
cmpq %r15, %r14
jae 0xab52
movq (%rbx), %rdi
movq (%r13), %rsi
callq 0x3060
xorl %ecx, %ecx
testl %eax, %eax
jne 0xab49
movl %r14d, 0x8(%r13)
movl %ebp, %ecx
incq %r14
addq $0x50, %rbx
jmp 0xab28
incq %r12
jmp 0xab0c
movq 0x8(%rsp), %rax
addq $0x10, %rax
movq %rax, 0x10(%rsp)
xorl %r13d, %r13d
cmpq 0x18(%rsp), %r13
je 0xabcf
movq %r13, %rbp
shlq $0x4, %rbp
movq (%rsp), %rax
cmpl $0xffffff, 0x8(%rax,%rbp) # imm = 0xFFFFFF
jne 0xabca
addq (%rsp), %rbp
xorl %r14d, %r14d
movq 0x10(%rsp), %rbx
xorl %r12d, %r12d
testl %r12d, %r12d
jne 0xabca
cmpq %r15, %r14
jae 0xabca
movq (%rbp), %rsi
xorl %r12d, %r12d
movq %rbx, %rdi
xorl %edx, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x33e0
testl %eax, %eax
jne 0xabc1
movl %r14d, 0x8(%rbp)
pushq $0x1
popq %rax
movl %eax, %r12d
incq %r14
addq $0x50, %rbx
jmp 0xab93
incq %r13
jmp 0xab68
leaq 0x76(%rip), %rcx        # 0xac4c
pushq $0x10
popq %rdx
movq (%rsp), %rdi
movq 0x18(%rsp), %rsi
callq 0x30f0
xorl %eax, %eax
movq 0x28(%rsp), %rdx
movq 0x20(%rsp), %rsi
cmpq %rax, %rsi
je 0xac0a
movq (%rsp), %rcx
movq (%rcx,%rax,2), %rcx
movq %rcx, (%rdx,%rax)
addq $0x8, %rax
jmp 0xabf3
movq 0x8(%rsp), %rbx
addq $0x10, %rbx
subq $0x1, %r15
jb 0xac27
movq %rbx, %rdi
callq 0x3180
addq $0x50, %rbx
jmp 0xac13
movq (%rsp), %rdi
callq 0x3050
movq 0x8(%rsp), %rdi
callq 0x3050
addq $0x438, %rsp            # imm = 0x438
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq