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

/* Variables and functions */
int PATH_MAX;
int /*<<< orphan*/ addtobuffer(char **, int *, char *);
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *opendir(char *);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ sprintf(char *, char *, char *, ...);
int stat(char *, struct stat *);
char *strdup(char *);
scalar_t__ strncmp(char *, char *, int);
char *clientdata(char *cpath) {
  char *res = NULL;
  int ressz = 0;
  DIR *cdir;
  struct dirent *d;
  char fn[PATH_MAX];
  struct stat st;
  int n;
  FILE *fd;
  char buf[4096];
  cdir = opendir(cpath);
  while ((d = readdir(cdir)) != NULL) {
    if (strncmp(d->d_name, "client_", 7) != 0)
      continue;
    sprintf(fn, "%s/%s", cpath, d->d_name);
    if (stat(fn, &st) == -1)
      continue;
    fd = fopen(fn, "r");
    if (fd == NULL)
      continue;
    sprintf(buf, "[%s]\n", d->d_name + 7);
    addtobuffer(&res, &ressz, buf);
    while ((n = fread(buf, 1, sizeof(buf) - 1, fd)) > 0) {
      *(buf + n) = '\0';
      addtobuffer(&res, &ressz, buf);
    }
    fclose(fd);
  }
  closedir(cdir);
  if (!res)
    res = strdup("");
  return res;
}


