#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 */
typedef scalar_t__ off_t;

/* Variables and functions */
char *content_length_line(scalar_t__);
int /*<<< orphan*/ free(char *);
char *generate_index(char *);
char *malloc(long);
scalar_t__ numlength(scalar_t__);
int respond_with_500(int);
int send(int, char *, scalar_t__, int /*<<< orphan*/);
int sprintf(char *, char *, char *, char *, char *);
scalar_t__ strlen(char *);
int respond_with_index(int connfd) {
  int rval = 0;
  long message_size;
  char *message;
  char *status;
  char *content_length;
  off_t filesize;
  char *string = generate_index(".");
  if (string == NULL) {
    return respond_with_500(connfd);
  }
  message_size = 0;
  status = "HTTP/1.1 200 OK\n";
  message_size += strlen(status);
  filesize = strlen(string);
  message_size += filesize;
  message_size += numlength(filesize);
  if ((content_length = content_length_line(filesize)) == NULL) {
    rval = -1;
    goto end;
  }
  message_size += strlen(content_length);
  message = malloc(message_size * sizeof(char));
  if (sprintf(message, "%s%s\n%s", status, content_length, string) == -1) {
    rval = -1;
    goto out;
  }
  if (send(connfd, message, strlen(message), 0) == -1) {
    rval = -1;
    goto out;
  }
out:
  free(message);
  free(content_length);
end:
  free(string);
  return rval;
}


