// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x450, %rsp            # imm = 0x450
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movq %rdx, -0x18(%rbp)
movl %ecx, -0x1c(%rbp)
cmpl $0x0, -0xc(%rbp)
jge 0xe7c4
movl $0x0, -0xc(%rbp)
movq -0x8(%rbp), %rax
movslq -0xc(%rbp), %rcx
cmpq $0x0, (%rax,%rcx,8)
je 0xe7c2
jmp 0xe7b7
movl -0xc(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xc(%rbp)
jmp 0xe7a6
jmp 0xe7c4
cmpl $0x0, -0x1c(%rbp)
jge 0xe7ef
movl $0x0, -0x1c(%rbp)
movq -0x18(%rbp), %rax
movslq -0x1c(%rbp), %rcx
cmpq $0x0, (%rax,%rcx,8)
je 0xe7ed
jmp 0xe7e2
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
jmp 0xe7d1
jmp 0xe7ef
movslq -0xc(%rbp), %rdi
movl $0x10, %esi
callq 0x32c0
movq %rax, -0x28(%rbp)
movslq -0x1c(%rbp), %rdi
movl $0x50, %esi
callq 0x32c0
movq %rax, -0x30(%rbp)
movl $0x0, -0x34(%rbp)
movl -0x34(%rbp), %eax
cmpl -0xc(%rbp), %eax
jge 0xe861
movq -0x8(%rbp), %rax
movslq -0x34(%rbp), %rcx
movq (%rax,%rcx,8), %rcx
movq -0x28(%rbp), %rax
movslq -0x34(%rbp), %rdx
shlq $0x4, %rdx
addq %rdx, %rax
movq %rcx, (%rax)
movq -0x28(%rbp), %rax
movslq -0x34(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movl $0xffffff, 0x8(%rax)    # imm = 0xFFFFFF
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0xe81a
movl $0x0, -0x34(%rbp)
movl -0x34(%rbp), %eax
cmpl -0x1c(%rbp), %eax
jge 0xe8ae
movq -0x18(%rbp), %rax
movslq -0x34(%rbp), %rcx
movq (%rax,%rcx,8), %rcx
movq -0x30(%rbp), %rax
movslq -0x34(%rbp), %rdx
imulq $0x50, %rdx, %rdx
addq %rdx, %rax
movq %rcx, 0x8(%rax)
movl -0x34(%rbp), %ecx
movq -0x30(%rbp), %rax
movslq -0x34(%rbp), %rdx
imulq $0x50, %rdx, %rdx
addq %rdx, %rax
movl %ecx, (%rax)
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0xe868
movl $0x0, -0x34(%rbp)
movl -0x34(%rbp), %eax
cmpl -0x1c(%rbp), %eax
jge 0xe950
movq -0x30(%rbp), %rdi
movslq -0x34(%rbp), %rax
imulq $0x50, %rax, %rax
addq %rax, %rdi
addq $0x10, %rdi
movq -0x30(%rbp), %rax
movslq -0x34(%rbp), %rcx
imulq $0x50, %rcx, %rcx
addq %rcx, %rax
movq 0x8(%rax), %rsi
movl $0xa, %edx
callq 0x3280
movl %eax, -0x3c(%rbp)
cmpl $0x0, -0x3c(%rbp)
je 0xe940
movl -0x3c(%rbp), %edi
movq -0x30(%rbp), %rsi
movslq -0x34(%rbp), %rax
imulq $0x50, %rax, %rax
addq %rax, %rsi
addq $0x10, %rsi
leaq -0x440(%rbp), %rdx
movl $0x400, %ecx            # imm = 0x400
callq 0x3340
leaq 0x16c08(%rip), %rax     # 0x25530
movq (%rax), %rdi
leaq -0x440(%rbp), %rdx
leaq 0xdf87(%rip), %rsi      # 0x1c8c0
movb $0x0, %al
callq 0x3300
jmp 0xe942
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0xe8b5
movl $0x0, -0x34(%rbp)
movl -0x34(%rbp), %eax
cmpl -0xc(%rbp), %eax
jge 0xea0e
movl $0x0, -0x38(%rbp)
movl $0x0, -0x444(%rbp)
xorl %eax, %eax
cmpl $0x0, -0x444(%rbp)
movb %al, -0x445(%rbp)
jne 0xe994
movl -0x38(%rbp), %eax
cmpl -0x1c(%rbp), %eax
setl %al
movb %al, -0x445(%rbp)
movb -0x445(%rbp), %al
testb $0x1, %al
jne 0xe9a0
jmp 0xe9fe
movq -0x30(%rbp), %rax
movslq -0x38(%rbp), %rcx
imulq $0x50, %rcx, %rcx
addq %rcx, %rax
movq 0x8(%rax), %rdi
movq -0x28(%rbp), %rax
movslq -0x34(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq (%rax), %rsi
callq 0x3050
cmpl $0x0, %eax
jne 0xe9ee
movl -0x38(%rbp), %ecx
movq -0x28(%rbp), %rax
movslq -0x34(%rbp), %rdx
shlq $0x4, %rdx
addq %rdx, %rax
movl %ecx, 0x8(%rax)
movl $0x1, -0x444(%rbp)
jmp 0xe9f0
movl -0x38(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x38(%rbp)
jmp 0xe974
jmp 0xea00
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0xe957
movl $0x0, -0x34(%rbp)
movl -0x34(%rbp), %eax
cmpl -0xc(%rbp), %eax
jge 0xeaf4
movq -0x28(%rbp), %rax
movslq -0x34(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
cmpl $0xffffff, 0x8(%rax)    # imm = 0xFFFFFF
jne 0xeae4
movl $0x0, -0x38(%rbp)
movl $0x0, -0x444(%rbp)
xorl %eax, %eax
cmpl $0x0, -0x444(%rbp)
movb %al, -0x446(%rbp)
jne 0xea6e
movl -0x38(%rbp), %eax
cmpl -0x1c(%rbp), %eax
setl %al
movb %al, -0x446(%rbp)
movb -0x446(%rbp), %al
testb $0x1, %al
jne 0xea7a
jmp 0xeae2
movq -0x30(%rbp), %rdi
movslq -0x38(%rbp), %rax
imulq $0x50, %rax, %rax
addq %rax, %rdi
addq $0x10, %rdi
movq -0x28(%rbp), %rax
movslq -0x34(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq (%rax), %rsi
xorl %eax, %eax
movl %eax, %ecx
xorl %r8d, %r8d
movq %rcx, %rdx
callq 0x33d0
cmpl $0x0, %eax
jne 0xead2
movl -0x38(%rbp), %ecx
movq -0x28(%rbp), %rax
movslq -0x34(%rbp), %rdx
shlq $0x4, %rdx
addq %rdx, %rax
movl %ecx, 0x8(%rax)
movl $0x1, -0x444(%rbp)
jmp 0xead4
movl -0x38(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x38(%rbp)
jmp 0xea4e
jmp 0xeae4
jmp 0xeae6
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0xea15
movq -0x28(%rbp), %rdi
movslq -0xc(%rbp), %rsi
movl $0x10, %edx
leaq 0x98(%rip), %rcx        # 0xeba0
callq 0x30f0
movl $0x0, -0x34(%rbp)
movl -0x34(%rbp), %eax
cmpl -0xc(%rbp), %eax
jge 0xeb45
movq -0x28(%rbp), %rax
movslq -0x34(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq (%rax), %rdx
movq -0x8(%rbp), %rax
movslq -0x34(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0xeb14
movl $0x0, -0x34(%rbp)
movl -0x34(%rbp), %eax
cmpl -0x1c(%rbp), %eax
jge 0xeb77
movq -0x30(%rbp), %rdi
movslq -0x34(%rbp), %rax
imulq $0x50, %rax, %rax
addq %rax, %rdi
addq $0x10, %rdi
callq 0x3180
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0xeb4c
movq -0x28(%rbp), %rdi
callq 0x3040
movq -0x30(%rbp), %rdi
callq 0x3040
addq $0x450, %rsp            # imm = 0x450
popq %rbp
retq
nopw %cs:(%rax,%rax)