// File: /workspace/github_c_makefile_star2_O1/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 $0x448, %rsp            # imm = 0x448
movl %ecx, %ebx
movq %rdx, %r14
movl %esi, %ebp
movq %rdi, %r15
testl %esi, %esi
jns 0xb413
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
movq %r15, %rax
incl %ebp
cmpq $0x0, (%rax)
leaq 0x8(%rax), %rax
jne 0xb407
testl %ebx, %ebx
jns 0xb42b
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
movq %r14, %rax
incl %ebx
cmpq $0x0, (%rax)
leaq 0x8(%rax), %rax
jne 0xb41f
movl %ebp, %edi
movl $0x10, %esi
movq %rdi, 0x20(%rsp)
callq 0x32e0
movq %rax, %r13
movl %ebx, %r12d
movl $0x50, %esi
movq %r12, %rdi
callq 0x32e0
movq %rax, 0x8(%rsp)
movl %ebp, 0x18(%rsp)
testl %ebp, %ebp
je 0xb486
movq 0x20(%rsp), %rax
leaq (,%rax,8), %rax
xorl %ecx, %ecx
movq (%r15,%rcx), %rdx
movq %rdx, (%r13,%rcx,2)
movl $0xffffff, 0x8(%r13,%rcx,2) # imm = 0xFFFFFF
addq $0x8, %rcx
cmpq %rcx, %rax
jne 0xb46b
movq %r13, 0x28(%rsp)
testl %ebx, %ebx
je 0xb50c
movq 0x8(%rsp), %rax
addq $0x8, %rax
xorl %ecx, %ecx
movq (%r14,%rcx,8), %rdx
movq %rdx, (%rax)
movl %ecx, -0x8(%rax)
incq %rcx
addq $0x50, %rax
cmpq %rcx, %r12
jne 0xb49a
movq 0x8(%rsp), %rax
leaq 0x10(%rax), %r14
movq %r12, %rbp
movq -0x8(%r14), %rsi
movq %r14, %rdi
movl $0xa, %edx
callq 0x3290
testl %eax, %eax
je 0xb503
movl $0x400, %ecx            # imm = 0x400
movl %eax, %edi
movq %r14, %rsi
leaq 0x40(%rsp), %r13
movq %r13, %rdx
callq 0x3360
leaq 0x12d51(%rip), %rax     # 0x1e240
movq (%rax), %rdi
leaq 0xa49d(%rip), %rsi      # 0x15996
movq %r13, %rdx
xorl %eax, %eax
callq 0x3320
addq $0x50, %r14
decq %rbp
jne 0xb4bc
movq %r15, 0x30(%rsp)
cmpl $0x0, 0x18(%rsp)
movq 0x20(%rsp), %r15
je 0xb624
movq 0x8(%rsp), %rax
addq $0x8, %rax
movq %rax, 0x38(%rsp)
movq $0x0, 0x10(%rsp)
movl %ebx, 0x1c(%rsp)
testl %ebx, %ebx
je 0xb589
movq 0x10(%rsp), %rax
shlq $0x4, %rax
movq 0x28(%rsp), %rcx
leaq (%rcx,%rax), %rbx
movq (%rcx,%rax), %r14
xorl %ebp, %ebp
movq 0x38(%rsp), %r15
xorl %r13d, %r13d
movq (%r15), %rdi
movq %r14, %rsi
callq 0x3060
testl %eax, %eax
jne 0xb578
movl %ebp, 0x8(%rbx)
movl $0x1, %r13d
testl %r13d, %r13d
jne 0xb589
incq %rbp
addq $0x50, %r15
cmpq %r12, %rbp
jb 0xb560
movq 0x10(%rsp), %rcx
incq %rcx
movq %rcx, 0x10(%rsp)
cmpq 0x20(%rsp), %rcx
movl 0x1c(%rsp), %ebx
jne 0xb53c
movq 0x8(%rsp), %rax
addq $0x10, %rax
movq %rax, 0x10(%rsp)
xorl %ebp, %ebp
testl %ebx, %ebx
sete %al
movq %rbp, %r13
shlq $0x4, %r13
movq 0x28(%rsp), %rcx
cmpl $0xffffff, 0x8(%rcx,%r13) # imm = 0xFFFFFF
setne %cl
orb %al, %cl
jne 0xb613
addq 0x28(%rsp), %r13
xorl %ebx, %ebx
movq 0x10(%rsp), %r14
xorl %r15d, %r15d
movq (%r13), %rsi
movq %r14, %rdi
xorl %edx, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x33f0
testl %eax, %eax
jne 0xb602
movl %ebx, 0x8(%r13)
movl $0x1, %r15d
testl %r15d, %r15d
jne 0xb613
incq %rbx
addq $0x50, %r14
cmpq %r12, %rbx
jb 0xb5e1
incq %rbp
movq 0x20(%rsp), %r15
cmpq %r15, %rbp
movl 0x1c(%rsp), %ebx
jne 0xb5b1
leaq 0x7a(%rip), %rcx        # 0xb6a5
movl $0x10, %edx
movq 0x28(%rsp), %r14
movq %r14, %rdi
movq %r15, %rsi
callq 0x3100
cmpl $0x0, 0x18(%rsp)
movq 0x30(%rsp), %rdx
je 0xb663
shlq $0x3, %r15
xorl %eax, %eax
movq (%r14,%rax,2), %rcx
movq %rcx, (%rdx,%rax)
addq $0x8, %rax
cmpq %rax, %r15
jne 0xb652
testl %ebx, %ebx
je 0xb681
movq 0x8(%rsp), %rbx
addq $0x10, %rbx
movq %rbx, %rdi
callq 0x3190
addq $0x50, %rbx
decq %r12
jne 0xb670
movq %r14, %rdi
callq 0x3050
movq 0x8(%rsp), %rdi
callq 0x3050
addq $0x448, %rsp            # imm = 0x448
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq