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

/* Variables and functions */
#define DT_DIR 129
#define DT_REG 128
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
scalar_t__ malloc(size_t);
int /*<<< orphan*/ *opendir(char const *);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ sprintf(char *, char *, char const *, char const *);
int /*<<< orphan*/ strcmp(char *, char const *);
size_t strlen(char const *);
int /*<<< orphan*/ write(int, ...);
void list_files(const char *path, int clientfd, int level) {
  struct dirent *dp;
  size_t full_cpath_len = 0;
  char *full_cpath = 0;
  size_t pathlen = strlen(path);
  DIR *dir = opendir(path);
  while ((dp = readdir(dir)) != NULL) {
    unsigned char type = dp->d_type;
    switch (type) {
    case DT_DIR:
      if (!strcmp(".", dp->d_name) || !strcmp("..", dp->d_name)) {
        continue;
      }
      full_cpath_len = pathlen + strlen(dp->d_name) + 2;
      full_cpath = (char *)malloc(full_cpath_len * sizeof(char));
      sprintf(full_cpath, "%s/%s", path, dp->d_name);
      list_files(full_cpath, clientfd, level + 1);
      free(full_cpath);
      break;
    case DT_REG:
      full_cpath_len = pathlen + strlen(dp->d_name) + 2;
      full_cpath = (char *)malloc(full_cpath_len * sizeof(char));
      sprintf(full_cpath, "%s/%s", path, dp->d_name);
      write(clientfd, &full_cpath_len, sizeof(size_t));
      write(clientfd, full_cpath, full_cpath_len);
      free(full_cpath);
      break;
    };
  }
  closedir(dir);
  full_cpath_len = 0;
  if (level == 0) {
    write(clientfd, &full_cpath_len, sizeof(size_t));
  }
}


