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

/* Variables and functions */
scalar_t__ EACCES;
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ do_file(char *, char const *const *);
scalar_t__ errno;
int /*<<< orphan*/ error(int, int, char *, scalar_t__);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ *opendir(char const *);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ strcat(char *, char *);
scalar_t__ strcmp(char *, char *);
int /*<<< orphan*/ strcpy(char *, char const *);
int strlen(char const *);
void do_dir(const char *dir_name, const char *const *parms) {
  DIR *dir;
  struct dirent *d;
  char filename[4096];
  if ((dir = opendir(dir_name)) == NULL) {
    if (errno != EACCES) {
      error(1, 1, "%d", errno);
      exit(EXIT_FAILURE);
    }
  } else {
    while ((d = readdir(dir)) != NULL) {
      if (strcmp(d->d_name, ".") == 0 || strcmp(d->d_name, "..") == 0)
        continue;
      else {
        strcpy(filename, dir_name);
        if (dir_name[strlen(dir_name) - 1] != '/') {
          strcat(filename, "/");
        }
        strcat(filename, d->d_name);
        do_file(filename, parms);
      }
    }
  }
  closedir(dir);
}


x86 assembly (clang, -O0):
<do_dir>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1030,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
call   1050 <opendir@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    1214 <do_dir+0x74>
mov    0x2e07(%rip),%rax
mov    (%rax),%rax
mov    0x2e0d(%rip),%rcx
cmp    (%rcx),%rax
je     120f <do_dir+0x6f>
mov    0x2ded(%rip),%rax
mov    (%rax),%rcx
mov    $0x1,%esi
lea    0xe06(%rip),%rdx
mov    %esi,%edi
call   10a0 <error@plt>
mov    0x2dc8(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
jmp    12ef <do_dir+0x14f>
jmp    1219 <do_dir+0x79>
mov    -0x18(%rbp),%rdi
call   1090 <readdir@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     12ea <do_dir+0x14a>
mov    -0x20(%rbp),%rax
mov    (%rax),%rdi
lea    0xdc6(%rip),%rsi
call   1080 <strcmp@plt>
cmp    $0x0,%rax
je     126a <do_dir+0xca>
mov    -0x20(%rbp),%rax
mov    (%rax),%rdi
lea    0xda8(%rip),%rsi
call   1080 <strcmp@plt>
cmp    $0x0,%rax
jne    126f <do_dir+0xcf>
jmp    1219 <do_dir+0x79>
lea    -0x1020(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x8(%rbp),%rax
mov    %rax,-0x1028(%rbp)
mov    -0x8(%rbp),%rdi
call   1060 <strlen@plt>
mov    %eax,%ecx
mov    -0x1028(%rbp),%rax
sub    $0x1,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x2f,%eax
je     12c2 <do_dir+0x122>
lea    -0x1020(%rbp),%rdi
lea    0xd49(%rip),%rsi
call   10b0 <strcat@plt>
lea    -0x1020(%rbp),%rdi
mov    -0x20(%rbp),%rax
mov    (%rax),%rsi
call   10b0 <strcat@plt>
lea    -0x1020(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1040 <do_file@plt>
jmp    1219 <do_dir+0x79>
jmp    12ef <do_dir+0x14f>
mov    -0x18(%rbp),%rdi
call   1070 <closedir@plt>
add    $0x1030,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_dir>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x1008,%rsp
mov    %rsi,%r14
mov    %rdi,%rbx
call   1050 <opendir@plt>
mov    %rax,%r15
test   %rax,%rax
je     126a <do_dir+0xca>
mov    %r15,%rdi
call   1090 <readdir@plt>
test   %rax,%rax
je     1280 <do_dir+0xe0>
mov    %rax,%r13
lea    0xe21(%rip),%r12
mov    %rsp,%rbp
jmp    1217 <do_dir+0x77>
nopl   0x0(%rax,%rax,1)
mov    0x0(%r13),%rsi
mov    %rbp,%rdi
call   10b0 <strcat@plt>
mov    %rbp,%rdi
mov    %r14,%rsi
call   1040 <do_file@plt>
mov    %r15,%rdi
call   1090 <readdir@plt>
mov    %rax,%r13
test   %rax,%rax
je     1280 <do_dir+0xe0>
mov    0x0(%r13),%rdi
mov    %r12,%rsi
call   1080 <strcmp@plt>
test   %rax,%rax
je     1207 <do_dir+0x67>
mov    0x0(%r13),%rdi
lea    0xdd0(%rip),%rsi
call   1080 <strcmp@plt>
test   %rax,%rax
je     1207 <do_dir+0x67>
mov    %rbp,%rdi
mov    %rbx,%rsi
call   1030 <strcpy@plt>
mov    %rbx,%rdi
call   1060 <strlen@plt>
cltq
cmpb   $0x2f,-0x1(%rax,%rbx,1)
je     11f0 <do_dir+0x50>
mov    %rbp,%rdi
lea    0xda3(%rip),%rsi
call   10b0 <strcat@plt>
jmp    11f0 <do_dir+0x50>
mov    0x2d67(%rip),%rax
mov    (%rax),%rcx
mov    0x2d6d(%rip),%rax
cmp    (%rax),%rcx
jne    129a <do_dir+0xfa>
mov    %r15,%rdi
call   1070 <closedir@plt>
add    $0x1008,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd5f(%rip),%rdx
mov    $0x1,%edi
mov    $0x1,%esi
call   10a0 <error@plt>
mov    0x2d19(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
%struct.dirent = type { i8* }
@errno = global i64 0
@EACCES = global i64 0
@.str = [3 x i8] c"%d\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [2 x i8] c".\00"
@.str.2 = [3 x i8] c"..\00"
@.str.3 = [2 x i8] c"/\00"
define void @do_dir(i8* %0, i8** %1) {
%3 = alloca i8*
%4 = alloca i8**
%5 = alloca i32*
%6 = alloca %struct.dirent*
%7 = alloca [4096 x i8]
store i8* %0, i8** %3
store i8** %1, i8*** %4
%8 = load i8*, i8** %3
%9 = call i32* @opendir(i8* %8)
store i32* %9, i32** %5
%10 = icmp eq i32* %9, null
br i1 %10, label %11, label %21
11: ; preds = %2
%12 = load i64, i64* @errno
%13 = load i64, i64* @EACCES
%14 = icmp ne i64 %12, %13
br i1 %14, label %15, label %20
15: ; preds = %11
%16 = load i64, i64* @errno
%17 = call i32 @error(i32 1, i32 1, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i64 %16)
%18 = load i32, i32* @EXIT_FAILURE
%19 = call i32 @exit(i32 %18)
unreachable
20: ; preds = %11
br label %66
21: ; preds = %2
br label %22
22: ; preds = %64, %38, %21
%23 = load i32*, i32** %5
%24 = call %struct.dirent* @readdir(i32* %23)
store %struct.dirent* %24, %struct.dirent** %6
%25 = icmp ne %struct.dirent* %24, null
br i1 %25, label %26, label %65
26: ; preds = %22
%27 = load %struct.dirent*, %struct.dirent** %6
%28 = getelementptr inbounds %struct.dirent, %struct.dirent* %27, i32 0, i32 0
%29 = load i8*, i8** %28
%30 = call i64 @strcmp(i8* %29, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%31 = icmp eq i64 %30, 0
br i1 %31, label %38, label %32
32: ; preds = %26
%33 = load %struct.dirent*, %struct.dirent** %6
%34 = getelementptr inbounds %struct.dirent, %struct.dirent* %33, i32 0, i32 0
%35 = load i8*, i8** %34
%36 = call i64 @strcmp(i8* %35, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%37 = icmp eq i64 %36, 0
br i1 %37, label %38, label %39
38: ; preds = %32, %26
br label %22
39: ; preds = %32
%40 = getelementptr inbounds [4096 x i8], [4096 x i8]* %7, i64 0, i64 0
%41 = load i8*, i8** %3
%42 = call i32 @strcpy(i8* %40, i8* %41)
%43 = load i8*, i8** %3
%44 = load i8*, i8** %3
%45 = call i32 @strlen(i8* %44)
%46 = sub nsw i32 %45, 1
%47 = sext i32 %46 to i64
%48 = getelementptr inbounds i8, i8* %43, i64 %47
%49 = load i8, i8* %48
%50 = sext i8 %49 to i32
%51 = icmp ne i32 %50, 47
br i1 %51, label %52, label %55
52: ; preds = %39
%53 = getelementptr inbounds [4096 x i8], [4096 x i8]* %7, i64 0, i64 0
%54 = call i32 @strcat(i8* %53, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
br label %55
55: ; preds = %52, %39
%56 = getelementptr inbounds [4096 x i8], [4096 x i8]* %7, i64 0, i64 0
%57 = load %struct.dirent*, %struct.dirent** %6
%58 = getelementptr inbounds %struct.dirent, %struct.dirent* %57, i32 0, i32 0
%59 = load i8*, i8** %58
%60 = call i32 @strcat(i8* %56, i8* %59)
%61 = getelementptr inbounds [4096 x i8], [4096 x i8]* %7, i64 0, i64 0
%62 = load i8**, i8*** %4
%63 = call i32 @do_file(i8* %61, i8** %62)
br label %64
64: ; preds = %55
br label %22
65: ; preds = %22
br label %66
66: ; preds = %65, %20
%67 = load i32*, i32** %5
%68 = call i32 @closedir(i32* %67)
ret void
}

LLVM IR (-O2):
%struct.dirent = type { i8* }
@errno = global i64 0
@EACCES = global i64 0
@.str = [3 x i8] c"%d\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [2 x i8] c".\00"
@.str.2 = [3 x i8] c"..\00"
@.str.3 = [2 x i8] c"/\00"
define void @do_dir(i8* %0, i8** %1) {
%3 = alloca [4096 x i8]
%4 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%5 = tail call i32* @opendir(i8* %0)
%6 = icmp eq i32* %5, null
br i1 %6, label %10, label %7
7: ; preds = %2
%8 = call %struct.dirent* @readdir(i32* %5)
%9 = icmp eq %struct.dirent* %8, null
br i1 %9, label %45, label %18
10: ; preds = %2
%11 = load i64, i64* @errno
%12 = load i64, i64* @EACCES
%13 = icmp eq i64 %11, %12
br i1 %13, label %45, label %14
14: ; preds = %10
%15 = tail call i32 @error(i32 1, i32 1, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i64 %11)
%16 = load i32, i32* @EXIT_FAILURE
%17 = tail call i32 @exit(i32 %16)
unreachable
18: ; preds = %7, %28
%19 = phi %struct.dirent* [ %29, %28 ], [ %8, %7 ]
%20 = getelementptr inbounds %struct.dirent, %struct.dirent* %19, i64 0, i32 0
%21 = load i8*, i8** %20
%22 = call i64 @strcmp(i8* %21, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%23 = icmp eq i64 %22, 0
br i1 %23, label %28, label %24
24: ; preds = %18
%25 = load i8*, i8** %20
%26 = call i64 @strcmp(i8* %25, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%27 = icmp eq i64 %26, 0
br i1 %27, label %28, label %31
28: ; preds = %18, %24, %41
%29 = call %struct.dirent* @readdir(i32* %5)
%30 = icmp eq %struct.dirent* %29, null
br i1 %30, label %45, label %18
31: ; preds = %24
%32 = call i32 @strcpy(i8* %4, i8* %0)
%33 = call i32 @strlen(i8* %0)
%34 = add nsw i32 %33, -1
%35 = sext i32 %34 to i64
%36 = getelementptr inbounds i8, i8* %0, i64 %35
%37 = load i8, i8* %36
%38 = icmp eq i8 %37, 47
br i1 %38, label %41, label %39
39: ; preds = %31
%40 = call i32 @strcat(i8* %4, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
br label %41
41: ; preds = %39, %31
%42 = load i8*, i8** %20
%43 = call i32 @strcat(i8* %4, i8* %42)
%44 = call i32 @do_file(i8* %4, i8** %1)
br label %28
45: ; preds = %28, %7, %10
%46 = call i32 @closedir(i32* %5)
ret void
}