x86 assembly (clang, -O0):
<clientdata>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1060,%rsp
mov    %rdi,-0x8(%rbp)
movq   $0x0,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
mov    0x2e1b(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x30(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x1058(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x38(%rbp)
mov    -0x8(%rbp),%rdi
call   1060 <opendir@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
call   10a0 <readdir@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
je     133a <clientdata+0x18a>
mov    -0x28(%rbp),%rax
mov    (%rax),%rdi
lea    0xdd1(%rip),%rsi
mov    $0x7,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%rax
je     1248 <clientdata+0x98>
jmp    120a <clientdata+0x5a>
mov    -0x1058(%rbp),%rdi
mov    -0x8(%rbp),%rdx
mov    -0x28(%rbp),%rax
mov    (%rax),%rcx
lea    0xda7(%rip),%rsi
mov    $0x0,%al
call   10c0 <sprintf@plt>
mov    -0x1058(%rbp),%rdi
lea    -0x40(%rbp),%rsi
call   1090 <stat@plt>
cmp    $0xffffffff,%eax
jne    1286 <clientdata+0xd6>
jmp    120a <clientdata+0x5a>
mov    -0x1058(%rbp),%rdi
lea    0xd7a(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x50(%rbp)
cmpq   $0x0,-0x50(%rbp)
jne    12ad <clientdata+0xfd>
jmp    120a <clientdata+0x5a>
lea    -0x1050(%rbp),%rdi
mov    -0x28(%rbp),%rax
mov    (%rax),%rdx
add    $0x7,%rdx
lea    0xd4a(%rip),%rsi
mov    $0x0,%al
call   10c0 <sprintf@plt>
lea    -0x1050(%rbp),%rdx
lea    -0x10(%rbp),%rdi
lea    -0x14(%rbp),%rsi
call   1070 <addtobuffer@plt>
lea    -0x1050(%rbp),%rdi
mov    -0x50(%rbp),%rcx
mov    $0x1,%esi
mov    $0xfff,%edx
call   1040 <fread@plt>
mov    %eax,-0x44(%rbp)
cmp    $0x0,%eax
jle    132c <clientdata+0x17c>
movslq -0x44(%rbp),%rax
movb   $0x0,-0x1050(%rbp,%rax,1)
lea    -0x1050(%rbp),%rdx
lea    -0x10(%rbp),%rdi
lea    -0x14(%rbp),%rsi
call   1070 <addtobuffer@plt>
jmp    12e1 <clientdata+0x131>
mov    -0x50(%rbp),%rdi
call   1050 <fclose@plt>
jmp    120a <clientdata+0x5a>
mov    -0x20(%rbp),%rdi
call   1080 <closedir@plt>
cmpq   $0x0,-0x10(%rbp)
jne    135e <clientdata+0x1ae>
lea    0xcc0(%rip),%rdi
call   10d0 <strdup@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    -0x30(%rbp),%rcx
mov    %rcx,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<clientdata>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x1038,%rsp
mov    %rsp,-0x50(%rbp)
movq   $0x0,-0x30(%rbp)
movl   $0x0,-0x34(%rbp)
mov    0x2e12(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rcx
mov    %rcx,-0x40(%rbp)
mov    %rcx,%rsp
mov    %rdi,-0x58(%rbp)
call   1060 <opendir@plt>
mov    %rax,-0x48(%rbp)
mov    %rax,%rdi
call   10a0 <readdir@plt>
test   %rax,%rax
je     12f7 <clientdata+0x147>
mov    %rax,%r15
lea    -0x1060(%rbp),%r13
lea    -0x30(%rbp),%r14
lea    -0x34(%rbp),%r12
jmp    1245 <clientdata+0x95>
mov    %rbx,%rdi
call   1050 <fclose@plt>
nop
mov    -0x48(%rbp),%rdi
call   10a0 <readdir@plt>
mov    %rax,%r15
test   %rax,%rax
je     12f7 <clientdata+0x147>
mov    (%r15),%rdi
lea    0xdb1(%rip),%rsi
mov    $0x7,%edx
call   1030 <strncmp@plt>
test   %rax,%rax
jne    1230 <clientdata+0x80>
mov    (%r15),%rcx
mov    -0x40(%rbp),%rbx
mov    %rbx,%rdi
lea    0xd99(%rip),%rsi
mov    -0x58(%rbp),%rdx
xor    %eax,%eax
call   10c0 <sprintf@plt>
mov    %rbx,%rdi
lea    -0x60(%rbp),%rsi
call   1090 <stat@plt>
cmp    $0xffffffff,%eax
je     1230 <clientdata+0x80>
mov    -0x40(%rbp),%rdi
lea    0xd78(%rip),%rsi
call   10b0 <fopen@plt>
test   %rax,%rax
je     1230 <clientdata+0x80>
mov    %rax,%rbx
mov    (%r15),%rdx
add    $0x7,%rdx
mov    %r13,%rdi
lea    0xd5c(%rip),%rsi
xor    %eax,%eax
call   10c0 <sprintf@plt>
nopl   0x0(%rax,%rax,1)
mov    %r14,%rdi
mov    %r12,%rsi
mov    %r13,%rdx
call   1070 <addtobuffer@plt>
mov    %r13,%rdi
mov    $0x1,%esi
mov    $0xfff,%edx
mov    %rbx,%rcx
call   1040 <fread@plt>
test   %eax,%eax
jle    1227 <clientdata+0x77>
mov    %eax,%eax
movb   $0x0,-0x1060(%rbp,%rax,1)
jmp    12c0 <clientdata+0x110>
mov    -0x48(%rbp),%rdi
call   1080 <closedir@plt>
mov    -0x30(%rbp),%rax
test   %rax,%rax
jne    1319 <clientdata+0x169>
lea    0xd05(%rip),%rdi
call   10d0 <strdup@plt>
mov    %rax,-0x30(%rbp)
mov    -0x50(%rbp),%rsp
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.dirent = type { i8* }
%struct.stat = type { i32 }
@PATH_MAX = global i32 0
@.str = [8 x i8] c"client_\00"
@.str.1 = [6 x i8] c"%s/%s\00"
@.str.2 = [2 x i8] c"r\00"
@.str.3 = [6 x i8] c"[%s]\0A\00"
@.str.4 = [1 x i8] zeroinitializer
define i8* @clientdata(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32*
%6 = alloca %struct.dirent*
%7 = alloca i8*
%8 = alloca i64
%9 = alloca %struct.stat
%10 = alloca i32
%11 = alloca i32*
%12 = alloca [4096 x i8]
store i8* %0, i8** %2
store i8* null, i8** %3
store i32 0, i32* %4
%13 = load i32, i32* @PATH_MAX
%14 = zext i32 %13 to i64
%15 = call i8* @llvm.stacksave()
store i8* %15, i8** %7
%16 = alloca i8, i64 %14
store i64 %14, i64* %8
%17 = load i8*, i8** %2
%18 = call i32* @opendir(i8* %17)
store i32* %18, i32** %5
br label %19
19: ; preds = %65, %43, %38, %29, %1
%20 = load i32*, i32** %5
%21 = call %struct.dirent* @readdir(i32* %20)
store %struct.dirent* %21, %struct.dirent** %6
%22 = icmp ne %struct.dirent* %21, null
br i1 %22, label %23, label %68
23: ; preds = %19
%24 = load %struct.dirent*, %struct.dirent** %6
%25 = getelementptr inbounds %struct.dirent, %struct.dirent* %24, i32 0, i32 0
%26 = load i8*, i8** %25
%27 = call i64 @strncmp(i8* %26, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i32 7)
%28 = icmp ne i64 %27, 0
br i1 %28, label %29, label %30
29: ; preds = %23
br label %19
30: ; preds = %23
%31 = load i8*, i8** %2
%32 = load %struct.dirent*, %struct.dirent** %6
%33 = getelementptr inbounds %struct.dirent, %struct.dirent* %32, i32 0, i32 0
%34 = load i8*, i8** %33
%35 = call i32 (i8*, i8*, i8*, ...) @sprintf(i8* %16, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* %31, i8* %34)
%36 = call i32 @stat(i8* %16, %struct.stat* %9)
%37 = icmp eq i32 %36, -1
br i1 %37, label %38, label %39
38: ; preds = %30
br label %19
39: ; preds = %30
%40 = call i32* @fopen(i8* %16, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %40, i32** %11
%41 = load i32*, i32** %11
%42 = icmp eq i32* %41, null
br i1 %42, label %43, label %44
43: ; preds = %39
br label %19
44: ; preds = %39
%45 = getelementptr inbounds [4096 x i8], [4096 x i8]* %12, i64 0, i64 0
%46 = load %struct.dirent*, %struct.dirent** %6
%47 = getelementptr inbounds %struct.dirent, %struct.dirent* %46, i32 0, i32 0
%48 = load i8*, i8** %47
%49 = getelementptr inbounds i8, i8* %48, i64 7
%50 = call i32 (i8*, i8*, i8*, ...) @sprintf(i8* %45, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i8* %49)
%51 = getelementptr inbounds [4096 x i8], [4096 x i8]* %12, i64 0, i64 0
%52 = call i32 @addtobuffer(i8** %3, i32* %4, i8* %51)
br label %53
53: ; preds = %58, %44
%54 = getelementptr inbounds [4096 x i8], [4096 x i8]* %12, i64 0, i64 0
%55 = load i32*, i32** %11
%56 = call i32 @fread(i8* %54, i32 1, i32 4095, i32* %55)
store i32 %56, i32* %10
%57 = icmp sgt i32 %56, 0
br i1 %57, label %58, label %65
58: ; preds = %53
%59 = getelementptr inbounds [4096 x i8], [4096 x i8]* %12, i64 0, i64 0
%60 = load i32, i32* %10
%61 = sext i32 %60 to i64
%62 = getelementptr inbounds i8, i8* %59, i64 %61
store i8 0, i8* %62
%63 = getelementptr inbounds [4096 x i8], [4096 x i8]* %12, i64 0, i64 0
%64 = call i32 @addtobuffer(i8** %3, i32* %4, i8* %63)
br label %53
65: ; preds = %53
%66 = load i32*, i32** %11
%67 = call i32 @fclose(i32* %66)
br label %19
68: ; preds = %19
%69 = load i32*, i32** %5
%70 = call i32 @closedir(i32* %69)
%71 = load i8*, i8** %3
%72 = icmp ne i8* %71, null
br i1 %72, label %75, label %73
73: ; preds = %68
%74 = call i8* @strdup(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.4, i64 0, i64 0))
store i8* %74, i8** %3
br label %75
75: ; preds = %73, %68
%76 = load i8*, i8** %3
%77 = load i8*, i8** %7
ret i8* %76
}

LLVM IR (-O2):
%struct.stat = type { i32 }
%struct.dirent = type { i8* }
@PATH_MAX = global i32 0
@.str = [8 x i8] c"client_\00"
@.str.1 = [6 x i8] c"%s/%s\00"
@.str.2 = [2 x i8] c"r\00"
@.str.3 = [6 x i8] c"[%s]\0A\00"
@.str.4 = [1 x i8] zeroinitializer
define i8* @clientdata(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca %struct.stat
%5 = alloca [4096 x i8]
%6 = bitcast i8** %2 to i8*
store i8* null, i8** %2
%7 = bitcast i32* %3 to i8*
store i32 0, i32* %3
%8 = load i32, i32* @PATH_MAX
%9 = zext i32 %8 to i64
%10 = tail call i8* @llvm.stacksave()
%11 = alloca i8, i64 %9
%12 = bitcast %struct.stat* %4 to i8*
%13 = getelementptr inbounds [4096 x i8], [4096 x i8]* %5, i64 0, i64 0
%14 = tail call i32* @opendir(i8* %0)
%15 = call %struct.dirent* @readdir(i32* %14)
%16 = icmp eq %struct.dirent* %15, null
br i1 %16, label %50, label %17
17: ; preds = %1, %47
%18 = phi %struct.dirent* [ %48, %47 ], [ %15, %1 ]
%19 = getelementptr inbounds %struct.dirent, %struct.dirent* %18, i64 0, i32 0
%20 = load i8*, i8** %19
%21 = call i64 @strncmp(i8* %20, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i32 7)
%22 = icmp eq i64 %21, 0
br i1 %22, label %23, label %47
23: ; preds = %17
%24 = load i8*, i8** %19
%25 = call i32 (i8*, i8*, i8*, ...) @sprintf(i8* %11, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* %0, i8* %24)
%26 = call i32 @stat(i8* %11, %struct.stat* %4)
%27 = icmp eq i32 %26, -1
br i1 %27, label %47, label %28
28: ; preds = %23
%29 = call i32* @fopen(i8* %11, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%30 = icmp eq i32* %29, null
br i1 %30, label %47, label %31
31: ; preds = %28
%32 = load i8*, i8** %19
%33 = getelementptr inbounds i8, i8* %32, i64 7
%34 = call i32 (i8*, i8*, i8*, ...) @sprintf(i8* %13, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i8* %33)
%35 = call i32 @addtobuffer(i8** %2, i32* %3, i8* %13)
%36 = call i32 @fread(i8* %13, i32 1, i32 4095, i32* %29)
%37 = icmp sgt i32 %36, 0
br i1 %37, label %38, label %45
38: ; preds = %31, %38
%39 = phi i32 [ %43, %38 ], [ %36, %31 ]
%40 = zext i32 %39 to i64
%41 = getelementptr inbounds [4096 x i8], [4096 x i8]* %5, i64 0, i64 %40
store i8 0, i8* %41
%42 = call i32 @addtobuffer(i8** %2, i32* %3, i8* %13)
%43 = call i32 @fread(i8* %13, i32 1, i32 4095, i32* %29)
%44 = icmp sgt i32 %43, 0
br i1 %44, label %38, label %45
45: ; preds = %38, %31
%46 = call i32 @fclose(i32* %29)
br label %47
47: ; preds = %45, %17, %23, %28
%48 = call %struct.dirent* @readdir(i32* %14)
%49 = icmp eq %struct.dirent* %48, null
br i1 %49, label %50, label %17
50: ; preds = %47, %1
%51 = call i32 @closedir(i32* %14)
%52 = load i8*, i8** %2
%53 = icmp eq i8* %52, null
br i1 %53, label %54, label %56
54: ; preds = %50
%55 = call dereferenceable_or_null(1) i8* @strdup(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.4, i64 0, i64 0))
store i8* %55, i8** %2
br label %56
56: ; preds = %54, %50
%57 = phi i8* [ %55, %54 ], [ %52, %50 ]
ret i8* %57
}