#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*/ close(int);
int /*<<< orphan*/ fsync(int);
int /*<<< orphan*/ memset(char *, int const, int /*<<< orphan*/);
int /*<<< orphan*/ send(int, char *, int, int /*<<< orphan*/);
int strlen(char *);
int /*<<< orphan*/ strncpy(char *, char *, int const);
char *strstr(char *, char *);
void return404(char *buffer, int csock, char *reason) {
  char *eol = strstr(buffer, "\n");
  const int maxlen = 1024;
  char output[maxlen];
  memset(output, maxlen, 0);
  if (eol == NULL || ((eol - buffer) > (maxlen - 1))) {
    strncpy(output, buffer, maxlen - 1);
  } else {
    strncpy(output, buffer, (eol - buffer));
  }
  char *notfound = "HTTP/1.0 404 Not Found\nContent-Type: text/plain\n\nError: "
                   "cannot handle request ";
  send(csock, notfound, strlen(notfound), 0);
  send(csock, output, strlen(output), 0);
  if (reason != NULL) {
    send(csock, "\n", 1, 0);
    send(csock, reason, strlen(reason), 0);
  }
  fsync(csock);
  close(csock);
}


x86 assembly (clang, -O0):
<return404>:
push   %rbp
mov    %rsp,%rbp
sub    $0x470,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe6f(%rip),%rsi
call   1090 <strstr@plt>
mov    %rax,-0x20(%rbp)
movl   $0x400,-0x24(%rbp)
mov    %rsp,%rax
mov    %rax,-0x30(%rbp)
lea    -0x440(%rbp),%rdi
mov    $0x400,%esi
xor    %edx,%edx
call   1060 <memset@plt>
cmpq   $0x0,-0x20(%rbp)
je     11dd <return404+0x6d>
mov    -0x20(%rbp),%rax
mov    -0x8(%rbp),%rcx
sub    %rcx,%rax
cmp    $0x3ff,%rax
jle    11f7 <return404+0x87>
mov    -0x8(%rbp),%rsi
lea    -0x440(%rbp),%rdi
mov    $0x3ff,%edx
call   1030 <strncpy@plt>
jmp    1214 <return404+0xa4>
mov    -0x8(%rbp),%rsi
mov    -0x20(%rbp),%rax
mov    -0x8(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,%edx
lea    -0x440(%rbp),%rdi
call   1030 <strncpy@plt>
lea    0xde7(%rip),%rax
mov    %rax,-0x38(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x454(%rbp)
mov    -0x38(%rbp),%rax
mov    %rax,-0x450(%rbp)
mov    -0x38(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x454(%rbp),%edi
mov    -0x450(%rbp),%rsi
mov    %eax,%edx
xor    %ecx,%ecx
call   1050 <send@plt>
mov    -0xc(%rbp),%eax
mov    %eax,-0x444(%rbp)
lea    -0x440(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x444(%rbp),%edi
mov    %eax,%edx
lea    -0x440(%rbp),%rsi
xor    %ecx,%ecx
call   1050 <send@plt>
cmpq   $0x0,-0x18(%rbp)
je     12d1 <return404+0x161>
mov    -0xc(%rbp),%edi
lea    0xd6e(%rip),%rsi
mov    $0x1,%edx
xor    %ecx,%ecx
call   1050 <send@plt>
mov    -0xc(%rbp),%eax
mov    %eax,-0x464(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x460(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x464(%rbp),%edi
mov    -0x460(%rbp),%rsi
mov    %eax,%edx
xor    %ecx,%ecx
call   1050 <send@plt>
mov    -0xc(%rbp),%edi
call   1080 <fsync@plt>
mov    -0xc(%rbp),%edi
call   1070 <close@plt>
mov    -0x30(%rbp),%rax
mov    %rax,%rsp
add    $0x470,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<return404>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x400,%rsp
mov    %rdx,%r14
mov    %esi,%r12d
mov    %rdi,%rbx
mov    $0xa,%esi
call   1060 <strchr@plt>
mov    %rax,%rbp
mov    %rsp,%r15
mov    %r15,%rdi
mov    $0x400,%esi
xor    %edx,%edx
call   1070 <memset@plt>
mov    %rbp,%rdx
sub    %rbx,%rdx
cmp    $0x3ff,%rdx
mov    $0x3ff,%eax
cmovge %eax,%edx
test   %rbp,%rbp
cmove  %eax,%edx
mov    %r15,%rdi
mov    %rbx,%rsi
call   1030 <strncpy@plt>
lea    0xe2e(%rip),%rbx
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    %r12d,%edi
mov    %rbx,%rsi
mov    %eax,%edx
xor    %ecx,%ecx
call   1050 <send@plt>
mov    %r15,%rdi
call   1040 <strlen@plt>
mov    %r12d,%edi
mov    %r15,%rsi
mov    %eax,%edx
xor    %ecx,%ecx
call   1050 <send@plt>
test   %r14,%r14
je     1234 <return404+0xc4>
lea    0xdf2(%rip),%rsi
mov    %r12d,%edi
mov    $0x1,%edx
xor    %ecx,%ecx
call   1050 <send@plt>
mov    %r14,%rdi
call   1040 <strlen@plt>
mov    %r12d,%edi
mov    %r14,%rsi
mov    %eax,%edx
xor    %ecx,%ecx
call   1050 <send@plt>
mov    %r12d,%edi
call   1090 <fsync@plt>
mov    %r12d,%edi
call   1080 <close@plt>
add    $0x400,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c"\0A\00"
@.str.1 = [79 x i8] c"HTTP/1.0 404 Not Found\0AContent-Type: text/plain\0A\0AError: cannot handle request \00"
define void @return404(i8* %0, i32 %1, i8* %2) {
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i8*
store i8* %0, i8** %4
store i32 %1, i32* %5
store i8* %2, i8** %6
%11 = load i8*, i8** %4
%12 = call i8* @strstr(i8* %11, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i8* %12, i8** %7
store i32 1024, i32* %8
%13 = call i8* @llvm.stacksave()
store i8* %13, i8** %9
%14 = alloca i8, i64 1024
%15 = call i32 @memset(i8* %14, i32 1024, i32 0)
%16 = load i8*, i8** %7
%17 = icmp eq i8* %16, null
br i1 %17, label %25, label %18
18: ; preds = %3
%19 = load i8*, i8** %7
%20 = load i8*, i8** %4
%21 = ptrtoint i8* %19 to i64
%22 = ptrtoint i8* %20 to i64
%23 = sub i64 %21, %22
%24 = icmp sgt i64 %23, 1023
br i1 %24, label %25, label %28
25: ; preds = %18, %3
%26 = load i8*, i8** %4
%27 = call i32 @strncpy(i8* %14, i8* %26, i32 1023)
br label %37
28: ; preds = %18
%29 = load i8*, i8** %4
%30 = load i8*, i8** %7
%31 = load i8*, i8** %4
%32 = ptrtoint i8* %30 to i64
%33 = ptrtoint i8* %31 to i64
%34 = sub i64 %32, %33
%35 = trunc i64 %34 to i32
%36 = call i32 @strncpy(i8* %14, i8* %29, i32 %35)
br label %37
37: ; preds = %28, %25
store i8* getelementptr inbounds ([79 x i8], [79 x i8]* @.str.1, i64 0, i64 0), i8** %10
%38 = load i32, i32* %5
%39 = load i8*, i8** %10
%40 = load i8*, i8** %10
%41 = call i32 @strlen(i8* %40)
%42 = call i32 @send(i32 %38, i8* %39, i32 %41, i32 0)
%43 = load i32, i32* %5
%44 = call i32 @strlen(i8* %14)
%45 = call i32 @send(i32 %43, i8* %14, i32 %44, i32 0)
%46 = load i8*, i8** %6
%47 = icmp ne i8* %46, null
br i1 %47, label %48, label %56
48: ; preds = %37
%49 = load i32, i32* %5
%50 = call i32 @send(i32 %49, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i32 1, i32 0)
%51 = load i32, i32* %5
%52 = load i8*, i8** %6
%53 = load i8*, i8** %6
%54 = call i32 @strlen(i8* %53)
%55 = call i32 @send(i32 %51, i8* %52, i32 %54, i32 0)
br label %56
56: ; preds = %48, %37
%57 = load i32, i32* %5
%58 = call i32 @fsync(i32 %57)
%59 = load i32, i32* %5
%60 = call i32 @close(i32 %59)
%61 = load i8*, i8** %9
ret void
}

LLVM IR (-O2):
@.str = [2 x i8] c"\0A\00"
@.str.1 = [79 x i8] c"HTTP/1.0 404 Not Found\0AContent-Type: text/plain\0A\0AError: cannot handle request \00"
define void @return404(i8* %0, i32 %1, i8* %2) {
%4 = tail call i8* @strchr(i8* dereferenceable(1) %0, i32 10)
%5 = alloca [1024 x i8]
%6 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%7 = call i32 @memset(i8* %6, i32 1024, i32 0)
%8 = icmp eq i8* %4, null
%9 = ptrtoint i8* %4 to i64
%10 = ptrtoint i8* %0 to i64
%11 = sub i64 %9, %10
%12 = icmp slt i64 %11, 1023
%13 = select i1 %12, i64 %11, i64 1023
%14 = trunc i64 %13 to i32
%15 = select i1 %8, i32 1023, i32 %14
%16 = call i32 @strncpy(i8* %6, i8* %0, i32 %15)
%17 = call i32 @strlen(i8* getelementptr inbounds ([79 x i8], [79 x i8]* @.str.1, i64 0, i64 0))
%18 = call i32 @send(i32 %1, i8* getelementptr inbounds ([79 x i8], [79 x i8]* @.str.1, i64 0, i64 0), i32 %17, i32 0)
%19 = call i32 @strlen(i8* %6)
%20 = call i32 @send(i32 %1, i8* %6, i32 %19, i32 0)
%21 = icmp eq i8* %2, null
br i1 %21, label %26, label %22
22: ; preds = %3
%23 = call i32 @send(i32 %1, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i32 1, i32 0)
%24 = call i32 @strlen(i8* %2)
%25 = call i32 @send(i32 %1, i8* %2, i32 %24, i32 0)
br label %26
26: ; preds = %22, %3
%27 = call i32 @fsync(i32 %1)
%28 = call i32 @close(i32 %1)
ret void
}