#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 *, ...);
struct dirent *readdir(int /*<<< orphan*/ *);
void ls(void) {
  DIR *dir;
  struct dirent *ent;
  if ((dir = opendir(".")) != NULL) {
    while ((ent = readdir(dir)) != NULL)
      printf("%s\n", ent->d_name);
    closedir(dir);
  } else {
    printf("An error has occurred.\n");
  }
}


x86 assembly (clang, -O0):
<ls>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
lea    0xeb1(%rip),%rdi
call   1030 <opendir@plt>
mov    %rax,-0x8(%rbp)
cmp    $0x0,%rax
je     11a6 <ls+0x66>
jmp    1167 <ls+0x27>
mov    -0x8(%rbp),%rdi
call   1060 <readdir@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
je     1198 <ls+0x58>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe76(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1167 <ls+0x27>
mov    -0x8(%rbp),%rdi
call   1050 <closedir@plt>
jmp    11b4 <ls+0x74>
lea    0xe59(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ls>:
push   %rbx
lea    0xecd(%rip),%rdi
call   1040 <opendir@plt>
test   %rax,%rax
je     1180 <ls+0x40>
mov    %rax,%rbx
cs nopw 0x0(%rax,%rax,1)
nop
mov    %rbx,%rdi
call   1060 <readdir@plt>
test   %rax,%rax
je     1177 <ls+0x37>
mov    (%rax),%rdi
call   1030 <puts@plt>
jmp    1160 <ls+0x20>
mov    %rbx,%rdi
pop    %rbx
jmp    1050 <closedir@plt>
lea    0xe79(%rip),%rdi
pop    %rbx
jmp    1030 <puts@plt>

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

LLVM IR (-O2):
%struct.dirent = type { i8* }
@.str = [2 x i8] c".\00"
@str = [23 x i8] c"An error has occurred.\00"
define void @ls() {
%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 %15, label %3
3: ; preds = %0
%4 = tail call %struct.dirent* @readdir(i32* %1)
%5 = icmp eq %struct.dirent* %4, null
br i1 %5, label %13, label %6
6: ; preds = %3, %6
%7 = phi %struct.dirent* [ %11, %6 ], [ %4, %3 ]
%8 = getelementptr inbounds %struct.dirent, %struct.dirent* %7, i64 0, i32 0
%9 = load i8*, i8** %8
%10 = tail call i32 @puts(i8* dereferenceable(1) %9)
%11 = tail call %struct.dirent* @readdir(i32* %1)
%12 = icmp eq %struct.dirent* %11, null
br i1 %12, label %13, label %6
13: ; preds = %6, %3
%14 = tail call i32 @closedir(i32* %1)
br label %17
15: ; preds = %0
%16 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @str, i64 0, i64 0))
br label %17
17: ; preds = %15, %13
ret void
}