#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_socket(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int handle_get(int);
int handle_post(int);
int /*<<< orphan*/ recv_until(int, char *, int, char);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strcmp(char *, char *);
void handle_http(int socket) {
  char buf[100] = "";
  int err;
  recv_until(socket, buf, sizeof(buf), ' ');
  if (!strcmp(buf, "GET")) {
    err = handle_get(socket);
  } else if (!strcmp(buf, "POST")) {
    err = handle_post(socket);
  } else {
    err = 1;
  }
  if (err) {
    fprintf(stderr, "Error: malformed HTTP request/connection error.\n");
  }
  close_socket(socket);
  fprintf(stderr, "Connection closed.\n");
}


x86 assembly (clang, -O0):
<handle_http>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    %edi,-0x4(%rbp)
lea    -0x70(%rbp),%rdi
xor    %esi,%esi
mov    $0x64,%edx
call   1030 <memset@plt>
mov    -0x4(%rbp),%edi
lea    -0x70(%rbp),%rsi
mov    $0x64,%edx
mov    $0x20,%ecx
call   1070 <recv_until@plt>
lea    -0x70(%rbp),%rdi
lea    0xe51(%rip),%rsi
call   1040 <strcmp@plt>
cmp    $0x0,%eax
jne    11cd <handle_http+0x5d>
mov    -0x4(%rbp),%edi
call   1090 <handle_get@plt>
mov    %eax,-0x74(%rbp)
jmp    1202 <handle_http+0x92>
lea    -0x70(%rbp),%rdi
lea    0xe2c(%rip),%rsi
call   1040 <strcmp@plt>
cmp    $0x0,%eax
jne    11f6 <handle_http+0x86>
mov    -0x4(%rbp),%edi
call   1050 <handle_post@plt>
mov    %eax,-0x74(%rbp)
jmp    11fd <handle_http+0x8d>
movl   $0x1,-0x74(%rbp)
jmp    1202 <handle_http+0x92>
cmpl   $0x0,-0x74(%rbp)
je     1221 <handle_http+0xb1>
mov    0x2dcd(%rip),%rax
mov    (%rax),%edi
lea    0xded(%rip),%rsi
call   1060 <fprintf@plt>
mov    -0x4(%rbp),%edi
call   1080 <close_socket@plt>
mov    0x2db0(%rip),%rax
mov    (%rax),%edi
lea    0xe01(%rip),%rsi
call   1060 <fprintf@plt>
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<handle_http>:
push   %rbx
sub    $0x70,%rsp
mov    %edi,%ebx
xorps  %xmm0,%xmm0
movaps %xmm0,0x50(%rsp)
movaps %xmm0,0x40(%rsp)
movaps %xmm0,0x30(%rsp)
movaps %xmm0,0x20(%rsp)
movaps %xmm0,0x10(%rsp)
movaps %xmm0,(%rsp)
movl   $0x0,0x60(%rsp)
mov    %rsp,%rsi
mov    $0x64,%edx
mov    $0x20,%ecx
call   1050 <recv_until@plt>
cmpl   $0x544547,(%rsp)
jne    11a7 <handle_http+0x57>
mov    %ebx,%edi
call   1070 <handle_get@plt>
test   %eax,%eax
jne    11c3 <handle_http+0x73>
jmp    11d8 <handle_http+0x88>
mov    $0x54534f50,%eax
xor    (%rsp),%eax
movzbl 0x4(%rsp),%ecx
or     %eax,%ecx
jne    11c3 <handle_http+0x73>
mov    %ebx,%edi
call   1030 <handle_post@plt>
test   %eax,%eax
je     11d8 <handle_http+0x88>
mov    0x2e16(%rip),%rax
mov    (%rax),%edi
lea    0xe32(%rip),%rsi
call   1040 <fprintf@plt>
mov    %ebx,%edi
call   1060 <close_socket@plt>
mov    0x2dfa(%rip),%rax
mov    (%rax),%edi
lea    0xe47(%rip),%rsi
call   1040 <fprintf@plt>
add    $0x70,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@.str = [4 x i8] c"GET\00"
@.str.1 = [5 x i8] c"POST\00"
@stderr = global i32 0
@.str.2 = [49 x i8] c"Error: malformed HTTP request/connection error.\0A\00"
@.str.3 = [20 x i8] c"Connection closed.\0A\00"
define void @handle_http(i32 %0) {
%2 = alloca i32
%3 = alloca [100 x i8]
%4 = alloca i32
store i32 %0, i32* %2
%5 = bitcast [100 x i8]* %3 to i8*
%6 = load i32, i32* %2
%7 = getelementptr inbounds [100 x i8], [100 x i8]* %3, i64 0, i64 0
%8 = call i32 @recv_until(i32 %6, i8* %7, i32 100, i8 signext 32)
%9 = getelementptr inbounds [100 x i8], [100 x i8]* %3, i64 0, i64 0
%10 = call i32 @strcmp(i8* %9, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%11 = icmp ne i32 %10, 0
br i1 %11, label %15, label %12
12: ; preds = %1
%13 = load i32, i32* %2
%14 = call i32 @handle_get(i32 %13)
store i32 %14, i32* %4
br label %24
15: ; preds = %1
%16 = getelementptr inbounds [100 x i8], [100 x i8]* %3, i64 0, i64 0
%17 = call i32 @strcmp(i8* %16, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%18 = icmp ne i32 %17, 0
br i1 %18, label %22, label %19
19: ; preds = %15
%20 = load i32, i32* %2
%21 = call i32 @handle_post(i32 %20)
store i32 %21, i32* %4
br label %23
22: ; preds = %15
store i32 1, i32* %4
br label %23
23: ; preds = %22, %19
br label %24
24: ; preds = %23, %12
%25 = load i32, i32* %4
%26 = icmp ne i32 %25, 0
br i1 %26, label %27, label %30
27: ; preds = %24
%28 = load i32, i32* @stderr
%29 = call i32 @fprintf(i32 %28, i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.2, i64 0, i64 0))
br label %30
30: ; preds = %27, %24
%31 = load i32, i32* %2
%32 = call i32 @close_socket(i32 %31)
%33 = load i32, i32* @stderr
%34 = call i32 @fprintf(i32 %33, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0))
ret void
}

