// File: /workspace/github_c_makefile_star2_O0/archer1357[P]Offline-Raytracer/tinydir.c
// Function: tinydir_open_sorted
_TINYDIR_FUNC
int tinydir_open_sorted(tinydir_dir *dir, const char *path)
{
	/* Count the number of files first, to pre-allocate the files array */
	size_t n_files = 0;
	if (tinydir_open(dir, path) == -1)
	{
		return -1;
	}
	while (dir->has_next)
	{
		n_files++;
		if (tinydir_next(dir) == -1)
		{
			goto bail;
		}
	}
	tinydir_close(dir);

	if (tinydir_open(dir, path) == -1)
	{
		return -1;
	}

	dir->n_files = 0;
	dir->_files = (tinydir_file *)malloc(sizeof *dir->_files * n_files);
	if (dir->_files == NULL)
	{
		errno = ENOMEM;
		goto bail;
	}
	while (dir->has_next)
	{
		tinydir_file *p_file;
		dir->n_files++;

		p_file = &dir->_files[dir->n_files - 1];
		if (tinydir_readfile(dir, p_file) == -1)
		{
			goto bail;
		}

		if (tinydir_next(dir) == -1)
		{
			goto bail;
		}

		/* Just in case the number of files has changed between the first and
		second reads, terminate without writing into unallocated memory */
		if (dir->n_files == n_files)
		{
			break;
		}
	}

	qsort(dir->_files, dir->n_files, sizeof(tinydir_file), _tinydir_file_cmp);

	return 0;

bail:
	tinydir_close(dir);
	return -1;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x30, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq $0x0, -0x20(%rbp)
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x421240
cmpl $-0x1, %eax
jne 0x421476
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x4215de
jmp 0x421478
movq -0x10(%rbp), %rax
cmpl $0x0, 0x1000(%rax)
je 0x4214a6
movq -0x20(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x20(%rbp)
movq -0x10(%rbp), %rdi
callq 0x4215f0
cmpl $-0x1, %eax
jne 0x4214a4
jmp 0x4215ce
jmp 0x421478
movq -0x10(%rbp), %rdi
callq 0x421380
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x421240
cmpl $-0x1, %eax
jne 0x4214cd
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x4215de
movq -0x10(%rbp), %rax
movq $0x0, 0x1008(%rax)
imulq $0x11a0, -0x20(%rbp), %rdi # imm = 0x11A0
callq 0x452d20
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x1010(%rax)
movq -0x10(%rbp), %rax
cmpq $0x0, 0x1010(%rax)
jne 0x421515
callq 0x42de60
movl $0xc, (%rax)
jmp 0x4215ce
jmp 0x421517
movq -0x10(%rbp), %rax
cmpl $0x0, 0x1000(%rax)
je 0x42159e
movq -0x10(%rbp), %rax
movq 0x1008(%rax), %rcx
addq $0x1, %rcx
movq %rcx, 0x1008(%rax)
movq -0x10(%rbp), %rax
movq 0x1010(%rax), %rax
movq -0x10(%rbp), %rcx
movq 0x1008(%rcx), %rcx
subq $0x1, %rcx
imulq $0x11a0, %rcx, %rcx     # imm = 0x11A0
addq %rcx, %rax
movq %rax, -0x28(%rbp)
movq -0x10(%rbp), %rdi
movq -0x28(%rbp), %rsi
callq 0x421690
cmpl $-0x1, %eax
jne 0x421576
jmp 0x4215ce
movq -0x10(%rbp), %rdi
callq 0x4215f0
cmpl $-0x1, %eax
jne 0x421586
jmp 0x4215ce
movq -0x10(%rbp), %rax
movq 0x1008(%rax), %rax
cmpq -0x20(%rbp), %rax
jne 0x421599
jmp 0x42159e
jmp 0x421517
movq -0x10(%rbp), %rax
movq 0x1010(%rax), %rdi
movq -0x10(%rbp), %rax
movq 0x1008(%rax), %rsi
movl $0x11a0, %edx           # imm = 0x11A0
leaq 0x290(%rip), %rcx       # 0x421850
callq 0x4343b0
movl $0x0, -0x4(%rbp)
jmp 0x4215de
movq -0x10(%rbp), %rdi
callq 0x421380
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
movl -0x4(%rbp), %eax
addq $0x30, %rsp
popq %rbp
retq
nopw (%rax,%rax)