#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*/ *opendir(char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
struct dirent *readdir(int /*<<< orphan*/ *);
int main(int argc, char **argv) {
  DIR *dirh;
  struct dirent *dirp;
  static char mydir[20] = "/tmp";
  if ((dirh = opendir(mydir)) == NULL) {
    perror("opendir");
    return 1;
  }
  for (dirp = readdir(dirh); dirp != NULL; dirp = readdir(dirh)) {
    printf("Got dir entry: %s\n", dirp->d_name);
  }
  closedir(dirh);
  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)
lea    0x2ee3(%rip),%rdi
call   1030 <opendir@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    1198 <main+0x48>
lea    0xe79(%rip),%rdi
call   1070 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    11e5 <main+0x95>
mov    -0x18(%rbp),%rdi
call   1060 <readdir@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     11d5 <main+0x85>
mov    -0x20(%rbp),%rax
mov    (%rax),%rsi
lea    0xe4a(%rip),%rdi
call   1040 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1060 <readdir@plt>
mov    %rax,-0x20(%rbp)
jmp    11a5 <main+0x55>
mov    -0x18(%rbp),%rdi
call   1050 <closedir@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
lea    0x2ef5(%rip),%rdi
call   1030 <opendir@plt>
test   %rax,%rax
je     11aa <main+0x5a>
mov    %rax,%rbx
mov    %rax,%rdi
call   1060 <readdir@plt>
test   %rax,%rax
je     1198 <main+0x48>
lea    0xe8c(%rip),%r14
nopl   0x0(%rax)
mov    (%rax),%rsi
mov    %r14,%rdi
call   1040 <printf@plt>
mov    %rbx,%rdi
call   1060 <readdir@plt>
test   %rax,%rax
jne    1180 <main+0x30>
mov    %rbx,%rdi
call   1050 <closedir@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
lea    0xe4f(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%eax
jmp    11a2 <main+0x52>

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

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