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

/* Variables and functions */
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *);
int /*<<< orphan*/ *opendir(char const *);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdout;
void print_dir(const char *dirname) {
  DIR *d;
  struct dirent *dp;
  d = opendir(dirname);
  if (!d) {
    fprintf(stderr, "ls: Cannot open %s\n", dirname);
    return;
  }
  for (;;) {
    dp = readdir(d);
    if (!dp)
      break;
    fprintf(stdout, "%s\n", dp->d_name);
  }
  closedir(d);
}


x86 assembly (clang, -O0):
<print_dir>:
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    1182 <print_dir+0x42>
mov    0x2e75(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xe88(%rip),%rsi
call   1050 <fprintf@plt>
jmp    11ce <print_dir+0x8e>
jmp    1187 <print_dir+0x47>
mov    -0x10(%rbp),%rdi
call   1060 <readdir@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11a4 <print_dir+0x64>
jmp    11c5 <print_dir+0x85>
mov    0x2e2d(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rax
mov    (%rax),%rdx
lea    0xe55(%rip),%rsi
call   1050 <fprintf@plt>
jmp    1187 <print_dir+0x47>
mov    -0x10(%rbp),%rdi
call   1040 <closedir@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<print_dir>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%r14
call   1030 <opendir@plt>
test   %rax,%rax
je     1198 <print_dir+0x58>
mov    %rax,%rbx
mov    %rax,%rdi
call   1060 <readdir@plt>
test   %rax,%rax
je     118b <print_dir+0x4b>
mov    0x2e6f(%rip),%r15
lea    0xea0(%rip),%r14
mov    (%r15),%edi
mov    (%rax),%rdx
mov    %r14,%rsi
call   1050 <fprintf@plt>
mov    %rbx,%rdi
call   1060 <readdir@plt>
test   %rax,%rax
jne    1170 <print_dir+0x30>
mov    %rbx,%rdi
pop    %rbx
pop    %r14
pop    %r15
jmp    1040 <closedir@plt>
mov    0x2e41(%rip),%rax
mov    (%rax),%edi
lea    0xe58(%rip),%rsi
mov    %r14,%rdx
pop    %rbx
pop    %r14
pop    %r15
jmp    1050 <fprintf@plt>

LLVM IR (-O0):
%struct.dirent = type { i8* }
@stderr = global i32 0
@.str = [20 x i8] c"ls: Cannot open %s\0A\00"
@stdout = global i32 0
@.str.1 = [4 x i8] c"%s\0A\00"
define void @print_dir(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 i32, i32* @stderr
%11 = load i8*, i8** %2
%12 = call i32 @fprintf(i32 %10, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* %11)
br label %29
13: ; preds = %1
br label %14
14: ; preds = %20, %13
%15 = load i32*, i32** %3
%16 = call %struct.dirent* @readdir(i32* %15)
store %struct.dirent* %16, %struct.dirent** %4
%17 = load %struct.dirent*, %struct.dirent** %4
%18 = icmp ne %struct.dirent* %17, null
br i1 %18, label %20, label %19
19: ; preds = %14
br label %26
20: ; preds = %14
%21 = load i32, i32* @stdout
%22 = load %struct.dirent*, %struct.dirent** %4
%23 = getelementptr inbounds %struct.dirent, %struct.dirent* %22, i32 0, i32 0
%24 = load i8*, i8** %23
%25 = call i32 @fprintf(i32 %21, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* %24)
br label %14
26: ; preds = %19
%27 = load i32*, i32** %3
%28 = call i32 @closedir(i32* %27)
br label %29
29: ; preds = %26, %9
ret void
}

LLVM IR (-O2):
%struct.dirent = type { i8* }
@stderr = global i32 0
@.str = [20 x i8] c"ls: Cannot open %s\0A\00"
@stdout = global i32 0
@.str.1 = [4 x i8] c"%s\0A\00"
define void @print_dir(i8* %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 %18, label %10
7: ; preds = %1
%8 = load i32, i32* @stderr
%9 = tail call i32 @fprintf(i32 %8, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %20
10: ; preds = %4, %10
%11 = phi %struct.dirent* [ %16, %10 ], [ %5, %4 ]
%12 = load i32, i32* @stdout
%13 = getelementptr inbounds %struct.dirent, %struct.dirent* %11, i64 0, i32 0
%14 = load i8*, i8** %13
%15 = tail call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* %14)
%16 = tail call %struct.dirent* @readdir(i32* %2)
%17 = icmp eq %struct.dirent* %16, null
br i1 %17, label %18, label %10
18: ; preds = %10, %4
%19 = tail call i32 @closedir(i32* %2)
br label %20
20: ; preds = %18, %7
ret void
}