#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*/ EXIT_FAILURE;
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ do_stat(int /*<<< orphan*/);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ *opendir(char *);
int /*<<< orphan*/ perror(char *);
struct dirent *readdir(int /*<<< orphan*/ *);
extern void do_ls(char *dirname) {
  struct dirent *dirent_p;
  DIR *dir_p = opendir(dirname);
  if (dir_p == NULL) {
    perror(dirname);
    exit(EXIT_FAILURE);
  }
  while ((dirent_p = readdir(dir_p)) != NULL) {
    do_stat(dirent_p->d_name);
  }
  closedir(dir_p);
  return;
}


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,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    119b <do_ls+0x3b>
mov    -0x8(%rbp),%rdi
call   1060 <perror@plt>
mov    0x2e4c(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
jmp    11a0 <do_ls+0x40>
mov    -0x18(%rbp),%rdi
call   1050 <readdir@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
je     11c7 <do_ls+0x67>
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
call   1080 <do_stat@plt>
jmp    11a0 <do_ls+0x40>
mov    -0x18(%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     11a5 <do_ls+0x45>
mov    %rax,%rbx
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %rbx,%rdi
call   1050 <readdir@plt>
test   %rax,%rax
je     1196 <do_ls+0x36>
mov    (%rax),%edi
call   1080 <do_stat@plt>
jmp    1180 <do_ls+0x20>
mov    %rbx,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1040 <closedir@plt>
mov    %r14,%rdi
call   1060 <perror@plt>
mov    0x2e2c(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>

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

LLVM IR (-O2):
%struct.dirent = type { i32 }
@EXIT_FAILURE = global i32 0
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 %18, label %11
7: ; preds = %1
%8 = tail call i32 @perror(i8* %0)
%9 = load i32, i32* @EXIT_FAILURE
%10 = tail call i32 @exit(i32 %9)
unreachable
11: ; preds = %4, %11
%12 = phi %struct.dirent* [ %16, %11 ], [ %5, %4 ]
%13 = getelementptr inbounds %struct.dirent, %struct.dirent* %12, i64 0, i32 0
%14 = load i32, i32* %13
%15 = tail call i32 @do_stat(i32 %14)
%16 = tail call %struct.dirent* @readdir(i32* %2)
%17 = icmp eq %struct.dirent* %16, null
br i1 %17, label %18, label %11
18: ; preds = %11, %4
%19 = tail call i32 @closedir(i32* %2)
ret void
}