#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 check_request(char *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fork();
char *get_request_hostname(char *);
int /*<<< orphan*/ memset(char **, char, int);
long recv(int, char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ report_errno(char *);
int /*<<< orphan*/ send_200_request(int, char *);
int /*<<< orphan*/ send_400_response(int);
scalar_t__ strcasecmp(char *, char *);
int process_request(int connfd, char *hostname) {
  if (!fork()) {
    char request[65536];
    memset(&request, ' ', 65536);
    long recv_response = recv(connfd, request, 65536, 0);
    char *request_hostname = get_request_hostname(request);
    if (strcasecmp(hostname, request_hostname) != 0) {
      send_400_response(connfd);
      return 0;
    }
    if (recv_response == -1) {
      char *message = "error reading! errno: %d.\n";
      report_errno(message);
      return -1;
    }
    if (check_request(request) == 200) {
      send_200_request(connfd, request);
    } else {
      send_400_response(connfd);
    }
  }
  close(connfd);
  return 0;
}


x86 assembly (clang, -O0):
<process_request>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10030,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    $0x0,%al
call   10c0 <fork@plt>
cmp    $0x0,%eax
jne    12a2 <process_request+0x102>
lea    -0x10010(%rbp),%rdi
mov    $0x20,%esi
mov    $0x10000,%edx
call   1060 <memset@plt>
mov    -0x8(%rbp),%edi
lea    -0x10010(%rbp),%rsi
mov    $0x10000,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %rax,-0x10018(%rbp)
lea    -0x10010(%rbp),%rdi
call   10b0 <get_request_hostname@plt>
mov    %rax,-0x10020(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x10020(%rbp),%rsi
call   1040 <strcasecmp@plt>
cmp    $0x0,%rax
je     1236 <process_request+0x96>
mov    -0x8(%rbp),%edi
call   1050 <send_400_response@plt>
movl   $0x0,-0x4(%rbp)
jmp    12b1 <process_request+0x111>
cmpq   $0xffffffffffffffff,-0x10018(%rbp)
ff
jne    126a <process_request+0xca>
lea    0xdb5(%rip),%rax
mov    %rax,-0x10028(%rbp)
mov    -0x10028(%rbp),%rdi
call   1090 <report_errno@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12b1 <process_request+0x111>
lea    -0x10010(%rbp),%rdi
call   10a0 <check_request@plt>
cmp    $0xc8,%eax
jne    1295 <process_request+0xf5>
mov    -0x8(%rbp),%edi
lea    -0x10010(%rbp),%rsi
call   1070 <send_200_request@plt>
jmp    129d <process_request+0xfd>
mov    -0x8(%rbp),%edi
call   1050 <send_400_response@plt>
jmp    12a2 <process_request+0x102>
mov    -0x8(%rbp),%edi
call   1080 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10030,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<process_request>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10000,%rsp
mov    %rsi,%rbx
mov    %edi,%ebp
xor    %r14d,%r14d
xor    %eax,%eax
call   10c0 <fork@plt>
test   %eax,%eax
je     11cb <process_request+0x2b>
mov    %ebp,%edi
call   1080 <close@plt>
jmp    1216 <process_request+0x76>
mov    %rsp,%r12
mov    %r12,%rdi
mov    $0x20,%esi
mov    $0x10000,%edx
call   1060 <memset@plt>
mov    %ebp,%edi
mov    %r12,%rsi
mov    $0x10000,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %rax,%r15
mov    %r12,%rdi
call   10b0 <get_request_hostname@plt>
mov    %rbx,%rdi
mov    %rax,%rsi
call   1040 <strcasecmp@plt>
test   %rax,%rax
je     1229 <process_request+0x89>
mov    %ebp,%edi
call   1050 <send_400_response@plt>
xor    %r14d,%r14d
mov    %r14d,%eax
add    $0x10000,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
cmp    $0xffffffffffffffff,%r15
je     124d <process_request+0xad>
mov    %rsp,%rdi
call   10a0 <check_request@plt>
cmp    $0xc8,%eax
jne    1261 <process_request+0xc1>
mov    %rsp,%rsi
mov    %ebp,%edi
call   1070 <send_200_request@plt>
jmp    11c2 <process_request+0x22>
lea    0xdac(%rip),%rdi
call   1090 <report_errno@plt>
mov    $0xffffffff,%r14d
jmp    1216 <process_request+0x76>
mov    %ebp,%edi
call   1050 <send_400_response@plt>
jmp    11c2 <process_request+0x22>

LLVM IR (-O0):
@.str = [27 x i8] c"error reading! errno: %d.\0A\00"
define i32 @process_request(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca [65536 x i8]
%7 = alloca i64
%8 = alloca i8*
%9 = alloca i8*
store i32 %0, i32* %4
store i8* %1, i8** %5
%10 = call i32 (...) @fork()
%11 = icmp ne i32 %10, 0
br i1 %11, label %45, label %12
12: ; preds = %2
%13 = bitcast [65536 x i8]* %6 to i8**
%14 = call i32 @memset(i8** %13, i8 signext 32, i32 65536)
%15 = load i32, i32* %4
%16 = getelementptr inbounds [65536 x i8], [65536 x i8]* %6, i64 0, i64 0
%17 = call i64 @recv(i32 %15, i8* %16, i32 65536, i32 0)
store i64 %17, i64* %7
%18 = getelementptr inbounds [65536 x i8], [65536 x i8]* %6, i64 0, i64 0
%19 = call i8* @get_request_hostname(i8* %18)
store i8* %19, i8** %8
%20 = load i8*, i8** %5
%21 = load i8*, i8** %8
%22 = call i64 @strcasecmp(i8* %20, i8* %21)
%23 = icmp ne i64 %22, 0
br i1 %23, label %24, label %27
24: ; preds = %12
%25 = load i32, i32* %4
%26 = call i32 @send_400_response(i32 %25)
store i32 0, i32* %3
br label %48
27: ; preds = %12
%28 = load i64, i64* %7
%29 = icmp eq i64 %28, -1
br i1 %29, label %30, label %33
30: ; preds = %27
store i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i8** %9
%31 = load i8*, i8** %9
%32 = call i32 @report_errno(i8* %31)
store i32 -1, i32* %3
br label %48
33: ; preds = %27
%34 = getelementptr inbounds [65536 x i8], [65536 x i8]* %6, i64 0, i64 0
%35 = call i32 @check_request(i8* %34)
%36 = icmp eq i32 %35, 200
br i1 %36, label %37, label %41
37: ; preds = %33
%38 = load i32, i32* %4
%39 = getelementptr inbounds [65536 x i8], [65536 x i8]* %6, i64 0, i64 0
%40 = call i32 @send_200_request(i32 %38, i8* %39)
br label %44
41: ; preds = %33
%42 = load i32, i32* %4
%43 = call i32 @send_400_response(i32 %42)
br label %44
44: ; preds = %41, %37
br label %45
45: ; preds = %44, %2
%46 = load i32, i32* %4
%47 = call i32 @close(i32 %46)
store i32 0, i32* %3
br label %48
48: ; preds = %45, %30, %24
%49 = load i32, i32* %3
ret i32 %49
}

LLVM IR (-O2):
@.str = [27 x i8] c"error reading! errno: %d.\0A\00"
define i32 @process_request(i32 %0, i8* nocapture %1) {
%3 = alloca [65536 x i8]
%4 = tail call i32 (...) @fork()
%5 = icmp eq i32 %4, 0
br i1 %5, label %6, label %30
6: ; preds = %2
%7 = getelementptr inbounds [65536 x i8], [65536 x i8]* %3, i64 0, i64 0
%8 = bitcast [65536 x i8]* %3 to i8**
%9 = call i32 @memset(i8** %8, i8 signext 32, i32 65536)
%10 = call i64 @recv(i32 %0, i8* %7, i32 65536, i32 0)
%11 = call i8* @get_request_hostname(i8* %7)
%12 = call i64 @strcasecmp(i8* %1, i8* %11)
%13 = icmp eq i64 %12, 0
br i1 %13, label %16, label %14
14: ; preds = %6
%15 = call i32 @send_400_response(i32 %0)
br label %28
16: ; preds = %6
%17 = icmp eq i64 %10, -1
br i1 %17, label %18, label %20
18: ; preds = %16
%19 = call i32 @report_errno(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0))
br label %28
20: ; preds = %16
%21 = call i32 @check_request(i8* %7)
%22 = icmp eq i32 %21, 200
br i1 %22, label %23, label %25
23: ; preds = %20
%24 = call i32 @send_200_request(i32 %0, i8* %7)
br label %27
25: ; preds = %20
%26 = call i32 @send_400_response(i32 %0)
br label %27
27: ; preds = %25, %23
br label %30
28: ; preds = %18, %14
%29 = phi i32 [ 0, %14 ], [ -1, %18 ]
br label %32
30: ; preds = %27, %2
%31 = call i32 @close(i32 %0)
br label %32
32: ; preds = %28, %30
%33 = phi i32 [ 0, %30 ], [ %29, %28 ]
ret i32 %33
}