#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*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int open(char *, int /*<<< orphan*/);
int read(int, char *, int);
int /*<<< orphan*/ snprintf(char *, int, char *);
char *strchr(char *, char);
int strlen(char *);
scalar_t__ strncmp(char *, char *, int);
int write(int, char *, int);
void do_handle_dude(int s) {
  char buf[4096];
  int rc;
  char *p;
  int fd;
  rc = read(s, buf, sizeof(buf) - 1);
  if (rc <= 0)
    return;
  buf[rc] = 0;
  p = strchr(buf, '\r');
  if (p)
    *p = 0;
  p = strchr(buf, '\n');
  if (p)
    *p = 0;
  if (strncmp(buf, "GET / HTTP/", 11) != 0)
    return;
  snprintf(buf, sizeof(buf), "HTTP/1.0 200 OK\nContent-Type: text/html\n\n");
  if (write(s, buf, strlen(buf)) != strlen(buf))
    return;
  fd = open("index.html", O_RDONLY);
  if (fd == -1)
    return;
  while ((rc = read(fd, buf, sizeof(buf))) > 0) {
    if (write(s, buf, rc) != rc) {
      close(fd);
      return;
    }
  }
  close(fd);
  return;
}


x86 assembly (clang, -O0):
<do_handle_dude>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1040,%rsp
mov    %edi,-0x4(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x1010(%rbp),%rsi
mov    $0xfff,%edx
call   1090 <read@plt>
mov    %eax,-0x1014(%rbp)
cmpl   $0x0,-0x1014(%rbp)
jg     11ba <do_handle_dude+0x3a>
jmp    135f <do_handle_dude+0x1df>
movslq -0x1014(%rbp),%rax
movb   $0x0,-0x1010(%rbp,%rax,1)
lea    -0x1010(%rbp),%rdi
mov    $0xd,%esi
call   1060 <strchr@plt>
mov    %rax,-0x1020(%rbp)
cmpq   $0x0,-0x1020(%rbp)
je     11f9 <do_handle_dude+0x79>
mov    -0x1020(%rbp),%rax
movb   $0x0,(%rax)
lea    -0x1010(%rbp),%rdi
mov    $0xa,%esi
call   1060 <strchr@plt>
mov    %rax,-0x1020(%rbp)
cmpq   $0x0,-0x1020(%rbp)
je     1229 <do_handle_dude+0xa9>
mov    -0x1020(%rbp),%rax
movb   $0x0,(%rax)
lea    -0x1010(%rbp),%rdi
lea    0xdc9(%rip),%rsi
mov    $0xb,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%rax
je     1250 <do_handle_dude+0xd0>
jmp    135f <do_handle_dude+0x1df>
lea    -0x1010(%rbp),%rdi
mov    $0x1000,%esi
lea    0xda9(%rip),%rdx
call   1070 <snprintf@plt>
mov    -0x4(%rbp),%eax
mov    %eax,-0x1034(%rbp)
lea    -0x1010(%rbp),%rax
mov    %rax,-0x1030(%rbp)
lea    -0x1010(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x1034(%rbp),%edi
mov    -0x1030(%rbp),%rsi
mov    %eax,%edx
call   1040 <write@plt>
mov    %eax,-0x1028(%rbp)
lea    -0x1010(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,%ecx
mov    -0x1028(%rbp),%eax
cmp    %ecx,%eax
je     12c6 <do_handle_dude+0x146>
jmp    135f <do_handle_dude+0x1df>
mov    0x2d1b(%rip),%rax
mov    (%rax),%esi
lea    0xd60(%rip),%rdi
call   10a0 <open@plt>
mov    %eax,-0x1024(%rbp)
cmpl   $0xffffffff,-0x1024(%rbp)
jne    12f3 <do_handle_dude+0x173>
jmp    135f <do_handle_dude+0x1df>
jmp    12f8 <do_handle_dude+0x178>
mov    -0x1024(%rbp),%edi
lea    -0x1010(%rbp),%rsi
mov    $0x1000,%edx
call   1090 <read@plt>
mov    %eax,-0x1014(%rbp)
cmp    $0x0,%eax
jle    1354 <do_handle_dude+0x1d4>
mov    -0x4(%rbp),%edi
lea    -0x1010(%rbp),%rsi
mov    -0x1014(%rbp),%edx
call   1040 <write@plt>
cmp    -0x1014(%rbp),%eax
je     134f <do_handle_dude+0x1cf>
mov    -0x1024(%rbp),%edi
call   1080 <close@plt>
jmp    135f <do_handle_dude+0x1df>
jmp    12f8 <do_handle_dude+0x178>
mov    -0x1024(%rbp),%edi
call   1080 <close@plt>
add    $0x1040,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_handle_dude>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x1008,%rsp
mov    %edi,%r14d
mov    %rsp,%rsi
mov    $0xfff,%edx
call   1090 <read@plt>
test   %eax,%eax
jle    127d <do_handle_dude+0xfd>
mov    %eax,%eax
movb   $0x0,(%rsp,%rax,1)
mov    %rsp,%rdi
mov    $0xd,%esi
call   1060 <strchr@plt>
test   %rax,%rax
je     11c0 <do_handle_dude+0x40>
movb   $0x0,(%rax)
mov    %rsp,%rdi
mov    $0xa,%esi
call   1060 <strchr@plt>
test   %rax,%rax
je     11d5 <do_handle_dude+0x55>
movb   $0x0,(%rax)
lea    0xe24(%rip),%rsi
mov    %rsp,%rdi
mov    $0xb,%edx
call   1030 <strncmp@plt>
test   %rax,%rax
jne    127d <do_handle_dude+0xfd>
lea    0xe13(%rip),%rdx
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0x1000,%esi
call   1070 <snprintf@plt>
mov    %rbx,%rdi
call   1050 <strlen@plt>
mov    %r14d,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1040 <write@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1050 <strlen@plt>
cmp    %eax,%ebp
jne    127d <do_handle_dude+0xfd>
mov    0x2db5(%rip),%rax
mov    (%rax),%esi
lea    0xdfa(%rip),%rdi
call   10a0 <open@plt>
cmp    $0xffffffff,%eax
je     127d <do_handle_dude+0xfd>
mov    %eax,%ebp
mov    %rsp,%r15
nopl   0x0(%rax,%rax,1)
mov    %ebp,%edi
mov    %r15,%rsi
mov    $0x1000,%edx
call   1090 <read@plt>
test   %eax,%eax
jle    1276 <do_handle_dude+0xf6>
mov    %eax,%ebx
mov    %r14d,%edi
mov    %r15,%rsi
mov    %eax,%edx
call   1040 <write@plt>
cmp    %ebx,%eax
je     1250 <do_handle_dude+0xd0>
mov    %ebp,%edi
call   1080 <close@plt>
add    $0x1008,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [12 x i8] c"GET / HTTP/\00"
@.str.1 = [42 x i8] c"HTTP/1.0 200 OK\0AContent-Type: text/html\0A\0A\00"
@.str.2 = [11 x i8] c"index.html\00"
@O_RDONLY = global i32 0
define void @do_handle_dude(i32 %0) {
%2 = alloca i32
%3 = alloca [4096 x i8]
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
store i32 %0, i32* %2
%7 = load i32, i32* %2
%8 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%9 = call i32 @read(i32 %7, i8* %8, i32 4095)
store i32 %9, i32* %4
%10 = load i32, i32* %4
%11 = icmp sle i32 %10, 0
br i1 %11, label %12, label %13
12: ; preds = %1
br label %73
13: ; preds = %1
%14 = load i32, i32* %4
%15 = sext i32 %14 to i64
%16 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 %15
store i8 0, i8* %16
%17 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%18 = call i8* @strchr(i8* %17, i8 signext 13)
store i8* %18, i8** %5
%19 = load i8*, i8** %5
%20 = icmp ne i8* %19, null
br i1 %20, label %21, label %23
21: ; preds = %13
%22 = load i8*, i8** %5
store i8 0, i8* %22
br label %23
23: ; preds = %21, %13
%24 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%25 = call i8* @strchr(i8* %24, i8 signext 10)
store i8* %25, i8** %5
%26 = load i8*, i8** %5
%27 = icmp ne i8* %26, null
br i1 %27, label %28, label %30
28: ; preds = %23
%29 = load i8*, i8** %5
store i8 0, i8* %29
br label %30
30: ; preds = %28, %23
%31 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%32 = call i64 @strncmp(i8* %31, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 11)
%33 = icmp ne i64 %32, 0
br i1 %33, label %34, label %35
34: ; preds = %30
br label %73
35: ; preds = %30
%36 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%37 = call i32 @snprintf(i8* %36, i32 4096, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.1, i64 0, i64 0))
%38 = load i32, i32* %2
%39 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%40 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%41 = call i32 @strlen(i8* %40)
%42 = call i32 @write(i32 %38, i8* %39, i32 %41)
%43 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%44 = call i32 @strlen(i8* %43)
%45 = icmp ne i32 %42, %44
br i1 %45, label %46, label %47
46: ; preds = %35
br label %73
47: ; preds = %35
%48 = load i32, i32* @O_RDONLY
%49 = call i32 @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i32 %48)
store i32 %49, i32* %6
%50 = load i32, i32* %6
%51 = icmp eq i32 %50, -1
br i1 %51, label %52, label %53
52: ; preds = %47
br label %73
53: ; preds = %47
br label %54
54: ; preds = %69, %53
%55 = load i32, i32* %6
%56 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%57 = call i32 @read(i32 %55, i8* %56, i32 4096)
store i32 %57, i32* %4
%58 = icmp sgt i32 %57, 0
br i1 %58, label %59, label %70
59: ; preds = %54
%60 = load i32, i32* %2
%61 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%62 = load i32, i32* %4
%63 = call i32 @write(i32 %60, i8* %61, i32 %62)
%64 = load i32, i32* %4
%65 = icmp ne i32 %63, %64
br i1 %65, label %66, label %69
66: ; preds = %59
%67 = load i32, i32* %6
%68 = call i32 @close(i32 %67)
br label %73
69: ; preds = %59
br label %54
70: ; preds = %54
%71 = load i32, i32* %6
%72 = call i32 @close(i32 %71)
br label %73
73: ; preds = %70, %66, %52, %46, %34, %12
ret void
}

LLVM IR (-O2):
@.str = [12 x i8] c"GET / HTTP/\00"
@.str.1 = [42 x i8] c"HTTP/1.0 200 OK\0AContent-Type: text/html\0A\0A\00"
@.str.2 = [11 x i8] c"index.html\00"
@O_RDONLY = global i32 0
define void @do_handle_dude(i32 %0) {
%2 = alloca [4096 x i8]
%3 = getelementptr inbounds [4096 x i8], [4096 x i8]* %2, i64 0, i64 0
%4 = call i32 @read(i32 %0, i8* %3, i32 4095)
%5 = icmp slt i32 %4, 1
br i1 %5, label %37, label %6
6: ; preds = %1
%7 = zext i32 %4 to i64
%8 = getelementptr inbounds [4096 x i8], [4096 x i8]* %2, i64 0, i64 %7
store i8 0, i8* %8
%9 = call i8* @strchr(i8* %3, i8 signext 13)
%10 = icmp eq i8* %9, null
br i1 %10, label %12, label %11
11: ; preds = %6
store i8 0, i8* %9
br label %12
12: ; preds = %11, %6
%13 = call i8* @strchr(i8* %3, i8 signext 10)
%14 = icmp eq i8* %13, null
br i1 %14, label %16, label %15
15: ; preds = %12
store i8 0, i8* %13
br label %16
16: ; preds = %15, %12
%17 = call i64 @strncmp(i8* %3, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 11)
%18 = icmp eq i64 %17, 0
br i1 %18, label %19, label %37
19: ; preds = %16
%20 = call i32 @snprintf(i8* %3, i32 4096, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.1, i64 0, i64 0))
%21 = call i32 @strlen(i8* %3)
%22 = call i32 @write(i32 %0, i8* %3, i32 %21)
%23 = call i32 @strlen(i8* %3)
%24 = icmp eq i32 %22, %23
br i1 %24, label %25, label %37
25: ; preds = %19
%26 = load i32, i32* @O_RDONLY
%27 = call i32 @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i32 %26)
%28 = icmp eq i32 %27, -1
br i1 %28, label %37, label %29
29: ; preds = %25, %32
%30 = call i32 @read(i32 %27, i8* %3, i32 4096)
%31 = icmp sgt i32 %30, 0
br i1 %31, label %32, label %35
32: ; preds = %29
%33 = call i32 @write(i32 %0, i8* %3, i32 %30)
%34 = icmp eq i32 %33, %30
br i1 %34, label %29, label %35
35: ; preds = %29, %32
%36 = call i32 @close(i32 %27)
br label %37
37: ; preds = %35, %25, %19, %16, %1
ret void
}