#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 message_s {
  int dummy;
};
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*/ *);
int /*<<< orphan*/ *opendir(char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ strcpy(char *, char *);
int strlen(char *);
int /*<<< orphan*/ swrite(int, char *, int);
int /*<<< orphan*/ write_head(int, int, int, int);
int req_ls(int sockfd, struct message_s *msg) {
  char buff[2048];
  DIR *dirp;
  if (!(dirp = opendir("filedir"))) {
    perror("filedir"
           " not exist");
    return -1;
  }
  struct dirent *dp;
  int char_len = 0;
  while ((dp = readdir(dirp))) {
    if (dp->d_type == DT_DIR)
      continue;
    int new_len = char_len + (strlen(dp->d_name) + 1);
    if (new_len >= (2048 - 1))
      break;
    strcpy(buff + char_len, dp->d_name);
    strcpy(buff + char_len + strlen(dp->d_name), "\n");
    printf("%d\n", char_len);
    char_len = new_len;
  }
  closedir(dirp);
  printf("%d\n", char_len);
  write_head(sockfd, 0xA6, 1, char_len);
  swrite(sockfd, buff, char_len);
  return 0;
}


x86 assembly (clang, -O0):
<req_ls>:
push   %rbp
mov    %rsp,%rbp
sub    $0x840,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xe57(%rip),%rdi
call   1040 <opendir@plt>
mov    %rax,-0x818(%rbp)
cmp    $0x0,%rax
jne    11d7 <req_ls+0x47>
lea    0xe42(%rip),%rdi
call   10a0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1337 <req_ls+0x1a7>
movl   $0x0,-0x824(%rbp)
mov    -0x818(%rbp),%rdi
call   1080 <readdir@plt>
mov    %rax,-0x820(%rbp)
cmp    $0x0,%rax
je     12e5 <req_ls+0x155>
mov    -0x820(%rbp),%rax
mov    (%rax),%rax
mov    0x2dd1(%rip),%rcx
cmp    (%rcx),%rax
jne    121d <req_ls+0x8d>
jmp    11e1 <req_ls+0x51>
mov    -0x824(%rbp),%eax
mov    %eax,-0x82c(%rbp)
mov    -0x820(%rbp),%rax
mov    0x8(%rax),%rdi
call   1050 <strlen@plt>
mov    %eax,%ecx
mov    -0x82c(%rbp),%eax
add    $0x1,%ecx
add    %ecx,%eax
mov    %eax,-0x828(%rbp)
cmpl   $0x7ff,-0x828(%rbp)
jl     1261 <req_ls+0xd1>
jmp    12e5 <req_ls+0x155>
lea    -0x810(%rbp),%rdi
movslq -0x824(%rbp),%rax
add    %rax,%rdi
mov    -0x820(%rbp),%rax
mov    0x8(%rax),%rsi
call   1030 <strcpy@plt>
lea    -0x810(%rbp),%rax
movslq -0x824(%rbp),%rcx
add    %rcx,%rax
mov    %rax,-0x838(%rbp)
mov    -0x820(%rbp),%rax
mov    0x8(%rax),%rdi
call   1050 <strlen@plt>
mov    -0x838(%rbp),%rdi
cltq
add    %rax,%rdi
lea    0xd5f(%rip),%rsi
call   1030 <strcpy@plt>
mov    -0x824(%rbp),%esi
lea    0xd4b(%rip),%rdi
call   1060 <printf@plt>
mov    -0x828(%rbp),%eax
mov    %eax,-0x824(%rbp)
jmp    11e1 <req_ls+0x51>
mov    -0x818(%rbp),%rdi
call   1070 <closedir@plt>
mov    -0x824(%rbp),%esi
lea    0xd1c(%rip),%rdi
call   1060 <printf@plt>
mov    -0x8(%rbp),%edi
mov    -0x824(%rbp),%ecx
mov    $0xa6,%esi
mov    $0x1,%edx
call   1090 <write_head@plt>
mov    -0x8(%rbp),%edi
lea    -0x810(%rbp),%rsi
mov    -0x824(%rbp),%edx
call   10b0 <swrite@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x840,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<req_ls>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x818,%rsp
mov    %edi,%r15d
lea    0xe55(%rip),%rdi
call   1040 <opendir@plt>
test   %rax,%rax
je     12a6 <req_ls+0x116>
mov    %rax,%rbp
xor    %r13d,%r13d
mov    0x2e1a(%rip),%r12
cs nopw 0x0(%rax,%rax,1)
mov    %rbp,%rdi
call   1080 <readdir@plt>
test   %rax,%rax
je     1256 <req_ls+0xc6>
mov    %rax,%rbx
mov    (%rax),%rax
cmp    (%r12),%rax
je     11d0 <req_ls+0x40>
mov    0x8(%rbx),%rdi
call   1050 <strlen@plt>
lea    (%rax,%r13,1),%r14d
add    $0x1,%r14d
cmp    $0x7fe,%r14d
jg     1256 <req_ls+0xc6>
movslq %r13d,%rax
mov    %r15d,0xc(%rsp)
lea    (%rsp,%rax,1),%r15
add    $0x10,%r15
mov    0x8(%rbx),%rsi
mov    %r15,%rdi
call   1030 <strcpy@plt>
mov    0x8(%rbx),%rdi
call   1050 <strlen@plt>
movslq %eax,%rdi
add    %r15,%rdi
mov    0xc(%rsp),%r15d
lea    0xde2(%rip),%rsi
call   1030 <strcpy@plt>
lea    0xdd4(%rip),%rdi
mov    %r13d,%esi
call   1060 <printf@plt>
mov    %r14d,%r13d
jmp    11d0 <req_ls+0x40>
mov    %rbp,%rdi
call   1070 <closedir@plt>
lea    0xdb5(%rip),%rdi
mov    %r13d,%esi
call   1060 <printf@plt>
mov    %r15d,%edi
mov    $0xa6,%esi
mov    $0x1,%edx
mov    %r13d,%ecx
call   1090 <write_head@plt>
lea    0x10(%rsp),%rsi
mov    %r15d,%edi
mov    %r13d,%edx
call   10b0 <swrite@plt>
xor    %eax,%eax
add    $0x818,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd5b(%rip),%rdi
call   10a0 <perror@plt>
mov    $0xffffffff,%eax
jmp    1294 <req_ls+0x104>

LLVM IR (-O0):
%struct.message_s = type { i32 }
%struct.dirent = type { i64, i8* }
@.str = [8 x i8] c"filedir\00"
@.str.1 = [18 x i8] c"filedir not exist\00"
@DT_DIR = global i64 0
@.str.2 = [2 x i8] c"\0A\00"
@.str.3 = [4 x i8] c"%d\0A\00"
define i32 @req_ls(i32 %0, %struct.message_s* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.message_s*
%6 = alloca [2048 x i8]
%7 = alloca i32*
%8 = alloca %struct.dirent*
%9 = alloca i32
%10 = alloca i32
store i32 %0, i32* %4
store %struct.message_s* %1, %struct.message_s** %5
%11 = call i32* @opendir(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
store i32* %11, i32** %7
%12 = icmp ne i32* %11, null
br i1 %12, label %15, label %13
13: ; preds = %2
%14 = call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %3
br label %73
15: ; preds = %2
store i32 0, i32* %9
br label %16
16: ; preds = %38, %26, %15
%17 = load i32*, i32** %7
%18 = call %struct.dirent* @readdir(i32* %17)
store %struct.dirent* %18, %struct.dirent** %8
%19 = icmp ne %struct.dirent* %18, null
br i1 %19, label %20, label %61
20: ; preds = %16
%21 = load %struct.dirent*, %struct.dirent** %8
%22 = getelementptr inbounds %struct.dirent, %struct.dirent* %21, i32 0, i32 0
%23 = load i64, i64* %22
%24 = load i64, i64* @DT_DIR
%25 = icmp eq i64 %23, %24
br i1 %25, label %26, label %27
26: ; preds = %20
br label %16
27: ; preds = %20
%28 = load i32, i32* %9
%29 = load %struct.dirent*, %struct.dirent** %8
%30 = getelementptr inbounds %struct.dirent, %struct.dirent* %29, i32 0, i32 1
%31 = load i8*, i8** %30
%32 = call i32 @strlen(i8* %31)
%33 = add nsw i32 %32, 1
%34 = add nsw i32 %28, %33
store i32 %34, i32* %10
%35 = load i32, i32* %10
%36 = icmp sge i32 %35, 2047
br i1 %36, label %37, label %38
37: ; preds = %27
br label %61
38: ; preds = %27
%39 = getelementptr inbounds [2048 x i8], [2048 x i8]* %6, i64 0, i64 0
%40 = load i32, i32* %9
%41 = sext i32 %40 to i64
%42 = getelementptr inbounds i8, i8* %39, i64 %41
%43 = load %struct.dirent*, %struct.dirent** %8
%44 = getelementptr inbounds %struct.dirent, %struct.dirent* %43, i32 0, i32 1
%45 = load i8*, i8** %44
%46 = call i32 @strcpy(i8* %42, i8* %45)
%47 = getelementptr inbounds [2048 x i8], [2048 x i8]* %6, i64 0, i64 0
%48 = load i32, i32* %9
%49 = sext i32 %48 to i64
%50 = getelementptr inbounds i8, i8* %47, i64 %49
%51 = load %struct.dirent*, %struct.dirent** %8
%52 = getelementptr inbounds %struct.dirent, %struct.dirent* %51, i32 0, i32 1
%53 = load i8*, i8** %52
%54 = call i32 @strlen(i8* %53)
%55 = sext i32 %54 to i64
%56 = getelementptr inbounds i8, i8* %50, i64 %55
%57 = call i32 @strcpy(i8* %56, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%58 = load i32, i32* %9
%59 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i32 %58)
%60 = load i32, i32* %10
store i32 %60, i32* %9
br label %16
61: ; preds = %37, %16
%62 = load i32*, i32** %7
%63 = call i32 @closedir(i32* %62)
%64 = load i32, i32* %9
%65 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i32 %64)
%66 = load i32, i32* %4
%67 = load i32, i32* %9
%68 = call i32 @write_head(i32 %66, i32 166, i32 1, i32 %67)
%69 = load i32, i32* %4
%70 = getelementptr inbounds [2048 x i8], [2048 x i8]* %6, i64 0, i64 0
%71 = load i32, i32* %9
%72 = call i32 @swrite(i32 %69, i8* %70, i32 %71)
store i32 0, i32* %3
br label %73
73: ; preds = %61, %13
%74 = load i32, i32* %3
ret i32 %74
}

LLVM IR (-O2):
%struct.message_s = type { i32 }
%struct.dirent = type { i64, i8* }
@.str = [8 x i8] c"filedir\00"
@.str.1 = [18 x i8] c"filedir not exist\00"
@DT_DIR = global i64 0
@.str.2 = [2 x i8] c"\0A\00"
@.str.3 = [4 x i8] c"%d\0A\00"
define i32 @req_ls(i32 %0, %struct.message_s* nocapture readnone %1) {
%3 = alloca [2048 x i8]
%4 = getelementptr inbounds [2048 x i8], [2048 x i8]* %3, i64 0, i64 0
%5 = tail call i32* @opendir(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%6 = icmp eq i32* %5, null
br i1 %6, label %7, label %35
7: ; preds = %2
%8 = tail call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
br label %42
9: ; preds = %35, %12
%10 = call %struct.dirent* @readdir(i32* %5)
%11 = icmp eq %struct.dirent* %10, null
br i1 %11, label %37, label %12
12: ; preds = %9
%13 = getelementptr inbounds %struct.dirent, %struct.dirent* %10, i64 0, i32 0
%14 = load i64, i64* %13
%15 = load i64, i64* @DT_DIR
%16 = icmp eq i64 %14, %15
br i1 %16, label %9, label %17
17: ; preds = %12
%18 = getelementptr inbounds %struct.dirent, %struct.dirent* %10, i64 0, i32 1
%19 = load i8*, i8** %18
%20 = call i32 @strlen(i8* %19)
%21 = add i32 %36, 1
%22 = add i32 %21, %20
%23 = icmp sgt i32 %22, 2046
br i1 %23, label %37, label %24
24: ; preds = %17
%25 = sext i32 %36 to i64
%26 = getelementptr inbounds [2048 x i8], [2048 x i8]* %3, i64 0, i64 %25
%27 = load i8*, i8** %18
%28 = call i32 @strcpy(i8* %26, i8* %27)
%29 = load i8*, i8** %18
%30 = call i32 @strlen(i8* %29)
%31 = sext i32 %30 to i64
%32 = getelementptr inbounds i8, i8* %26, i64 %31
%33 = call i32 @strcpy(i8* %32, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%34 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i32 %36)
br label %35
35: ; preds = %2, %24
%36 = phi i32 [ %22, %24 ], [ 0, %2 ]
br label %9
37: ; preds = %17, %9
%38 = call i32 @closedir(i32* %5)
%39 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i32 %36)
%40 = call i32 @write_head(i32 %0, i32 166, i32 1, i32 %36)
%41 = call i32 @swrite(i32 %0, i8* %4, i32 %36)
br label %42
42: ; preds = %37, %7
%43 = phi i32 [ 0, %37 ], [ -1, %7 ]
ret i32 %43
}