x86 assembly (clang, -O0):
<respond_with_index>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
lea    0xe57(%rip),%rdi
call   1040 <generate_index@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
jne    11cd <respond_with_index+0x3d>
mov    -0x8(%rbp),%edi
call   1050 <respond_with_500@plt>
mov    %eax,-0x4(%rbp)
jmp    12f4 <respond_with_index+0x164>
movq   $0x0,-0x18(%rbp)
lea    0xe26(%rip),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
call   1060 <strlen@plt>
add    -0x18(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x40(%rbp),%rdi
call   1060 <strlen@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
add    -0x18(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x38(%rbp),%rdi
call   1070 <numlength@plt>
add    -0x18(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x38(%rbp),%rdi
call   10b0 <content_length_line@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
jne    123e <respond_with_index+0xae>
movl   $0xffffffff,-0xc(%rbp)
jmp    12e5 <respond_with_index+0x155>
mov    -0x30(%rbp),%rdi
call   1060 <strlen@plt>
add    -0x18(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
shl    $0x0,%rdi
call   1090 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x28(%rbp),%rdx
mov    -0x30(%rbp),%rcx
mov    -0x40(%rbp),%r8
lea    0xd9c(%rip),%rsi
call   10a0 <sprintf@plt>
cmp    $0xffffffff,%eax
jne    1291 <respond_with_index+0x101>
movl   $0xffffffff,-0xc(%rbp)
jmp    12d3 <respond_with_index+0x143>
mov    -0x8(%rbp),%eax
mov    %eax,-0x4c(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x20(%rbp),%rdi
call   1060 <strlen@plt>
mov    -0x4c(%rbp),%edi
mov    -0x48(%rbp),%rsi
mov    %rax,%rdx
xor    %ecx,%ecx
call   1080 <send@plt>
cmp    $0xffffffff,%eax
jne    12ce <respond_with_index+0x13e>
movl   $0xffffffff,-0xc(%rbp)
jmp    12d3 <respond_with_index+0x143>
jmp    12d3 <respond_with_index+0x143>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
mov    -0x40(%rbp),%rdi
call   1030 <free@plt>
mov    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<respond_with_index>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %edi,%r14d
lea    0xe5e(%rip),%rdi
call   1040 <generate_index@plt>
test   %rax,%rax
je     123a <respond_with_index+0xaa>
mov    %rax,%rbx
mov    %rax,%rdi
call   1060 <strlen@plt>
mov    %rax,%rbp
mov    %rax,%rdi
call   1070 <numlength@plt>
mov    %rax,%r15
mov    %rbp,%rdi
call   10b0 <content_length_line@plt>
test   %rax,%rax
je     124a <respond_with_index+0xba>
mov    %rax,%r12
add    %r15,%rbp
mov    %rax,%rdi
call   1060 <strlen@plt>
lea    (%rax,%rbp,1),%rdi
add    $0x10,%rdi
call   1090 <malloc@plt>
mov    %rax,%r15
lea    0xe18(%rip),%rsi
lea    0xe00(%rip),%rdx
mov    %rax,%rdi
mov    %r12,%rcx
mov    %rbx,%r8
call   10a0 <sprintf@plt>
cmp    $0xffffffff,%eax
je     1251 <respond_with_index+0xc1>
mov    %r15,%rdi
call   1060 <strlen@plt>
mov    %r14d,%edi
mov    %r15,%rsi
mov    %rax,%rdx
xor    %ecx,%ecx
call   1080 <send@plt>
xor    %ebp,%ebp
cmp    $0xffffffff,%eax
sete   %bpl
neg    %ebp
jmp    1256 <respond_with_index+0xc6>
mov    %r14d,%edi
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1050 <respond_with_500@plt>
mov    $0xffffffff,%ebp
jmp    1266 <respond_with_index+0xd6>
mov    $0xffffffff,%ebp
mov    %r15,%rdi
call   1030 <free@plt>
mov    %r12,%rdi
call   1030 <free@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %ebp,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c".\00"
@.str.1 = [17 x i8] c"HTTP/1.1 200 OK\0A\00"
@.str.2 = [8 x i8] c"%s%s\0A%s\00"
define i32 @respond_with_index(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i64
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i64
%10 = alloca i8*
store i32 %0, i32* %3
store i32 0, i32* %4
%11 = call i8* @generate_index(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i8* %11, i8** %10
%12 = load i8*, i8** %10
%13 = icmp eq i8* %12, null
br i1 %13, label %14, label %17
14: ; preds = %1
%15 = load i32, i32* %3
%16 = call i32 @respond_with_500(i32 %15)
store i32 %16, i32* %2
br label %68
17: ; preds = %1
store i64 0, i64* %5
store i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i8** %7
%18 = load i8*, i8** %7
%19 = call i64 @strlen(i8* %18)
%20 = load i64, i64* %5
%21 = add nsw i64 %20, %19
store i64 %21, i64* %5
%22 = load i8*, i8** %10
%23 = call i64 @strlen(i8* %22)
store i64 %23, i64* %9
%24 = load i64, i64* %9
%25 = load i64, i64* %5
%26 = add nsw i64 %25, %24
store i64 %26, i64* %5
%27 = load i64, i64* %9
%28 = call i64 @numlength(i64 %27)
%29 = load i64, i64* %5
%30 = add nsw i64 %29, %28
store i64 %30, i64* %5
%31 = load i64, i64* %9
%32 = call i8* @content_length_line(i64 %31)
store i8* %32, i8** %8
%33 = icmp eq i8* %32, null
br i1 %33, label %34, label %35
34: ; preds = %17
store i32 -1, i32* %4
br label %64
35: ; preds = %17
%36 = load i8*, i8** %8
%37 = call i64 @strlen(i8* %36)
%38 = load i64, i64* %5
%39 = add nsw i64 %38, %37
store i64 %39, i64* %5
%40 = load i64, i64* %5
%41 = mul i64 %40, 1
%42 = call i8* @malloc(i64 %41)
store i8* %42, i8** %6
%43 = load i8*, i8** %6
%44 = load i8*, i8** %7
%45 = load i8*, i8** %8
%46 = load i8*, i8** %10
%47 = call i32 @sprintf(i8* %43, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i8* %44, i8* %45, i8* %46)
%48 = icmp eq i32 %47, -1
br i1 %48, label %49, label %50
49: ; preds = %35
store i32 -1, i32* %4
br label %59
50: ; preds = %35
%51 = load i32, i32* %3
%52 = load i8*, i8** %6
%53 = load i8*, i8** %6
%54 = call i64 @strlen(i8* %53)
%55 = call i32 @send(i32 %51, i8* %52, i64 %54, i32 0)
%56 = icmp eq i32 %55, -1
br i1 %56, label %57, label %58
57: ; preds = %50
store i32 -1, i32* %4
br label %59
58: ; preds = %50
br label %59
59: ; preds = %58, %57, %49
%60 = load i8*, i8** %6
%61 = call i32 @free(i8* %60)
%62 = load i8*, i8** %8
%63 = call i32 @free(i8* %62)
br label %64
64: ; preds = %59, %34
%65 = load i8*, i8** %10
%66 = call i32 @free(i8* %65)
%67 = load i32, i32* %4
store i32 %67, i32* %2
br label %68
68: ; preds = %64, %14
%69 = load i32, i32* %2
ret i32 %69
}

LLVM IR (-O2):
@.str = [2 x i8] c".\00"
@.str.1 = [17 x i8] c"HTTP/1.1 200 OK\0A\00"
@.str.2 = [8 x i8] c"%s%s\0A%s\00"
define i32 @respond_with_index(i32 %0) {
%2 = tail call i8* @generate_index(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%3 = icmp eq i8* %2, null
br i1 %3, label %4, label %6
4: ; preds = %1
%5 = tail call i32 @respond_with_500(i32 %0)
br label %31
6: ; preds = %1
%7 = tail call i64 @strlen(i8* %2)
%8 = tail call i64 @numlength(i64 %7)
%9 = tail call i8* @content_length_line(i64 %7)
%10 = icmp eq i8* %9, null
br i1 %10, label %28, label %11
11: ; preds = %6
%12 = add nsw i64 %7, 16
%13 = add nsw i64 %12, %8
%14 = tail call i64 @strlen(i8* %9)
%15 = add nsw i64 %13, %14
%16 = tail call i8* @malloc(i64 %15)
%17 = tail call i32 @sprintf(i8* dereferenceable(1) %16, i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i8* %9, i8* %2)
%18 = icmp eq i32 %17, -1
br i1 %18, label %24, label %19
19: ; preds = %11
%20 = tail call i64 @strlen(i8* dereferenceable(1) %16)
%21 = tail call i32 @send(i32 %0, i8* %16, i64 %20, i32 0)
%22 = icmp eq i32 %21, -1
%23 = sext i1 %22 to i32
br label %24
24: ; preds = %19, %11
%25 = phi i32 [ -1, %11 ], [ %23, %19 ]
%26 = tail call i32 @free(i8* %16)
%27 = tail call i32 @free(i8* %9)
br label %28
28: ; preds = %6, %24
%29 = phi i32 [ %25, %24 ], [ -1, %6 ]
%30 = tail call i32 @free(i8* %2)
br label %31
31: ; preds = %28, %4
%32 = phi i32 [ %5, %4 ], [ %29, %28 ]
ret i32 %32
}