#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 {
  int /*<<< orphan*/ d_name;
};
typedef int /*<<< orphan*/ DIR;

/* Variables and functions */
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ *opendir(char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ puts(int /*<<< orphan*/);
struct dirent *readdir(int /*<<< orphan*/ *);
void do_ls() {
  DIR *dp;
  struct dirent *ep;
  dp = opendir("./");
  if (dp != NULL) {
    while (ep = readdir(dp))
      puts(ep->d_name);
    (void)closedir(dp);
  } else {
    perror("Couldn't open the directory");
  }
}


x86 assembly (clang, -O0):
<do_ls>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
lea    0xea1(%rip),%rdi
call   1040 <opendir@plt>
mov    %rax,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
je     11ad <do_ls+0x5d>
jmp    1178 <do_ls+0x28>
mov    -0x8(%rbp),%rdi
call   1060 <readdir@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
je     119f <do_ls+0x4f>
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
call   1030 <puts@plt>
jmp    1178 <do_ls+0x28>
mov    -0x8(%rbp),%rdi
call   1050 <closedir@plt>
jmp    11b9 <do_ls+0x69>
lea    0xe4f(%rip),%rdi
call   1070 <perror@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_ls>:
push   %rbx
lea    0xea8(%rip),%rdi
call   1040 <opendir@plt>
test   %rax,%rax
je     118f <do_ls+0x3f>
mov    %rax,%rbx
cs nopw 0x0(%rax,%rax,1)
nop
mov    %rbx,%rdi
call   1060 <readdir@plt>
test   %rax,%rax
je     1186 <do_ls+0x36>
mov    (%rax),%edi
call   1030 <puts@plt>
jmp    1170 <do_ls+0x20>
mov    %rbx,%rdi
pop    %rbx
jmp    1050 <closedir@plt>
lea    0xe6d(%rip),%rdi
pop    %rbx
jmp    1070 <perror@plt>

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

LLVM IR (-O2):
%struct.dirent = type { i32 }
@.str = [3 x i8] c"./\00"
@.str.1 = [28 x i8] c"Couldn't open the directory\00"
define void @do_ls() {
%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 %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 i32, i32* %8
%10 = tail call i32 @puts(i32 %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 @perror(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0))
br label %17
17: ; preds = %15, %13
ret void
}