#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 */

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ free(char *);
int get_file_size(int);
char *malloc(int);
char *object_ninit(int);
int open(char *, int /*<<< orphan*/, int /*<<< orphan*/);
int read(int, char *, int);
int send(int, char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ sendString(int, char *);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
int /*<<< orphan*/ sprintf(char *, char *, char *);
int strlen(char *);
int sendFile(int sockfd, char *file_name) {
  int file, length;
  char *ptr;
  char *bPtr;
  if ((file = open(file_name, O_RDONLY, 0)) == -1)
    return -1;
  length = get_file_size(file);
  int header_length = strlen("Content-Length: ") + 5;
  char length_header[header_length];
  snprintf(length_header, header_length, "Content-Length: %d\r\n", length);
  sendString(sockfd, length_header);
  char filetype_format[] = "Content-Type: %s; charset=UTF-8\r\n";
  char filetype[] = "text/html";
  char *filetype_header = object_ninit(
      (strlen(filetype_format) + strlen(filetype) - 1) * sizeof(char));
  sprintf(filetype_header, filetype_format, filetype);
  sendString(sockfd, filetype_header);
  free(filetype_header);
  sendString(sockfd, "\r\n");
  if ((ptr = malloc(length * sizeof(char))) == NULL)
    return -1;
  bPtr = ptr;
  if (read(file, ptr, length) == -1)
    return -1;
  int send_bytes = 0;
  int bytes_to_send = length;
  while (bytes_to_send > 0) {
    send_bytes = send(sockfd, ptr, bytes_to_send, 0);
    if (send_bytes == -1)
      return -1;
    bytes_to_send -= send_bytes;
    ptr += send_bytes;
  }
  free(bPtr);
  return send_bytes;
}


x86 assembly (clang, -O0):
<sendFile>:
push   %rbp
mov    %rsp,%rbp
sub    $0xa0,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e0b(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10d0 <open@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
jne    11fe <sendFile+0x3e>
movl   $0xffffffff,-0x4(%rbp)
jmp    141d <sendFile+0x25d>
mov    -0x14(%rbp),%edi
call   10c0 <get_file_size@plt>
mov    %eax,-0x18(%rbp)
lea    0xdf0(%rip),%rdi
call   1060 <strlen@plt>
add    $0x5,%eax
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
mov    %rsp,%rcx
mov    %rcx,-0x38(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdi
sub    %rcx,%rdi
mov    %rdi,-0xa0(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x40(%rbp)
mov    -0x2c(%rbp),%esi
mov    -0x18(%rbp),%ecx
lea    0xdc0(%rip),%rdx
call   1080 <snprintf@plt>
mov    -0xa0(%rbp),%rsi
mov    -0x8(%rbp),%edi
call   1040 <sendString@plt>
lea    -0x70(%rbp),%rdi
lea    0xdc0(%rip),%rsi
mov    $0x22,%edx
call   10a0 <memcpy@plt>
mov    0xda5(%rip),%rax
mov    %rax,-0x7a(%rbp)
mov    0xda2(%rip),%ax
mov    %ax,-0x72(%rbp)
lea    -0x70(%rbp),%rdi
call   1060 <strlen@plt>
mov    %eax,-0x98(%rbp)
lea    -0x7a(%rbp),%rdi
call   1060 <strlen@plt>
mov    %eax,%ecx
mov    -0x98(%rbp),%eax
add    %ecx,%eax
sub    $0x1,%eax
cltq
shl    $0x0,%rax
mov    %eax,%edi
call   1050 <object_ninit@plt>
mov    %rax,-0x88(%rbp)
mov    -0x88(%rbp),%rdi
lea    -0x70(%rbp),%rsi
lea    -0x7a(%rbp),%rdx
call   10e0 <sprintf@plt>
mov    -0x8(%rbp),%edi
mov    -0x88(%rbp),%rsi
call   1040 <sendString@plt>
mov    -0x88(%rbp),%rdi
call   1030 <free@plt>
mov    -0x8(%rbp),%edi
lea    0xd21(%rip),%rsi
call   1040 <sendString@plt>
movslq -0x18(%rbp),%rax
shl    $0x0,%rax
mov    %eax,%edi
call   10b0 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    133a <sendFile+0x17a>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x8c(%rbp)
jmp    1416 <sendFile+0x256>
mov    -0x20(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x14(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    -0x18(%rbp),%edx
call   1090 <read@plt>
cmp    $0xffffffff,%eax
jne    1370 <sendFile+0x1b0>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x8c(%rbp)
jmp    1416 <sendFile+0x256>
movl   $0x0,-0x90(%rbp)
mov    -0x18(%rbp),%eax
mov    %eax,-0x94(%rbp)
cmpl   $0x0,-0x94(%rbp)
jle    13fa <sendFile+0x23a>
mov    -0x8(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    -0x94(%rbp),%edx
xor    %ecx,%ecx
call   1070 <send@plt>
mov    %eax,-0x90(%rbp)
cmpl   $0xffffffff,-0x90(%rbp)
jne    13cd <sendFile+0x20d>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x8c(%rbp)
jmp    1416 <sendFile+0x256>
mov    -0x90(%rbp),%ecx
mov    -0x94(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x94(%rbp)
mov    -0x90(%rbp),%ecx
mov    -0x20(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x20(%rbp)
jmp    1383 <sendFile+0x1c3>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x90(%rbp),%eax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x8c(%rbp)
mov    -0x38(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sendFile>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x48,%rsp
mov    %rsi,%rax
mov    %edi,%r14d
mov    0x2e1a(%rip),%rcx
mov    (%rcx),%esi
mov    %rax,%rdi
xor    %edx,%edx
call   10c0 <open@plt>
cmp    $0xffffffff,%eax
je     1314 <sendFile+0x164>
mov    %eax,%r15d
mov    %eax,%edi
call   10b0 <get_file_size@plt>
mov    %eax,%r12d
lea    0xe09(%rip),%rdi
call   1060 <strlen@plt>
mov    %rsp,-0x30(%rbp)
lea    0x5(%rax),%esi
mov    %rsp,%rbx
lea    0xf(%rsi),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
lea    0xdf6(%rip),%rdx
mov    %rbx,%rdi
mov    %r12d,%ecx
call   1080 <snprintf@plt>
mov    %r14d,%edi
mov    %rbx,%rsi
call   1040 <sendString@plt>
movaps 0xe08(%rip),%xmm0
movaps %xmm0,-0x60(%rbp)
movaps 0xded(%rip),%xmm0
movaps %xmm0,-0x70(%rbp)
movw   $0xa,-0x50(%rbp)
movabs $0x6d74682f74786574,%rax
68 74 6d
mov    %rax,-0x40(%rbp)
movw   $0x6c,-0x38(%rbp)
lea    -0x70(%rbp),%rdi
call   1060 <strlen@plt>
mov    %eax,%ebx
lea    -0x40(%rbp),%r13
mov    %r13,%rdi
call   1060 <strlen@plt>
lea    (%rbx,%rax,1),%edi
add    $0xffffffff,%edi
call   1050 <object_ninit@plt>
mov    %rax,%rbx
mov    %rax,%rdi
lea    -0x70(%rbp),%rsi
mov    %r13,%rdx
call   10d0 <sprintf@plt>
mov    %r14d,%edi
mov    %rbx,%rsi
call   1040 <sendString@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
lea    0xd74(%rip),%rsi
mov    %r14d,%edi
call   1040 <sendString@plt>
mov    %r12d,%edi
call   10a0 <malloc@plt>
mov    $0xffffffff,%ebx
test   %rax,%rax
je     1325 <sendFile+0x175>
mov    %rax,%r13
mov    %r15d,%edi
mov    %rax,%rsi
mov    %r12d,%edx
call   1090 <read@plt>
cmp    $0xffffffff,%eax
je     1325 <sendFile+0x175>
test   %r12d,%r12d
jle    131b <sendFile+0x16b>
mov    %r13,%rbx
nopw   0x0(%rax,%rax,1)
mov    %r14d,%edi
mov    %rbx,%rsi
mov    %r12d,%edx
xor    %ecx,%ecx
call   1070 <send@plt>
cmp    $0xffffffff,%eax
je     133d <sendFile+0x18d>
movslq %eax,%rcx
add    %rcx,%rbx
sub    %eax,%r12d
jg     12f0 <sendFile+0x140>
mov    %eax,%ebx
jmp    131d <sendFile+0x16d>
mov    $0xffffffff,%ebx
jmp    132c <sendFile+0x17c>
xor    %ebx,%ebx
mov    %r13,%rdi
call   1030 <free@plt>
mov    -0x30(%rbp),%rax
mov    %rax,%rsp
mov    %ebx,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    -0x30(%rbp),%rax
mov    $0xffffffff,%ebx
jmp    1329 <sendFile+0x179>

LLVM IR (-O0):
@O_RDONLY = global i32 0
@.str = [17 x i8] c"Content-Length: \00"
@.str.1 = [21 x i8] c"Content-Length: %d\0D\0A\00"
@__const.sendFile.filetype_format = [34 x i8] c"Content-Type: %s; charset=UTF-8\0D\0A\00"
@__const.sendFile.filetype = [10 x i8] c"text/html\00"
@.str.2 = [3 x i8] c"\0D\0A\00"
define i32 @sendFile(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i32
%11 = alloca i8*
%12 = alloca i64
%13 = alloca [34 x i8]
%14 = alloca [10 x i8]
%15 = alloca i8*
%16 = alloca i32
%17 = alloca i32
%18 = alloca i32
store i32 %0, i32* %4
store i8* %1, i8** %5
%19 = load i8*, i8** %5
%20 = load i32, i32* @O_RDONLY
%21 = call i32 @open(i8* %19, i32 %20, i32 0)
store i32 %21, i32* %6
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %24
23: ; preds = %2
store i32 -1, i32* %3
br label %103
24: ; preds = %2
%25 = load i32, i32* %6
%26 = call i32 @get_file_size(i32 %25)
store i32 %26, i32* %7
%27 = call i32 @strlen(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
%28 = add nsw i32 %27, 5
store i32 %28, i32* %10
%29 = load i32, i32* %10
%30 = zext i32 %29 to i64
%31 = call i8* @llvm.stacksave()
store i8* %31, i8** %11
%32 = alloca i8, i64 %30
store i64 %30, i64* %12
%33 = load i32, i32* %10
%34 = load i32, i32* %7
%35 = call i32 @snprintf(i8* %32, i32 %33, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i32 %34)
%36 = load i32, i32* %4
%37 = call i32 @sendString(i32 %36, i8* %32)
%38 = bitcast [34 x i8]* %13 to i8*
%39 = bitcast [10 x i8]* %14 to i8*
%40 = getelementptr inbounds [34 x i8], [34 x i8]* %13, i64 0, i64 0
%41 = call i32 @strlen(i8* %40)
%42 = getelementptr inbounds [10 x i8], [10 x i8]* %14, i64 0, i64 0
%43 = call i32 @strlen(i8* %42)
%44 = add nsw i32 %41, %43
%45 = sub nsw i32 %44, 1
%46 = sext i32 %45 to i64
%47 = mul i64 %46, 1
%48 = trunc i64 %47 to i32
%49 = call i8* @object_ninit(i32 %48)
store i8* %49, i8** %15
%50 = load i8*, i8** %15
%51 = getelementptr inbounds [34 x i8], [34 x i8]* %13, i64 0, i64 0
%52 = getelementptr inbounds [10 x i8], [10 x i8]* %14, i64 0, i64 0
%53 = call i32 @sprintf(i8* %50, i8* %51, i8* %52)
%54 = load i32, i32* %4
%55 = load i8*, i8** %15
%56 = call i32 @sendString(i32 %54, i8* %55)
%57 = load i8*, i8** %15
%58 = call i32 @free(i8* %57)
%59 = load i32, i32* %4
%60 = call i32 @sendString(i32 %59, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%61 = load i32, i32* %7
%62 = sext i32 %61 to i64
%63 = mul i64 %62, 1
%64 = trunc i64 %63 to i32
%65 = call i8* @malloc(i32 %64)
store i8* %65, i8** %8
%66 = icmp eq i8* %65, null
br i1 %66, label %67, label %68
67: ; preds = %24
store i32 -1, i32* %3
store i32 1, i32* %16
br label %101
68: ; preds = %24
%69 = load i8*, i8** %8
store i8* %69, i8** %9
%70 = load i32, i32* %6
%71 = load i8*, i8** %8
%72 = load i32, i32* %7
%73 = call i32 @read(i32 %70, i8* %71, i32 %72)
%74 = icmp eq i32 %73, -1
br i1 %74, label %75, label %76
75: ; preds = %68
store i32 -1, i32* %3
store i32 1, i32* %16
br label %101
76: ; preds = %68
store i32 0, i32* %17
%77 = load i32, i32* %7
store i32 %77, i32* %18
br label %78
78: ; preds = %89, %76
%79 = load i32, i32* %18
%80 = icmp sgt i32 %79, 0
br i1 %80, label %81, label %97
81: ; preds = %78
%82 = load i32, i32* %4
%83 = load i8*, i8** %8
%84 = load i32, i32* %18
%85 = call i32 @send(i32 %82, i8* %83, i32 %84, i32 0)
store i32 %85, i32* %17
%86 = load i32, i32* %17
%87 = icmp eq i32 %86, -1
br i1 %87, label %88, label %89
88: ; preds = %81
store i32 -1, i32* %3
store i32 1, i32* %16
br label %101
89: ; preds = %81
%90 = load i32, i32* %17
%91 = load i32, i32* %18
%92 = sub nsw i32 %91, %90
store i32 %92, i32* %18
%93 = load i32, i32* %17
%94 = load i8*, i8** %8
%95 = sext i32 %93 to i64
%96 = getelementptr inbounds i8, i8* %94, i64 %95
store i8* %96, i8** %8
br label %78
97: ; preds = %78
%98 = load i8*, i8** %9
%99 = call i32 @free(i8* %98)
%100 = load i32, i32* %17
store i32 %100, i32* %3
store i32 1, i32* %16
br label %101
101: ; preds = %97, %88, %75, %67
%102 = load i8*, i8** %11
br label %103
103: ; preds = %101, %23
%104 = load i32, i32* %3
ret i32 %104
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@.str = [17 x i8] c"Content-Length: \00"
@.str.1 = [21 x i8] c"Content-Length: %d\0D\0A\00"
@__const.sendFile.filetype_format = [34 x i8] c"Content-Type: %s; charset=UTF-8\0D\0A\00"
@__const.sendFile.filetype = [10 x i8] c"text/html\00"
@.str.2 = [3 x i8] c"\0D\0A\00"
define i32 @sendFile(i32 %0, i8* nocapture %1) {
%3 = alloca [34 x i8]
%4 = alloca [10 x i8]
%5 = load i32, i32* @O_RDONLY
%6 = tail call i32 @open(i8* %1, i32 %5, i32 0)
%7 = icmp eq i32 %6, -1
br i1 %7, label %50, label %8
8: ; preds = %2
%9 = tail call i32 @get_file_size(i32 %6)
%10 = tail call i32 @strlen(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
%11 = add nsw i32 %10, 5
%12 = zext i32 %11 to i64
%13 = tail call i8* @llvm.stacksave()
%14 = alloca i8, i64 %12
%15 = call i32 @snprintf(i8* %14, i32 %11, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i32 %9)
%16 = call i32 @sendString(i32 %0, i8* %14)
%17 = getelementptr inbounds [34 x i8], [34 x i8]* %3, i64 0, i64 0
%18 = getelementptr inbounds [10 x i8], [10 x i8]* %4, i64 0, i64 0
%19 = call i32 @strlen(i8* %17)
%20 = call i32 @strlen(i8* %18)
%21 = add i32 %19, -1
%22 = add i32 %21, %20
%23 = call i8* @object_ninit(i32 %22)
%24 = call i32 @sprintf(i8* dereferenceable(1) %23, i8* %17, i8* %18)
%25 = call i32 @sendString(i32 %0, i8* %23)
%26 = call i32 @free(i8* %23)
%27 = call i32 @sendString(i32 %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%28 = call i8* @malloc(i32 %9)
%29 = icmp eq i8* %28, null
br i1 %29, label %48, label %30
30: ; preds = %8
%31 = call i32 @read(i32 %6, i8* %28, i32 %9)
%32 = icmp eq i32 %31, -1
br i1 %32, label %48, label %33
33: ; preds = %30
%34 = icmp sgt i32 %9, 0
br i1 %34, label %35, label %45
35: ; preds = %33, %40
%36 = phi i32 [ %41, %40 ], [ %9, %33 ]
%37 = phi i8* [ %43, %40 ], [ %28, %33 ]
%38 = call i32 @send(i32 %0, i8* %37, i32 %36, i32 0)
%39 = icmp eq i32 %38, -1
br i1 %39, label %48, label %40
40: ; preds = %35
%41 = sub nsw i32 %36, %38
%42 = sext i32 %38 to i64
%43 = getelementptr inbounds i8, i8* %37, i64 %42
%44 = icmp sgt i32 %41, 0
br i1 %44, label %35, label %45
45: ; preds = %40, %33
%46 = phi i32 [ 0, %33 ], [ %38, %40 ]
%47 = call i32 @free(i8* %28)
br label %48
48: ; preds = %35, %45, %30, %8
%49 = phi i32 [ -1, %8 ], [ -1, %30 ], [ %46, %45 ], [ -1, %35 ]
br label %50
50: ; preds = %2, %48
%51 = phi i32 [ %49, %48 ], [ -1, %2 ]
ret i32 %51
}