#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*/ do_stat(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ *opendir(char *);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
void do_ls(char *dirname) {
  DIR *dir_ptr;
  struct dirent *direntp;
  if ((dir_ptr = opendir(dirname)) == NULL) {
    fprintf(stderr, "ls2: cannot open %s\n", dirname);
  } else {
    while ((direntp = readdir(dir_ptr)) != NULL)
      do_stat(direntp->d_name);
    closedir(dir_ptr);
  }
}


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)
cmp    $0x0,%rax
jne    1191 <do_ls+0x41>
mov    0x2e66(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xe79(%rip),%rsi
call   1050 <fprintf@plt>
jmp    11c6 <do_ls+0x76>
jmp    1196 <do_ls+0x46>
mov    -0x10(%rbp),%rdi
call   1060 <readdir@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
je     11bd <do_ls+0x6d>
mov    -0x18(%rbp),%rax
mov    (%rax),%edi
call   1070 <do_stat@plt>
jmp    1196 <do_ls+0x46>
mov    -0x10(%rbp),%rdi
call   1040 <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     1195 <do_ls+0x45>
mov    %rax,%rbx
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %rbx,%rdi
call   1060 <readdir@plt>
test   %rax,%rax
je     1186 <do_ls+0x36>
mov    (%rax),%edi
call   1070 <do_stat@plt>
jmp    1170 <do_ls+0x20>
mov    %rbx,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1040 <closedir@plt>
mov    0x2e44(%rip),%rax
mov    (%rax),%edi
lea    0xe5b(%rip),%rsi
mov    %r14,%rdx
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1050 <fprintf@plt>

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

LLVM IR (-O2):
%struct.dirent = type { i32 }
@stderr = global i32 0
@.str = [21 x i8] c"ls2: cannot open %s\0A\00"
define void @do_ls(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 %17, label %10
7: ; preds = %1
%8 = load i32, i32* @stderr
%9 = tail call i32 @fprintf(i32 %8, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %19
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 i32, i32* %12
%14 = tail call i32 @do_stat(i32 %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)
br label %19
19: ; preds = %17, %7
ret void
}