#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*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *, char *, char *);
int read(int, char *, int);
int serve_file(int, char *, int);
int /*<<< orphan*/ sscanf(char *, char *, char *, char *, char *);
int /*<<< orphan*/ stderr;
scalar_t__ strcmp(char *, char *);
int /*<<< orphan*/ strcpy(char *, char const *);
int strlen(char *);
int /*<<< orphan*/ write(int, char *, int);
int process_request(int sockfd, const char *path) {
  int n;
  char buffer[(1 << 22)], verb[10], httpver[10], filepath[1024], *url;
  strcpy(filepath, path);
  url = filepath + strlen(filepath);
  n = read(sockfd, buffer, (1 << 22));
  if (n < 0)
    goto error;
  buffer[n] = 0;
  sscanf(buffer, "%s %s %s", verb, url, httpver);
  printf("%s %s %s ... ", verb, url, httpver);
  if (strcmp("HTTP/1.1", httpver) != 0 && strcmp("HTTP/1.0", httpver) != 0) {
    write(sockfd, "HTTP/1.0 505 HTTP Version Not Supported\r\n\r\n", 43);
    fprintf(stderr, "version %s not supported, aborting.\n", httpver);
    return 0;
  } else if (strcmp("GET", verb) == 0) {
    return serve_file(sockfd, filepath, 1);
  } else if (strcmp("HEAD", verb) == 0) {
    return serve_file(sockfd, filepath, 0);
  } else {
    write(sockfd, "HTTP/1.0 400 Method Not Supported\r\n\r\n", 37);
    fprintf(stderr, "requested method %s not supported.\n", verb);
    return 0;
  }
error:
  perror("ERROR processing the request");
  return 1;
}


