#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*/ LOG_NOTICE;
char **calloc(size_t, int);
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *);
int /*<<< orphan*/ *opendir(char const *);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ rewinddir(int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
scalar_t__ strcmp(char *, char *);
char *strdup(int /*<<< orphan*/);
int /*<<< orphan*/ syslog(int /*<<< orphan*/, char *, char *);
size_t getDCIMDirectories(const char *path, char ***ls) {
  size_t numberOfDirectories = 0;
  size_t length = 0;
  DIR *dp = NULL;
  struct dirent *ep = NULL;
  dp = opendir(path);
  if (NULL == dp) {
    fprintf(stderr, "no such directory: '%s'", path);
    return 0;
  }
  *ls = NULL;
  ep = readdir(dp);
  while (NULL != ep) {
    if (strcmp(strdup(ep->d_name), ".") != 0) {
      if (strcmp(strdup(ep->d_name), "..") != 0) {
        numberOfDirectories++;
      }
    }
    ep = readdir(dp);
  }
  rewinddir(dp);
  *ls = calloc(numberOfDirectories, sizeof(char *));
  numberOfDirectories = 0;
  ep = readdir(dp);
  while (NULL != ep) {
    if (strcmp(strdup(ep->d_name), ".") != 0) {
      if (strcmp(strdup(ep->d_name), "..") != 0) {
        (*ls)[numberOfDirectories++] = strdup(ep->d_name);
        syslog(LOG_NOTICE, "Directory %s exists.", strdup(ep->d_name));
      }
    }
    ep = readdir(dp);
  }
  closedir(dp);
  return numberOfDirectories;
}


x86 assembly (clang, -O0):
<getDCIMDirectories>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
movq   $0x0,-0x38(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <opendir@plt>
mov    %rax,-0x30(%rbp)
xor    %eax,%eax
cmp    -0x30(%rbp),%rax
jne    11ff <getDCIMDirectories+0x6f>
mov    0x2e00(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe13(%rip),%rsi
call   1080 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
jmp    1391 <getDCIMDirectories+0x201>
mov    -0x18(%rbp),%rax
movq   $0x0,(%rax)
mov    -0x30(%rbp),%rdi
call   1090 <readdir@plt>
mov    %rax,-0x38(%rbp)
xor    %eax,%eax
cmp    -0x38(%rbp),%rax
je     128e <getDCIMDirectories+0xfe>
mov    -0x38(%rbp),%rax
mov    (%rax),%edi
call   10b0 <strdup@plt>
mov    %rax,%rdi
lea    0xde1(%rip),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%rax
je     127c <getDCIMDirectories+0xec>
mov    -0x38(%rbp),%rax
mov    (%rax),%edi
call   10b0 <strdup@plt>
mov    %rax,%rdi
lea    0xdbc(%rip),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%rax
je     1277 <getDCIMDirectories+0xe7>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    127c <getDCIMDirectories+0xec>
mov    -0x30(%rbp),%rdi
call   1090 <readdir@plt>
mov    %rax,-0x38(%rbp)
jmp    1217 <getDCIMDirectories+0x87>
mov    -0x30(%rbp),%rdi
call   1040 <rewinddir@plt>
mov    -0x20(%rbp),%rdi
mov    $0x8,%esi
call   1060 <calloc@plt>
mov    %rax,%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
movq   $0x0,-0x20(%rbp)
mov    -0x30(%rbp),%rdi
call   1090 <readdir@plt>
mov    %rax,-0x38(%rbp)
xor    %eax,%eax
cmp    -0x38(%rbp),%rax
je     1380 <getDCIMDirectories+0x1f0>
mov    -0x38(%rbp),%rax
mov    (%rax),%edi
call   10b0 <strdup@plt>
mov    %rax,%rdi
lea    0xd34(%rip),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%rax
je     136e <getDCIMDirectories+0x1de>
mov    -0x38(%rbp),%rax
mov    (%rax),%edi
call   10b0 <strdup@plt>
mov    %rax,%rdi
lea    0xd0f(%rip),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%rax
je     1369 <getDCIMDirectories+0x1d9>
mov    -0x38(%rbp),%rax
mov    (%rax),%edi
call   10b0 <strdup@plt>
mov    %rax,%rdx
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
mov    -0x20(%rbp),%rcx
mov    %rcx,%rsi
add    $0x1,%rsi
mov    %rsi,-0x20(%rbp)
mov    %rdx,(%rax,%rcx,8)
mov    0x2c91(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x38(%rbp),%rax
mov    (%rax),%edi
call   10b0 <strdup@plt>
mov    -0x3c(%rbp),%edi
mov    %rax,%rdx
lea    0xcb7(%rip),%rsi
call   10a0 <syslog@plt>
jmp    136e <getDCIMDirectories+0x1de>
mov    -0x30(%rbp),%rdi
call   1090 <readdir@plt>
mov    %rax,-0x38(%rbp)
jmp    12c4 <getDCIMDirectories+0x134>
mov    -0x30(%rbp),%rdi
call   1050 <closedir@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getDCIMDirectories>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r13
mov    %rdi,%rbx
call   1030 <opendir@plt>
test   %rax,%rax
je     1223 <getDCIMDirectories+0x93>
mov    %rax,%r14
movq   $0x0,0x0(%r13)
mov    %rax,%rdi
call   1090 <readdir@plt>
test   %rax,%rax
je     1243 <getDCIMDirectories+0xb3>
mov    %rax,%rbx
xor    %ebp,%ebp
lea    0xe46(%rip),%r15
lea    0xe3e(%rip),%r12
jmp    11f0 <getDCIMDirectories+0x60>
nopl   0x0(%rax)
mov    %r14,%rdi
call   1090 <readdir@plt>
mov    %rax,%rbx
test   %rax,%rax
je     1245 <getDCIMDirectories+0xb5>
mov    (%rbx),%edi
call   10b0 <strdup@plt>
mov    %rax,%rdi
mov    %r15,%rsi
call   1070 <strcmp@plt>
test   %rax,%rax
je     11e0 <getDCIMDirectories+0x50>
mov    (%rbx),%edi
call   10b0 <strdup@plt>
mov    %rax,%rdi
mov    %r12,%rsi
call   1070 <strcmp@plt>
cmp    $0x1,%rax
sbb    $0xffffffffffffffff,%rbp
jmp    11e0 <getDCIMDirectories+0x50>
mov    0x2db6(%rip),%rax
mov    (%rax),%edi
lea    0xdcd(%rip),%rsi
mov    %rbx,%rdx
call   1080 <fprintf@plt>
xor    %r15d,%r15d
jmp    1307 <getDCIMDirectories+0x177>
xor    %ebp,%ebp
mov    %r14,%rdi
call   1040 <rewinddir@plt>
mov    %rbp,%rdi
mov    $0x8,%esi
call   1060 <calloc@plt>
mov    %rax,0x0(%r13)
mov    %r14,%rdi
call   1090 <readdir@plt>
test   %rax,%rax
je     12fc <getDCIMDirectories+0x16c>
mov    %rax,%rbp
xor    %r15d,%r15d
lea    0xd9d(%rip),%r12
jmp    1290 <getDCIMDirectories+0x100>
xchg   %ax,%ax
mov    %r14,%rdi
call   1090 <readdir@plt>
mov    %rax,%rbp
test   %rax,%rax
je     12ff <getDCIMDirectories+0x16f>
mov    0x0(%rbp),%edi
call   10b0 <strdup@plt>
mov    %rax,%rdi
mov    %r12,%rsi
call   1070 <strcmp@plt>
test   %rax,%rax
je     1280 <getDCIMDirectories+0xf0>
mov    0x0(%rbp),%edi
call   10b0 <strdup@plt>
mov    %rax,%rdi
lea    0xd5e(%rip),%rsi
call   1070 <strcmp@plt>
test   %rax,%rax
je     1280 <getDCIMDirectories+0xf0>
mov    0x0(%rbp),%edi
call   10b0 <strdup@plt>
mov    0x0(%r13),%rcx
mov    %rax,(%rcx,%r15,8)
add    $0x1,%r15
mov    0x2cf9(%rip),%rax
mov    (%rax),%ebx
mov    0x0(%rbp),%edi
call   10b0 <strdup@plt>
mov    %ebx,%edi
lea    0xd29(%rip),%rsi
mov    %rax,%rdx
call   10a0 <syslog@plt>
jmp    1280 <getDCIMDirectories+0xf0>
xor    %r15d,%r15d
mov    %r14,%rdi
call   1050 <closedir@plt>
mov    %r15,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.dirent = type { i32 }
@stderr = global i32 0
@.str = [24 x i8] c"no such directory: '%s'\00"
@.str.1 = [2 x i8] c".\00"
@.str.2 = [3 x i8] c"..\00"
@LOG_NOTICE = global i32 0
@.str.3 = [21 x i8] c"Directory %s exists.\00"
define i64 @getDCIMDirectories(i8* %0, i8*** %1) {
%3 = alloca i64
%4 = alloca i8*
%5 = alloca i8***
%6 = alloca i64
%7 = alloca i64
%8 = alloca i32*
%9 = alloca %struct.dirent*
store i8* %0, i8** %4
store i8*** %1, i8**** %5
store i64 0, i64* %6
store i64 0, i64* %7
store i32* null, i32** %8
store %struct.dirent* null, %struct.dirent** %9
%10 = load i8*, i8** %4
%11 = call i32* @opendir(i8* %10)
store i32* %11, i32** %8
%12 = load i32*, i32** %8
%13 = icmp eq i32* null, %12
br i1 %13, label %14, label %18
14: ; preds = %2
%15 = load i32, i32* @stderr
%16 = load i8*, i8** %4
%17 = call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i8* %16)
store i64 0, i64* %3
br label %95
18: ; preds = %2
%19 = load i8***, i8**** %5
store i8** null, i8*** %19
%20 = load i32*, i32** %8
%21 = call %struct.dirent* @readdir(i32* %20)
store %struct.dirent* %21, %struct.dirent** %9
br label %22
22: ; preds = %43, %18
%23 = load %struct.dirent*, %struct.dirent** %9
%24 = icmp ne %struct.dirent* null, %23
br i1 %24, label %25, label %46
25: ; preds = %22
%26 = load %struct.dirent*, %struct.dirent** %9
%27 = getelementptr inbounds %struct.dirent, %struct.dirent* %26, i32 0, i32 0
%28 = load i32, i32* %27
%29 = call i8* @strdup(i32 %28)
%30 = call i64 @strcmp(i8* %29, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%31 = icmp ne i64 %30, 0
br i1 %31, label %32, label %43
32: ; preds = %25
%33 = load %struct.dirent*, %struct.dirent** %9
%34 = getelementptr inbounds %struct.dirent, %struct.dirent* %33, i32 0, i32 0
%35 = load i32, i32* %34
%36 = call i8* @strdup(i32 %35)
%37 = call i64 @strcmp(i8* %36, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%38 = icmp ne i64 %37, 0
br i1 %38, label %39, label %42
39: ; preds = %32
%40 = load i64, i64* %6
%41 = add i64 %40, 1
store i64 %41, i64* %6
br label %42
42: ; preds = %39, %32
br label %43
43: ; preds = %42, %25
%44 = load i32*, i32** %8
%45 = call %struct.dirent* @readdir(i32* %44)
store %struct.dirent* %45, %struct.dirent** %9
br label %22
46: ; preds = %22
%47 = load i32*, i32** %8
%48 = call i32 @rewinddir(i32* %47)
%49 = load i64, i64* %6
%50 = call i8** @calloc(i64 %49, i32 8)
%51 = load i8***, i8**** %5
store i8** %50, i8*** %51
store i64 0, i64* %6
%52 = load i32*, i32** %8
%53 = call %struct.dirent* @readdir(i32* %52)
store %struct.dirent* %53, %struct.dirent** %9
br label %54
54: ; preds = %88, %46
%55 = load %struct.dirent*, %struct.dirent** %9
%56 = icmp ne %struct.dirent* null, %55
br i1 %56, label %57, label %91
57: ; preds = %54
%58 = load %struct.dirent*, %struct.dirent** %9
%59 = getelementptr inbounds %struct.dirent, %struct.dirent* %58, i32 0, i32 0
%60 = load i32, i32* %59
%61 = call i8* @strdup(i32 %60)
%62 = call i64 @strcmp(i8* %61, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%63 = icmp ne i64 %62, 0
br i1 %63, label %64, label %88
64: ; preds = %57
%65 = load %struct.dirent*, %struct.dirent** %9
%66 = getelementptr inbounds %struct.dirent, %struct.dirent* %65, i32 0, i32 0
%67 = load i32, i32* %66
%68 = call i8* @strdup(i32 %67)
%69 = call i64 @strcmp(i8* %68, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%70 = icmp ne i64 %69, 0
br i1 %70, label %71, label %87
71: ; preds = %64
%72 = load %struct.dirent*, %struct.dirent** %9
%73 = getelementptr inbounds %struct.dirent, %struct.dirent* %72, i32 0, i32 0
%74 = load i32, i32* %73
%75 = call i8* @strdup(i32 %74)
%76 = load i8***, i8**** %5
%77 = load i8**, i8*** %76
%78 = load i64, i64* %6
%79 = add i64 %78, 1
store i64 %79, i64* %6
%80 = getelementptr inbounds i8*, i8** %77, i64 %78
store i8* %75, i8** %80
%81 = load i32, i32* @LOG_NOTICE
%82 = load %struct.dirent*, %struct.dirent** %9
%83 = getelementptr inbounds %struct.dirent, %struct.dirent* %82, i32 0, i32 0
%84 = load i32, i32* %83
%85 = call i8* @strdup(i32 %84)
%86 = call i32 @syslog(i32 %81, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0), i8* %85)
br label %87
87: ; preds = %71, %64
br label %88
88: ; preds = %87, %57
%89 = load i32*, i32** %8
%90 = call %struct.dirent* @readdir(i32* %89)
store %struct.dirent* %90, %struct.dirent** %9
br label %54
91: ; preds = %54
%92 = load i32*, i32** %8
%93 = call i32 @closedir(i32* %92)
%94 = load i64, i64* %6
store i64 %94, i64* %3
br label %95
95: ; preds = %91, %14
%96 = load i64, i64* %3
ret i64 %96
}

LLVM IR (-O2):
%struct.dirent = type { i32 }
@stderr = global i32 0
@.str = [24 x i8] c"no such directory: '%s'\00"
@.str.1 = [2 x i8] c".\00"
@.str.2 = [3 x i8] c"..\00"
@LOG_NOTICE = global i32 0
@.str.3 = [21 x i8] c"Directory %s exists.\00"
define i64 @getDCIMDirectories(i8* %0, i8*** nocapture %1) {
%3 = tail call i32* @opendir(i8* %0)
%4 = icmp eq i32* %3, null
br i1 %4, label %5, label %8
5: ; preds = %2
%6 = load i32, i32* @stderr
%7 = tail call i32 @fprintf(i32 %6, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %66
8: ; preds = %2
store i8** null, i8*** %1
%9 = tail call %struct.dirent* @readdir(i32* %3)
%10 = icmp eq %struct.dirent* %9, null
br i1 %10, label %30, label %11
11: ; preds = %8, %26
%12 = phi %struct.dirent* [ %28, %26 ], [ %9, %8 ]
%13 = phi i64 [ %27, %26 ], [ 0, %8 ]
%14 = getelementptr inbounds %struct.dirent, %struct.dirent* %12, i64 0, i32 0
%15 = load i32, i32* %14
%16 = tail call i8* @strdup(i32 %15)
%17 = tail call i64 @strcmp(i8* %16, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%18 = icmp eq i64 %17, 0
br i1 %18, label %26, label %19
19: ; preds = %11
%20 = load i32, i32* %14
%21 = tail call i8* @strdup(i32 %20)
%22 = tail call i64 @strcmp(i8* %21, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%23 = icmp ne i64 %22, 0
%24 = zext i1 %23 to i64
%25 = add i64 %13, %24
br label %26
26: ; preds = %19, %11
%27 = phi i64 [ %13, %11 ], [ %25, %19 ]
%28 = tail call %struct.dirent* @readdir(i32* %3)
%29 = icmp eq %struct.dirent* %28, null
br i1 %29, label %30, label %11
30: ; preds = %26, %8
%31 = phi i64 [ 0, %8 ], [ %27, %26 ]
%32 = tail call i32 @rewinddir(i32* %3)
%33 = tail call i8** @calloc(i64 %31, i32 8)
store i8** %33, i8*** %1
%34 = tail call %struct.dirent* @readdir(i32* %3)
%35 = icmp eq %struct.dirent* %34, null
br i1 %35, label %63, label %36
36: ; preds = %30, %59
%37 = phi %struct.dirent* [ %61, %59 ], [ %34, %30 ]
%38 = phi i64 [ %60, %59 ], [ 0, %30 ]
%39 = getelementptr inbounds %struct.dirent, %struct.dirent* %37, i64 0, i32 0
%40 = load i32, i32* %39
%41 = tail call i8* @strdup(i32 %40)
%42 = tail call i64 @strcmp(i8* %41, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%43 = icmp eq i64 %42, 0
br i1 %43, label %59, label %44
44: ; preds = %36
%45 = load i32, i32* %39
%46 = tail call i8* @strdup(i32 %45)
%47 = tail call i64 @strcmp(i8* %46, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%48 = icmp eq i64 %47, 0
br i1 %48, label %59, label %49
49: ; preds = %44
%50 = load i32, i32* %39
%51 = tail call i8* @strdup(i32 %50)
%52 = load i8**, i8*** %1
%53 = add i64 %38, 1
%54 = getelementptr inbounds i8*, i8** %52, i64 %38
store i8* %51, i8** %54
%55 = load i32, i32* @LOG_NOTICE
%56 = load i32, i32* %39
%57 = tail call i8* @strdup(i32 %56)
%58 = tail call i32 @syslog(i32 %55, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0), i8* %57)
br label %59
59: ; preds = %44, %49, %36
%60 = phi i64 [ %53, %49 ], [ %38, %44 ], [ %38, %36 ]
%61 = tail call %struct.dirent* @readdir(i32* %3)
%62 = icmp eq %struct.dirent* %61, null
br i1 %62, label %63, label %36
63: ; preds = %59, %30
%64 = phi i64 [ 0, %30 ], [ %60, %59 ]
%65 = tail call i32 @closedir(i32* %3)
br label %66
66: ; preds = %63, %5
%67 = phi i64 [ 0, %5 ], [ %64, %63 ]
ret i64 %67
}