// File: /workspace/github_cpp_makefile_star2_O1/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, 0x18(%rsp)
movq %rdi, 0x8(%rsp)
movq (%rdi), %rdi
movl $0x10000, %esi          # imm = 0x10000
xorl %eax, %eax
callq 0x21d0
testl %eax, %eax
js 0x6eff
movl %eax, %ebx
leaq 0x130(%rsp), %rdx
movl $0x4e, %edi
movl %eax, %esi
movl $0x1000, %ecx           # imm = 0x1000
xorl %eax, %eax
callq 0x2080
testl %eax, %eax
js 0x6ec9
leaq 0x60(%rsp), %r15
leaq 0x38(%rsp), %rdx
movl %ebx, 0x14(%rsp)
testl %eax, %eax
je 0x6ee4
xorl %ebx, %ebx
movq %rax, 0x20(%rsp)
movl %ebx, %eax
leaq (%rsp,%rax), %r14
addq $0x130, %r14            # imm = 0x130
movq %r15, 0x50(%rsp)
xorl %esi, %esi
movq %rsi, 0x58(%rsp)
movb %sil, 0x60(%rsp)
leaq 0x80(%rsp), %rcx
movq %rcx, 0x70(%rsp)
movq %rsi, 0x78(%rsp)
movb %sil, 0x80(%rsp)
movb %sil, 0x90(%rsp)
movb %sil, 0x128(%rsp)
leaq 0x142(%rsp,%rax), %r12
movzwl -0x2(%r12), %ebp
movq -0x12(%r12), %rax
movq %rax, 0x48(%rsp)
movq %rdx, 0x28(%rsp)
movq %r12, %rdi
movq %rdx, %r15
callq 0x2130
leaq (%rax,%r14), %rdx
addq $0x12, %rdx
leaq 0x28(%rsp), %r13
movq %r13, %rdi
movq %r12, %rsi
callq 0x3334
leaq 0x50(%rsp), %rdi
movq %r13, %rsi
callq 0x22b0
movq 0x28(%rsp), %rdi
cmpq %r15, %rdi
je 0x6e1c
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x2250
movb -0x1(%r14,%rbp), %al
movb %al, 0x90(%rsp)
leaq 0x70(%rsp), %rdi
movq 0x8(%rsp), %rsi
callq 0x2170
leaq 0x60(%rsp), %r15
movq 0x18(%rsp), %rdi
leaq 0x48(%rsp), %rsi
callq 0x6f28
movzwl 0x10(%r14), %ebp
movq 0x70(%rsp), %rdi
leaq 0x80(%rsp), %rax
cmpq %rax, %rdi
je 0x6e72
movq 0x80(%rsp), %rsi
incq %rsi
callq 0x2250
movq 0x50(%rsp), %rdi
cmpq %r15, %rdi
je 0x6e89
movq 0x60(%rsp), %rsi
incq %rsi
callq 0x2250
addl %ebp, %ebx
movq 0x20(%rsp), %rax
cmpl %eax, %ebx
leaq 0x38(%rsp), %rdx
jb 0x6d70
movl $0x4e, %edi
movl 0x14(%rsp), %ebx
movl %ebx, %esi
leaq 0x130(%rsp), %rdx
movl $0x1000, %ecx           # imm = 0x1000
xorl %eax, %eax
callq 0x2080
leaq 0x38(%rsp), %rdx
testl %eax, %eax
jns 0x6d61
movq 0x8(%rsp), %rax
movq (%rax), %rdx
leaq 0x1202(%rip), %rsi      # 0x80da
movl $0x1, %edi
xorl %eax, %eax
callq 0x2220
movl %ebx, %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 0x8(%rsp), %rax
movq (%rax), %rdx
leaq 0x11c4(%rip), %rsi      # 0x80d2
jmp 0x6ed8
jmp 0x6f12
movq %rax, %rbx
leaq 0x48(%rsp), %rdi
callq 0x31ac
movq %rbx, %rdi
callq 0x2390
nop