#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 chdir(char *);
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);
  }
  if (chdir(dirname) == -1) {
    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    11ab <do_ls+0x3b>
mov    -0x8(%rbp),%rdi
call   1070 <perror@plt>
mov    0x2e3c(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
mov    -0x8(%rbp),%rdi
call   1040 <chdir@plt>
cmp    $0xffffffff,%eax
jne    11d4 <do_ls+0x64>
mov    -0x8(%rbp),%rdi
call   1070 <perror@plt>
mov    0x2e13(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
jmp    11d9 <do_ls+0x69>
mov    -0x18(%rbp),%rdi
call   1060 <readdir@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
je     1200 <do_ls+0x90>
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
call   1090 <do_stat@plt>
jmp    11d9 <do_ls+0x69>
mov    -0x18(%rbp),%rdi
call   1050 <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     11c5 <do_ls+0x55>
mov    %rax,%rbx
mov    %r14,%rdi
call   1040 <chdir@plt>
cmp    $0xffffffff,%eax
je     11c5 <do_ls+0x55>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rdi
call   1060 <readdir@plt>
test   %rax,%rax
je     11b6 <do_ls+0x46>
mov    (%rax),%edi
call   1090 <do_stat@plt>
jmp    11a0 <do_ls+0x30>
mov    %rbx,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1050 <closedir@plt>
mov    %r14,%rdi
call   1070 <perror@plt>
mov    0x2e0c(%rip),%rax
mov    (%rax),%edi
call   1080 <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
%15 = load i8*, i8** %2
%16 = call i32 @chdir(i8* %15)
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %23
18: ; preds = %14
%19 = load i8*, i8** %2
%20 = call i32 @perror(i8* %19)
%21 = load i32, i32* @EXIT_FAILURE
%22 = call i32 @exit(i32 %21)
unreachable
23: ; preds = %14
br label %24
24: ; preds = %28, %23
%25 = load i32*, i32** %4
%26 = call %struct.dirent* @readdir(i32* %25)
store %struct.dirent* %26, %struct.dirent** %3
%27 = icmp ne %struct.dirent* %26, null
br i1 %27, label %28, label %33
28: ; preds = %24
%29 = load %struct.dirent*, %struct.dirent** %3
%30 = getelementptr inbounds %struct.dirent, %struct.dirent* %29, i32 0, i32 0
%31 = load i32, i32* %30
%32 = call i32 @do_stat(i32 %31)
br label %24
33: ; preds = %24
%34 = load i32*, i32** %4
%35 = call i32 @closedir(i32* %34)
ret void
}

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