x86 assembly (clang, -O0):
<process_request>:
push   %rbp
mov    %rsp,%rbp
sub    $0x400450,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x400440(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1030 <strcpy@plt>
lea    -0x400440(%rbp),%rax
mov    %rax,-0x400450(%rbp)
lea    -0x400440(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,%ecx
mov    -0x400450(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x400448(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x400020(%rbp),%rsi
mov    $0x400000,%edx
call   1070 <read@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    1218 <process_request+0x78>
jmp    138e <process_request+0x1ee>
movslq -0x14(%rbp),%rax
movb   $0x0,-0x400020(%rbp,%rax,1)
lea    -0x400020(%rbp),%rdi
lea    -0x40002a(%rbp),%rdx
mov    -0x400448(%rbp),%rcx
lea    -0x400034(%rbp),%r8
lea    0xdb9(%rip),%rsi
call   10a0 <sscanf@plt>
lea    -0x40002a(%rbp),%rsi
mov    -0x400448(%rbp),%rdx
lea    -0x400034(%rbp),%rcx
lea    0xda1(%rip),%rdi
call   1060 <printf@plt>
lea    -0x400034(%rbp),%rsi
lea    0xd9c(%rip),%rdi
call   1080 <strcmp@plt>
cmp    $0x0,%rax
je     12e3 <process_request+0x143>
lea    -0x400034(%rbp),%rsi
lea    0xd88(%rip),%rdi
call   1080 <strcmp@plt>
cmp    $0x0,%rax
je     12e3 <process_request+0x143>
mov    -0x8(%rbp),%edi
lea    0xd78(%rip),%rsi
mov    $0x2b,%edx
call   1040 <write@plt>
mov    0x2d1e(%rip),%rax
mov    (%rax),%edi
lea    -0x400034(%rbp),%rdx
lea    0xd83(%rip),%rsi
call   1090 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    13a1 <process_request+0x201>
lea    -0x40002a(%rbp),%rsi
lea    0xd89(%rip),%rdi
call   1080 <strcmp@plt>
cmp    $0x0,%rax
jne    131c <process_request+0x17c>
mov    -0x8(%rbp),%edi
lea    -0x400440(%rbp),%rsi
mov    $0x1,%edx
call   10b0 <serve_file@plt>
mov    %eax,-0x4(%rbp)
jmp    13a1 <process_request+0x201>
lea    -0x40002a(%rbp),%rsi
lea    0xd54(%rip),%rdi
call   1080 <strcmp@plt>
cmp    $0x0,%rax
jne    1352 <process_request+0x1b2>
mov    -0x8(%rbp),%edi
lea    -0x400440(%rbp),%rsi
xor    %edx,%edx
call   10b0 <serve_file@plt>
mov    %eax,-0x4(%rbp)
jmp    13a1 <process_request+0x201>
mov    -0x8(%rbp),%edi
lea    0xd27(%rip),%rsi
mov    $0x25,%edx
call   1040 <write@plt>
mov    0x2c73(%rip),%rax
mov    (%rax),%edi
lea    -0x40002a(%rbp),%rdx
lea    0xd2c(%rip),%rsi
call   1090 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    13a1 <process_request+0x201>
lea    0xd38(%rip),%rdi
call   10c0 <perror@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x400450,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<process_request>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x400428,%rsp
mov    %edi,%r14d
lea    0x20(%rsp),%rbp
mov    %rbp,%rdi
call   1030 <strcpy@plt>
mov    %rbp,%rdi
call   1050 <strlen@plt>
mov    %eax,%ebp
lea    0x420(%rsp),%rsi
mov    %r14d,%edi
mov    $0x400000,%edx
call   1070 <read@plt>
test   %eax,%eax
js     130d <process_request+0x16d>
movslq %ebp,%rcx
lea    (%rsp,%rcx,1),%rbp
add    $0x20,%rbp
mov    %eax,%eax
movb   $0x0,0x420(%rsp,%rax,1)
lea    0xe00(%rip),%rsi
lea    0x420(%rsp),%rdi
lea    0xc(%rsp),%r15
lea    0x16(%rsp),%rbx
mov    %r15,%rdx
mov    %rbp,%rcx
mov    %rbx,%r8
call   10a0 <sscanf@plt>
lea    0xde2(%rip),%rdi
mov    %r15,%rsi
mov    %rbp,%rdx
mov    %rbx,%rcx
call   1060 <printf@plt>
lea    0xddb(%rip),%rdi
mov    %rbx,%rsi
call   1080 <strcmp@plt>
test   %rax,%rax
je     128a <process_request+0xea>
lea    0xdd0(%rip),%rdi
lea    0x16(%rsp),%rsi
call   1080 <strcmp@plt>
test   %rax,%rax
je     128a <process_request+0xea>
lea    0xdc3(%rip),%rsi
mov    %r14d,%edi
mov    $0x2b,%edx
call   1040 <write@plt>
mov    0x2d66(%rip),%rax
mov    (%rax),%edi
lea    0xdd2(%rip),%rsi
lea    0x16(%rsp),%rdx
jmp    12df <process_request+0x13f>
lea    0xde9(%rip),%rdi
lea    0xc(%rsp),%rsi
call   1080 <strcmp@plt>
test   %rax,%rax
je     12e8 <process_request+0x148>
lea    0xdd7(%rip),%rdi
lea    0xc(%rsp),%rsi
call   1080 <strcmp@plt>
test   %rax,%rax
je     12fc <process_request+0x15c>
lea    0xdc6(%rip),%rsi
mov    %r14d,%edi
mov    $0x25,%edx
call   1040 <write@plt>
mov    0x2d0f(%rip),%rax
mov    (%rax),%edi
lea    0xdcf(%rip),%rsi
lea    0xc(%rsp),%rdx
call   1090 <fprintf@plt>
xor    %eax,%eax
jmp    131e <process_request+0x17e>
lea    0x20(%rsp),%rsi
mov    %r14d,%edi
mov    $0x1,%edx
call   10b0 <serve_file@plt>
jmp    131e <process_request+0x17e>
lea    0x20(%rsp),%rsi
mov    %r14d,%edi
xor    %edx,%edx
call   10b0 <serve_file@plt>
jmp    131e <process_request+0x17e>
lea    0xdb9(%rip),%rdi
call   10c0 <perror@plt>
mov    $0x1,%eax
add    $0x400428,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [9 x i8] c"%s %s %s\00"
@.str.1 = [14 x i8] c"%s %s %s ... \00"
@.str.2 = [9 x i8] c"HTTP/1.1\00"
@.str.3 = [9 x i8] c"HTTP/1.0\00"
@.str.4 = [44 x i8] c"HTTP/1.0 505 HTTP Version Not Supported\0D\0A\0D\0A\00"
@stderr = global i32 0
@.str.5 = [37 x i8] c"version %s not supported, aborting.\0A\00"
@.str.6 = [4 x i8] c"GET\00"
@.str.7 = [5 x i8] c"HEAD\00"
@.str.8 = [38 x i8] c"HTTP/1.0 400 Method Not Supported\0D\0A\0D\0A\00"
@.str.9 = [36 x i8] c"requested method %s not supported.\0A\00"
@.str.10 = [29 x i8] c"ERROR processing the request\00"
define i32 @process_request(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca [4194304 x i8]
%8 = alloca [10 x i8]
%9 = alloca [10 x i8]
%10 = alloca [1024 x i8]
%11 = alloca i8*
store i32 %0, i32* %4
store i8* %1, i8** %5
%12 = getelementptr inbounds [1024 x i8], [1024 x i8]* %10, i64 0, i64 0
%13 = load i8*, i8** %5
%14 = call i32 @strcpy(i8* %12, i8* %13)
%15 = getelementptr inbounds [1024 x i8], [1024 x i8]* %10, i64 0, i64 0
%16 = getelementptr inbounds [1024 x i8], [1024 x i8]* %10, i64 0, i64 0
%17 = call i32 @strlen(i8* %16)
%18 = sext i32 %17 to i64
%19 = getelementptr inbounds i8, i8* %15, i64 %18
store i8* %19, i8** %11
%20 = load i32, i32* %4
%21 = getelementptr inbounds [4194304 x i8], [4194304 x i8]* %7, i64 0, i64 0
%22 = call i32 @read(i32 %20, i8* %21, i32 4194304)
store i32 %22, i32* %6
%23 = load i32, i32* %6
%24 = icmp slt i32 %23, 0
br i1 %24, label %25, label %26
25: ; preds = %2
br label %74
26: ; preds = %2
%27 = load i32, i32* %6
%28 = sext i32 %27 to i64
%29 = getelementptr inbounds [4194304 x i8], [4194304 x i8]* %7, i64 0, i64 %28
store i8 0, i8* %29
%30 = getelementptr inbounds [4194304 x i8], [4194304 x i8]* %7, i64 0, i64 0
%31 = getelementptr inbounds [10 x i8], [10 x i8]* %8, i64 0, i64 0
%32 = load i8*, i8** %11
%33 = getelementptr inbounds [10 x i8], [10 x i8]* %9, i64 0, i64 0
%34 = call i32 @sscanf(i8* %30, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* %31, i8* %32, i8* %33)
%35 = getelementptr inbounds [10 x i8], [10 x i8]* %8, i64 0, i64 0
%36 = load i8*, i8** %11
%37 = getelementptr inbounds [10 x i8], [10 x i8]* %9, i64 0, i64 0
%38 = call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i8* %35, i8* %36, i8* %37)
%39 = getelementptr inbounds [10 x i8], [10 x i8]* %9, i64 0, i64 0
%40 = call i64 @strcmp(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i8* %39)
%41 = icmp ne i64 %40, 0
br i1 %41, label %42, label %52
42: ; preds = %26
%43 = getelementptr inbounds [10 x i8], [10 x i8]* %9, i64 0, i64 0
%44 = call i64 @strcmp(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i8* %43)
%45 = icmp ne i64 %44, 0
br i1 %45, label %46, label %52
46: ; preds = %42
%47 = load i32, i32* %4
%48 = call i32 @write(i32 %47, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.4, i64 0, i64 0), i32 43)
%49 = load i32, i32* @stderr
%50 = getelementptr inbounds [10 x i8], [10 x i8]* %9, i64 0, i64 0
%51 = call i32 @fprintf(i32 %49, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.5, i64 0, i64 0), i8* %50)
store i32 0, i32* %3
br label %76
52: ; preds = %42, %26
%53 = getelementptr inbounds [10 x i8], [10 x i8]* %8, i64 0, i64 0
%54 = call i64 @strcmp(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.6, i64 0, i64 0), i8* %53)
%55 = icmp eq i64 %54, 0
br i1 %55, label %56, label %60
56: ; preds = %52
%57 = load i32, i32* %4
%58 = getelementptr inbounds [1024 x i8], [1024 x i8]* %10, i64 0, i64 0
%59 = call i32 @serve_file(i32 %57, i8* %58, i32 1)
store i32 %59, i32* %3
br label %76
60: ; preds = %52
%61 = getelementptr inbounds [10 x i8], [10 x i8]* %8, i64 0, i64 0
%62 = call i64 @strcmp(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.7, i64 0, i64 0), i8* %61)
%63 = icmp eq i64 %62, 0
br i1 %63, label %64, label %68
64: ; preds = %60
%65 = load i32, i32* %4
%66 = getelementptr inbounds [1024 x i8], [1024 x i8]* %10, i64 0, i64 0
%67 = call i32 @serve_file(i32 %65, i8* %66, i32 0)
store i32 %67, i32* %3
br label %76
68: ; preds = %60
%69 = load i32, i32* %4
%70 = call i32 @write(i32 %69, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.8, i64 0, i64 0), i32 37)
%71 = load i32, i32* @stderr
%72 = getelementptr inbounds [10 x i8], [10 x i8]* %8, i64 0, i64 0
%73 = call i32 @fprintf(i32 %71, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.9, i64 0, i64 0), i8* %72)
store i32 0, i32* %3
br label %76
74: ; preds = %25
%75 = call i32 @perror(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.10, i64 0, i64 0))
store i32 1, i32* %3
br label %76
76: ; preds = %74, %68, %64, %56, %46
%77 = load i32, i32* %3
ret i32 %77
}

LLVM IR (-O2):
@.str = [9 x i8] c"%s %s %s\00"
@.str.1 = [14 x i8] c"%s %s %s ... \00"
@.str.2 = [9 x i8] c"HTTP/1.1\00"
@.str.3 = [9 x i8] c"HTTP/1.0\00"
@.str.4 = [44 x i8] c"HTTP/1.0 505 HTTP Version Not Supported\0D\0A\0D\0A\00"
@stderr = global i32 0
@.str.5 = [37 x i8] c"version %s not supported, aborting.\0A\00"
@.str.6 = [4 x i8] c"GET\00"
@.str.7 = [5 x i8] c"HEAD\00"
@.str.8 = [38 x i8] c"HTTP/1.0 400 Method Not Supported\0D\0A\0D\0A\00"
@.str.9 = [36 x i8] c"requested method %s not supported.\0A\00"
@.str.10 = [29 x i8] c"ERROR processing the request\00"
define i32 @process_request(i32 %0, i8* %1) {
%3 = alloca [4194304 x i8]
%4 = alloca [10 x i8]
%5 = alloca [10 x i8]
%6 = alloca [1024 x i8]
%7 = getelementptr inbounds [4194304 x i8], [4194304 x i8]* %3, i64 0, i64 0
%8 = getelementptr inbounds [10 x i8], [10 x i8]* %4, i64 0, i64 0
%9 = getelementptr inbounds [10 x i8], [10 x i8]* %5, i64 0, i64 0
%10 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%11 = call i32 @strcpy(i8* %10, i8* %1)
%12 = call i32 @strlen(i8* %10)
%13 = sext i32 %12 to i64
%14 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 %13
%15 = call i32 @read(i32 %0, i8* %7, i32 4194304)
%16 = icmp slt i32 %15, 0
br i1 %16, label %45, label %17
17: ; preds = %2
%18 = zext i32 %15 to i64
%19 = getelementptr inbounds [4194304 x i8], [4194304 x i8]* %3, i64 0, i64 %18
store i8 0, i8* %19
%20 = call i32 @sscanf(i8* %7, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* %8, i8* %14, i8* %9)
%21 = call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i8* %8, i8* %14, i8* %9)
%22 = call i64 @strcmp(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i8* %9)
%23 = icmp eq i64 %22, 0
br i1 %23, label %31, label %24
24: ; preds = %17
%25 = call i64 @strcmp(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i8* %9)
%26 = icmp eq i64 %25, 0
br i1 %26, label %31, label %27
27: ; preds = %24
%28 = call i32 @write(i32 %0, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.4, i64 0, i64 0), i32 43)
%29 = load i32, i32* @stderr
%30 = call i32 @fprintf(i32 %29, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.5, i64 0, i64 0), i8* %9)
br label %47
31: ; preds = %24, %17
%32 = call i64 @strcmp(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.6, i64 0, i64 0), i8* %8)
%33 = icmp eq i64 %32, 0
br i1 %33, label %34, label %36
34: ; preds = %31
%35 = call i32 @serve_file(i32 %0, i8* %10, i32 1)
br label %47
36: ; preds = %31
%37 = call i64 @strcmp(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.7, i64 0, i64 0), i8* %8)
%38 = icmp eq i64 %37, 0
br i1 %38, label %39, label %41
39: ; preds = %36
%40 = call i32 @serve_file(i32 %0, i8* %10, i32 0)
br label %47
41: ; preds = %36
%42 = call i32 @write(i32 %0, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.8, i64 0, i64 0), i32 37)
%43 = load i32, i32* @stderr
%44 = call i32 @fprintf(i32 %43, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.9, i64 0, i64 0), i8* %8)
br label %47
45: ; preds = %2
%46 = call i32 @perror(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.10, i64 0, i64 0))
br label %47
47: ; preds = %45, %41, %39, %34, %27
%48 = phi i32 [ 1, %45 ], [ 0, %27 ], [ %35, %34 ], [ %40, %39 ], [ 0, %41 ]
ret i32 %48
}