#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 /*<<< orphan*/ buf;

/* Variables and functions */
int /*<<< orphan*/ close(int);
int complete_header(char *, int);
int /*<<< orphan*/ free(void *);
int /*<<< orphan*/ http_error(int, int, char *);
int read(int, char *, int);
void *httpd_connection_handler(void *socket_ptr) {
  char buf[4096];
  int have = 0;
  int got = 0;
  int socket = *((int *)socket_ptr);
  int header_len;
  while ((got = read(socket, buf + have, sizeof(buf) - have)) > 0) {
    have += got;
    header_len = complete_header(buf, have);
    if (header_len)
      break;
  }
  http_error(socket, 500, "Internal Server Error");
  close(socket);
  free(socket_ptr);
  return (0);
}


x86 assembly (clang, -O0):
<httpd_connection_handler>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1020,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x0,-0x1014(%rbp)
movl   $0x0,-0x1018(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x101c(%rbp)
mov    -0x101c(%rbp),%edi
lea    -0x1010(%rbp),%rsi
movslq -0x1014(%rbp),%rax
add    %rax,%rsi
movslq -0x1014(%rbp),%rcx
mov    $0x1000,%eax
sub    %rcx,%rax
mov    %eax,%edx
call   1060 <read@plt>
mov    %eax,-0x1018(%rbp)
cmp    $0x0,%eax
jle    11fc <httpd_connection_handler+0xac>
mov    -0x1018(%rbp),%eax
add    -0x1014(%rbp),%eax
mov    %eax,-0x1014(%rbp)
lea    -0x1010(%rbp),%rdi
mov    -0x1014(%rbp),%esi
call   1070 <complete_header@plt>
mov    %eax,-0x1020(%rbp)
cmpl   $0x0,-0x1020(%rbp)
je     11f7 <httpd_connection_handler+0xa7>
jmp    11fc <httpd_connection_handler+0xac>
jmp    117f <httpd_connection_handler+0x2f>
mov    -0x101c(%rbp),%edi
mov    $0x1f4,%esi
lea    0xdf2(%rip),%rdx
call   1050 <http_error@plt>
mov    -0x101c(%rbp),%edi
call   1040 <close@plt>
mov    -0x8(%rbp),%rdi
call   1030 <free@plt>
xor    %eax,%eax
add    $0x1020,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<httpd_connection_handler>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x1008,%rsp
mov    %rdi,%r14
mov    (%rdi),%ebp
xor    %ebx,%ebx
mov    %rsp,%r15
nopw   0x0(%rax,%rax,1)
movslq %ebx,%rax
lea    (%rsp,%rax,1),%rsi
mov    $0x1000,%edx
sub    %eax,%edx
mov    %ebp,%edi
call   1060 <read@plt>
test   %eax,%eax
jle    1199 <httpd_connection_handler+0x49>
add    %eax,%ebx
mov    %r15,%rdi
mov    %ebx,%esi
call   1070 <complete_header@plt>
test   %eax,%eax
je     1170 <httpd_connection_handler+0x20>
lea    0xe60(%rip),%rdx
mov    %ebp,%edi
mov    $0x1f4,%esi
call   1050 <http_error@plt>
mov    %ebp,%edi
call   1040 <close@plt>
mov    %r14,%rdi
call   1030 <free@plt>
xor    %eax,%eax
add    $0x1008,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [22 x i8] c"Internal Server Error\00"
define i8* @httpd_connection_handler(i8* %0) {
%2 = alloca i8*
%3 = alloca [4096 x i8]
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store i8* %0, i8** %2
store i32 0, i32* %4
store i32 0, i32* %5
%8 = load i8*, i8** %2
%9 = bitcast i8* %8 to i32*
%10 = load i32, i32* %9
store i32 %10, i32* %6
br label %11
11: ; preds = %33, %1
%12 = load i32, i32* %6
%13 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%14 = load i32, i32* %4
%15 = sext i32 %14 to i64
%16 = getelementptr inbounds i8, i8* %13, i64 %15
%17 = load i32, i32* %4
%18 = sext i32 %17 to i64
%19 = sub i64 4096, %18
%20 = trunc i64 %19 to i32
%21 = call i32 @read(i32 %12, i8* %16, i32 %20)
store i32 %21, i32* %5
%22 = icmp sgt i32 %21, 0
br i1 %22, label %23, label %34
23: ; preds = %11
%24 = load i32, i32* %5
%25 = load i32, i32* %4
%26 = add nsw i32 %25, %24
store i32 %26, i32* %4
%27 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%28 = load i32, i32* %4
%29 = call i32 @complete_header(i8* %27, i32 %28)
store i32 %29, i32* %7
%30 = load i32, i32* %7
%31 = icmp ne i32 %30, 0
br i1 %31, label %32, label %33
32: ; preds = %23
br label %34
33: ; preds = %23
br label %11
34: ; preds = %32, %11
%35 = load i32, i32* %6
%36 = call i32 @http_error(i32 %35, i32 500, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
%37 = load i32, i32* %6
%38 = call i32 @close(i32 %37)
%39 = load i8*, i8** %2
%40 = call i32 @free(i8* %39)
ret i8* null
}

LLVM IR (-O2):
@.str = [22 x i8] c"Internal Server Error\00"
define noalias i8* @httpd_connection_handler(i8* nocapture %0) {
%2 = alloca [4096 x i8]
%3 = getelementptr inbounds [4096 x i8], [4096 x i8]* %2, i64 0, i64 0
%4 = bitcast i8* %0 to i32*
%5 = load i32, i32* %4
br label %6
6: ; preds = %13, %1
%7 = phi i32 [ 0, %1 ], [ %14, %13 ]
%8 = sext i32 %7 to i64
%9 = getelementptr inbounds [4096 x i8], [4096 x i8]* %2, i64 0, i64 %8
%10 = sub i32 4096, %7
%11 = call i32 @read(i32 %5, i8* %9, i32 %10)
%12 = icmp sgt i32 %11, 0
br i1 %12, label %13, label %17
13: ; preds = %6
%14 = add nsw i32 %11, %7
%15 = call i32 @complete_header(i8* %3, i32 %14)
%16 = icmp eq i32 %15, 0
br i1 %16, label %6, label %17
17: ; preds = %13, %6
%18 = call i32 @http_error(i32 %5, i32 500, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
%19 = call i32 @close(i32 %5)
%20 = call i32 @free(i8* %0)
ret i8* null
}