x86 assembly (clang, -O0):
<list_files>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
movq   $0x0,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x8(%rbp),%rdi
call   1060 <strlen@plt>
mov    %rax,-0x30(%rbp)
mov    -0x8(%rbp),%rdi
call   1050 <opendir@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rdi
call   10a0 <readdir@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
je     1360 <list_files+0x1c0>
mov    -0x18(%rbp),%rax
mov    (%rax),%al
mov    %al,-0x39(%rbp)
movzbl -0x39(%rbp),%eax
mov    %eax,-0x40(%rbp)
sub    $0x80,%eax
je     12d9 <list_files+0x139>
jmp    1213 <list_files+0x73>
mov    -0x40(%rbp),%eax
sub    $0x81,%eax
jne    135b <list_files+0x1bb>
jmp    1226 <list_files+0x86>
mov    -0x18(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xdcc(%rip),%rdi
call   1080 <strcmp@plt>
cmp    $0x0,%eax
je     1260 <list_files+0xc0>
mov    -0x18(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xdae(%rip),%rdi
call   1080 <strcmp@plt>
cmp    $0x0,%eax
jne    1265 <list_files+0xc5>
jmp    11dc <list_files+0x3c>
mov    -0x30(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x18(%rbp),%rax
mov    0x8(%rax),%rdi
call   1060 <strlen@plt>
mov    %rax,%rcx
mov    -0x48(%rbp),%rax
add    %rcx,%rax
add    $0x2,%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
shl    $0x0,%rdi
call   10b0 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x8(%rbp),%rdx
mov    -0x18(%rbp),%rax
mov    0x8(%rax),%rcx
lea    0xd4f(%rip),%rsi
call   10c0 <sprintf@plt>
mov    -0x28(%rbp),%rdi
mov    -0xc(%rbp),%esi
mov    -0x10(%rbp),%edx
add    $0x1,%edx
call   1090 <list_files@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
jmp    135b <list_files+0x1bb>
mov    -0x30(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    -0x18(%rbp),%rax
mov    0x8(%rax),%rdi
call   1060 <strlen@plt>
mov    %rax,%rcx
mov    -0x50(%rbp),%rax
add    %rcx,%rax
add    $0x2,%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
shl    $0x0,%rdi
call   10b0 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x8(%rbp),%rdx
mov    -0x18(%rbp),%rax
mov    0x8(%rax),%rcx
lea    0xcdb(%rip),%rsi
call   10c0 <sprintf@plt>
mov    -0xc(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x8,%edx
mov    $0x0,%al
call   1040 <write@plt>
mov    -0xc(%rbp),%edi
mov    -0x28(%rbp),%rsi
mov    -0x20(%rbp),%rdx
mov    $0x0,%al
call   1040 <write@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
jmp    11dc <list_files+0x3c>
mov    -0x38(%rbp),%rdi
call   1070 <closedir@plt>
movq   $0x0,-0x20(%rbp)
cmpl   $0x0,-0x10(%rbp)
jne    138e <list_files+0x1ee>
mov    -0xc(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x8,%edx
mov    $0x0,%al
call   1040 <write@plt>
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<list_files>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rdx,0x20(%rsp)
mov    %esi,%r12d
mov    %rdi,%r15
movq   $0x0,0x8(%rsp)
call   1060 <strlen@plt>
mov    %rax,0x10(%rsp)
mov    %r15,%rdi
call   1050 <opendir@plt>
mov    %rax,%rbx
mov    %rax,%rdi
call   10a0 <readdir@plt>
test   %rax,%rax
je     12f2 <list_files+0x152>
mov    %rax,%r13
addq   $0x2,0x10(%rsp)
mov    0x20(%rsp),%rax
add    $0x1,%eax
mov    %eax,0x1c(%rsp)
jmp    1273 <list_files+0xd3>
nop
mov    0x8(%r13),%rdi
call   1060 <strlen@plt>
mov    0x10(%rsp),%rcx
lea    (%rax,%rcx,1),%rdi
mov    %rdi,0x8(%rsp)
call   10b0 <malloc@plt>
mov    %rax,%r14
mov    0x8(%r13),%rcx
mov    %rax,%rdi
lea    0xdd6(%rip),%rsi
mov    %r15,%rdx
call   10c0 <sprintf@plt>
mov    $0x8,%edx
mov    %r12d,%edi
lea    0x8(%rsp),%rsi
xor    %eax,%eax
call   1040 <write@plt>
mov    0x8(%rsp),%rdx
mov    %r12d,%edi
mov    %r14,%rsi
xor    %eax,%eax
call   1040 <write@plt>
mov    %r14,%rdi
call   1030 <free@plt>
mov    %rbx,%rdi
call   10a0 <readdir@plt>
mov    %rax,%r13
test   %rax,%rax
je     12f2 <list_files+0x152>
movzbl 0x0(%r13),%eax
cmp    $0x80,%al
je     1200 <list_files+0x60>
cmp    $0x81,%al
jne    1263 <list_files+0xc3>
mov    0x8(%r13),%rbp
lea    0xd76(%rip),%rdi
mov    %rbp,%rsi
call   1080 <strcmp@plt>
test   %eax,%eax
je     1263 <list_files+0xc3>
lea    0xd62(%rip),%rdi
mov    %rbp,%rsi
call   1080 <strcmp@plt>
test   %eax,%eax
je     1263 <list_files+0xc3>
mov    %rbp,%rdi
call   1060 <strlen@plt>
mov    0x10(%rsp),%rcx
lea    (%rax,%rcx,1),%rdi
mov    %rdi,0x8(%rsp)
call   10b0 <malloc@plt>
mov    %rax,%r14
mov    0x8(%r13),%rcx
mov    %rax,%rdi
lea    0xd2d(%rip),%rsi
mov    %r15,%rdx
call   10c0 <sprintf@plt>
mov    %r14,%rdi
mov    %r12d,%esi
mov    0x1c(%rsp),%edx
call   1090 <list_files@plt>
jmp    125b <list_files+0xbb>
mov    %rbx,%rdi
call   1070 <closedir@plt>
movq   $0x0,0x8(%rsp)
cmpl   $0x0,0x20(%rsp)
jne    131e <list_files+0x17e>
lea    0x8(%rsp),%rsi
mov    $0x8,%edx
mov    %r12d,%edi
xor    %eax,%eax
call   1040 <write@plt>
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.dirent = type { i8, i8* }
@.str = [2 x i8] c".\00"
@.str.1 = [3 x i8] c"..\00"
@.str.2 = [6 x i8] c"%s/%s\00"
define void @list_files(i8* %0, i32 %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca %struct.dirent*
%8 = alloca i64
%9 = alloca i8*
%10 = alloca i64
%11 = alloca i32*
%12 = alloca i8
store i8* %0, i8** %4
store i32 %1, i32* %5
store i32 %2, i32* %6
store i64 0, i64* %8
store i8* null, i8** %9
%13 = load i8*, i8** %4
%14 = call i64 @strlen(i8* %13)
store i64 %14, i64* %10
%15 = load i8*, i8** %4
%16 = call i32* @opendir(i8* %15)
store i32* %16, i32** %11
br label %17
17: ; preds = %90, %39, %3
%18 = load i32*, i32** %11
%19 = call %struct.dirent* @readdir(i32* %18)
store %struct.dirent* %19, %struct.dirent** %7
%20 = icmp ne %struct.dirent* %19, null
br i1 %20, label %21, label %91
21: ; preds = %17
%22 = load %struct.dirent*, %struct.dirent** %7
%23 = getelementptr inbounds %struct.dirent, %struct.dirent* %22, i32 0, i32 0
%24 = load i8, i8* %23
store i8 %24, i8* %12
%25 = load i8, i8* %12
%26 = zext i8 %25 to i32
switch i32 %26, label %90 [
i32 129, label %27
i32 128, label %64
]
27: ; preds = %21
%28 = load %struct.dirent*, %struct.dirent** %7
%29 = getelementptr inbounds %struct.dirent, %struct.dirent* %28, i32 0, i32 1
%30 = load i8*, i8** %29
%31 = call i32 @strcmp(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i8* %30)
%32 = icmp ne i32 %31, 0
br i1 %32, label %33, label %39
33: ; preds = %27
%34 = load %struct.dirent*, %struct.dirent** %7
%35 = getelementptr inbounds %struct.dirent, %struct.dirent* %34, i32 0, i32 1
%36 = load i8*, i8** %35
%37 = call i32 @strcmp(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %36)
%38 = icmp ne i32 %37, 0
br i1 %38, label %40, label %39
39: ; preds = %33, %27
br label %17
40: ; preds = %33
%41 = load i64, i64* %10
%42 = load %struct.dirent*, %struct.dirent** %7
%43 = getelementptr inbounds %struct.dirent, %struct.dirent* %42, i32 0, i32 1
%44 = load i8*, i8** %43
%45 = call i64 @strlen(i8* %44)
%46 = add i64 %41, %45
%47 = add i64 %46, 2
store i64 %47, i64* %8
%48 = load i64, i64* %8
%49 = mul i64 %48, 1
%50 = call i64 @malloc(i64 %49)
%51 = inttoptr i64 %50 to i8*
store i8* %51, i8** %9
%52 = load i8*, i8** %9
%53 = load i8*, i8** %4
%54 = load %struct.dirent*, %struct.dirent** %7
%55 = getelementptr inbounds %struct.dirent, %struct.dirent* %54, i32 0, i32 1
%56 = load i8*, i8** %55
%57 = call i32 @sprintf(i8* %52, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8* %53, i8* %56)
%58 = load i8*, i8** %9
%59 = load i32, i32* %5
%60 = load i32, i32* %6
%61 = add nsw i32 %60, 1
%62 = load i8*, i8** %9
%63 = call i32 @free(i8* %62)
br label %90
64: ; preds = %21
%65 = load i64, i64* %10
%66 = load %struct.dirent*, %struct.dirent** %7
%67 = getelementptr inbounds %struct.dirent, %struct.dirent* %66, i32 0, i32 1
%68 = load i8*, i8** %67
%69 = call i64 @strlen(i8* %68)
%70 = add i64 %65, %69
%71 = add i64 %70, 2
store i64 %71, i64* %8
%72 = load i64, i64* %8
%73 = mul i64 %72, 1
%74 = call i64 @malloc(i64 %73)
%75 = inttoptr i64 %74 to i8*
store i8* %75, i8** %9
%76 = load i8*, i8** %9
%77 = load i8*, i8** %4
%78 = load %struct.dirent*, %struct.dirent** %7
%79 = getelementptr inbounds %struct.dirent, %struct.dirent* %78, i32 0, i32 1
%80 = load i8*, i8** %79
%81 = call i32 @sprintf(i8* %76, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8* %77, i8* %80)
%82 = load i32, i32* %5
%83 = call i32 (i32, ...) @write(i32 %82, i64* %8, i64 8)
%84 = load i32, i32* %5
%85 = load i8*, i8** %9
%86 = load i64, i64* %8
%87 = call i32 (i32, ...) @write(i32 %84, i8* %85, i64 %86)
%88 = load i8*, i8** %9
%89 = call i32 @free(i8* %88)
br label %90
90: ; preds = %21, %64, %40
br label %17
91: ; preds = %17
%92 = load i32*, i32** %11
%93 = call i32 @closedir(i32* %92)
store i64 0, i64* %8
%94 = load i32, i32* %6
%95 = icmp eq i32 %94, 0
br i1 %95, label %96, label %99
96: ; preds = %91
%97 = load i32, i32* %5
%98 = call i32 (i32, ...) @write(i32 %97, i64* %8, i64 8)
br label %99
99: ; preds = %96, %91
ret void
}

LLVM IR (-O2):
%struct.dirent = type { i8, i8* }
@.str = [2 x i8] c".\00"
@.str.1 = [3 x i8] c"..\00"
@.str.2 = [6 x i8] c"%s/%s\00"
define void @list_files(i8* %0, i32 %1, i32 %2) {
%4 = alloca i64
%5 = bitcast i64* %4 to i8*
store i64 0, i64* %4
%6 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%7 = tail call i32* @opendir(i8* %0)
%8 = call %struct.dirent* @readdir(i32* %7)
%9 = icmp eq %struct.dirent* %8, null
br i1 %9, label %50, label %10
10: ; preds = %3
%11 = add i64 %6, 2
%12 = add nsw i32 %2, 1
br label %13
13: ; preds = %10, %47
%14 = phi %struct.dirent* [ %8, %10 ], [ %48, %47 ]
%15 = getelementptr inbounds %struct.dirent, %struct.dirent* %14, i64 0, i32 0
%16 = load i8, i8* %15
switch i8 %16, label %47 [
i8 -127, label %17
i8 -128, label %32
]
17: ; preds = %13
%18 = getelementptr inbounds %struct.dirent, %struct.dirent* %14, i64 0, i32 1
%19 = load i8*, i8** %18
%20 = call i32 @strcmp(i8* dereferenceable(2) getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i8* dereferenceable(1) %19)
%21 = icmp eq i32 %20, 0
br i1 %21, label %47, label %22
22: ; preds = %17
%23 = call i32 @strcmp(i8* dereferenceable(3) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* dereferenceable(1) %19)
%24 = icmp eq i32 %23, 0
br i1 %24, label %47, label %25
25: ; preds = %22
%26 = call i64 @strlen(i8* dereferenceable(1) %19)
%27 = add i64 %11, %26
store i64 %27, i64* %4
%28 = call i64 @malloc(i64 %27)
%29 = inttoptr i64 %28 to i8*
%30 = load i8*, i8** %18
%31 = call i32 @sprintf(i8* dereferenceable(1) %29, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8* %0, i8* %30)
br label %44
32: ; preds = %13
%33 = getelementptr inbounds %struct.dirent, %struct.dirent* %14, i64 0, i32 1
%34 = load i8*, i8** %33
%35 = call i64 @strlen(i8* dereferenceable(1) %34)
%36 = add i64 %11, %35
store i64 %36, i64* %4
%37 = call i64 @malloc(i64 %36)
%38 = inttoptr i64 %37 to i8*
%39 = load i8*, i8** %33
%40 = call i32 @sprintf(i8* dereferenceable(1) %38, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8* %0, i8* %39)
%41 = call i32 (i32, ...) @write(i32 %1, i64* %4, i64 8)
%42 = load i64, i64* %4
%43 = call i32 (i32, ...) @write(i32 %1, i8* %38, i64 %42)
br label %44
44: ; preds = %32, %25
%45 = phi i8* [ %29, %25 ], [ %38, %32 ]
%46 = call i32 @free(i8* %45)
br label %47
47: ; preds = %44, %13, %17, %22
%48 = call %struct.dirent* @readdir(i32* %7)
%49 = icmp eq %struct.dirent* %48, null
br i1 %49, label %50, label %13
50: ; preds = %47, %3
%51 = call i32 @closedir(i32* %7)
store i64 0, i64* %4
%52 = icmp eq i32 %2, 0
br i1 %52, label %53, label %55
53: ; preds = %50
%54 = call i32 (i32, ...) @write(i32 %1, i64* %4, i64 8)
br label %55
55: ; preds = %53, %50
ret void
}