#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 stat {
  int st_mode;
};
struct dirent {
  scalar_t__ d_type;
  char *d_name;
};
typedef int /*<<< orphan*/ DIR;

/* Variables and functions */
scalar_t__ DT_DIR;
int MAXPATHLEN;
int S_IXOTH;
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *);
void *malloc(int);
int /*<<< orphan*/ *opendir(char const *);
int /*<<< orphan*/ readdir_r(int /*<<< orphan*/ *, struct dirent *,
                             struct dirent **);
int /*<<< orphan*/ rewinddir(int /*<<< orphan*/ *);
int /*<<< orphan*/ sprintf(char *, char *, char const *, char *);
int /*<<< orphan*/ stat(char *, struct stat *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strcpy(char *, char *);
int find_dirname(const char *dirname, char ***aptr) {
  char **earray = NULL;
  DIR *dirp = NULL;
  struct dirent *dresult = NULL;
  char fname[MAXPATHLEN];
  char buf[sizeof(struct dirent) + MAXPATHLEN];
  struct stat sbuf;
  int count = 0;
  int i;
  dirp = opendir(dirname);
  if (!dirp) {
    fprintf(stderr, "Error: Cannot open directory [%s]\n", dirname);
    exit(-1);
  }
  count = 0;
  while (!readdir_r(dirp, (struct dirent *)buf, &dresult)) {
    if (!dresult)
      break;
    if (dresult->d_type == DT_DIR) {
      if (*dresult->d_name == '.')
        continue;
      sprintf(fname, "%s/%s", dirname, dresult->d_name);
      if (!stat(fname, &sbuf)) {
        if (sbuf.st_mode & S_IXOTH)
          ++count;
      }
    }
  }
  rewinddir(dirp);
  *aptr = malloc((sizeof(char *) * (count + 1)));
  earray = *aptr;
  for (i = 0; i < count; ++i)
    earray[i] = malloc(sizeof(char) * MAXPATHLEN);
  earray[count] = NULL;
  i = 0;
  while (!readdir_r(dirp, (struct dirent *)buf, &dresult)) {
    if (!dresult)
      break;
    if (dresult->d_type == DT_DIR) {
      if (*dresult->d_name == '.')
        continue;
      sprintf(fname, "%s/%s", dirname, dresult->d_name);
      if (!stat(fname, &sbuf)) {
        if (sbuf.st_mode & S_IXOTH)
          strcpy(earray[i++], dresult->d_name);
      }
    }
  }
  closedir(dirp);
  return count;
}


x86 assembly (clang, -O0):
<find_dirname>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
mov    0x2e11(%rip),%rax
mov    (%rax),%ecx
mov    %rsp,%rdx
mov    %rdx,-0x30(%rbp)
mov    %rcx,%rsi
add    $0xf,%rsi
and    $0xfffffffffffffff0,%rsi
mov    %rsp,%rdx
sub    %rsi,%rdx
mov    %rdx,-0x60(%rbp)
mov    %rdx,%rsp
mov    %rcx,-0x38(%rbp)
movslq (%rax),%rdx
mov    %rdx,%rax
add    $0x10,%rax
add    $0x1f,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x58(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x40(%rbp)
movl   $0x0,-0x4c(%rbp)
mov    -0x8(%rbp),%rdi
call   1050 <opendir@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1259 <find_dirname+0xb9>
mov    0x2d93(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xdb6(%rip),%rsi
call   1080 <fprintf@plt>
mov    $0xffffffff,%edi
call   10c0 <exit@plt>
movl   $0x0,-0x4c(%rbp)
mov    -0x58(%rbp),%rsi
mov    -0x20(%rbp),%rdi
lea    -0x28(%rbp),%rdx
call   1040 <readdir_r@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    1286 <find_dirname+0xe6>
jmp    1325 <find_dirname+0x185>
cmpq   $0x0,-0x28(%rbp)
jne    1296 <find_dirname+0xf6>
jmp    1325 <find_dirname+0x185>
mov    -0x28(%rbp),%rax
mov    (%rax),%rax
mov    0x2d24(%rip),%rcx
cmp    (%rcx),%rax
jne    1320 <find_dirname+0x180>
mov    -0x28(%rbp),%rax
mov    0x8(%rax),%rax
movsbl (%rax),%eax
cmp    $0x2e,%eax
jne    12c6 <find_dirname+0x126>
jmp    1260 <find_dirname+0xc0>
mov    -0x60(%rbp),%rdi
mov    -0x8(%rbp),%rdx
mov    -0x28(%rbp),%rax
mov    0x8(%rax),%rcx
lea    0xd46(%rip),%rsi
call   10b0 <sprintf@plt>
mov    -0x60(%rbp),%rdi
lea    -0x48(%rbp),%rsi
call   1090 <stat@plt>
cmp    $0x0,%eax
jne    131b <find_dirname+0x17b>
mov    -0x48(%rbp),%eax
mov    0x2ce6(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     1316 <find_dirname+0x176>
mov    -0x4c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x4c(%rbp)
jmp    131b <find_dirname+0x17b>
jmp    1320 <find_dirname+0x180>
jmp    1260 <find_dirname+0xc0>
mov    -0x20(%rbp),%rdi
call   1060 <rewinddir@plt>
mov    -0x4c(%rbp),%eax
add    $0x1,%eax
cltq
shl    $0x3,%rax
mov    %eax,%edi
call   10a0 <malloc@plt>
mov    %rax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x18(%rbp)
movl   $0x0,-0x50(%rbp)
mov    -0x50(%rbp),%eax
cmp    -0x4c(%rbp),%eax
jge    139b <find_dirname+0x1fb>
mov    0x2c70(%rip),%rax
movslq (%rax),%rax
shl    $0x0,%rax
mov    %eax,%edi
call   10a0 <malloc@plt>
mov    %rax,%rdx
mov    -0x18(%rbp),%rax
movslq -0x50(%rbp),%rcx
mov    %rdx,(%rax,%rcx,8)
mov    -0x50(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x50(%rbp)
jmp    135d <find_dirname+0x1bd>
mov    -0x18(%rbp),%rax
movslq -0x4c(%rbp),%rcx
movq   $0x0,(%rax,%rcx,8)
movl   $0x0,-0x50(%rbp)
mov    -0x58(%rbp),%rsi
mov    -0x20(%rbp),%rdi
lea    -0x28(%rbp),%rdx
call   1040 <readdir_r@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    13d8 <find_dirname+0x238>
jmp    1491 <find_dirname+0x2f1>
cmpq   $0x0,-0x28(%rbp)
jne    13e8 <find_dirname+0x248>
jmp    1491 <find_dirname+0x2f1>
mov    -0x28(%rbp),%rax
mov    (%rax),%rax
mov    0x2bd2(%rip),%rcx
cmp    (%rcx),%rax
jne    148c <find_dirname+0x2ec>
mov    -0x28(%rbp),%rax
mov    0x8(%rax),%rax
movsbl (%rax),%eax
cmp    $0x2e,%eax
jne    1418 <find_dirname+0x278>
jmp    13b2 <find_dirname+0x212>
mov    -0x60(%rbp),%rdi
mov    -0x8(%rbp),%rdx
mov    -0x28(%rbp),%rax
mov    0x8(%rax),%rcx
lea    0xbf4(%rip),%rsi
call   10b0 <sprintf@plt>
mov    -0x60(%rbp),%rdi
lea    -0x48(%rbp),%rsi
call   1090 <stat@plt>
cmp    $0x0,%eax
jne    1487 <find_dirname+0x2e7>
mov    -0x48(%rbp),%eax
mov    0x2b94(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     1482 <find_dirname+0x2e2>
mov    -0x18(%rbp),%rax
mov    -0x50(%rbp),%ecx
mov    %ecx,%edx
add    $0x1,%edx
mov    %edx,-0x50(%rbp)
movslq %ecx,%rcx
mov    (%rax,%rcx,8),%rdi
mov    -0x28(%rbp),%rax
mov    0x8(%rax),%rsi
call   1030 <strcpy@plt>
jmp    1487 <find_dirname+0x2e7>
jmp    148c <find_dirname+0x2ec>
jmp    13b2 <find_dirname+0x212>
mov    -0x20(%rbp),%rdi
call   1070 <closedir@plt>
mov    -0x4c(%rbp),%eax
mov    -0x30(%rbp),%rcx
mov    %rcx,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<find_dirname>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x48,%rsp
mov    %rsi,%rbx
mov    %rsp,-0x68(%rbp)
movq   $0x0,-0x40(%rbp)
mov    0x2e19(%rip),%rdx
mov    (%rdx),%eax
mov    %rsp,%rcx
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rcx
mov    %rcx,-0x60(%rbp)
mov    %rcx,%rsp
movslq (%rdx),%rax
mov    %rsp,%r12
add    $0x1f,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r12
mov    %r12,%rsp
mov    %rdi,-0x58(%rbp)
call   1050 <opendir@plt>
test   %rax,%rax
je     13d7 <find_dirname+0x237>
mov    %rax,%r15
xor    %eax,%eax
mov    %rax,-0x38(%rbp)
lea    -0x40(%rbp),%r14
lea    -0x30(%rbp),%r13
cs nopw 0x0(%rax,%rax,1)
nop
mov    %r15,%rdi
mov    %r12,%rsi
mov    %r14,%rdx
call   1040 <readdir_r@plt>
test   %eax,%eax
jne    12a6 <find_dirname+0x106>
mov    -0x40(%rbp),%rax
test   %rax,%rax
je     12a6 <find_dirname+0x106>
mov    (%rax),%rcx
mov    0x2d83(%rip),%rdx
cmp    (%rdx),%rcx
jne    1220 <find_dirname+0x80>
mov    0x8(%rax),%rcx
cmpb   $0x2e,(%rcx)
je     1220 <find_dirname+0x80>
mov    %rbx,-0x50(%rbp)
mov    %r13,%rbx
mov    -0x60(%rbp),%r13
mov    %r13,%rdi
lea    0xdbb(%rip),%rsi
mov    -0x58(%rbp),%rdx
call   10b0 <sprintf@plt>
mov    %r13,%rdi
mov    %rbx,%r13
mov    -0x50(%rbp),%rbx
mov    %r13,%rsi
call   1090 <stat@plt>
test   %eax,%eax
jne    1220 <find_dirname+0x80>
mov    0x2d5a(%rip),%rax
mov    (%rax),%eax
and    -0x30(%rbp),%eax
cmp    $0x1,%eax
mov    -0x38(%rbp),%rax
sbb    $0xffffffff,%eax
mov    %rax,-0x38(%rbp)
jmp    1220 <find_dirname+0x80>
mov    %r15,%rdi
call   1060 <rewinddir@plt>
mov    -0x38(%rbp),%r14
lea    0x8(,%r14,8),%edi
call   10a0 <malloc@plt>
mov    %rax,(%rbx)
test   %r14d,%r14d
mov    %rax,-0x50(%rbp)
je     12ff <find_dirname+0x15f>
mov    0x2d0e(%rip),%rax
mov    (%rax),%r13d
mov    %r14d,%ebx
xor    %r14d,%r14d
nopl   0x0(%rax,%rax,1)
mov    %r13d,%edi
call   10a0 <malloc@plt>
mov    -0x50(%rbp),%rcx
mov    %rax,(%rcx,%r14,8)
add    $0x1,%r14
cmp    %r14,%rbx
jne    12e0 <find_dirname+0x140>
mov    -0x38(%rbp),%r14
jmp    1302 <find_dirname+0x162>
mov    %rax,%rcx
mov    %r14,-0x38(%rbp)
mov    %r14d,%eax
movq   $0x0,(%rcx,%rax,8)
movl   $0x0,-0x44(%rbp)
lea    -0x40(%rbp),%r13
lea    -0x30(%rbp),%r14
mov    -0x60(%rbp),%rbx
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %r15,%rdi
mov    %r12,%rsi
mov    %r13,%rdx
call   1040 <readdir_r@plt>
test   %eax,%eax
jne    13b8 <find_dirname+0x218>
mov    -0x40(%rbp),%rax
test   %rax,%rax
je     13b8 <find_dirname+0x218>
mov    (%rax),%rcx
mov    0x2c73(%rip),%rdx
cmp    (%rdx),%rcx
jne    1330 <find_dirname+0x190>
mov    0x8(%rax),%rcx
cmpb   $0x2e,(%rcx)
je     1330 <find_dirname+0x190>
mov    %rbx,%rdi
lea    0xcb6(%rip),%rsi
mov    -0x58(%rbp),%rdx
call   10b0 <sprintf@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
call   1090 <stat@plt>
test   %eax,%eax
jne    1330 <find_dirname+0x190>
mov    0x2c5c(%rip),%rax
mov    (%rax),%eax
test   %eax,-0x30(%rbp)
je     1330 <find_dirname+0x190>
mov    -0x44(%rbp),%ecx
mov    %ecx,%eax
add    $0x1,%ecx
mov    %ecx,-0x44(%rbp)
mov    -0x50(%rbp),%rcx
mov    (%rcx,%rax,8),%rdi
mov    -0x40(%rbp),%rax
mov    0x8(%rax),%rsi
call   1030 <strcpy@plt>
jmp    1330 <find_dirname+0x190>
mov    %r15,%rdi
call   1070 <closedir@plt>
mov    -0x68(%rbp),%rsp
mov    -0x38(%rbp),%rax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2bf2(%rip),%rax
mov    (%rax),%edi
lea    0xc19(%rip),%rsi
mov    -0x58(%rbp),%rdx
call   1080 <fprintf@plt>
mov    $0xffffffff,%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
%struct.dirent = type { i64, i8* }
%struct.stat = type { i32 }
@MAXPATHLEN = global i32 0
@stderr = global i32 0
@.str = [35 x i8] c"Error: Cannot open directory [%s]\0A\00"
@DT_DIR = global i64 0
@.str.1 = [6 x i8] c"%s/%s\00"
@S_IXOTH = global i32 0
define i32 @find_dirname(i8* %0, i8*** %1) {
%3 = alloca i8*
%4 = alloca i8***
%5 = alloca i8**
%6 = alloca i32*
%7 = alloca %struct.dirent*
%8 = alloca i8*
%9 = alloca i64
%10 = alloca i64
%11 = alloca %struct.stat
%12 = alloca i32
%13 = alloca i32
store i8* %0, i8** %3
store i8*** %1, i8**** %4
store i8** null, i8*** %5
store i32* null, i32** %6
store %struct.dirent* null, %struct.dirent** %7
%14 = load i32, i32* @MAXPATHLEN
%15 = zext i32 %14 to i64
%16 = call i8* @llvm.stacksave()
store i8* %16, i8** %8
%17 = alloca i8, i64 %15
store i64 %15, i64* %9
%18 = load i32, i32* @MAXPATHLEN
%19 = sext i32 %18 to i64
%20 = add i64 16, %19
%21 = alloca i8, i64 %20
store i64 %20, i64* %10
store i32 0, i32* %12
%22 = load i8*, i8** %3
%23 = call i32* @opendir(i8* %22)
store i32* %23, i32** %6
%24 = load i32*, i32** %6
%25 = icmp ne i32* %24, null
br i1 %25, label %31, label %26
26: ; preds = %2
%27 = load i32, i32* @stderr
%28 = load i8*, i8** %3
%29 = call i32 @fprintf(i32 %27, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0), i8* %28)
%30 = call i32 @exit(i32 -1)
unreachable
31: ; preds = %2
store i32 0, i32* %12
br label %32
32: ; preds = %75, %55, %31
%33 = load i32*, i32** %6
%34 = bitcast i8* %21 to %struct.dirent*
%35 = call i32 @readdir_r(i32* %33, %struct.dirent* %34, %struct.dirent** %7)
%36 = icmp ne i32 %35, 0
%37 = xor i1 %36, true
br i1 %37, label %38, label %76
38: ; preds = %32
%39 = load %struct.dirent*, %struct.dirent** %7
%40 = icmp ne %struct.dirent* %39, null
br i1 %40, label %42, label %41
41: ; preds = %38
br label %76
42: ; preds = %38
%43 = load %struct.dirent*, %struct.dirent** %7
%44 = getelementptr inbounds %struct.dirent, %struct.dirent* %43, i32 0, i32 0
%45 = load i64, i64* %44
%46 = load i64, i64* @DT_DIR
%47 = icmp eq i64 %45, %46
br i1 %47, label %48, label %75
48: ; preds = %42
%49 = load %struct.dirent*, %struct.dirent** %7
%50 = getelementptr inbounds %struct.dirent, %struct.dirent* %49, i32 0, i32 1
%51 = load i8*, i8** %50
%52 = load i8, i8* %51
%53 = sext i8 %52 to i32
%54 = icmp eq i32 %53, 46
br i1 %54, label %55, label %56
55: ; preds = %48
br label %32
56: ; preds = %48
%57 = load i8*, i8** %3
%58 = load %struct.dirent*, %struct.dirent** %7
%59 = getelementptr inbounds %struct.dirent, %struct.dirent* %58, i32 0, i32 1
%60 = load i8*, i8** %59
%61 = call i32 @sprintf(i8* %17, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* %57, i8* %60)
%62 = call i32 @stat(i8* %17, %struct.stat* %11)
%63 = icmp ne i32 %62, 0
br i1 %63, label %74, label %64
64: ; preds = %56
%65 = getelementptr inbounds %struct.stat, %struct.stat* %11, i32 0, i32 0
%66 = load i32, i32* %65
%67 = load i32, i32* @S_IXOTH
%68 = and i32 %66, %67
%69 = icmp ne i32 %68, 0
br i1 %69, label %70, label %73
70: ; preds = %64
%71 = load i32, i32* %12
%72 = add nsw i32 %71, 1
store i32 %72, i32* %12
br label %73
73: ; preds = %70, %64
br label %74
74: ; preds = %73, %56
br label %75
75: ; preds = %74, %42
br label %32
76: ; preds = %41, %32
%77 = load i32*, i32** %6
%78 = call i32 @rewinddir(i32* %77)
%79 = load i32, i32* %12
%80 = add nsw i32 %79, 1
%81 = sext i32 %80 to i64
%82 = mul i64 8, %81
%83 = trunc i64 %82 to i32
%84 = call i8* @malloc(i32 %83)
%85 = bitcast i8* %84 to i8**
%86 = load i8***, i8**** %4
store i8** %85, i8*** %86
%87 = load i8***, i8**** %4
%88 = load i8**, i8*** %87
store i8** %88, i8*** %5
store i32 0, i32* %13
br label %89
89: ; preds = %103, %76
%90 = load i32, i32* %13
%91 = load i32, i32* %12
%92 = icmp slt i32 %90, %91
br i1 %92, label %93, label %106
93: ; preds = %89
%94 = load i32, i32* @MAXPATHLEN
%95 = sext i32 %94 to i64
%96 = mul i64 1, %95
%97 = trunc i64 %96 to i32
%98 = call i8* @malloc(i32 %97)
%99 = load i8**, i8*** %5
%100 = load i32, i32* %13
%101 = sext i32 %100 to i64
%102 = getelementptr inbounds i8*, i8** %99, i64 %101
store i8* %98, i8** %102
br label %103
103: ; preds = %93
%104 = load i32, i32* %13
%105 = add nsw i32 %104, 1
store i32 %105, i32* %13
br label %89
106: ; preds = %89
%107 = load i8**, i8*** %5
%108 = load i32, i32* %12
%109 = sext i32 %108 to i64
%110 = getelementptr inbounds i8*, i8** %107, i64 %109
store i8* null, i8** %110
store i32 0, i32* %13
br label %111
111: ; preds = %162, %134, %106
%112 = load i32*, i32** %6
%113 = bitcast i8* %21 to %struct.dirent*
%114 = call i32 @readdir_r(i32* %112, %struct.dirent* %113, %struct.dirent** %7)
%115 = icmp ne i32 %114, 0
%116 = xor i1 %115, true
br i1 %116, label %117, label %163
117: ; preds = %111
%118 = load %struct.dirent*, %struct.dirent** %7
%119 = icmp ne %struct.dirent* %118, null
br i1 %119, label %121, label %120
120: ; preds = %117
br label %163
121: ; preds = %117
%122 = load %struct.dirent*, %struct.dirent** %7
%123 = getelementptr inbounds %struct.dirent, %struct.dirent* %122, i32 0, i32 0
%124 = load i64, i64* %123
%125 = load i64, i64* @DT_DIR
%126 = icmp eq i64 %124, %125
br i1 %126, label %127, label %162
127: ; preds = %121
%128 = load %struct.dirent*, %struct.dirent** %7
%129 = getelementptr inbounds %struct.dirent, %struct.dirent* %128, i32 0, i32 1
%130 = load i8*, i8** %129
%131 = load i8, i8* %130
%132 = sext i8 %131 to i32
%133 = icmp eq i32 %132, 46
br i1 %133, label %134, label %135
134: ; preds = %127
br label %111
135: ; preds = %127
%136 = load i8*, i8** %3
%137 = load %struct.dirent*, %struct.dirent** %7
%138 = getelementptr inbounds %struct.dirent, %struct.dirent* %137, i32 0, i32 1
%139 = load i8*, i8** %138
%140 = call i32 @sprintf(i8* %17, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* %136, i8* %139)
%141 = call i32 @stat(i8* %17, %struct.stat* %11)
%142 = icmp ne i32 %141, 0
br i1 %142, label %161, label %143
143: ; preds = %135
%144 = getelementptr inbounds %struct.stat, %struct.stat* %11, i32 0, i32 0
%145 = load i32, i32* %144
%146 = load i32, i32* @S_IXOTH
%147 = and i32 %145, %146
%148 = icmp ne i32 %147, 0
br i1 %148, label %149, label %160
149: ; preds = %143
%150 = load i8**, i8*** %5
%151 = load i32, i32* %13
%152 = add nsw i32 %151, 1
store i32 %152, i32* %13
%153 = sext i32 %151 to i64
%154 = getelementptr inbounds i8*, i8** %150, i64 %153
%155 = load i8*, i8** %154
%156 = load %struct.dirent*, %struct.dirent** %7
%157 = getelementptr inbounds %struct.dirent, %struct.dirent* %156, i32 0, i32 1
%158 = load i8*, i8** %157
%159 = call i32 @strcpy(i8* %155, i8* %158)
br label %160
160: ; preds = %149, %143
br label %161
161: ; preds = %160, %135
br label %162
162: ; preds = %161, %121
br label %111
163: ; preds = %120, %111
%164 = load i32*, i32** %6
%165 = call i32 @closedir(i32* %164)
%166 = load i32, i32* %12
%167 = load i8*, i8** %8
ret i32 %166
}

LLVM IR (-O2):
%struct.dirent = type { i64, i8* }
%struct.stat = type { i32 }
@MAXPATHLEN = global i32 0
@stderr = global i32 0
@.str = [35 x i8] c"Error: Cannot open directory [%s]\0A\00"
@DT_DIR = global i64 0
@.str.1 = [6 x i8] c"%s/%s\00"
@S_IXOTH = global i32 0
define i32 @find_dirname(i8* %0, i8*** nocapture writeonly %1) {
%3 = alloca %struct.dirent*
%4 = alloca %struct.stat
%5 = bitcast %struct.dirent** %3 to i8*
store %struct.dirent* null, %struct.dirent** %3
%6 = load i32, i32* @MAXPATHLEN
%7 = zext i32 %6 to i64
%8 = tail call i8* @llvm.stacksave()
%9 = alloca i8, i64 %7
%10 = load i32, i32* @MAXPATHLEN
%11 = sext i32 %10 to i64
%12 = add nsw i64 %11, 16
%13 = alloca i8, i64 %12
%14 = bitcast %struct.stat* %4 to i8*
%15 = tail call i32* @opendir(i8* %0)
%16 = icmp eq i32* %15, null
br i1 %16, label %20, label %17
17: ; preds = %2
%18 = bitcast i8* %13 to %struct.dirent*
%19 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 0
br label %52
20: ; preds = %2
%21 = load i32, i32* @stderr
%22 = tail call i32 @fprintf(i32 %21, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0), i8* %0)
%23 = tail call i32 @exit(i32 -1)
unreachable
24: ; preds = %54, %35
%25 = call i32 @readdir_r(i32* %15, %struct.dirent* %18, %struct.dirent** %3)
%26 = icmp eq i32 %25, 0
%27 = load %struct.dirent*, %struct.dirent** %3
%28 = icmp ne %struct.dirent* %27, null
%29 = select i1 %26, i1 %28, i1 false
br i1 %29, label %30, label %55
30: ; preds = %24
%31 = getelementptr inbounds %struct.dirent, %struct.dirent* %27, i64 0, i32 0
%32 = load i64, i64* %31
%33 = load i64, i64* @DT_DIR
%34 = icmp eq i64 %32, %33
br i1 %34, label %35, label %44
35: ; preds = %30
%36 = getelementptr inbounds %struct.dirent, %struct.dirent* %27, i64 0, i32 1
%37 = load i8*, i8** %36
%38 = load i8, i8* %37
%39 = icmp eq i8 %38, 46
br i1 %39, label %24, label %40
40: ; preds = %35
%41 = call i32 @sprintf(i8* %9, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* %0, i8* %37)
%42 = call i32 @stat(i8* %9, %struct.stat* %4)
%43 = icmp eq i32 %42, 0
br i1 %43, label %45, label %44
44: ; preds = %30, %40
br label %54
45: ; preds = %40
%46 = load i32, i32* %19
%47 = load i32, i32* @S_IXOTH
%48 = and i32 %47, %46
%49 = icmp ne i32 %48, 0
%50 = zext i1 %49 to i32
%51 = add nuw nsw i32 %53, %50
br label %52
52: ; preds = %45, %17
%53 = phi i32 [ %51, %45 ], [ 0, %17 ]
br label %54
54: ; preds = %44, %52
br label %24
55: ; preds = %24
%56 = call i32 @rewinddir(i32* %15)
%57 = shl i32 %53, 3
%58 = add i32 %57, 8
%59 = call i8* @malloc(i32 %58)
%60 = bitcast i8* %59 to i8**
%61 = bitcast i8*** %1 to i8**
store i8* %59, i8** %61
%62 = icmp eq i32 %53, 0
br i1 %62, label %72, label %63
63: ; preds = %55
%64 = load i32, i32* @MAXPATHLEN
%65 = zext i32 %53 to i64
br label %66
66: ; preds = %63, %66
%67 = phi i64 [ 0, %63 ], [ %70, %66 ]
%68 = call i8* @malloc(i32 %64)
%69 = getelementptr inbounds i8*, i8** %60, i64 %67
store i8* %68, i8** %69
%70 = add nuw nsw i64 %67, 1
%71 = icmp eq i64 %70, %65
br i1 %71, label %72, label %66
72: ; preds = %66, %55
%73 = zext i32 %53 to i64
%74 = getelementptr inbounds i8*, i8** %60, i64 %73
store i8* null, i8** %74
br label %75
75: ; preds = %104, %72
%76 = phi i32 [ %105, %104 ], [ 0, %72 ]
br label %77
77: ; preds = %98, %75
br label %78
78: ; preds = %77, %89
%79 = call i32 @readdir_r(i32* %15, %struct.dirent* %18, %struct.dirent** %3)
%80 = icmp eq i32 %79, 0
%81 = load %struct.dirent*, %struct.dirent** %3
%82 = icmp ne %struct.dirent* %81, null
%83 = select i1 %80, i1 %82, i1 false
br i1 %83, label %84, label %113
84: ; preds = %78
%85 = getelementptr inbounds %struct.dirent, %struct.dirent* %81, i64 0, i32 0
%86 = load i64, i64* %85
%87 = load i64, i64* @DT_DIR
%88 = icmp eq i64 %86, %87
br i1 %88, label %89, label %98
89: ; preds = %84
%90 = getelementptr inbounds %struct.dirent, %struct.dirent* %81, i64 0, i32 1
%91 = load i8*, i8** %90
%92 = load i8, i8* %91
%93 = icmp eq i8 %92, 46
br i1 %93, label %78, label %94
94: ; preds = %89
%95 = call i32 @sprintf(i8* %9, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* %0, i8* %91)
%96 = call i32 @stat(i8* %9, %struct.stat* %4)
%97 = icmp eq i32 %96, 0
br i1 %97, label %99, label %98
98: ; preds = %84, %94, %99
br label %77
99: ; preds = %94
%100 = load i32, i32* %19
%101 = load i32, i32* @S_IXOTH
%102 = and i32 %101, %100
%103 = icmp eq i32 %102, 0
br i1 %103, label %98, label %104
104: ; preds = %99
%105 = add nuw nsw i32 %76, 1
%106 = zext i32 %76 to i64
%107 = getelementptr inbounds i8*, i8** %60, i64 %106
%108 = load i8*, i8** %107
%109 = load %struct.dirent*, %struct.dirent** %3
%110 = getelementptr inbounds %struct.dirent, %struct.dirent* %109, i64 0, i32 1
%111 = load i8*, i8** %110
%112 = call i32 @strcpy(i8* %108, i8* %111)
br label %75
113: ; preds = %78
%114 = call i32 @closedir(i32* %15)
ret i32 %53
}