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

/* Variables and functions */
scalar_t__ DT_DIR;
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
scalar_t__ malloc(int);
int /*<<< orphan*/ *opendir(char *);
int /*<<< orphan*/ perror(char *);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ realloc(char *, int);
int /*<<< orphan*/ strcat(char *, char *);
int strlen(char *);
char *client_list_files_in_dir(char *dir_name) {
  DIR *d;
  struct dirent *dir;
  d = opendir(dir_name);
  char *buffer = (char *)malloc(sizeof(char) * 1024);
  int buffer_mul = 1;
  if (d) {
    while ((dir = readdir(d)) != NULL) {
      if (dir->d_type != DT_DIR) {
        if ((strlen(buffer) + 128) > 1024) {
          buffer_mul++;
          realloc(buffer, sizeof(char) * buffer_mul * 1024);
        }
        strcat(buffer, dir->d_name);
        strcat(buffer, "\n");
      }
    }
    closedir(d);
  } else {
    perror("opendir");
  }
  return buffer;
}


x86 assembly (clang, -O0):
<client_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     1259 <client_list_files_in_dir+0xd9>
jmp    11be <client_list_files_in_dir+0x3e>
mov    -0x10(%rbp),%rdi
call   1060 <readdir@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
je     124b <client_list_files_in_dir+0xcb>
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
mov    0x2dfd(%rip),%rcx
cmp    (%rcx),%rax
je     1246 <client_list_files_in_dir+0xc6>
mov    -0x20(%rbp),%rdi
call   1040 <strlen@plt>
add    $0x80,%eax
cmp    $0x400,%eax
jle    1225 <client_list_files_in_dir+0xa5>
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    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rax
mov    0x8(%rax),%rsi
call   10a0 <strcat@plt>
mov    -0x20(%rbp),%rdi
lea    0xdbf(%rip),%rsi
call   10a0 <strcat@plt>
jmp    11be <client_list_files_in_dir+0x3e>
mov    -0x10(%rbp),%rdi
call   1050 <closedir@plt>
jmp    1265 <client_list_files_in_dir+0xe5>
lea    0xda2(%rip),%rdi
call   1090 <perror@plt>
mov    -0x20(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<client_list_files_in_dir>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
call   1030 <opendir@plt>
mov    %rax,%r14
mov    $0x400,%edi
call   1070 <malloc@plt>
mov    %rax,%r12
test   %r14,%r14
je     123b <client_list_files_in_dir+0xbb>
mov    %r14,%rdi
call   1060 <readdir@plt>
test   %rax,%rax
je     1221 <client_list_files_in_dir+0xa1>
mov    %rax,%rbx
mov    $0x1,%ebp
mov    0x2e1b(%rip),%r13
lea    0xe34(%rip),%r15
jmp    11f7 <client_list_files_in_dir+0x77>
xchg   %ax,%ax
mov    0x8(%rbx),%rsi
mov    %r12,%rdi
call   10a0 <strcat@plt>
mov    %r12,%rdi
mov    %r15,%rsi
call   10a0 <strcat@plt>
mov    %r14,%rdi
call   1060 <readdir@plt>
mov    %rax,%rbx
test   %rax,%rax
je     1221 <client_list_files_in_dir+0xa1>
mov    (%rbx),%rax
cmp    0x0(%r13),%rax
je     11e7 <client_list_files_in_dir+0x67>
mov    %r12,%rdi
call   1040 <strlen@plt>
cmp    $0x381,%eax
jl     11d0 <client_list_files_in_dir+0x50>
add    $0x1,%ebp
mov    %ebp,%esi
shl    $0xa,%esi
mov    %r12,%rdi
call   1080 <realloc@plt>
jmp    11d0 <client_list_files_in_dir+0x50>
mov    %r14,%rdi
call   1050 <closedir@plt>
mov    %r12,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdc0(%rip),%rdi
call   1090 <perror@plt>
jmp    1229 <client_list_files_in_dir+0xa9>

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

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