#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;
};
typedef int /*<<< orphan*/ DIR;

/* Variables and functions */
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ create_files(int);
int /*<<< orphan*/ *opendir(char *);
int /*<<< orphan*/ printf(char *, char *);
struct dirent *readdir(int /*<<< orphan*/ *);
int main(int argc, char *argv[]) {
  struct dirent *ent_data;
  create_files(20);
  DIR *mydir = opendir("/tmp/tmpdir");
  ent_data = readdir(mydir);
  while (ent_data != NULL) {
    printf("%s\n", ent_data->d_name);
    ent_data = readdir(mydir);
  }
  closedir(mydir);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    $0x14,%edi
call   1050 <create_files@plt>
lea    0xe89(%rip),%rdi
call   1030 <opendir@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
call   1070 <readdir@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     11bd <main+0x6d>
mov    -0x18(%rbp),%rax
mov    (%rax),%rsi
lea    0xe66(%rip),%rdi
call   1040 <printf@plt>
mov    -0x20(%rbp),%rdi
call   1070 <readdir@plt>
mov    %rax,-0x18(%rbp)
jmp    118d <main+0x3d>
mov    -0x20(%rbp),%rdi
call   1060 <closedir@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
mov    $0x14,%edi
call   1050 <create_files@plt>
lea    0xe9b(%rip),%rdi
call   1030 <opendir@plt>
mov    %rax,%rbx
mov    %rax,%rdi
call   1070 <readdir@plt>
test   %rax,%rax
je     11a8 <main+0x58>
lea    0xe8b(%rip),%r14
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    (%rax),%rsi
mov    %r14,%rdi
call   1040 <printf@plt>
mov    %rbx,%rdi
call   1070 <readdir@plt>
test   %rax,%rax
jne    1190 <main+0x40>
mov    %rbx,%rdi
call   1060 <closedir@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.dirent = type { i8* }
@.str = [12 x i8] c"/tmp/tmpdir\00"
@.str.1 = [4 x i8] c"%s\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca %struct.dirent*
%7 = alloca i32*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%8 = call i32 @create_files(i32 20)
%9 = call i32* @opendir(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
store i32* %9, i32** %7
%10 = load i32*, i32** %7
%11 = call %struct.dirent* @readdir(i32* %10)
store %struct.dirent* %11, %struct.dirent** %6
br label %12
12: ; preds = %15, %2
%13 = load %struct.dirent*, %struct.dirent** %6
%14 = icmp ne %struct.dirent* %13, null
br i1 %14, label %15, label %22
15: ; preds = %12
%16 = load %struct.dirent*, %struct.dirent** %6
%17 = getelementptr inbounds %struct.dirent, %struct.dirent* %16, i32 0, i32 0
%18 = load i8*, i8** %17
%19 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* %18)
%20 = load i32*, i32** %7
%21 = call %struct.dirent* @readdir(i32* %20)
store %struct.dirent* %21, %struct.dirent** %6
br label %12
22: ; preds = %12
%23 = load i32*, i32** %7
%24 = call i32 @closedir(i32* %23)
ret i32 0
}

LLVM IR (-O2):
%struct.dirent = type { i8* }
@.str = [12 x i8] c"/tmp/tmpdir\00"
@.str.1 = [4 x i8] c"%s\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = tail call i32 @create_files(i32 20)
%4 = tail call i32* @opendir(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%5 = tail call %struct.dirent* @readdir(i32* %4)
%6 = icmp eq %struct.dirent* %5, null
br i1 %6, label %14, label %7
7: ; preds = %2, %7
%8 = phi %struct.dirent* [ %12, %7 ], [ %5, %2 ]
%9 = getelementptr inbounds %struct.dirent, %struct.dirent* %8, i64 0, i32 0
%10 = load i8*, i8** %9
%11 = tail call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* %10)
%12 = tail call %struct.dirent* @readdir(i32* %4)
%13 = icmp eq %struct.dirent* %12, null
br i1 %13, label %14, label %7
14: ; preds = %7, %2
%15 = tail call i32 @closedir(i32* %4)
ret i32 0
}