#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*/ exit(int);
int /*<<< orphan*/ *opendir(char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
struct dirent *readdir(int /*<<< orphan*/ *);
void do_ls(char *path) {
  DIR *d;
  struct dirent *ent;
  d = opendir(path);
  if (!d) {
    perror(path);
    exit(1);
  }
  while (ent = readdir(d)) {
    printf("%s\n", ent->d_name);
  }
  closedir(d);
}


x86 assembly (clang, -O0):
<do_ls>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <opendir@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1197 <do_ls+0x37>
mov    -0x8(%rbp),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
jmp    119c <do_ls+0x3c>
mov    -0x10(%rbp),%rdi
call   1060 <readdir@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
je     11cb <do_ls+0x6b>
mov    -0x18(%rbp),%rax
mov    (%rax),%rsi
lea    0xe3f(%rip),%rdi
call   1040 <printf@plt>
jmp    119c <do_ls+0x3c>
mov    -0x10(%rbp),%rdi
call   1050 <closedir@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_ls>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
call   1030 <opendir@plt>
test   %rax,%rax
je     11b7 <do_ls+0x57>
mov    %rax,%rbx
mov    %rax,%rdi
call   1060 <readdir@plt>
test   %rax,%rax
je     11a8 <do_ls+0x48>
lea    0xe78(%rip),%r14
nopl   0x0(%rax,%rax,1)
mov    (%rax),%rsi
mov    %r14,%rdi
call   1040 <printf@plt>
mov    %rbx,%rdi
call   1060 <readdir@plt>
test   %rax,%rax
jne    1190 <do_ls+0x30>
mov    %rbx,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1050 <closedir@plt>
mov    %r14,%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>

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

LLVM IR (-O2):
%struct.dirent = type { i8* }
@.str = [4 x i8] c"%s\0A\00"
define void @do_ls(i8* nocapture %0) {
%2 = tail call i32* @opendir(i8* %0)
%3 = icmp eq i32* %2, null
br i1 %3, label %7, label %4
4: ; preds = %1
%5 = tail call %struct.dirent* @readdir(i32* %2)
%6 = icmp eq %struct.dirent* %5, null
br i1 %6, label %17, label %10
7: ; preds = %1
%8 = tail call i32 @perror(i8* %0)
%9 = tail call i32 @exit(i32 1)
unreachable
10: ; preds = %4, %10
%11 = phi %struct.dirent* [ %15, %10 ], [ %5, %4 ]
%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 ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* %13)
%15 = tail call %struct.dirent* @readdir(i32* %2)
%16 = icmp eq %struct.dirent* %15, null
br i1 %16, label %17, label %10
17: ; preds = %10, %4
%18 = tail call i32 @closedir(i32* %2)
ret void
}