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

/* Variables and functions */
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
scalar_t__ isDirectory(char *);
int /*<<< orphan*/ *opendir(char *);
int /*<<< orphan*/ puts(char *);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ respond(int, char *, int);
int /*<<< orphan*/ strcat(char *, int /*<<< orphan*/);
scalar_t__ strcmp(int /*<<< orphan*/, char *);
int /*<<< orphan*/ strcpy(char *, char *);
int /*<<< orphan*/ strlen(char *);
int /*<<< orphan*/ write(int, char *, int);
void getd(char *uri, int socketfd) {
  DIR *dp;
  char buffer[50000], *bp = buffer, *here;
  struct dirent *ep;
  int len;
  if (!*uri)
    uri = "./";
  if ((dp = opendir(uri)) != NULL) {
    while ((ep = readdir(dp))) {
      if (strcmp(ep->d_name, ".") && strcmp(ep->d_name, "..")) {
        strcpy(here = bp, uri);
        strcat(bp, ep->d_name);
        bp += strlen(bp);
        if (isDirectory(here))
          *bp++ = '/';
        *bp++ = '\n';
      }
    }
    closedir(dp);
    len = bp - buffer;
    respond(len, "text", socketfd);
    write(socketfd, buffer, len);
  } else {
    puts("Couldn't open the directory.");
  }
}


