#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*/ get_content_type(char *);
long get_file_contents(char *, char *);
scalar_t__ malloc(int);
int /*<<< orphan*/ report_errno(char *);
long send(int, char *, size_t, int /*<<< orphan*/);
int /*<<< orphan*/ send_file_not_found(int);
int /*<<< orphan*/ sprintf(char *, char *, int /*<<< orphan*/, long, char *);
scalar_t__ strlen(char *);
int send_packets(int connfd, char *file_contents, char *filepath) {
  int offset;
  long bytes_to_send = get_file_contents(filepath, file_contents);
  if (bytes_to_send == 0) {
    send_file_not_found(connfd);
    return -1;
  }
  char *message = (char *)malloc(65536);
  message = "HTTP/1.1 200 OK\r\nContent-Type: %s\r\nContent-Length: %ld"
            "\r\nConnection: close\r\n\r\n%s";
  bytes_to_send += strlen(message);
  char temp[2 * 65536];
  sprintf(temp, message, get_content_type(filepath), bytes_to_send,
          file_contents);
  long to_send = 0;
  message = temp;
  for (offset = 0; offset < bytes_to_send; offset += 512) {
    to_send = 512;
    if (offset + 512 > bytes_to_send) {
      to_send = bytes_to_send % 512;
    }
    long amount_sent = send(connfd, message + offset, (size_t)to_send, 0);
    if (amount_sent == -1) {
      char *error_message = "error sending to socket with file "
                            "descriptor %d.\n";
      report_errno(error_message);
      return -1;
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<send_packets>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20060,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1060 <get_file_contents@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11c6 <send_packets+0x46>
mov    -0x8(%rbp),%edi
call   1040 <send_file_not_found@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1301 <send_packets+0x181>
mov    $0x10000,%edi
call   1070 <malloc@plt>
mov    %rax,-0x30(%rbp)
lea    0xe25(%rip),%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
call   1030 <strlen@plt>
add    -0x28(%rbp),%rax
mov    %rax,-0x28(%rbp)
lea    -0x20030(%rbp),%rax
mov    %rax,-0x20058(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,-0x20050(%rbp)
mov    -0x18(%rbp),%rdi
call   10a0 <get_content_type@plt>
mov    -0x20058(%rbp),%rdi
mov    -0x20050(%rbp),%rsi
mov    %eax,%edx
mov    -0x28(%rbp),%rcx
mov    -0x10(%rbp),%r8
call   1090 <sprintf@plt>
movq   $0x0,-0x20038(%rbp)
lea    -0x20030(%rbp),%rax
mov    %rax,-0x30(%rbp)
movl   $0x0,-0x1c(%rbp)
movslq -0x1c(%rbp),%rax
cmp    -0x28(%rbp),%rax
jge    12fa <send_packets+0x17a>
movq   $0x200,-0x20038(%rbp)
mov    -0x1c(%rbp),%eax
add    $0x200,%eax
cltq
cmp    -0x28(%rbp),%rax
jle    128e <send_packets+0x10e>
mov    -0x28(%rbp),%rax
mov    $0x200,%ecx
cqto
idiv   %rcx
mov    %rdx,-0x20038(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x30(%rbp),%rsi
movslq -0x1c(%rbp),%rax
add    %rax,%rsi
mov    -0x20038(%rbp),%rdx
xor    %ecx,%ecx
call   1050 <send@plt>
mov    %rax,-0x20040(%rbp)
cmpq   $0xffffffffffffffff,-0x20040(%rbp)
ff
jne    12e5 <send_packets+0x165>
lea    0xd8a(%rip),%rax
mov    %rax,-0x20048(%rbp)
mov    -0x20048(%rbp),%rdi
call   1080 <report_errno@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1301 <send_packets+0x181>
jmp    12ea <send_packets+0x16a>
mov    -0x1c(%rbp),%eax
add    $0x200,%eax
mov    %eax,-0x1c(%rbp)
jmp    124c <send_packets+0xcc>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20060,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<send_packets>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x20000,%rsp
mov    %rdx,%r15
mov    %rsi,%r14
mov    %edi,%r12d
mov    %rdx,%rdi
call   1050 <get_file_contents@plt>
test   %rax,%rax
je     1225 <send_packets+0xb5>
mov    %rax,%rbx
mov    $0x10000,%edi
call   1060 <malloc@plt>
add    $0x4f,%rbx
mov    %r15,%rdi
call   1090 <get_content_type@plt>
lea    0xe47(%rip),%rsi
mov    %rsp,%rdi
mov    %eax,%edx
mov    %rbx,%rcx
mov    %r14,%r8
call   1080 <sprintf@plt>
xor    %r14d,%r14d
lea    0x1ff(%rbx),%r15
xor    %ecx,%ecx
jmp    11f7 <send_packets+0x87>
nopw   0x0(%rax,%rax,1)
lea    (%rsp,%rcx,1),%rsi
mov    %r12d,%edi
xor    %ecx,%ecx
call   1040 <send@plt>
mov    %rbp,%rcx
cmp    $0xffffffffffffffff,%rax
je     122f <send_packets+0xbf>
cmp    %rcx,%rbx
jle    1241 <send_packets+0xd1>
lea    0x200(%rcx),%rbp
mov    $0x200,%edx
cmp    %rbp,%rbx
jge    11e0 <send_packets+0x70>
test   %rbx,%rbx
mov    %rbx,%rax
cmovs  %r15,%rax
and    $0xfffffffffffffe00,%rax
mov    %rbx,%rdx
sub    %rax,%rdx
jmp    11e0 <send_packets+0x70>
mov    %r12d,%edi
call   1030 <send_file_not_found@plt>
jmp    123b <send_packets+0xcb>
lea    0xe1a(%rip),%rdi
call   1070 <report_errno@plt>
mov    $0xffffffff,%r14d
mov    %r14d,%eax
add    $0x20000,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [80 x i8] c"HTTP/1.1 200 OK\0D\0AContent-Type: %s\0D\0AContent-Length: %ld\0D\0AConnection: close\0D\0A\0D\0A%s\00"
@.str.1 = [50 x i8] c"error sending to socket with file descriptor %d.\0A\00"
define i32 @send_packets(i32 %0, i8* %1, i8* %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i64
%10 = alloca i8*
%11 = alloca [131072 x i8]
%12 = alloca i64
%13 = alloca i64
%14 = alloca i8*
store i32 %0, i32* %5
store i8* %1, i8** %6
store i8* %2, i8** %7
%15 = load i8*, i8** %7
%16 = load i8*, i8** %6
%17 = call i64 @get_file_contents(i8* %15, i8* %16)
store i64 %17, i64* %9
%18 = load i64, i64* %9
%19 = icmp eq i64 %18, 0
br i1 %19, label %20, label %23
20: ; preds = %3
%21 = load i32, i32* %5
%22 = call i32 @send_file_not_found(i32 %21)
store i32 -1, i32* %4
br label %70
23: ; preds = %3
%24 = call i64 @malloc(i32 65536)
%25 = inttoptr i64 %24 to i8*
store i8* %25, i8** %10
store i8* getelementptr inbounds ([80 x i8], [80 x i8]* @.str, i64 0, i64 0), i8** %10
%26 = load i8*, i8** %10
%27 = call i64 @strlen(i8* %26)
%28 = load i64, i64* %9
%29 = add nsw i64 %28, %27
store i64 %29, i64* %9
%30 = getelementptr inbounds [131072 x i8], [131072 x i8]* %11, i64 0, i64 0
%31 = load i8*, i8** %10
%32 = load i8*, i8** %7
%33 = call i32 @get_content_type(i8* %32)
%34 = load i64, i64* %9
%35 = load i8*, i8** %6
%36 = call i32 @sprintf(i8* %30, i8* %31, i32 %33, i64 %34, i8* %35)
store i64 0, i64* %12
%37 = getelementptr inbounds [131072 x i8], [131072 x i8]* %11, i64 0, i64 0
store i8* %37, i8** %10
store i32 0, i32* %8
br label %38
38: ; preds = %66, %23
%39 = load i32, i32* %8
%40 = sext i32 %39 to i64
%41 = load i64, i64* %9
%42 = icmp slt i64 %40, %41
br i1 %42, label %43, label %69
43: ; preds = %38
store i64 512, i64* %12
%44 = load i32, i32* %8
%45 = add nsw i32 %44, 512
%46 = sext i32 %45 to i64
%47 = load i64, i64* %9
%48 = icmp sgt i64 %46, %47
br i1 %48, label %49, label %52
49: ; preds = %43
%50 = load i64, i64* %9
%51 = srem i64 %50, 512
store i64 %51, i64* %12
br label %52
52: ; preds = %49, %43
%53 = load i32, i32* %5
%54 = load i8*, i8** %10
%55 = load i32, i32* %8
%56 = sext i32 %55 to i64
%57 = getelementptr inbounds i8, i8* %54, i64 %56
%58 = load i64, i64* %12
%59 = call i64 @send(i32 %53, i8* %57, i64 %58, i32 0)
store i64 %59, i64* %13
%60 = load i64, i64* %13
%61 = icmp eq i64 %60, -1
br i1 %61, label %62, label %65
62: ; preds = %52
store i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.1, i64 0, i64 0), i8** %14
%63 = load i8*, i8** %14
%64 = call i32 @report_errno(i8* %63)
store i32 -1, i32* %4
br label %70
65: ; preds = %52
br label %66
66: ; preds = %65
%67 = load i32, i32* %8
%68 = add nsw i32 %67, 512
store i32 %68, i32* %8
br label %38
69: ; preds = %38
store i32 0, i32* %4
br label %70
70: ; preds = %69, %62, %20
%71 = load i32, i32* %4
ret i32 %71
}

LLVM IR (-O2):
@.str = [80 x i8] c"HTTP/1.1 200 OK\0D\0AContent-Type: %s\0D\0AContent-Length: %ld\0D\0AConnection: close\0D\0A\0D\0A%s\00"
@.str.1 = [50 x i8] c"error sending to socket with file descriptor %d.\0A\00"
define i32 @send_packets(i32 %0, i8* %1, i8* %2) {
%4 = alloca [131072 x i8]
%5 = tail call i64 @get_file_contents(i8* %2, i8* %1)
%6 = icmp eq i64 %5, 0
br i1 %6, label %7, label %9
7: ; preds = %3
%8 = tail call i32 @send_file_not_found(i32 %0)
br label %30
9: ; preds = %3
%10 = tail call i64 @malloc(i32 65536)
%11 = add nsw i64 %5, 79
%12 = getelementptr inbounds [131072 x i8], [131072 x i8]* %4, i64 0, i64 0
%13 = tail call i32 @get_content_type(i8* %2)
%14 = call i32 @sprintf(i8* %12, i8* dereferenceable(1) getelementptr inbounds ([80 x i8], [80 x i8]* @.str, i64 0, i64 0), i32 %13, i64 %11, i8* %1)
%15 = srem i64 %11, 512
br label %16
16: ; preds = %19, %9
%17 = phi i64 [ %20, %19 ], [ 0, %9 ]
%18 = icmp sgt i64 %11, %17
br i1 %18, label %19, label %28
19: ; preds = %16
%20 = add nuw i64 %17, 512
%21 = icmp slt i64 %11, %20
%22 = select i1 %21, i64 %15, i64 512
%23 = getelementptr inbounds [131072 x i8], [131072 x i8]* %4, i64 0, i64 %17
%24 = call i64 @send(i32 %0, i8* %23, i64 %22, i32 0)
%25 = icmp eq i64 %24, -1
br i1 %25, label %26, label %16
26: ; preds = %19
%27 = call i32 @report_errno(i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.1, i64 0, i64 0))
br label %28
28: ; preds = %16, %26
%29 = phi i32 [ -1, %26 ], [ 0, %16 ]
br label %30
30: ; preds = %28, %7
%31 = phi i32 [ -1, %7 ], [ %29, %28 ]
ret i32 %31
}