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

/* Variables and functions */
char *calloc(int, int);
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ *opendir(char *);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ strcat(char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
size_t strlen(char *);
int listcontents(char **res) {
  uint32_t size = 128;
  while (1) {
    char tempres[size];
    memset(tempres, 0, size);
    int cont = 1;
    DIR *dp = opendir("./");
    if (dp != NULL) {
      struct dirent *ep = readdir(dp);
      while (ep && cont) {
        if ((strlen(tempres) + strlen(ep->d_name) + 1) > size) {
          size *= 2;
          cont = 0;
          break;
        }
        strcat(tempres, ep->d_name);
        strcat(tempres, "\n");
        ep = readdir(dp);
      }
      closedir(dp);
      if (cont) {
        *res = calloc(size, sizeof(char));
        tempres[strlen(tempres) - 1] = '\0';
        strcpy(*res, tempres);
        return 1;
      }
    } else {
      break;
    }
  }
  *res = 0;
  return 0;
}


x86 assembly (clang, -O0):
<listcontents>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0x80,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    %rsp,%rcx
mov    %rcx,-0x20(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdi
sub    %rcx,%rdi
mov    %rdi,-0x50(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x28(%rbp)
mov    -0x14(%rbp),%edx
xor    %esi,%esi
call   1060 <memset@plt>
movl   $0x1,-0x2c(%rbp)
lea    0xe2f(%rip),%rdi
call   1040 <opendir@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
je     12fa <listcontents+0x17a>
mov    -0x38(%rbp),%rdi
call   1090 <readdir@plt>
mov    %rax,-0x40(%rbp)
xor    %eax,%eax
cmpq   $0x0,-0x40(%rbp)
mov    %al,-0x51(%rbp)
je     120c <listcontents+0x8c>
cmpl   $0x0,-0x2c(%rbp)
setne  %al
mov    %al,-0x51(%rbp)
mov    -0x51(%rbp),%al
test   $0x1,%al
jne    121c <listcontents+0x9c>
jmp    1297 <listcontents+0x117>
mov    -0x50(%rbp),%rdi
call   1050 <strlen@plt>
mov    %rax,-0x60(%rbp)
mov    -0x40(%rbp),%rax
mov    (%rax),%rdi
call   1050 <strlen@plt>
mov    %rax,%rcx
mov    -0x60(%rbp),%rax
add    %rcx,%rax
add    $0x1,%rax
movslq -0x14(%rbp),%rcx
cmp    %rcx,%rax
jbe    1265 <listcontents+0xe5>
mov    -0x14(%rbp),%eax
shl    $0x1,%eax
mov    %eax,-0x14(%rbp)
movl   $0x0,-0x2c(%rbp)
jmp    1297 <listcontents+0x117>
mov    -0x50(%rbp),%rdi
mov    -0x40(%rbp),%rax
mov    (%rax),%rsi
call   10a0 <strcat@plt>
mov    -0x50(%rbp),%rdi
lea    0xd83(%rip),%rsi
call   10a0 <strcat@plt>
mov    -0x38(%rbp),%rdi
call   1090 <readdir@plt>
mov    %rax,-0x40(%rbp)
jmp    11f2 <listcontents+0x72>
mov    -0x38(%rbp),%rdi
call   1070 <closedir@plt>
cmpl   $0x0,-0x2c(%rbp)
je     12f5 <listcontents+0x175>
mov    -0x14(%rbp),%edi
mov    $0x1,%esi
call   1080 <calloc@plt>
mov    -0x50(%rbp),%rdi
mov    %rax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
call   1050 <strlen@plt>
mov    -0x50(%rbp),%rsi
sub    $0x1,%rax
movb   $0x0,(%rsi,%rax,1)
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1030 <strcpy@plt>
movl   $0x1,-0x4(%rbp)
movl   $0x1,-0x44(%rbp)
jmp    130d <listcontents+0x18d>
jmp    1306 <listcontents+0x186>
movl   $0x3,-0x44(%rbp)
jmp    130d <listcontents+0x18d>
movl   $0x0,-0x44(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,%rsp
mov    -0x44(%rbp),%eax
mov    %eax,-0x64(%rbp)
test   %eax,%eax
je     133d <listcontents+0x1bd>
jmp    1327 <listcontents+0x1a7>
mov    -0x64(%rbp),%eax
sub    $0x1,%eax
je     1354 <listcontents+0x1d4>
jmp    1338 <listcontents+0x1b8>
jmp    1342 <listcontents+0x1c2>
jmp    1193 <listcontents+0x13>
mov    -0x10(%rbp),%rax
movq   $0x0,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<listcontents>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0xa8,%rsp
mov    %rdi,-0x38(%rbp)
mov    %rsp,%r14
lea    -0xd0(%rbp),%r12
mov    $0x80,%eax
mov    %rax,-0x30(%rbp)
mov    %r12,%rdi
xor    %esi,%esi
mov    $0x80,%edx
call   1060 <memset@plt>
lea    0xe3f(%rip),%rdi
call   1040 <opendir@plt>
test   %rax,%rax
je     12ae <listcontents+0x12e>
mov    %rax,%rbx
mov    %r14,-0x40(%rbp)
mov    %rbx,%rdi
call   1090 <readdir@plt>
test   %rax,%rax
je     12c1 <listcontents+0x141>
mov    %rax,%r15
movslq -0x30(%rbp),%rax
mov    %rax,-0x48(%rbp)
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %rbx,%r14
mov    %r12,%rdi
call   1050 <strlen@plt>
mov    %rax,%r13
mov    (%r15),%rbx
mov    %rbx,%rdi
call   1050 <strlen@plt>
add    %r13,%rax
add    $0x1,%rax
cmp    -0x48(%rbp),%rax
ja     1260 <listcontents+0xe0>
mov    %r12,%rdi
mov    %rbx,%rsi
call   10a0 <strcat@plt>
mov    %r12,%rdi
lea    0xdc8(%rip),%rsi
call   10a0 <strcat@plt>
mov    %r14,%rbx
mov    %r14,%rdi
call   1090 <readdir@plt>
mov    %rax,%r15
test   %rax,%rax
jne    1200 <listcontents+0x80>
jmp    12c1 <listcontents+0x141>
cs nopw 0x0(%rax,%rax,1)
nop
mov    -0x30(%rbp),%rbx
add    %ebx,%ebx
mov    %r14,%rdi
call   1070 <closedir@plt>
mov    -0x40(%rbp),%rsp
mov    %rsp,%r14
mov    %rsp,%r12
lea    0xf(%rbx),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r12
mov    %r12,%rsp
mov    %r12,%rdi
xor    %esi,%esi
mov    %rbx,-0x30(%rbp)
mov    %ebx,%edx
call   1060 <memset@plt>
lea    0xd63(%rip),%rdi
call   1040 <opendir@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    11d2 <listcontents+0x52>
mov    %r14,%rsp
mov    -0x38(%rbp),%rax
movq   $0x0,(%rax)
xor    %r14d,%r14d
jmp    1304 <listcontents+0x184>
mov    %rbx,%rdi
call   1070 <closedir@plt>
mov    $0x1,%r14d
mov    -0x30(%rbp),%rdi
mov    $0x1,%esi
call   1080 <calloc@plt>
mov    %rax,%rbx
mov    -0x38(%rbp),%rax
mov    %rbx,(%rax)
mov    %r12,%rdi
call   1050 <strlen@plt>
movb   $0x0,-0x1(%rax,%r12,1)
mov    %rbx,%rdi
mov    %r12,%rsi
call   1030 <strcpy@plt>
mov    -0x40(%rbp),%rsp
mov    %r14d,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.dirent = type { i8* }
@.str = [3 x i8] c"./\00"
@.str.1 = [2 x i8] c"\0A\00"
define i32 @listcontents(i8** %0) {
%2 = alloca i32
%3 = alloca i8**
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i32
%8 = alloca i32*
%9 = alloca %struct.dirent*
%10 = alloca i32
store i8** %0, i8*** %3
store i32 128, i32* %4
br label %11
11: ; preds = %1, %75
%12 = load i32, i32* %4
%13 = zext i32 %12 to i64
%14 = call i8* @llvm.stacksave()
store i8* %14, i8** %5
%15 = alloca i8, i64 %13
store i64 %13, i64* %6
%16 = load i32, i32* %4
%17 = call i32 @memset(i8* %15, i32 0, i32 %16)
store i32 1, i32* %7
%18 = call i32* @opendir(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %18, i32** %8
%19 = load i32*, i32** %8
%20 = icmp ne i32* %19, null
br i1 %20, label %21, label %70
21: ; preds = %11
%22 = load i32*, i32** %8
%23 = call %struct.dirent* @readdir(i32* %22)
store %struct.dirent* %23, %struct.dirent** %9
br label %24
24: ; preds = %46, %21
%25 = load %struct.dirent*, %struct.dirent** %9
%26 = icmp ne %struct.dirent* %25, null
br i1 %26, label %27, label %30
27: ; preds = %24
%28 = load i32, i32* %7
%29 = icmp ne i32 %28, 0
br label %30
30: ; preds = %27, %24
%31 = phi i1 [ false, %24 ], [ %29, %27 ]
br i1 %31, label %32, label %54
32: ; preds = %30
%33 = call i64 @strlen(i8* %15)
%34 = load %struct.dirent*, %struct.dirent** %9
%35 = getelementptr inbounds %struct.dirent, %struct.dirent* %34, i32 0, i32 0
%36 = load i8*, i8** %35
%37 = call i64 @strlen(i8* %36)
%38 = add i64 %33, %37
%39 = add i64 %38, 1
%40 = load i32, i32* %4
%41 = sext i32 %40 to i64
%42 = icmp ugt i64 %39, %41
br i1 %42, label %43, label %46
43: ; preds = %32
%44 = load i32, i32* %4
%45 = mul nsw i32 %44, 2
store i32 %45, i32* %4
store i32 0, i32* %7
br label %54
46: ; preds = %32
%47 = load %struct.dirent*, %struct.dirent** %9
%48 = getelementptr inbounds %struct.dirent, %struct.dirent* %47, i32 0, i32 0
%49 = load i8*, i8** %48
%50 = call i32 @strcat(i8* %15, i8* %49)
%51 = call i32 @strcat(i8* %15, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%52 = load i32*, i32** %8
%53 = call %struct.dirent* @readdir(i32* %52)
store %struct.dirent* %53, %struct.dirent** %9
br label %24
54: ; preds = %43, %30
%55 = load i32*, i32** %8
%56 = call i32 @closedir(i32* %55)
%57 = load i32, i32* %7
%58 = icmp ne i32 %57, 0
br i1 %58, label %59, label %69
59: ; preds = %54
%60 = load i32, i32* %4
%61 = call i8* @calloc(i32 %60, i32 1)
%62 = load i8**, i8*** %3
store i8* %61, i8** %62
%63 = call i64 @strlen(i8* %15)
%64 = sub i64 %63, 1
%65 = getelementptr inbounds i8, i8* %15, i64 %64
store i8 0, i8* %65
%66 = load i8**, i8*** %3
%67 = load i8*, i8** %66
%68 = call i32 @strcpy(i8* %67, i8* %15)
store i32 1, i32* %2
store i32 1, i32* %10
br label %72
69: ; preds = %54
br label %71
70: ; preds = %11
store i32 3, i32* %10
br label %72
71: ; preds = %69
store i32 0, i32* %10
br label %72
72: ; preds = %71, %70, %59
%73 = load i8*, i8** %5
%74 = load i32, i32* %10
switch i32 %74, label %80 [
i32 0, label %75
i32 1, label %78
i32 3, label %76
]
75: ; preds = %72
br label %11
76: ; preds = %72
%77 = load i8**, i8*** %3
store i8* null, i8** %77
store i32 0, i32* %2
br label %78
78: ; preds = %76, %72
%79 = load i32, i32* %2
ret i32 %79
80: ; preds = %72
unreachable
}

LLVM IR (-O2):
%struct.dirent = type { i8* }
@.str = [3 x i8] c"./\00"
@.str.1 = [2 x i8] c"\0A\00"
define i32 @listcontents(i8** nocapture writeonly %0) {
%2 = call i8* @llvm.stacksave()
%3 = alloca [128 x i8]
%4 = getelementptr inbounds [128 x i8], [128 x i8]* %3, i64 0, i64 0
%5 = call i32 @memset(i8* %4, i32 0, i32 128)
%6 = call i32* @opendir(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%7 = icmp eq i32* %6, null
br i1 %7, label %47, label %8
8: ; preds = %1, %38
%9 = phi i32* [ %45, %38 ], [ %6, %1 ]
%10 = phi i8* [ %43, %38 ], [ %4, %1 ]
%11 = phi i8* [ %42, %38 ], [ %2, %1 ]
%12 = phi i32 [ %39, %38 ], [ 128, %1 ]
%13 = call %struct.dirent* @readdir(i32* %9)
%14 = icmp eq %struct.dirent* %13, null
br i1 %14, label %31, label %15
15: ; preds = %8
%16 = sext i32 %12 to i64
br label %17
17: ; preds = %15, %26
%18 = phi %struct.dirent* [ %13, %15 ], [ %29, %26 ]
%19 = call i64 @strlen(i8* %10)
%20 = getelementptr inbounds %struct.dirent, %struct.dirent* %18, i64 0, i32 0
%21 = load i8*, i8** %20
%22 = call i64 @strlen(i8* dereferenceable(1) %21)
%23 = add i64 %19, 1
%24 = add i64 %23, %22
%25 = icmp ugt i64 %24, %16
br i1 %25, label %38, label %26
26: ; preds = %17
%27 = call i32 @strcat(i8* %10, i8* %21)
%28 = call i32 @strcat(i8* %10, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%29 = call %struct.dirent* @readdir(i32* %9)
%30 = icmp eq %struct.dirent* %29, null
br i1 %30, label %31, label %17
31: ; preds = %8, %26
%32 = call i32 @closedir(i32* %9)
%33 = call i8* @calloc(i32 %12, i32 1)
store i8* %33, i8** %0
%34 = call i64 @strlen(i8* %10)
%35 = add i64 %34, -1
%36 = getelementptr inbounds i8, i8* %10, i64 %35
store i8 0, i8* %36
%37 = call i32 @strcpy(i8* %33, i8* %10)
br label %49
38: ; preds = %17
%39 = shl nsw i32 %12, 1
%40 = call i32 @closedir(i32* %9)
%41 = zext i32 %39 to i64
%42 = call i8* @llvm.stacksave()
%43 = alloca i8, i64 %41
%44 = call i32 @memset(i8* %43, i32 0, i32 %39)
%45 = call i32* @opendir(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%46 = icmp eq i32* %45, null
br i1 %46, label %47, label %8
47: ; preds = %38, %1
%48 = phi i8* [ %2, %1 ], [ %42, %38 ]
store i8* null, i8** %0
br label %49
49: ; preds = %31, %47
%50 = phi i32 [ 0, %47 ], [ 1, %31 ]
ret i32 %50
}