x86 assembly (clang, -O0):
<getd>:
push   %rbp
mov    %rsp,%rbp
sub    $0xc390,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
lea    -0xc370(%rbp),%rax
mov    %rax,-0xc378(%rbp)
mov    -0x8(%rbp),%rax
cmpb   $0x0,(%rax)
jne    11e8 <getd+0x38>
lea    0xe1c(%rip),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1080 <opendir@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
je     1344 <getd+0x194>
jmp    1204 <getd+0x54>
mov    -0x18(%rbp),%rdi
call   10c0 <readdir@plt>
mov    %rax,-0xc388(%rbp)
cmp    $0x0,%rax
je     12f5 <getd+0x145>
mov    -0xc388(%rbp),%rax
mov    (%rax),%edi
lea    0xdd6(%rip),%rsi
call   10b0 <strcmp@plt>
cmp    $0x0,%rax
je     12f0 <getd+0x140>
mov    -0xc388(%rbp),%rax
mov    (%rax),%edi
lea    0xdb6(%rip),%rsi
call   10b0 <strcmp@plt>
cmp    $0x0,%rax
je     12f0 <getd+0x140>
mov    -0xc378(%rbp),%rdi
mov    %rdi,-0xc380(%rbp)
mov    -0x8(%rbp),%rsi
call   1040 <strcpy@plt>
mov    -0xc378(%rbp),%rdi
mov    -0xc388(%rbp),%rax
mov    (%rax),%esi
call   10d0 <strcat@plt>
mov    -0xc378(%rbp),%rdi
call   1090 <strlen@plt>
mov    %eax,%ecx
mov    -0xc378(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0xc378(%rbp)
mov    -0xc380(%rbp),%rdi
call   1070 <isDirectory@plt>
cmp    $0x0,%rax
je     12d8 <getd+0x128>
mov    -0xc378(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0xc378(%rbp)
movb   $0x2f,(%rax)
mov    -0xc378(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0xc378(%rbp)
movb   $0xa,(%rax)
jmp    1204 <getd+0x54>
mov    -0x18(%rbp),%rdi
call   10a0 <closedir@plt>
mov    -0xc378(%rbp),%rax
lea    -0xc370(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,-0xc38c(%rbp)
mov    -0xc38c(%rbp),%edi
mov    -0xc(%rbp),%edx
lea    0xce1(%rip),%rsi
call   1030 <respond@plt>
mov    -0xc(%rbp),%edi
lea    -0xc370(%rbp),%rsi
mov    -0xc38c(%rbp),%edx
call   1060 <write@plt>
jmp    1350 <getd+0x1a0>
lea    0xcc0(%rip),%rdi
call   1050 <puts@plt>
add    $0xc390,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getd>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0xc368,%rsp
cmpb   $0x0,(%rdi)
mov    %esi,%ebp
lea    0xe33(%rip),%r15
cmovne %rdi,%r15
mov    %r15,%rdi
call   1080 <opendir@plt>
test   %rax,%rax
je     128e <getd+0xde>
mov    %rax,%rbx
mov    %ebp,0xc(%rsp)
mov    %rax,%rdi
call   10c0 <readdir@plt>
test   %rax,%rax
je     12ab <getd+0xfb>
mov    %rax,%r14
lea    0x10(%rsp),%r13
lea    0xdfb(%rip),%r12
jmp    122f <getd+0x7f>
nopl   0x0(%rax,%rax,1)
movb   $0xa,0x0(%rbp)
add    $0x1,%rbp
mov    %rbp,%r13
mov    %rbx,%rdi
call   10c0 <readdir@plt>
mov    %rax,%r14
test   %rax,%rax
je     12b0 <getd+0x100>
mov    (%r14),%edi
mov    %r12,%rsi
call   10b0 <strcmp@plt>
test   %rax,%rax
je     121b <getd+0x6b>
mov    (%r14),%edi
lea    0xdba(%rip),%rsi
call   10b0 <strcmp@plt>
test   %rax,%rax
je     121b <getd+0x6b>
mov    %r13,%rdi
mov    %r15,%rsi
call   1040 <strcpy@plt>
mov    (%r14),%esi
mov    %r13,%rdi
call   10d0 <strcat@plt>
mov    %r13,%rdi
call   1090 <strlen@plt>
movslq %eax,%rbp
add    %r13,%rbp
mov    %r13,%rdi
call   1070 <isDirectory@plt>
test   %rax,%rax
je     1210 <getd+0x60>
movb   $0x2f,0x0(%rbp)
add    $0x1,%rbp
jmp    1210 <getd+0x60>
lea    0xd76(%rip),%rdi
add    $0xc368,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1050 <puts@plt>
lea    0x10(%rsp),%r13
mov    %rbx,%rdi
call   10a0 <closedir@plt>
lea    0x10(%rsp),%rbx
sub    %ebx,%r13d
lea    0xd3f(%rip),%rsi
mov    %r13d,%edi
mov    0xc(%rsp),%ebp
mov    %ebp,%edx
call   1030 <respond@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %r13d,%edx
call   1060 <write@plt>
add    $0xc368,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.dirent = type { i32 }
@.str = [3 x i8] c"./\00"
@.str.1 = [2 x i8] c".\00"
@.str.2 = [3 x i8] c"..\00"
@.str.3 = [5 x i8] c"text\00"
@.str.4 = [29 x i8] c"Couldn't open the directory.\00"
define void @getd(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32*
%6 = alloca [50000 x i8]
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca %struct.dirent*
%10 = alloca i32
store i8* %0, i8** %3
store i32 %1, i32* %4
%11 = getelementptr inbounds [50000 x i8], [50000 x i8]* %6, i64 0, i64 0
store i8* %11, i8** %7
%12 = load i8*, i8** %3
%13 = load i8, i8* %12
%14 = icmp ne i8 %13, 0
br i1 %14, label %16, label %15
15: ; preds = %2
store i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8** %3
br label %16
16: ; preds = %15, %2
%17 = load i8*, i8** %3
%18 = call i32* @opendir(i8* %17)
store i32* %18, i32** %5
%19 = icmp ne i32* %18, null
br i1 %19, label %20, label %77
20: ; preds = %16
br label %21
21: ; preds = %60, %20
%22 = load i32*, i32** %5
%23 = call %struct.dirent* @readdir(i32* %22)
store %struct.dirent* %23, %struct.dirent** %9
%24 = icmp ne %struct.dirent* %23, null
br i1 %24, label %25, label %61
25: ; preds = %21
%26 = load %struct.dirent*, %struct.dirent** %9
%27 = getelementptr inbounds %struct.dirent, %struct.dirent* %26, i32 0, i32 0
%28 = load i32, i32* %27
%29 = call i64 @strcmp(i32 %28, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%30 = icmp ne i64 %29, 0
br i1 %30, label %31, label %60
31: ; preds = %25
%32 = load %struct.dirent*, %struct.dirent** %9
%33 = getelementptr inbounds %struct.dirent, %struct.dirent* %32, i32 0, i32 0
%34 = load i32, i32* %33
%35 = call i64 @strcmp(i32 %34, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%36 = icmp ne i64 %35, 0
br i1 %36, label %37, label %60
37: ; preds = %31
%38 = load i8*, i8** %7
store i8* %38, i8** %8
%39 = load i8*, i8** %3
%40 = call i32 @strcpy(i8* %38, i8* %39)
%41 = load i8*, i8** %7
%42 = load %struct.dirent*, %struct.dirent** %9
%43 = getelementptr inbounds %struct.dirent, %struct.dirent* %42, i32 0, i32 0
%44 = load i32, i32* %43
%45 = call i32 @strcat(i8* %41, i32 %44)
%46 = load i8*, i8** %7
%47 = call i32 @strlen(i8* %46)
%48 = load i8*, i8** %7
%49 = sext i32 %47 to i64
%50 = getelementptr inbounds i8, i8* %48, i64 %49
store i8* %50, i8** %7
%51 = load i8*, i8** %8
%52 = call i64 @isDirectory(i8* %51)
%53 = icmp ne i64 %52, 0
br i1 %53, label %54, label %57
54: ; preds = %37
%55 = load i8*, i8** %7
%56 = getelementptr inbounds i8, i8* %55, i32 1
store i8* %56, i8** %7
store i8 47, i8* %55
br label %57
57: ; preds = %54, %37
%58 = load i8*, i8** %7
%59 = getelementptr inbounds i8, i8* %58, i32 1
store i8* %59, i8** %7
store i8 10, i8* %58
br label %60
60: ; preds = %57, %31, %25
br label %21
61: ; preds = %21
%62 = load i32*, i32** %5
%63 = call i32 @closedir(i32* %62)
%64 = load i8*, i8** %7
%65 = getelementptr inbounds [50000 x i8], [50000 x i8]* %6, i64 0, i64 0
%66 = ptrtoint i8* %64 to i64
%67 = ptrtoint i8* %65 to i64
%68 = sub i64 %66, %67
%69 = trunc i64 %68 to i32
store i32 %69, i32* %10
%70 = load i32, i32* %10
%71 = load i32, i32* %4
%72 = call i32 @respond(i32 %70, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i32 %71)
%73 = load i32, i32* %4
%74 = getelementptr inbounds [50000 x i8], [50000 x i8]* %6, i64 0, i64 0
%75 = load i32, i32* %10
%76 = call i32 @write(i32 %73, i8* %74, i32 %75)
br label %79
77: ; preds = %16
%78 = call i32 @puts(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.4, i64 0, i64 0))
br label %79
79: ; preds = %77, %61
ret void
}

LLVM IR (-O2):
%struct.dirent = type { i32 }
@.str = [3 x i8] c"./\00"
@.str.1 = [2 x i8] c".\00"
@.str.2 = [3 x i8] c"..\00"
@.str.3 = [5 x i8] c"text\00"
@.str.4 = [29 x i8] c"Couldn't open the directory.\00"
define void @getd(i8* %0, i32 %1) {
%3 = alloca [50000 x i8]
%4 = getelementptr inbounds [50000 x i8], [50000 x i8]* %3, i64 0, i64 0
%5 = load i8, i8* %0
%6 = icmp eq i8 %5, 0
%7 = select i1 %6, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %0
%8 = tail call i32* @opendir(i8* %7)
%9 = icmp eq i32* %8, null
br i1 %9, label %51, label %10
10: ; preds = %2
%11 = call %struct.dirent* @readdir(i32* %8)
%12 = icmp eq %struct.dirent* %11, null
br i1 %12, label %42, label %13
13: ; preds = %10, %38
%14 = phi %struct.dirent* [ %40, %38 ], [ %11, %10 ]
%15 = phi i8* [ %39, %38 ], [ %4, %10 ]
%16 = getelementptr inbounds %struct.dirent, %struct.dirent* %14, i64 0, i32 0
%17 = load i32, i32* %16
%18 = call i64 @strcmp(i32 %17, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%19 = icmp eq i64 %18, 0
br i1 %19, label %38, label %20
20: ; preds = %13
%21 = load i32, i32* %16
%22 = call i64 @strcmp(i32 %21, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%23 = icmp eq i64 %22, 0
br i1 %23, label %38, label %24
24: ; preds = %20
%25 = call i32 @strcpy(i8* %15, i8* %7)
%26 = load i32, i32* %16
%27 = call i32 @strcat(i8* %15, i32 %26)
%28 = call i32 @strlen(i8* %15)
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds i8, i8* %15, i64 %29
%31 = call i64 @isDirectory(i8* %15)
%32 = icmp eq i64 %31, 0
br i1 %32, label %35, label %33
33: ; preds = %24
%34 = getelementptr inbounds i8, i8* %30, i64 1
store i8 47, i8* %30
br label %35
35: ; preds = %33, %24
%36 = phi i8* [ %34, %33 ], [ %30, %24 ]
%37 = getelementptr inbounds i8, i8* %36, i64 1
store i8 10, i8* %36
br label %38
38: ; preds = %35, %20, %13
%39 = phi i8* [ %37, %35 ], [ %15, %20 ], [ %15, %13 ]
%40 = call %struct.dirent* @readdir(i32* %8)
%41 = icmp eq %struct.dirent* %40, null
br i1 %41, label %42, label %13
42: ; preds = %38, %10
%43 = phi i8* [ %4, %10 ], [ %39, %38 ]
%44 = call i32 @closedir(i32* %8)
%45 = ptrtoint i8* %43 to i64
%46 = ptrtoint [50000 x i8]* %3 to i64
%47 = sub i64 %45, %46
%48 = trunc i64 %47 to i32
%49 = call i32 @respond(i32 %48, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i32 %1)
%50 = call i32 @write(i32 %1, i8* %4, i32 %48)
br label %53
51: ; preds = %2
%52 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @.str.4, i64 0, i64 0))
br label %53
53: ; preds = %51, %42
ret void
}