#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*/ MSG_NOSIGNAL;
scalar_t__ malloc(int);
int send(int, char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ sprintf(char *, char *, unsigned long);
int /*<<< orphan*/ strcat(char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
int strlen(char *);
int send_header(int client_socket, int http_code,
                unsigned long content_length) {
  char *ans = (char *)malloc(1024);
  strcpy(ans, "HTTP/1.0 ");
  switch (http_code) {
  case 200:
    strcat(ans, "200 OK\n");
    break;
  case 404:
    strcat(ans, "404 Not Found\n");
    break;
  default:
    strcat(ans, "500 Internal Server Error\n");
    break;
  }
  sprintf(ans + strlen(ans), "Content-length: %lu\r\n", content_length);
  strcat(ans, "Content-Type: text/html\r\n");
  strcat(ans, "\r\n");
  ans[1024 - 1] = 0;
  if (-1 == send(client_socket, ans, strlen(ans), MSG_NOSIGNAL))
    return -1;
  return 0;
}


x86 assembly (clang, -O0):
<send_header>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    $0x400,%edi
call   1060 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
lea    0xe75(%rip),%rsi
call   1030 <strcpy@plt>
mov    -0xc(%rbp),%eax
mov    %eax,-0x24(%rbp)
sub    $0xc8,%eax
je     11b9 <send_header+0x59>
jmp    11a6 <send_header+0x46>
mov    -0x24(%rbp),%eax
sub    $0x194,%eax
je     11ce <send_header+0x6e>
jmp    11e3 <send_header+0x83>
mov    -0x20(%rbp),%rdi
lea    0xe46(%rip),%rsi
call   1070 <strcat@plt>
jmp    11f3 <send_header+0x93>
mov    -0x20(%rbp),%rdi
lea    0xe39(%rip),%rsi
call   1070 <strcat@plt>
jmp    11f3 <send_header+0x93>
mov    -0x20(%rbp),%rdi
lea    0xe33(%rip),%rsi
call   1070 <strcat@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x20(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x40(%rbp),%rdi
cltq
add    %rax,%rdi
mov    -0x18(%rbp),%rdx
lea    0xe24(%rip),%rsi
call   1080 <sprintf@plt>
mov    -0x20(%rbp),%rdi
lea    0xe2a(%rip),%rsi
call   1070 <strcat@plt>
mov    -0x20(%rbp),%rdi
lea    0xe31(%rip),%rsi
call   1070 <strcat@plt>
mov    -0x20(%rbp),%rax
movb   $0x0,0x3ff(%rax)
mov    -0x8(%rbp),%eax
mov    %eax,-0x34(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x20(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x34(%rbp),%edi
mov    -0x30(%rbp),%rsi
mov    %eax,%edx
mov    0x2d71(%rip),%rax
mov    (%rax),%ecx
call   1050 <send@plt>
mov    %eax,%ecx
mov    $0xffffffff,%eax
cmp    %ecx,%eax
jne    1291 <send_header+0x131>
movl   $0xffffffff,-0x4(%rbp)
jmp    1298 <send_header+0x138>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<send_header>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdx,%r15
mov    %esi,%ebp
mov    %edi,%r14d
mov    $0x400,%edi
call   1060 <malloc@plt>
mov    %rax,%rbx
lea    0xe7d(%rip),%rsi
mov    %rax,%rdi
call   1030 <strcpy@plt>
cmp    $0x194,%ebp
lea    0xe7a(%rip),%rax
lea    0xe82(%rip),%rcx
cmove  %rax,%rcx
cmp    $0xc8,%ebp
lea    0xe5a(%rip),%rsi
cmovne %rcx,%rsi
mov    %rbx,%rdi
call   1070 <strcat@plt>
mov    %rbx,%rdi
call   1040 <strlen@plt>
movslq %eax,%rdi
add    %rbx,%rdi
lea    0xe6b(%rip),%rsi
mov    %r15,%rdx
call   1080 <sprintf@plt>
lea    0xe72(%rip),%rsi
mov    %rbx,%rdi
call   1070 <strcat@plt>
lea    0xe7a(%rip),%rsi
mov    %rbx,%rdi
call   1070 <strcat@plt>
movb   $0x0,0x3ff(%rbx)
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    0x2dd3(%rip),%rcx
mov    (%rcx),%ecx
mov    %r14d,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1050 <send@plt>
xor    %ecx,%ecx
cmp    $0xffffffff,%eax
sete   %cl
neg    %ecx
mov    %ecx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [10 x i8] c"HTTP/1.0 \00"
@.str.1 = [8 x i8] c"200 OK\0A\00"
@.str.2 = [15 x i8] c"404 Not Found\0A\00"
@.str.3 = [27 x i8] c"500 Internal Server Error\0A\00"
@.str.4 = [22 x i8] c"Content-length: %lu\0D\0A\00"
@.str.5 = [26 x i8] c"Content-Type: text/html\0D\0A\00"
@.str.6 = [3 x i8] c"\0D\0A\00"
@MSG_NOSIGNAL = global i32 0
define i32 @send_header(i32 %0, i32 %1, i64 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i64
%8 = alloca i8*
store i32 %0, i32* %5
store i32 %1, i32* %6
store i64 %2, i64* %7
%9 = call i64 @malloc(i32 1024)
%10 = inttoptr i64 %9 to i8*
store i8* %10, i8** %8
%11 = load i8*, i8** %8
%12 = call i32 @strcpy(i8* %11, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
%13 = load i32, i32* %6
switch i32 %13, label %20 [
i32 200, label %14
i32 404, label %17
]
14: ; preds = %3
%15 = load i8*, i8** %8
%16 = call i32 @strcat(i8* %15, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
br label %23
17: ; preds = %3
%18 = load i8*, i8** %8
%19 = call i32 @strcat(i8* %18, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
br label %23
20: ; preds = %3
%21 = load i8*, i8** %8
%22 = call i32 @strcat(i8* %21, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.3, i64 0, i64 0))
br label %23
23: ; preds = %20, %17, %14
%24 = load i8*, i8** %8
%25 = load i8*, i8** %8
%26 = call i32 @strlen(i8* %25)
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds i8, i8* %24, i64 %27
%29 = load i64, i64* %7
%30 = call i32 @sprintf(i8* %28, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0), i64 %29)
%31 = load i8*, i8** %8
%32 = call i32 @strcat(i8* %31, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.5, i64 0, i64 0))
%33 = load i8*, i8** %8
%34 = call i32 @strcat(i8* %33, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.6, i64 0, i64 0))
%35 = load i8*, i8** %8
%36 = getelementptr inbounds i8, i8* %35, i64 1023
store i8 0, i8* %36
%37 = load i32, i32* %5
%38 = load i8*, i8** %8
%39 = load i8*, i8** %8
%40 = call i32 @strlen(i8* %39)
%41 = load i32, i32* @MSG_NOSIGNAL
%42 = call i32 @send(i32 %37, i8* %38, i32 %40, i32 %41)
%43 = icmp eq i32 -1, %42
br i1 %43, label %44, label %45
44: ; preds = %23
store i32 -1, i32* %4
br label %46
45: ; preds = %23
store i32 0, i32* %4
br label %46
46: ; preds = %45, %44
%47 = load i32, i32* %4
ret i32 %47
}

LLVM IR (-O2):
@.str = [10 x i8] c"HTTP/1.0 \00"
@.str.1 = [8 x i8] c"200 OK\0A\00"
@.str.2 = [15 x i8] c"404 Not Found\0A\00"
@.str.3 = [27 x i8] c"500 Internal Server Error\0A\00"
@.str.4 = [22 x i8] c"Content-length: %lu\0D\0A\00"
@.str.5 = [26 x i8] c"Content-Type: text/html\0D\0A\00"
@.str.6 = [3 x i8] c"\0D\0A\00"
@MSG_NOSIGNAL = global i32 0
define i32 @send_header(i32 %0, i32 %1, i64 %2) {
%4 = tail call i64 @malloc(i32 1024)
%5 = inttoptr i64 %4 to i8*
%6 = tail call i32 @strcpy(i8* %5, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
%7 = icmp eq i32 %1, 404
%8 = select i1 %7, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.3, i64 0, i64 0)
%9 = icmp eq i32 %1, 200
%10 = select i1 %9, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i8* %8
%11 = tail call i32 @strcat(i8* %5, i8* %10)
%12 = tail call i32 @strlen(i8* %5)
%13 = sext i32 %12 to i64
%14 = getelementptr inbounds i8, i8* %5, i64 %13
%15 = tail call i32 @sprintf(i8* dereferenceable(1) %14, i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0), i64 %2)
%16 = tail call i32 @strcat(i8* %5, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.5, i64 0, i64 0))
%17 = tail call i32 @strcat(i8* %5, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.6, i64 0, i64 0))
%18 = getelementptr inbounds i8, i8* %5, i64 1023
store i8 0, i8* %18
%19 = tail call i32 @strlen(i8* %5)
%20 = load i32, i32* @MSG_NOSIGNAL
%21 = tail call i32 @send(i32 %0, i8* %5, i32 %19, i32 %20)
%22 = icmp eq i32 %21, -1
%23 = sext i1 %22 to i32
ret i32 %23
}