#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 */
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
char *malloc(int);
int /*<<< orphan*/ *opendir(char *);
struct dirent *readdir(int /*<<< orphan*/ *);
scalar_t__ realloc(char *, int);
int /*<<< orphan*/ strcat(char *, char *);
int strlen(char *);
char *list_files_in_dir(char *dir_name) {
  DIR *d;
  struct dirent *dir;
  d = opendir(dir_name);
  char *buffer = malloc(sizeof(char) * 1024);
  int buffer_mul = 1;
  if (d) {
    while ((dir = readdir(d)) != NULL) {
      if ((strlen(buffer) + 128) > 1024) {
        buffer_mul++;
        buffer = (char *)realloc(buffer, sizeof(char) * buffer_mul * 1024);
      }
      strcat(buffer, dir->d_name);
      strcat(buffer, "\n");
    }
    closedir(d);
  }
  return buffer;
}


x86 assembly (clang, -O0):
<list_files_in_dir>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <opendir@plt>
mov    %rax,-0x10(%rbp)
mov    $0x400,%edi
call   1070 <malloc@plt>
mov    %rax,-0x20(%rbp)
movl   $0x1,-0x24(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1230 <list_files_in_dir+0xc0>
jmp    11ae <list_files_in_dir+0x3e>
mov    -0x10(%rbp),%rdi
call   1060 <readdir@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
je     1227 <list_files_in_dir+0xb7>
mov    -0x20(%rbp),%rdi
call   1040 <strlen@plt>
add    $0x80,%eax
cmp    $0x400,%eax
jle    1202 <list_files_in_dir+0x92>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
mov    -0x20(%rbp),%rdi
movslq -0x24(%rbp),%rax
shl    $0x0,%rax
shl    $0xa,%rax
mov    %eax,%esi
call   1080 <realloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rax
mov    (%rax),%rsi
call   1090 <strcat@plt>
mov    -0x20(%rbp),%rdi
lea    0xde3(%rip),%rsi
call   1090 <strcat@plt>
jmp    11ae <list_files_in_dir+0x3e>
mov    -0x10(%rbp),%rdi
call   1050 <closedir@plt>
mov    -0x20(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<list_files_in_dir>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
call   1030 <opendir@plt>
mov    %rax,%r14
mov    $0x400,%edi
call   1070 <malloc@plt>
mov    %rax,%r12
test   %r14,%r14
je     1202 <list_files_in_dir+0x92>
mov    %r14,%rdi
call   1060 <readdir@plt>
test   %rax,%rax
je     11fa <list_files_in_dir+0x8a>
mov    %rax,%rbx
mov    $0x1,%ebp
lea    0xe52(%rip),%r15
jmp    11d6 <list_files_in_dir+0x66>
mov    (%rbx),%rsi
mov    %r12,%rdi
call   1090 <strcat@plt>
mov    %r12,%rdi
mov    %r15,%rsi
call   1090 <strcat@plt>
mov    %r14,%rdi
call   1060 <readdir@plt>
mov    %rax,%rbx
test   %rax,%rax
je     11fa <list_files_in_dir+0x8a>
mov    %r12,%rdi
call   1040 <strlen@plt>
cmp    $0x381,%eax
jl     11b0 <list_files_in_dir+0x40>
add    $0x1,%ebp
mov    %ebp,%esi
shl    $0xa,%esi
mov    %r12,%rdi
call   1080 <realloc@plt>
mov    %rax,%r12
jmp    11b0 <list_files_in_dir+0x40>
mov    %r14,%rdi
call   1050 <closedir@plt>
mov    %r12,%rax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.dirent = type { i8* }
@.str = [2 x i8] c"\0A\00"
define i8* @list_files_in_dir(i8* %0) {
%2 = alloca i8*
%3 = alloca i32*
%4 = alloca %struct.dirent*
%5 = alloca i8*
%6 = alloca i32
store i8* %0, i8** %2
%7 = load i8*, i8** %2
%8 = call i32* @opendir(i8* %7)
store i32* %8, i32** %3
%9 = call i8* @malloc(i32 1024)
store i8* %9, i8** %5
store i32 1, i32* %6
%10 = load i32*, i32** %3
%11 = icmp ne i32* %10, null
br i1 %11, label %12, label %44
12: ; preds = %1
br label %13
13: ; preds = %33, %12
%14 = load i32*, i32** %3
%15 = call %struct.dirent* @readdir(i32* %14)
store %struct.dirent* %15, %struct.dirent** %4
%16 = icmp ne %struct.dirent* %15, null
br i1 %16, label %17, label %41
17: ; preds = %13
%18 = load i8*, i8** %5
%19 = call i32 @strlen(i8* %18)
%20 = add nsw i32 %19, 128
%21 = icmp sgt i32 %20, 1024
br i1 %21, label %22, label %33
22: ; preds = %17
%23 = load i32, i32* %6
%24 = add nsw i32 %23, 1
store i32 %24, i32* %6
%25 = load i8*, i8** %5
%26 = load i32, i32* %6
%27 = sext i32 %26 to i64
%28 = mul i64 1, %27
%29 = mul i64 %28, 1024
%30 = trunc i64 %29 to i32
%31 = call i64 @realloc(i8* %25, i32 %30)
%32 = inttoptr i64 %31 to i8*
store i8* %32, i8** %5
br label %33
33: ; preds = %22, %17
%34 = load i8*, i8** %5
%35 = load %struct.dirent*, %struct.dirent** %4
%36 = getelementptr inbounds %struct.dirent, %struct.dirent* %35, i32 0, i32 0
%37 = load i8*, i8** %36
%38 = call i32 @strcat(i8* %34, i8* %37)
%39 = load i8*, i8** %5
%40 = call i32 @strcat(i8* %39, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
br label %13
41: ; preds = %13
%42 = load i32*, i32** %3
%43 = call i32 @closedir(i32* %42)
br label %44
44: ; preds = %41, %1
%45 = load i8*, i8** %5
ret i8* %45
}

LLVM IR (-O2):
%struct.dirent = type { i8* }
@.str = [2 x i8] c"\0A\00"
define i8* @list_files_in_dir(i8* nocapture %0) {
%2 = tail call i32* @opendir(i8* %0)
%3 = tail call dereferenceable_or_null(1024) i8* @malloc(i32 1024)
%4 = icmp eq i32* %2, null
br i1 %4, label %31, label %5
5: ; preds = %1
%6 = tail call %struct.dirent* @readdir(i32* %2)
%7 = icmp eq %struct.dirent* %6, null
br i1 %7, label %28, label %8
8: ; preds = %5, %19
%9 = phi %struct.dirent* [ %26, %19 ], [ %6, %5 ]
%10 = phi i32 [ %21, %19 ], [ 1, %5 ]
%11 = phi i8* [ %20, %19 ], [ %3, %5 ]
%12 = tail call i32 @strlen(i8* %11)
%13 = icmp sgt i32 %12, 896
br i1 %13, label %14, label %19
14: ; preds = %8
%15 = add nsw i32 %10, 1
%16 = shl i32 %15, 10
%17 = tail call i64 @realloc(i8* %11, i32 %16)
%18 = inttoptr i64 %17 to i8*
br label %19
19: ; preds = %14, %8
%20 = phi i8* [ %18, %14 ], [ %11, %8 ]
%21 = phi i32 [ %15, %14 ], [ %10, %8 ]
%22 = getelementptr inbounds %struct.dirent, %struct.dirent* %9, i64 0, i32 0
%23 = load i8*, i8** %22
%24 = tail call i32 @strcat(i8* %20, i8* %23)
%25 = tail call i32 @strcat(i8* %20, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%26 = tail call %struct.dirent* @readdir(i32* %2)
%27 = icmp eq %struct.dirent* %26, null
br i1 %27, label %28, label %8
28: ; preds = %19, %5
%29 = phi i8* [ %3, %5 ], [ %20, %19 ]
%30 = tail call i32 @closedir(i32* %2)
br label %31
31: ; preds = %28, %1
%32 = phi i8* [ %29, %28 ], [ %3, %1 ]
ret i8* %32
}