#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*/ *opendir(char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
struct dirent *readdir(int /*<<< orphan*/ *);
int main() {
  DIR *D = opendir(".");
  struct dirent *d;
  if (!D) {
    perror("opendir");
    return 1;
  }
  while ((d = readdir(D))) {
    printf("found %s\n", d->d_name);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xeaa(%rip),%rdi
call   1030 <opendir@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1182 <main+0x42>
lea    0xe91(%rip),%rdi
call   1060 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    11bd <main+0x7d>
jmp    1187 <main+0x47>
mov    -0x10(%rbp),%rdi
call   1050 <readdir@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
je     11b6 <main+0x76>
mov    -0x18(%rbp),%rax
mov    (%rax),%rsi
lea    0xe5e(%rip),%rdi
call   1040 <printf@plt>
jmp    1187 <main+0x47>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
lea    0xeb5(%rip),%rdi
call   1030 <opendir@plt>
test   %rax,%rax
je     1190 <main+0x50>
mov    %rax,%rbx
mov    %rax,%rdi
call   1050 <readdir@plt>
xor    %r14d,%r14d
test   %rax,%rax
je     1188 <main+0x48>
lea    0xe9b(%rip),%rbp
nop
mov    (%rax),%rsi
mov    %rbp,%rdi
call   1040 <printf@plt>
mov    %rbx,%rdi
call   1050 <readdir@plt>
test   %rax,%rax
jne    1170 <main+0x30>
mov    %r14d,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xe6b(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%r14d
jmp    1188 <main+0x48>

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

LLVM IR (-O2):
%struct.dirent = type { i8* }
@.str = [2 x i8] c".\00"
@.str.1 = [8 x i8] c"opendir\00"
@.str.2 = [10 x i8] c"found %s\0A\00"
define i32 @main() {
%1 = tail call i32* @opendir(i8* getelementptr inbounds ([2 x i8], [2 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 %15, label %8
6: ; preds = %0
%7 = tail call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
br label %15
8: ; preds = %3, %8
%9 = phi %struct.dirent* [ %13, %8 ], [ %4, %3 ]
%10 = getelementptr inbounds %struct.dirent, %struct.dirent* %9, i64 0, i32 0
%11 = load i8*, i8** %10
%12 = tail call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i8* %11)
%13 = tail call %struct.dirent* @readdir(i32* %1)
%14 = icmp eq %struct.dirent* %13, null
br i1 %14, label %15, label %8
15: ; preds = %8, %3, %6
%16 = phi i32 [ 1, %6 ], [ 0, %3 ], [ 0, %8 ]
ret i32 %16
}