// File: /workspace/github_cpp_makefile_star2_O3/chlunde[P]qtar/linux_getdents.cc
// Function: dir_read(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>&, std::vector<us_dirent, std::allocator<us_dirent>>&)
int
dir_read(std::string& path, std::vector<struct us_dirent>& dest)
{
	int fd = open(path.c_str(), O_DIRECTORY | O_RDONLY);
	int written;
	int offset = 0;

#define buf_size 4096
	char buf[buf_size];

	if (fd < 0) {
		err(EXIT_FAILURE, "open %s", path.c_str());
	}

	do {
		written = linux_getdents(fd, buf, buf_size);
		if (written < 0) {
			err(EXIT_FAILURE, "getdents %s", path.c_str());
		}

		offset = 0;

		while (offset < written) {
			struct linux_dirent *s = (struct linux_dirent*)(buf + offset);
			struct us_dirent d;
			char *type = buf + offset + s->d_reclen - 1;

			d.d_ino = s->d_ino;
			d.d_name = std::string(s->d_name);
			d.d_type = *type;
			d.parent = path;

			dest.push_back(d);
#if 0
			std::push_heap(dest.begin(), dest.end());
#endif

			offset += s->d_reclen;
		}
	} while (written > 0);

	close(fd);

	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1138, %rsp           # imm = 0x1138
movq %rsi, 0x20(%rsp)
movq %rdi, 0x10(%rsp)
movq (%rdi), %rdi
movl $0x10000, %esi          # imm = 0x10000
xorl %eax, %eax
callq 0x21d0
movl %eax, 0xc(%rsp)
testl %eax, %eax
js 0x6e3c
leaq 0x60(%rsp), %r15
leaq 0x80(%rsp), %r12
leaq 0x28(%rsp), %rbp
movl $0x4e, %edi
movl 0xc(%rsp), %esi
leaq 0x130(%rsp), %rdx
movl $0x1000, %ecx           # imm = 0x1000
xorl %eax, %eax
callq 0x2080
movq %rax, 0x18(%rsp)
testl %eax, %eax
js 0x6e21
je 0x6e04
xorl %ebx, %ebx
movl %ebx, %eax
leaq (%rsp,%rax), %r14
addq $0x130, %r14            # imm = 0x130
movq %r15, 0x50(%rsp)
xorl %ecx, %ecx
movq %rcx, 0x58(%rsp)
movb %cl, 0x60(%rsp)
movq %r12, 0x70(%rsp)
movq %rcx, 0x78(%rsp)
movb %cl, 0x80(%rsp)
movb %cl, 0x90(%rsp)
movb %cl, 0x128(%rsp)
leaq 0x142(%rsp,%rax), %r12
movzwl -0x2(%r12), %r13d
movq -0x12(%r12), %rax
movq %rax, 0x48(%rsp)
leaq 0x38(%rsp), %r15
movq %r15, 0x28(%rsp)
movq %r12, %rdi
callq 0x2130
leaq (%rax,%r14), %rdx
addq $0x12, %rdx
movq %rbp, %rdi
movq %r12, %rsi
callq 0x3342
leaq 0x50(%rsp), %rdi
movq %rbp, %rsi
callq 0x22b0
movq 0x28(%rsp), %rdi
cmpq %r15, %rdi
je 0x6d7f
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x2250
movb -0x1(%r14,%r13), %al
movb %al, 0x90(%rsp)
leaq 0x70(%rsp), %rdi
movq 0x10(%rsp), %rsi
callq 0x2170
movq 0x20(%rsp), %rdi
leaq 0x48(%rsp), %rsi
callq 0x6e64
movzwl 0x10(%r14), %r14d
movq 0x70(%rsp), %rdi
leaq 0x80(%rsp), %r12
cmpq %r12, %rdi
je 0x6dd0
movq 0x80(%rsp), %rsi
incq %rsi
callq 0x2250
movq 0x50(%rsp), %rdi
leaq 0x60(%rsp), %r15
cmpq %r15, %rdi
je 0x6dec
movq 0x60(%rsp), %rsi
incq %rsi
callq 0x2250
addl %r14d, %ebx
cmpl 0x18(%rsp), %ebx
jl 0x6ce2
cmpl $0x0, 0x18(%rsp)
jne 0x6cb0
movl 0xc(%rsp), %edi
callq 0x2410
xorl %eax, %eax
addq $0x1138, %rsp           # imm = 0x1138
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x10(%rsp), %rax
movq (%rax), %rdx
leaq 0x12aa(%rip), %rsi      # 0x80da
movl $0x1, %edi
xorl %eax, %eax
callq 0x2220
movq 0x10(%rsp), %rax
movq (%rax), %rdx
leaq 0x1287(%rip), %rsi      # 0x80d2
jmp 0x6e30
jmp 0x6e4f
movq %rax, %rbx
leaq 0x48(%rsp), %rdi
callq 0x31ac
movq %rbx, %rdi
callq 0x2390