#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 */
scalar_t__ calloc(int, int);
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ memcpy(char *, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ *opendir(char *);
int /*<<< orphan*/ perror(char *);
int readdir_r(int /*<<< orphan*/ *, struct dirent *, struct dirent **);
int /*<<< orphan*/ strlen(int /*<<< orphan*/);
char **readDir(char *path) {
  char **returnBuffer = (char **)calloc(512, sizeof(char *));
  int return_code;
  DIR *dir;
  struct dirent *entry =
      (struct dirent *)calloc(sizeof(struct dirent) + 256, 1);
  struct dirent *result;
  if ((dir = opendir(path)) == NULL)
    perror("opendir() error");
  else {
    int index = 0;
    while (1) {
      return_code = readdir_r(dir, entry, &result);
      if (return_code == 0)
        break;
      returnBuffer[index] = (char *)calloc(256, sizeof(char));
      memcpy(returnBuffer[index], entry->d_name, strlen(entry->d_name));
      index++;
    }
    closedir(dir);
  }
  return returnBuffer;
}


x86 assembly (clang, -O0):
<readDir>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    $0x200,%edi
mov    $0x8,%esi
call   1070 <calloc@plt>
mov    %rax,-0x10(%rbp)
mov    $0x104,%edi
mov    $0x1,%esi
call   1070 <calloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <opendir@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    11ca <readDir+0x5a>
lea    0xe40(%rip),%rdi
call   1090 <perror@plt>
jmp    125b <readDir+0xeb>
movl   $0x0,-0x34(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x28(%rbp),%rsi
lea    -0x30(%rbp),%rdx
call   1030 <readdir_r@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jne    11f4 <readDir+0x84>
jmp    1252 <readDir+0xe2>
mov    $0x100,%edi
mov    $0x1,%esi
call   1070 <calloc@plt>
mov    %rax,%rdx
mov    -0x10(%rbp),%rax
movslq -0x34(%rbp),%rcx
mov    %rdx,(%rax,%rcx,8)
mov    -0x10(%rbp),%rax
movslq -0x34(%rbp),%rcx
mov    (%rax,%rcx,8),%rax
mov    %rax,-0x40(%rbp)
mov    -0x28(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x38(%rbp)
mov    -0x28(%rbp),%rax
mov    (%rax),%edi
call   1050 <strlen@plt>
mov    -0x40(%rbp),%rdi
mov    -0x38(%rbp),%esi
mov    %eax,%edx
call   1080 <memcpy@plt>
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    11d1 <readDir+0x61>
mov    -0x20(%rbp),%rdi
call   1060 <closedir@plt>
mov    -0x10(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readDir>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdi,%r15
mov    $0x200,%edi
mov    $0x8,%esi
call   1070 <calloc@plt>
mov    %rax,0x8(%rsp)
mov    $0x104,%edi
mov    $0x1,%esi
call   1070 <calloc@plt>
mov    %rax,%r13
mov    %r15,%rdi
call   1040 <opendir@plt>
test   %rax,%rax
je     123e <readDir+0xce>
mov    %rax,%r15
lea    0x10(%rsp),%rdx
mov    %rax,%rdi
mov    %r13,%rsi
call   1030 <readdir_r@plt>
test   %eax,%eax
je     1222 <readDir+0xb2>
lea    0x10(%rsp),%r12
mov    0x8(%rsp),%r14
nopl   0x0(%rax)
mov    $0x100,%edi
mov    $0x1,%esi
call   1070 <calloc@plt>
mov    %rax,%rbx
mov    %rax,(%r14)
mov    0x0(%r13),%ebp
mov    %ebp,%edi
call   1050 <strlen@plt>
mov    %rbx,%rdi
mov    %ebp,%esi
mov    %eax,%edx
call   1080 <memcpy@plt>
mov    %r15,%rdi
mov    %r13,%rsi
mov    %r12,%rdx
call   1030 <readdir_r@plt>
add    $0x8,%r14
test   %eax,%eax
jne    11e0 <readDir+0x70>
mov    %r15,%rdi
call   1060 <closedir@plt>
mov    0x8(%rsp),%rax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdbb(%rip),%rdi
call   1090 <perror@plt>
jmp    122a <readDir+0xba>

LLVM IR (-O0):
%struct.dirent = type { i32 }
@.str = [16 x i8] c"opendir() error\00"
define i8** @readDir(i8* %0) {
%2 = alloca i8*
%3 = alloca i8**
%4 = alloca i32
%5 = alloca i32*
%6 = alloca %struct.dirent*
%7 = alloca %struct.dirent*
%8 = alloca i32
store i8* %0, i8** %2
%9 = call i64 @calloc(i32 512, i32 8)
%10 = inttoptr i64 %9 to i8**
store i8** %10, i8*** %3
%11 = call i64 @calloc(i32 260, i32 1)
%12 = inttoptr i64 %11 to %struct.dirent*
store %struct.dirent* %12, %struct.dirent** %6
%13 = load i8*, i8** %2
%14 = call i32* @opendir(i8* %13)
store i32* %14, i32** %5
%15 = icmp eq i32* %14, null
br i1 %15, label %16, label %18
16: ; preds = %1
%17 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
br label %51
18: ; preds = %1
store i32 0, i32* %8
br label %19
19: ; preds = %18, %26
%20 = load i32*, i32** %5
%21 = load %struct.dirent*, %struct.dirent** %6
%22 = call i32 @readdir_r(i32* %20, %struct.dirent* %21, %struct.dirent** %7)
store i32 %22, i32* %4
%23 = load i32, i32* %4
%24 = icmp eq i32 %23, 0
br i1 %24, label %25, label %26
25: ; preds = %19
br label %48
26: ; preds = %19
%27 = call i64 @calloc(i32 256, i32 1)
%28 = inttoptr i64 %27 to i8*
%29 = load i8**, i8*** %3
%30 = load i32, i32* %8
%31 = sext i32 %30 to i64
%32 = getelementptr inbounds i8*, i8** %29, i64 %31
store i8* %28, i8** %32
%33 = load i8**, i8*** %3
%34 = load i32, i32* %8
%35 = sext i32 %34 to i64
%36 = getelementptr inbounds i8*, i8** %33, i64 %35
%37 = load i8*, i8** %36
%38 = load %struct.dirent*, %struct.dirent** %6
%39 = getelementptr inbounds %struct.dirent, %struct.dirent* %38, i32 0, i32 0
%40 = load i32, i32* %39
%41 = load %struct.dirent*, %struct.dirent** %6
%42 = getelementptr inbounds %struct.dirent, %struct.dirent* %41, i32 0, i32 0
%43 = load i32, i32* %42
%44 = call i32 @strlen(i32 %43)
%45 = call i32 @memcpy(i8* %37, i32 %40, i32 %44)
%46 = load i32, i32* %8
%47 = add nsw i32 %46, 1
store i32 %47, i32* %8
br label %19
48: ; preds = %25
%49 = load i32*, i32** %5
%50 = call i32 @closedir(i32* %49)
br label %51
51: ; preds = %48, %16
%52 = load i8**, i8*** %3
ret i8** %52
}

LLVM IR (-O2):
%struct.dirent = type { i32 }
@.str = [16 x i8] c"opendir() error\00"
define i8** @readDir(i8* nocapture %0) {
%2 = alloca %struct.dirent*
%3 = tail call i64 @calloc(i32 512, i32 8)
%4 = inttoptr i64 %3 to i8**
%5 = tail call i64 @calloc(i32 260, i32 1)
%6 = inttoptr i64 %5 to %struct.dirent*
%7 = bitcast %struct.dirent** %2 to i8*
%8 = tail call i32* @opendir(i8* %0)
%9 = icmp eq i32* %8, null
br i1 %9, label %15, label %10
10: ; preds = %1
%11 = call i32 @readdir_r(i32* %8, %struct.dirent* %6, %struct.dirent** %2)
%12 = icmp eq i32 %11, 0
br i1 %12, label %28, label %13
13: ; preds = %10
%14 = getelementptr inbounds %struct.dirent, %struct.dirent* %6, i64 0, i32 0
br label %17
15: ; preds = %1
%16 = tail call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
br label %30
17: ; preds = %13, %17
%18 = phi i64 [ 0, %13 ], [ %25, %17 ]
%19 = call i64 @calloc(i32 256, i32 1)
%20 = inttoptr i64 %19 to i8*
%21 = getelementptr inbounds i8*, i8** %4, i64 %18
store i8* %20, i8** %21
%22 = load i32, i32* %14
%23 = call i32 @strlen(i32 %22)
%24 = call i32 @memcpy(i8* %20, i32 %22, i32 %23)
%25 = add nuw i64 %18, 1
%26 = call i32 @readdir_r(i32* %8, %struct.dirent* %6, %struct.dirent** %2)
%27 = icmp eq i32 %26, 0
br i1 %27, label %28, label %17
28: ; preds = %17, %10
%29 = call i32 @closedir(i32* %8)
br label %30
30: ; preds = %28, %15
ret i8** %4
}