// File: /workspace/github_c_makefile_star2_O3/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, %r12
movl %esi, %ebp
movq %rdi, %rbx
testl %esi, %esi
jns 0xb3a8
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
movq %rbx, %rax
incl %ebp
cmpq $0x0, (%rax)
leaq 0x8(%rax), %rax
jne 0xb39c
testl %r15d, %r15d
jns 0xb3c3
movl $0xffffffff, %r15d      # imm = 0xFFFFFFFF
movq %r12, %rax
incl %r15d
cmpq $0x0, (%rax)
leaq 0x8(%rax), %rax
jne 0xb3b6
movl %ebp, %r13d
movl $0x10, %esi
movq %r13, %rdi
callq 0x32d0
movq %rax, %r14
movl %r15d, %r15d
movl $0x50, %esi
movq %r15, %rdi
callq 0x32d0
movq %rax, (%rsp)
testl %ebp, %ebp
je 0xb412
leaq (,%r13,8), %rax
xorl %ecx, %ecx
movq (%rbx,%rcx), %rdx
movq %rdx, (%r14,%rcx,2)
movl $0xffffff, 0x8(%r14,%rcx,2) # imm = 0xFFFFFF
addq $0x8, %rcx
cmpq %rcx, %rax
jne 0xb3f8
movq %r13, 0x18(%rsp)
movq %rbx, 0x28(%rsp)
testq %r15, %r15
je 0xb4a6
movq (%rsp), %rax
addq $0x8, %rax
xorl %ecx, %ecx
movq (%r12,%rcx,8), %rdx
movq %rdx, (%rax)
movl %ecx, -0x8(%rax)
incq %rcx
addq $0x50, %rax
cmpq %rcx, %r15
jne 0xb42f
testl %r15d, %r15d
je 0xb4a6
movq (%rsp), %rax
leaq 0x10(%rax), %r12
leaq 0x30(%rsp), %rbx
movq %r15, %r13
movq -0x8(%r12), %rsi
movq %r12, %rdi
movl $0xa, %edx
callq 0x3280
testl %eax, %eax
je 0xb49d
movl $0x400, %ecx            # imm = 0x400
movl %eax, %edi
movq %r12, %rsi
movq %rbx, %rdx
callq 0x3350
leaq 0x12da7(%rip), %rax     # 0x1e230
movq (%rax), %rdi
leaq 0xa567(%rip), %rsi      # 0x159fa
movq %rbx, %rdx
xorl %eax, %eax
callq 0x3310
addq $0x50, %r12
decq %r13
jne 0xb45a
testl %ebp, %ebp
je 0xb5d4
movl %ebp, 0xc(%rsp)
movq %r14, 0x10(%rsp)
movq (%rsp), %rax
addq $0x8, %rax
movq %rax, 0x20(%rsp)
xorl %r13d, %r13d
testl %r15d, %r15d
je 0xb50a
movq %r13, %rax
shlq $0x4, %rax
movq 0x10(%rsp), %rcx
leaq (%rcx,%rax), %rbp
movq (%rcx,%rax), %rbx
movq 0x20(%rsp), %r12
xorl %r14d, %r14d
movq (%r12), %rdi
movq %rbx, %rsi
callq 0x3060
testl %eax, %eax
je 0xb506
incq %r14
addq $0x50, %r12
cmpq %r15, %r14
jb 0xb4e8
jmp 0xb50a
movl %r14d, 0x8(%rbp)
incq %r13
cmpq 0x18(%rsp), %r13
jne 0xb4c7
cmpl $0x0, 0xc(%rsp)
movq 0x10(%rsp), %r14
je 0xb5d4
movq (%rsp), %rax
addq $0x10, %rax
movq %rax, 0x20(%rsp)
xorl %r13d, %r13d
movq %r13, %rbp
shlq $0x4, %rbp
cmpl $0xffffff, 0x8(%r14,%rbp) # imm = 0xFFFFFF
jne 0xb584
testl %r15d, %r15d
je 0xb584
addq 0x10(%rsp), %rbp
movq (%rbp), %rbx
movq 0x20(%rsp), %r12
xorl %r14d, %r14d
movq %r12, %rdi
movq %rbx, %rsi
xorl %edx, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x33e0
testl %eax, %eax
je 0xb580
incq %r14
addq $0x50, %r12
cmpq %r15, %r14
jb 0xb55c
jmp 0xb584
movl %r14d, 0x8(%rbp)
incq %r13
cmpq 0x18(%rsp), %r13
movq 0x10(%rsp), %r14
jne 0xb534
leaq 0x94(%rip), %rcx        # 0xb62e
movl $0x10, %edx
movq %r14, %rdi
movq 0x18(%rsp), %rbx
movq %rbx, %rsi
callq 0x30f0
cmpl $0x0, 0xc(%rsp)
movq 0x28(%rsp), %rdx
je 0xb5ed
shlq $0x3, %rbx
xorl %eax, %eax
movq (%r14,%rax,2), %rcx
movq %rcx, (%rdx,%rax)
addq $0x8, %rax
cmpq %rax, %rbx
jne 0xb5c1
jmp 0xb5ed
leaq 0x53(%rip), %rcx        # 0xb62e
movl $0x10, %edx
movq %r14, %rdi
movq 0x18(%rsp), %rsi
callq 0x30f0
testl %r15d, %r15d
je 0xb60b
movq (%rsp), %rbx
addq $0x10, %rbx
movq %rbx, %rdi
callq 0x3180
addq $0x50, %rbx
decq %r15
jne 0xb5fa
movq %r14, %rdi
callq 0x3050
movq (%rsp), %rdi
callq 0x3050
addq $0x438, %rsp            # imm = 0x438
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq