#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 scalar_t__ ssize_t;

/* Variables and functions */
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ free(char *);
char *parse_get(char *);
int /*<<< orphan*/ printf(char *);
scalar_t__ read(int, char *, int);
char *safe_malloc(int);
int /*<<< orphan*/ server_error(int, char *);
int /*<<< orphan*/ write_client(int, char *);
void *handle_req(void *socket) {
  int sock = *((int *)socket);
  char *buf = safe_malloc(1024);
  ssize_t bytes = read(sock, buf, 1024);
  if (bytes < 0)
    server_error(sock, "reading from client");
  else {
    char *file = parse_get(buf);
    if (file == NULL) {
      printf("not a GET request\n");
      free(file);
      close(sock);
      return NULL;
    }
    write_client(sock, file);
    free(file);
  }
  close(sock);
  free(buf);
  return NULL;
}


x86 assembly (clang, -O0):
<handle_req>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    $0x400,%edi
call   1040 <safe_malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x14(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    $0x400,%edx
call   1080 <read@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jge    11d7 <handle_req+0x57>
mov    -0x14(%rbp),%edi
lea    0xe33(%rip),%rsi
call   10a0 <server_error@plt>
jmp    122e <handle_req+0xae>
mov    -0x20(%rbp),%rdi
call   1090 <parse_get@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    1219 <handle_req+0x99>
lea    0xe1e(%rip),%rdi
call   1050 <printf@plt>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
mov    -0x14(%rbp),%edi
call   1070 <close@plt>
movq   $0x0,-0x8(%rbp)
jmp    1247 <handle_req+0xc7>
mov    -0x14(%rbp),%edi
mov    -0x30(%rbp),%rsi
call   1060 <write_client@plt>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
mov    -0x14(%rbp),%edi
call   1070 <close@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<handle_req>:
push   %rbp
push   %r14
push   %rbx
mov    (%rdi),%r14d
mov    $0x400,%edi
call   1040 <safe_malloc@plt>
mov    %rax,%rbx
mov    %r14d,%edi
mov    %rax,%rsi
mov    $0x400,%edx
call   1080 <read@plt>
test   %rax,%rax
js     11ce <handle_req+0x4e>
mov    %rbx,%rdi
call   1090 <parse_get@plt>
test   %rax,%rax
je     11ef <handle_req+0x6f>
mov    %rax,%rbp
mov    %r14d,%edi
mov    %rax,%rsi
call   1060 <write_client@plt>
mov    %rbp,%rdi
call   1030 <free@plt>
jmp    11dd <handle_req+0x5d>
lea    0xe2b(%rip),%rsi
mov    %r14d,%edi
call   10a0 <server_error@plt>
mov    %r14d,%edi
call   1070 <close@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
jmp    120a <handle_req+0x8a>
lea    0xe1e(%rip),%rdi
call   1050 <puts@plt>
xor    %edi,%edi
call   1030 <free@plt>
mov    %r14d,%edi
call   1070 <close@plt>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [20 x i8] c"reading from client\00"
@.str.1 = [19 x i8] c"not a GET request\0A\00"
define i8* @handle_req(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i8*
store i8* %0, i8** %3
%8 = load i8*, i8** %3
%9 = bitcast i8* %8 to i32*
%10 = load i32, i32* %9
store i32 %10, i32* %4
%11 = call i8* @safe_malloc(i32 1024)
store i8* %11, i8** %5
%12 = load i32, i32* %4
%13 = load i8*, i8** %5
%14 = call i64 @read(i32 %12, i8* %13, i32 1024)
store i64 %14, i64* %6
%15 = load i64, i64* %6
%16 = icmp slt i64 %15, 0
br i1 %16, label %17, label %20
17: ; preds = %1
%18 = load i32, i32* %4
%19 = call i32 @server_error(i32 %18, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
br label %37
20: ; preds = %1
%21 = load i8*, i8** %5
%22 = call i8* @parse_get(i8* %21)
store i8* %22, i8** %7
%23 = load i8*, i8** %7
%24 = icmp eq i8* %23, null
br i1 %24, label %25, label %31
25: ; preds = %20
%26 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0))
%27 = load i8*, i8** %7
%28 = call i32 @free(i8* %27)
%29 = load i32, i32* %4
%30 = call i32 @close(i32 %29)
store i8* null, i8** %2
br label %42
31: ; preds = %20
%32 = load i32, i32* %4
%33 = load i8*, i8** %7
%34 = call i32 @write_client(i32 %32, i8* %33)
%35 = load i8*, i8** %7
%36 = call i32 @free(i8* %35)
br label %37
37: ; preds = %31, %17
%38 = load i32, i32* %4
%39 = call i32 @close(i32 %38)
%40 = load i8*, i8** %5
%41 = call i32 @free(i8* %40)
store i8* null, i8** %2
br label %42
42: ; preds = %37, %25
%43 = load i8*, i8** %2
ret i8* %43
}

LLVM IR (-O2):
@.str = [20 x i8] c"reading from client\00"
@str = [18 x i8] c"not a GET request\00"
define noalias i8* @handle_req(i8* nocapture %0) {
%2 = bitcast i8* %0 to i32*
%3 = load i32, i32* %2
%4 = tail call i8* @safe_malloc(i32 1024)
%5 = tail call i64 @read(i32 %3, i8* %4, i32 1024)
%6 = icmp slt i64 %5, 0
br i1 %6, label %7, label %9
7: ; preds = %1
%8 = tail call i32 @server_error(i32 %3, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
br label %19
9: ; preds = %1
%10 = tail call i8* @parse_get(i8* %4)
%11 = icmp eq i8* %10, null
br i1 %11, label %12, label %16
12: ; preds = %9
%13 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @str, i64 0, i64 0))
%14 = tail call i32 @free(i8* null)
%15 = tail call i32 @close(i32 %3)
br label %22
16: ; preds = %9
%17 = tail call i32 @write_client(i32 %3, i8* %10)
%18 = tail call i32 @free(i8* %10)
br label %19
19: ; preds = %16, %7
%20 = tail call i32 @close(i32 %3)
%21 = tail call i32 @free(i8* %4)
br label %22
22: ; preds = %12, %19
ret i8* null
}