#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 int off_t;

/* Variables and functions */
int /*<<< orphan*/ free(char *);
int fsize(char *);
char *malloc(long);
int numlength(int);
char *readfile(char *);
int send(int, char *, scalar_t__, int /*<<< orphan*/);
int sprintf(char *, char *, ...);
scalar_t__ strlen(char *);
int respond_with_file(int connfd, char *filename) {
  int rval = 0;
  long message_size;
  char *message;
  char *status;
  char *content_length;
  char *content;
  off_t filesize;
  message_size = 0;
  status = "HTTP/1.1 200 OK\n";
  message_size += strlen(status);
  if ((filesize = fsize(filename)) == -1) {
    return -1;
  }
  if ((content = readfile(filename)) == NULL) {
    return -1;
  }
  message_size += strlen(content);
  message_size += numlength(filesize);
  content_length = malloc((18 + numlength(filesize)) * sizeof(char));
  if (sprintf(content_length, "Content-Length:%d\n", (int)filesize - 1) == -1) {
    rval = -1;
    goto out;
  }
  message_size += strlen(content_length);
  message = malloc(message_size * sizeof(char));
  if (sprintf(message, "%s%s\n%s", status, content_length, content) == -1) {
    rval = -1;
    goto end;
  }
  if (send(connfd, message, strlen(message), 0) == -1) {
    rval = -1;
    goto end;
  }
end:
  free(message);
out:
  free(content_length);
  free(content);
  return rval;
}