LLVM IR (-O2):
@.str.1 = [5 x i8] c"POST\00"
@stderr = global i32 0
@.str.2 = [49 x i8] c"Error: malformed HTTP request/connection error.\0A\00"
@.str.3 = [20 x i8] c"Connection closed.\0A\00"
define void @handle_http(i32 %0) {
%2 = alloca [100 x i8]
%3 = getelementptr inbounds [100 x i8], [100 x i8]* %2, i64 0, i64 0
%4 = call i32 @recv_until(i32 %0, i8* %3, i32 100, i8 signext 32)
%5 = bitcast [100 x i8]* %2 to i32*
%6 = load i32, i32* %5
%7 = icmp eq i32 %6, 5522759
br i1 %7, label %8, label %10
8: ; preds = %1
%9 = call i32 @handle_get(i32 %0)
br label %15
10: ; preds = %1
%11 = call i32 @bcmp(i8* dereferenceable(5) %3, i8* dereferenceable(5) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i64 5)
%12 = icmp eq i32 %11, 0
br i1 %12, label %13, label %18
13: ; preds = %10
%14 = call i32 @handle_post(i32 %0)
br label %15
15: ; preds = %13, %8
%16 = phi i32 [ %14, %13 ], [ %9, %8 ]
%17 = icmp eq i32 %16, 0
br i1 %17, label %21, label %18
18: ; preds = %10, %15
%19 = load i32, i32* @stderr
%20 = call i32 @fprintf(i32 %19, i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.2, i64 0, i64 0))
br label %21
21: ; preds = %18, %15
%22 = call i32 @close_socket(i32 %0)
%23 = load i32, i32* @stderr
%24 = call i32 @fprintf(i32 %23, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0))
ret void
}