#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*/ printf(char *, int, char *);
int /*<<< orphan*/ puts(char *);
struct dirent *readdir(int /*<<< orphan*/ *);
int main() {
  DIR *dir;
  struct dirent *dir_entry;
  int i = 0;
  dir = opendir("./");
  if (dir == NULL) {
    puts("error");
    return 1;
  } else {
    while (dir_entry = readdir(dir)) {
      i++;
      printf("file list %d: %s\n", i, dir_entry->d_name);
    }
    closedir(dir);
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0x1c(%rbp)
lea    0xe93(%rip),%rdi
call   1040 <opendir@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1199 <main+0x49>
lea    0xe7b(%rip),%rdi
call   1030 <puts@plt>
movl   $0x1,-0x4(%rbp)
jmp    11e2 <main+0x92>
jmp    119e <main+0x4e>
mov    -0x10(%rbp),%rdi
call   1070 <readdir@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
je     11d9 <main+0x89>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%esi
mov    -0x18(%rbp),%rax
mov    (%rax),%rdx
lea    0xe3a(%rip),%rdi
call   1050 <printf@plt>
jmp    119e <main+0x4e>
mov    -0x10(%rbp),%rdi
call   1060 <closedir@plt>
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
lea    0xea5(%rip),%rdi
call   1040 <opendir@plt>
test   %rax,%rax
je     11b9 <main+0x69>
mov    %rax,%rbx
mov    %rax,%rdi
call   1070 <readdir@plt>
test   %rax,%rax
je     11ad <main+0x5d>
mov    $0x1,%ebp
lea    0xe88(%rip),%r14
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    (%rax),%rdx
mov    %r14,%rdi
mov    %ebp,%esi
call   1050 <printf@plt>
mov    %rbx,%rdi
call   1070 <readdir@plt>
add    $0x1,%ebp
test   %rax,%rax
jne    1190 <main+0x40>
mov    %rbx,%rdi
call   1060 <closedir@plt>
xor    %eax,%eax
jmp    11ca <main+0x7a>
lea    0xe43(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.dirent = type { i8* }
@.str = [3 x i8] c"./\00"
@.str.1 = [6 x i8] c"error\00"
@.str.2 = [18 x i8] c"file list %d: %s\0A\00"
define i32 @main() {
%1 = tail call i32* @opendir(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%2 = icmp eq i32* %1, null
br i1 %2, label %6, label %3
3: ; preds = %0
%4 = tail call %struct.dirent* @readdir(i32* %1)
%5 = icmp eq %struct.dirent* %4, null
br i1 %5, label %17, label %8
6: ; preds = %0
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
br label %19
8: ; preds = %3, %8
%9 = phi %struct.dirent* [ %15, %8 ], [ %4, %3 ]
%10 = phi i32 [ %11, %8 ], [ 0, %3 ]
%11 = add nuw nsw i32 %10, 1
%12 = getelementptr inbounds %struct.dirent, %struct.dirent* %9, i64 0, i32 0
%13 = load i8*, i8** %12
%14 = tail call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i32 %11, i8* %13)
%15 = tail call %struct.dirent* @readdir(i32* %1)
%16 = icmp eq %struct.dirent* %15, null
br i1 %16, label %17, label %8
17: ; preds = %8, %3
%18 = tail call i32 @closedir(i32* %1)
br label %19
19: ; preds = %17, %6
%20 = phi i32 [ 1, %6 ], [ 0, %17 ]
ret i32 %20
}