x86 assembly (clang, -O0):
<respond_with_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
movq   $0x0,-0x20(%rbp)
lea    0xe5b(%rip),%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
call   1040 <strlen@plt>
add    -0x20(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1080 <fsize@plt>
mov    %eax,-0x44(%rbp)
cmp    $0xffffffff,%eax
jne    11db <respond_with_file+0x5b>
movl   $0xffffffff,-0x4(%rbp)
jmp    1321 <respond_with_file+0x1a1>
mov    -0x10(%rbp),%rdi
call   10a0 <readfile@plt>
mov    %rax,-0x40(%rbp)
cmp    $0x0,%rax
jne    11fe <respond_with_file+0x7e>
movl   $0xffffffff,-0x4(%rbp)
jmp    1321 <respond_with_file+0x1a1>
mov    -0x40(%rbp),%rdi
call   1040 <strlen@plt>
add    -0x20(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x44(%rbp),%edi
call   1050 <numlength@plt>
cltq
add    -0x20(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x44(%rbp),%edi
call   1050 <numlength@plt>
add    $0x12,%eax
movslq %eax,%rdi
shl    $0x0,%rdi
call   1070 <malloc@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rdi
mov    -0x44(%rbp),%edx
sub    $0x1,%edx
lea    0xdc4(%rip),%rsi
mov    $0x0,%al
call   1090 <sprintf@plt>
cmp    $0xffffffff,%eax
jne    1269 <respond_with_file+0xe9>
movl   $0xffffffff,-0x14(%rbp)
jmp    1309 <respond_with_file+0x189>
mov    -0x38(%rbp),%rdi
call   1040 <strlen@plt>
add    -0x20(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
shl    $0x0,%rdi
call   1070 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x30(%rbp),%rdx
mov    -0x38(%rbp),%rcx
mov    -0x40(%rbp),%r8
lea    0xd82(%rip),%rsi
mov    $0x0,%al
call   1090 <sprintf@plt>
cmp    $0xffffffff,%eax
jne    12be <respond_with_file+0x13e>
movl   $0xffffffff,-0x14(%rbp)
jmp    1300 <respond_with_file+0x180>
mov    -0x8(%rbp),%eax
mov    %eax,-0x54(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    -0x28(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x54(%rbp),%edi
mov    -0x50(%rbp),%rsi
mov    %rax,%rdx
xor    %ecx,%ecx
call   1060 <send@plt>
cmp    $0xffffffff,%eax
jne    12fb <respond_with_file+0x17b>
movl   $0xffffffff,-0x14(%rbp)
jmp    1300 <respond_with_file+0x180>
jmp    1300 <respond_with_file+0x180>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
mov    -0x40(%rbp),%rdi
call   1030 <free@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<respond_with_file>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%rbp
mov    %edi,%r14d
mov    %rsi,%rdi
call   1080 <fsize@plt>
mov    $0xffffffff,%r13d
cmp    $0xffffffff,%eax
je     1291 <respond_with_file+0x111>
mov    %eax,%ebx
mov    %rbp,%rdi
call   10a0 <readfile@plt>
test   %rax,%rax
je     1291 <respond_with_file+0x111>
mov    %rax,%r12
mov    %rax,%rdi
call   1040 <strlen@plt>
mov    %rax,%rbp
mov    %ebx,%edi
call   1050 <numlength@plt>
mov    %eax,%r13d
mov    %ebx,%edi
call   1050 <numlength@plt>
add    $0x12,%eax
movslq %eax,%rdi
call   1070 <malloc@plt>
mov    %rax,%r15
add    $0xffffffff,%ebx
lea    0xe1f(%rip),%rsi
mov    %rax,%rdi
mov    %ebx,%edx
xor    %eax,%eax
call   1090 <sprintf@plt>
cmp    $0xffffffff,%eax
je     126b <respond_with_file+0xeb>
movslq %r13d,%rax
add    %rax,%rbp
mov    %r15,%rdi
call   1040 <strlen@plt>
lea    (%rax,%rbp,1),%rdi
add    $0x10,%rdi
call   1070 <malloc@plt>
mov    %rax,%rbx
lea    0xdfc(%rip),%rsi
lea    0xdd1(%rip),%rdx
mov    %rax,%rdi
mov    %r15,%rcx
mov    %r12,%r8
xor    %eax,%eax
call   1090 <sprintf@plt>
cmp    $0xffffffff,%eax
je     1273 <respond_with_file+0xf3>
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    %r14d,%edi
mov    %rbx,%rsi
mov    %rax,%rdx
xor    %ecx,%ecx
call   1060 <send@plt>
xor    %r13d,%r13d
cmp    $0xffffffff,%eax
sete   %r13b
neg    %r13d
jmp    1279 <respond_with_file+0xf9>
mov    $0xffffffff,%r13d
jmp    1281 <respond_with_file+0x101>
mov    $0xffffffff,%r13d
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %r15,%rdi
call   1030 <free@plt>
mov    %r12,%rdi
call   1030 <free@plt>
mov    %r13d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [17 x i8] c"HTTP/1.1 200 OK\0A\00"
@.str.1 = [19 x i8] c"Content-Length:%d\0A\00"
@.str.2 = [8 x i8] c"%s%s\0A%s\00"
define i32 @respond_with_file(i32 %0, i8* %1) {
%3 = tail call i32 @fsize(i8* %1)
%4 = icmp eq i32 %3, -1
br i1 %4, label %39, label %5
5: ; preds = %2
%6 = tail call i8* @readfile(i8* %1)
%7 = icmp eq i8* %6, null
br i1 %7, label %39, label %8
8: ; preds = %5
%9 = tail call i64 @strlen(i8* %6)
%10 = tail call i32 @numlength(i32 %3)
%11 = tail call i32 @numlength(i32 %3)
%12 = add nsw i32 %11, 18
%13 = sext i32 %12 to i64
%14 = tail call i8* @malloc(i64 %13)
%15 = add nsw i32 %3, -1
%16 = tail call i32 (i8*, i8*, ...) @sprintf(i8* dereferenceable(1) %14, i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i32 %15)
%17 = icmp eq i32 %16, -1
br i1 %17, label %35, label %18
18: ; preds = %8
%19 = add nsw i64 %9, 16
%20 = sext i32 %10 to i64
%21 = add nsw i64 %19, %20
%22 = tail call i64 @strlen(i8* dereferenceable(1) %14)
%23 = add nsw i64 %21, %22
%24 = tail call i8* @malloc(i64 %23)
%25 = tail call i32 (i8*, i8*, ...) @sprintf(i8* dereferenceable(1) %24, 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, i64 0, i64 0), i8* %14, i8* %6)
%26 = icmp eq i32 %25, -1
br i1 %26, label %32, label %27
27: ; preds = %18
%28 = tail call i64 @strlen(i8* dereferenceable(1) %24)
%29 = tail call i32 @send(i32 %0, i8* %24, i64 %28, i32 0)
%30 = icmp eq i32 %29, -1
%31 = sext i1 %30 to i32
br label %32
32: ; preds = %27, %18
%33 = phi i32 [ -1, %18 ], [ %31, %27 ]
%34 = tail call i32 @free(i8* %24)
br label %35
35: ; preds = %8, %32
%36 = phi i32 [ %33, %32 ], [ -1, %8 ]
%37 = tail call i32 @free(i8* %14)
%38 = tail call i32 @free(i8* %6)
br label %39
39: ; preds = %5, %2, %35
%40 = phi i32 [ %36, %35 ], [ -1, %2 ], [ -1, %5 ]
ret i32 %40
}