#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */

/* Type definitions */
struct dirent {
  char *d_name;
  unsigned long long d_ino;
  unsigned int d_type;
};
typedef int /*<<< orphan*/ DIR;

/* Variables and functions */
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ seekdir(int /*<<< orphan*/ *, unsigned long long);
int strlen(char *);
unsigned long long telldir(int /*<<< orphan*/ *);
char *read_dir(void *stream, unsigned long long *pos,
               unsigned long long *ino_out, int *len_out,
               unsigned int *type_out) {
  DIR *dir = stream;
  struct dirent *ent;
  seekdir(dir, *pos);
  ent = readdir(dir);
  if (ent == NULL)
    return NULL;
  *len_out = strlen(ent->d_name);
  *ino_out = ent->d_ino;
  *type_out = ent->d_type;
  *pos = telldir(dir);
  return ent->d_name;
}


x86 assembly (clang, -O0):
<read_dir>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    %r8,-0x30(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rdi
mov    -0x18(%rbp),%rax
mov    (%rax),%rsi
call   1050 <seekdir@plt>
mov    -0x38(%rbp),%rdi
call   1040 <readdir@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
jne    1199 <read_dir+0x59>
movq   $0x0,-0x8(%rbp)
jmp    11e7 <read_dir+0xa7>
mov    -0x40(%rbp),%rax
mov    (%rax),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x28(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x40(%rbp),%rax
mov    0x8(%rax),%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x40(%rbp),%rax
mov    0x10(%rax),%ecx
mov    -0x30(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x38(%rbp),%rdi
call   1060 <telldir@plt>
mov    %rax,%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x40(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_dir>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %r8,%r14
mov    %rcx,%r13
mov    %rdx,%r12
mov    %rsi,%r15
mov    %rdi,%rbx
mov    (%rsi),%rsi
call   1050 <seekdir@plt>
mov    %rbx,%rdi
call   1040 <readdir@plt>
test   %rax,%rax
je     119d <read_dir+0x5d>
mov    %rax,%rbp
mov    (%rax),%rdi
call   1030 <strlen@plt>
mov    %eax,0x0(%r13)
mov    0x8(%rbp),%rax
mov    %rax,(%r12)
mov    0x10(%rbp),%eax
mov    %eax,(%r14)
mov    %rbx,%rdi
call   1060 <telldir@plt>
mov    %rax,(%r15)
mov    0x0(%rbp),%rax
jmp    119f <read_dir+0x5f>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.dirent = type { i8*, i64, i32 }
define i8* @read_dir(i8* %0, i64* %1, i64* %2, i32* %3, i32* %4) {
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i64*
%9 = alloca i64*
%10 = alloca i32*
%11 = alloca i32*
%12 = alloca i32*
%13 = alloca %struct.dirent*
store i8* %0, i8** %7
store i64* %1, i64** %8
store i64* %2, i64** %9
store i32* %3, i32** %10
store i32* %4, i32** %11
%14 = load i8*, i8** %7
%15 = bitcast i8* %14 to i32*
store i32* %15, i32** %12
%16 = load i32*, i32** %12
%17 = load i64*, i64** %8
%18 = load i64, i64* %17
%19 = call i32 @seekdir(i32* %16, i64 %18)
%20 = load i32*, i32** %12
%21 = call %struct.dirent* @readdir(i32* %20)
store %struct.dirent* %21, %struct.dirent** %13
%22 = load %struct.dirent*, %struct.dirent** %13
%23 = icmp eq %struct.dirent* %22, null
br i1 %23, label %24, label %25
24: ; preds = %5
store i8* null, i8** %6
br label %45
25: ; preds = %5
%26 = load %struct.dirent*, %struct.dirent** %13
%27 = getelementptr inbounds %struct.dirent, %struct.dirent* %26, i32 0, i32 0
%28 = load i8*, i8** %27
%29 = call i32 @strlen(i8* %28)
%30 = load i32*, i32** %10
store i32 %29, i32* %30
%31 = load %struct.dirent*, %struct.dirent** %13
%32 = getelementptr inbounds %struct.dirent, %struct.dirent* %31, i32 0, i32 1
%33 = load i64, i64* %32
%34 = load i64*, i64** %9
store i64 %33, i64* %34
%35 = load %struct.dirent*, %struct.dirent** %13
%36 = getelementptr inbounds %struct.dirent, %struct.dirent* %35, i32 0, i32 2
%37 = load i32, i32* %36
%38 = load i32*, i32** %11
store i32 %37, i32* %38
%39 = load i32*, i32** %12
%40 = call i64 @telldir(i32* %39)
%41 = load i64*, i64** %8
store i64 %40, i64* %41
%42 = load %struct.dirent*, %struct.dirent** %13
%43 = getelementptr inbounds %struct.dirent, %struct.dirent* %42, i32 0, i32 0
%44 = load i8*, i8** %43
store i8* %44, i8** %6
br label %45
45: ; preds = %25, %24
%46 = load i8*, i8** %6
ret i8* %46
}

LLVM IR (-O2):
%struct.dirent = type { i8*, i64, i32 }
define i8* @read_dir(i8* %0, i64* nocapture %1, i64* nocapture writeonly %2, i32* nocapture writeonly %3, i32* nocapture writeonly %4) {
%6 = bitcast i8* %0 to i32*
%7 = load i64, i64* %1
%8 = tail call i32 @seekdir(i32* %6, i64 %7)
%9 = tail call %struct.dirent* @readdir(i32* %6)
%10 = icmp eq %struct.dirent* %9, null
br i1 %10, label %21, label %11
11: ; preds = %5
%12 = getelementptr inbounds %struct.dirent, %struct.dirent* %9, i64 0, i32 0
%13 = load i8*, i8** %12
%14 = tail call i32 @strlen(i8* %13)
store i32 %14, i32* %3
%15 = getelementptr inbounds %struct.dirent, %struct.dirent* %9, i64 0, i32 1
%16 = load i64, i64* %15
store i64 %16, i64* %2
%17 = getelementptr inbounds %struct.dirent, %struct.dirent* %9, i64 0, i32 2
%18 = load i32, i32* %17
store i32 %18, i32* %4
%19 = tail call i64 @telldir(i32* %6)
store i64 %19, i64* %1
%20 = load i8*, i8** %12
br label %21
21: ; preds = %5, %11
%22 = phi i8* [ %20, %11 ], [ null, %5 ]
ret i8* %22
}