#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 */
struct sockaddr_in {
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_addr;
};

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ SHUT_RDWR;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fatal(char *);
int /*<<< orphan*/ free(unsigned char *);
int get_file_size(int);
char *inet_ntoa(int /*<<< orphan*/);
scalar_t__ malloc(int);
int ntohs(int /*<<< orphan*/);
int open(unsigned char *, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ read(int, unsigned char *, int);
int recv_line(int, unsigned char *);
int /*<<< orphan*/ send(int, unsigned char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ send_string(int, char *);
int /*<<< orphan*/ shutdown(int, int /*<<< orphan*/);
int /*<<< orphan*/ sprintf(unsigned char *, char *, char *, int,
                           unsigned char *);
int /*<<< orphan*/ strcat(unsigned char *, ...);
int /*<<< orphan*/ strcpy(unsigned char *, char *);
int strlen(unsigned char *);
scalar_t__ strncmp(unsigned char *, char *, int);
unsigned char *strstr(unsigned char *, char *);
int /*<<< orphan*/ timestamp(int);
int /*<<< orphan*/ write(int, unsigned char *, int);
void handle_connection(int sockfd, struct sockaddr_in *client_addr_ptr,
                       int logfd) {
  unsigned char *ptr, request[500], resource[500], log_buffer[500];
  int fd, length;
  length = recv_line(sockfd, request);
  sprintf(log_buffer, "From %s:%d \"%s\"\t",
          inet_ntoa(client_addr_ptr->sin_addr),
          ntohs(client_addr_ptr->sin_port), request);
  ptr = strstr(request, " HTTP/");
  if (ptr == NULL) {
    strcat(log_buffer, " NOT HTTP!\n");
  } else {
    *ptr = 0;
    ptr = NULL;
    if (strncmp(request, "GET ", 4) == 0)
      ptr = request + 4;
    if (strncmp(request, "HEAD ", 5) == 0)
      ptr = request + 5;
    if (ptr == NULL) {
      strcat(log_buffer, " UNKNOWN REQUEST!\n");
    } else {
      if (ptr[strlen(ptr) - 1] == '/')
        strcat(ptr, "index.html");
      strcpy(resource, "./webroot");
      strcat(resource, ptr);
      fd = open(resource, O_RDONLY, 0);
      if (fd == -1) {
        strcat(log_buffer, " 404 Not Found\n");
        send_string(sockfd, "HTTP/1.0 404 NOT FOUND\r\n");
        send_string(sockfd, "Server: Tiny webserver\r\n\r\n");
        send_string(sockfd, "<html><head><title>404 Not Found</title></head>");
        send_string(sockfd, "<body><h1>URL not found</h1></body></html>\r\n");
      } else {
        strcat(log_buffer, " 200 OK\n");
        send_string(sockfd, "HTTP/1.0 200 OK\r\n");
        send_string(sockfd, "Server: Tiny webserver\r\n\r\n");
        if (ptr == request + 4) {
          if ((length = get_file_size(fd)) == -1)
            fatal("getting resource file size");
          if ((ptr = (unsigned char *)malloc(length)) == NULL)
            fatal("allocating memory for reading resource");
          read(fd, ptr, length);
          send(sockfd, ptr, length, 0);
          free(ptr);
        }
        close(fd);
      }
    }
  }
  timestamp(logfd);
  length = strlen(log_buffer);
  write(logfd, log_buffer, length);
  shutdown(sockfd, SHUT_RDWR);
}


x86 assembly (clang, -O0):
<handle_connection>:
push   %rbp
mov    %rsp,%rbp
sub    $0x640,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x220(%rbp),%rsi
call   1130 <recv_line@plt>
mov    %eax,-0x628(%rbp)
lea    -0x620(%rbp),%rax
mov    %rax,-0x638(%rbp)
mov    -0x10(%rbp),%rax
mov    0x4(%rax),%edi
call   1080 <inet_ntoa@plt>
mov    %rax,-0x630(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
call   1100 <ntohs@plt>
mov    -0x638(%rbp),%rdi
mov    -0x630(%rbp),%rdx
mov    %eax,%ecx
lea    -0x220(%rbp),%r8
lea    0xd3c(%rip),%rsi
call   1150 <sprintf@plt>
lea    -0x220(%rbp),%rdi
lea    0xd3a(%rip),%rsi
call   1170 <strstr@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1305 <handle_connection+0xb5>
lea    -0x620(%rbp),%rdi
lea    0xd1f(%rip),%rsi
mov    $0x0,%al
call   1140 <strcat@plt>
jmp    155f <handle_connection+0x30f>
mov    -0x20(%rbp),%rax
movb   $0x0,(%rax)
movq   $0x0,-0x20(%rbp)
lea    -0x220(%rbp),%rdi
lea    0xd02(%rip),%rsi
mov    $0x4,%edx
call   1050 <strncmp@plt>
cmp    $0x0,%rax
jne    1345 <handle_connection+0xf5>
lea    -0x220(%rbp),%rax
add    $0x4,%rax
mov    %rax,-0x20(%rbp)
lea    -0x220(%rbp),%rdi
lea    0xcd6(%rip),%rsi
mov    $0x5,%edx
call   1050 <strncmp@plt>
cmp    $0x0,%rax
jne    1376 <handle_connection+0x126>
lea    -0x220(%rbp),%rax
add    $0x5,%rax
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    139b <handle_connection+0x14b>
lea    -0x620(%rbp),%rdi
lea    0xca0(%rip),%rsi
mov    $0x0,%al
call   1140 <strcat@plt>
jmp    155a <handle_connection+0x30a>
mov    -0x20(%rbp),%rax
mov    %rax,-0x640(%rbp)
mov    -0x20(%rbp),%rdi
call   10a0 <strlen@plt>
mov    %eax,%ecx
mov    -0x640(%rbp),%rax
sub    $0x1,%ecx
movslq %ecx,%rcx
movzbl (%rax,%rcx,1),%eax
cmp    $0x2f,%eax
jne    13dd <handle_connection+0x18d>
mov    -0x20(%rbp),%rdi
lea    0xc6c(%rip),%rsi
mov    $0x0,%al
call   1140 <strcat@plt>
lea    -0x420(%rbp),%rdi
lea    0xc62(%rip),%rsi
call   1060 <strcpy@plt>
lea    -0x420(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    $0x0,%al
call   1140 <strcat@plt>
lea    -0x420(%rbp),%rdi
mov    0x2bd0(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1120 <open@plt>
mov    %eax,-0x624(%rbp)
cmpl   $0xffffffff,-0x624(%rbp)
jne    1482 <handle_connection+0x232>
lea    -0x620(%rbp),%rdi
lea    0xc1d(%rip),%rsi
mov    $0x0,%al
call   1140 <strcat@plt>
mov    -0x4(%rbp),%edi
lea    0xc1c(%rip),%rsi
call   1040 <send_string@plt>
mov    -0x4(%rbp),%edi
lea    0xc26(%rip),%rsi
call   1040 <send_string@plt>
mov    -0x4(%rbp),%edi
lea    0xc32(%rip),%rsi
call   1040 <send_string@plt>
mov    -0x4(%rbp),%edi
lea    0xc53(%rip),%rsi
call   1040 <send_string@plt>
jmp    1555 <handle_connection+0x305>
lea    -0x620(%rbp),%rdi
lea    0xc68(%rip),%rsi
mov    $0x0,%al
call   1140 <strcat@plt>
mov    -0x4(%rbp),%edi
lea    0xc60(%rip),%rsi
call   1040 <send_string@plt>
mov    -0x4(%rbp),%edi
lea    0xbd0(%rip),%rsi
call   1040 <send_string@plt>
mov    -0x20(%rbp),%rax
lea    -0x220(%rbp),%rcx
add    $0x4,%rcx
cmp    %rcx,%rax
jne    154a <handle_connection+0x2fa>
mov    -0x624(%rbp),%edi
call   1110 <get_file_size@plt>
mov    %eax,-0x628(%rbp)
cmp    $0xffffffff,%eax
jne    14f3 <handle_connection+0x2a3>
lea    0xc25(%rip),%rdi
call   1160 <fatal@plt>
mov    -0x628(%rbp),%edi
call   10f0 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    1518 <handle_connection+0x2c8>
lea    0xc1b(%rip),%rdi
call   1160 <fatal@plt>
mov    -0x624(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    -0x628(%rbp),%edx
call   10e0 <read@plt>
mov    -0x4(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    -0x628(%rbp),%edx
xor    %ecx,%ecx
call   10c0 <send@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x624(%rbp),%edi
call   10d0 <close@plt>
jmp    155a <handle_connection+0x30a>
jmp    155f <handle_connection+0x30f>
mov    -0x14(%rbp),%edi
call   10b0 <timestamp@plt>
lea    -0x620(%rbp),%rdi
call   10a0 <strlen@plt>
mov    %eax,-0x628(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x620(%rbp),%rsi
mov    -0x628(%rbp),%edx
call   1070 <write@plt>
mov    -0x4(%rbp),%edi
mov    0x2a58(%rip),%rax
mov    (%rax),%esi
call   1090 <shutdown@plt>
add    $0x640,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<handle_connection>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x5f8,%rsp
mov    %edx,%r14d
mov    %rsi,%rbp
mov    %edi,%r13d
lea    0x200(%rsp),%r15
mov    %r15,%rsi
call   1130 <recv_line@plt>
mov    0x4(%rbp),%edi
call   1080 <inet_ntoa@plt>
mov    %rax,%rbx
mov    0x0(%rbp),%edi
call   1100 <ntohs@plt>
lea    0xd6c(%rip),%rsi
mov    %rsp,%rdi
mov    %rbx,%rdx
mov    %eax,%ecx
mov    %r15,%r8
call   1150 <sprintf@plt>
lea    0xd66(%rip),%rsi
mov    %r15,%rdi
call   1170 <strstr@plt>
test   %rax,%rax
je     1419 <handle_connection+0x1c9>
movb   $0x0,(%rax)
lea    0xd5e(%rip),%rsi
lea    0x200(%rsp),%rbp
mov    %rbp,%rdi
mov    $0x4,%edx
call   1050 <strncmp@plt>
xor    %ebx,%ebx
test   %rax,%rax
lea    0x204(%rsp),%r12
cmove  %r12,%rbx
lea    0xd36(%rip),%rsi
mov    %rbp,%rdi
mov    $0x5,%edx
call   1050 <strncmp@plt>
test   %rax,%rax
lea    0x205(%rsp),%rbp
cmovne %rbx,%rbp
test   %rbp,%rbp
je     1422 <handle_connection+0x1d2>
mov    %rbp,%rdi
call   10a0 <strlen@plt>
cltq
cmpb   $0x2f,-0x1(%rax,%rbp,1)
jne    133a <handle_connection+0xea>
lea    0xd12(%rip),%rsi
mov    %rbp,%rdi
xor    %eax,%eax
call   1140 <strcat@plt>
lea    0xd0c(%rip),%rsi
lea    0x400(%rsp),%rbx
mov    %rbx,%rdi
call   1060 <strcpy@plt>
mov    %rbx,%rdi
mov    %rbp,%rsi
xor    %eax,%eax
call   1140 <strcat@plt>
mov    0x2c7b(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
xor    %edx,%edx
call   1120 <open@plt>
cmp    $0xffffffff,%eax
je     1476 <handle_connection+0x226>
mov    %eax,%r15d
lea    0xd74(%rip),%rsi
mov    %rsp,%rdi
xor    %eax,%eax
call   1140 <strcat@plt>
lea    0xd6c(%rip),%rsi
mov    %r13d,%edi
call   1040 <send_string@plt>
lea    0xcdc(%rip),%rsi
mov    %r13d,%edi
call   1040 <send_string@plt>
cmp    %r12,%rbp
jne    140f <handle_connection+0x1bf>
mov    %r15d,%edi
call   1110 <get_file_size@plt>
mov    %eax,%r12d
cmp    $0xffffffff,%eax
jne    13cd <handle_connection+0x17d>
lea    0xd4b(%rip),%rdi
call   1160 <fatal@plt>
mov    %r12d,%edi
call   10f0 <malloc@plt>
mov    %rax,%rbp
test   %rax,%rax
jne    13e9 <handle_connection+0x199>
lea    0xd4a(%rip),%rdi
call   1160 <fatal@plt>
mov    %r15d,%edi
mov    %rbp,%rsi
mov    %r12d,%edx
call   10e0 <read@plt>
mov    %r13d,%edi
mov    %rbp,%rsi
mov    %r12d,%edx
xor    %ecx,%ecx
call   10c0 <send@plt>
mov    %rbp,%rdi
call   1030 <free@plt>
mov    %r15d,%edi
call   10d0 <close@plt>
jmp    1433 <handle_connection+0x1e3>
lea    0xbf8(%rip),%rsi
jmp    1429 <handle_connection+0x1d9>
lea    0xc06(%rip),%rsi
mov    %rsp,%rdi
xor    %eax,%eax
call   1140 <strcat@plt>
mov    %r14d,%edi
call   10b0 <timestamp@plt>
mov    %rsp,%rbx
mov    %rbx,%rdi
call   10a0 <strlen@plt>
mov    %r14d,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1070 <write@plt>
mov    0x2b96(%rip),%rax
mov    (%rax),%esi
mov    %r13d,%edi
call   1090 <shutdown@plt>
add    $0x5f8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xbda(%rip),%rsi
mov    %rsp,%rdi
xor    %eax,%eax
call   1140 <strcat@plt>
lea    0xbd9(%rip),%rsi
mov    %r13d,%edi
call   1040 <send_string@plt>
lea    0xbe3(%rip),%rsi
mov    %r13d,%edi
call   1040 <send_string@plt>
lea    0xbef(%rip),%rsi
mov    %r13d,%edi
call   1040 <send_string@plt>
lea    0xc10(%rip),%rsi
mov    %r13d,%edi
call   1040 <send_string@plt>
jmp    1433 <handle_connection+0x1e3>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32 }
@.str = [17 x i8] c"From %s:%d \22%s\22\09\00"
@.str.1 = [7 x i8] c" HTTP/\00"
@.str.2 = [12 x i8] c" NOT HTTP!\0A\00"
@.str.3 = [5 x i8] c"GET \00"
@.str.4 = [6 x i8] c"HEAD \00"
@.str.5 = [19 x i8] c" UNKNOWN REQUEST!\0A\00"
@.str.6 = [11 x i8] c"index.html\00"
@.str.7 = [10 x i8] c"./webroot\00"
@O_RDONLY = global i32 0
@.str.8 = [16 x i8] c" 404 Not Found\0A\00"
@.str.9 = [25 x i8] c"HTTP/1.0 404 NOT FOUND\0D\0A\00"
@.str.10 = [27 x i8] c"Server: Tiny webserver\0D\0A\0D\0A\00"
@.str.11 = [48 x i8] c"<html><head><title>404 Not Found</title></head>\00"
@.str.12 = [45 x i8] c"<body><h1>URL not found</h1></body></html>\0D\0A\00"
@.str.13 = [9 x i8] c" 200 OK\0A\00"
@.str.14 = [18 x i8] c"HTTP/1.0 200 OK\0D\0A\00"
@.str.15 = [27 x i8] c"getting resource file size\00"
@.str.16 = [39 x i8] c"allocating memory for reading resource\00"
@SHUT_RDWR = global i32 0
define void @handle_connection(i32 %0, %struct.sockaddr_in* %1, i32 %2) {
%4 = alloca i32
%5 = alloca %struct.sockaddr_in*
%6 = alloca i32
%7 = alloca i8*
%8 = alloca [500 x i8]
%9 = alloca [500 x i8]
%10 = alloca [500 x i8]
%11 = alloca i32
%12 = alloca i32
store i32 %0, i32* %4
store %struct.sockaddr_in* %1, %struct.sockaddr_in** %5
store i32 %2, i32* %6
%13 = load i32, i32* %4
%14 = getelementptr inbounds [500 x i8], [500 x i8]* %8, i64 0, i64 0
%15 = call i32 @recv_line(i32 %13, i8* %14)
store i32 %15, i32* %12
%16 = getelementptr inbounds [500 x i8], [500 x i8]* %10, i64 0, i64 0
%17 = load %struct.sockaddr_in*, %struct.sockaddr_in** %5
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %17, i32 0, i32 1
%19 = load i32, i32* %18
%20 = call i8* @inet_ntoa(i32 %19)
%21 = load %struct.sockaddr_in*, %struct.sockaddr_in** %5
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %21, i32 0, i32 0
%23 = load i32, i32* %22
%24 = call i32 @ntohs(i32 %23)
%25 = getelementptr inbounds [500 x i8], [500 x i8]* %8, i64 0, i64 0
%26 = call i32 @sprintf(i8* %16, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8* %20, i32 %24, i8* %25)
%27 = getelementptr inbounds [500 x i8], [500 x i8]* %8, i64 0, i64 0
%28 = call i8* @strstr(i8* %27, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
store i8* %28, i8** %7
%29 = load i8*, i8** %7
%30 = icmp eq i8* %29, null
br i1 %30, label %31, label %34
31: ; preds = %3
%32 = getelementptr inbounds [500 x i8], [500 x i8]* %10, i64 0, i64 0
%33 = call i32 (i8*, ...) @strcat(i8* %32, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
br label %130
34: ; preds = %3
%35 = load i8*, i8** %7
store i8 0, i8* %35
store i8* null, i8** %7
%36 = getelementptr inbounds [500 x i8], [500 x i8]* %8, i64 0, i64 0
%37 = call i64 @strncmp(i8* %36, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i32 4)
%38 = icmp eq i64 %37, 0
br i1 %38, label %39, label %42
39: ; preds = %34
%40 = getelementptr inbounds [500 x i8], [500 x i8]* %8, i64 0, i64 0
%41 = getelementptr inbounds i8, i8* %40, i64 4
store i8* %41, i8** %7
br label %42
42: ; preds = %39, %34
%43 = getelementptr inbounds [500 x i8], [500 x i8]* %8, i64 0, i64 0
%44 = call i64 @strncmp(i8* %43, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0), i32 5)
%45 = icmp eq i64 %44, 0
br i1 %45, label %46, label %49
46: ; preds = %42
%47 = getelementptr inbounds [500 x i8], [500 x i8]* %8, i64 0, i64 0
%48 = getelementptr inbounds i8, i8* %47, i64 5
store i8* %48, i8** %7
br label %49
49: ; preds = %46, %42
%50 = load i8*, i8** %7
%51 = icmp eq i8* %50, null
br i1 %51, label %52, label %55
52: ; preds = %49
%53 = getelementptr inbounds [500 x i8], [500 x i8]* %10, i64 0, i64 0
%54 = call i32 (i8*, ...) @strcat(i8* %53, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.5, i64 0, i64 0))
br label %129
55: ; preds = %49
%56 = load i8*, i8** %7
%57 = load i8*, i8** %7
%58 = call i32 @strlen(i8* %57)
%59 = sub nsw i32 %58, 1
%60 = sext i32 %59 to i64
%61 = getelementptr inbounds i8, i8* %56, i64 %60
%62 = load i8, i8* %61
%63 = zext i8 %62 to i32
%64 = icmp eq i32 %63, 47
br i1 %64, label %65, label %68
65: ; preds = %55
%66 = load i8*, i8** %7
%67 = call i32 (i8*, ...) @strcat(i8* %66, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.6, i64 0, i64 0))
br label %68
68: ; preds = %65, %55
%69 = getelementptr inbounds [500 x i8], [500 x i8]* %9, i64 0, i64 0
%70 = call i32 @strcpy(i8* %69, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.7, i64 0, i64 0))
%71 = getelementptr inbounds [500 x i8], [500 x i8]* %9, i64 0, i64 0
%72 = load i8*, i8** %7
%73 = call i32 (i8*, ...) @strcat(i8* %71, i8* %72)
%74 = getelementptr inbounds [500 x i8], [500 x i8]* %9, i64 0, i64 0
%75 = load i32, i32* @O_RDONLY
%76 = call i32 @open(i8* %74, i32 %75, i32 0)
store i32 %76, i32* %11
%77 = load i32, i32* %11
%78 = icmp eq i32 %77, -1
br i1 %78, label %79, label %90
79: ; preds = %68
%80 = getelementptr inbounds [500 x i8], [500 x i8]* %10, i64 0, i64 0
%81 = call i32 (i8*, ...) @strcat(i8* %80, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.8, i64 0, i64 0))
%82 = load i32, i32* %4
%83 = call i32 @send_string(i32 %82, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.9, i64 0, i64 0))
%84 = load i32, i32* %4
%85 = call i32 @send_string(i32 %84, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.10, i64 0, i64 0))
%86 = load i32, i32* %4
%87 = call i32 @send_string(i32 %86, i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.11, i64 0, i64 0))
%88 = load i32, i32* %4
%89 = call i32 @send_string(i32 %88, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.12, i64 0, i64 0))
br label %128
90: ; preds = %68
%91 = getelementptr inbounds [500 x i8], [500 x i8]* %10, i64 0, i64 0
%92 = call i32 (i8*, ...) @strcat(i8* %91, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.13, i64 0, i64 0))
%93 = load i32, i32* %4
%94 = call i32 @send_string(i32 %93, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.14, i64 0, i64 0))
%95 = load i32, i32* %4
%96 = call i32 @send_string(i32 %95, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.10, i64 0, i64 0))
%97 = load i8*, i8** %7
%98 = getelementptr inbounds [500 x i8], [500 x i8]* %8, i64 0, i64 0
%99 = getelementptr inbounds i8, i8* %98, i64 4
%100 = icmp eq i8* %97, %99
br i1 %100, label %101, label %125
101: ; preds = %90
%102 = load i32, i32* %11
%103 = call i32 @get_file_size(i32 %102)
store i32 %103, i32* %12
%104 = icmp eq i32 %103, -1
br i1 %104, label %105, label %107
105: ; preds = %101
%106 = call i32 @fatal(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.15, i64 0, i64 0))
br label %107
107: ; preds = %105, %101
%108 = load i32, i32* %12
%109 = call i64 @malloc(i32 %108)
%110 = inttoptr i64 %109 to i8*
store i8* %110, i8** %7
%111 = icmp eq i8* %110, null
br i1 %111, label %112, label %114
112: ; preds = %107
%113 = call i32 @fatal(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.16, i64 0, i64 0))
br label %114
114: ; preds = %112, %107
%115 = load i32, i32* %11
%116 = load i8*, i8** %7
%117 = load i32, i32* %12
%118 = call i32 @read(i32 %115, i8* %116, i32 %117)
%119 = load i32, i32* %4
%120 = load i8*, i8** %7
%121 = load i32, i32* %12
%122 = call i32 @send(i32 %119, i8* %120, i32 %121, i32 0)
%123 = load i8*, i8** %7
%124 = call i32 @free(i8* %123)
br label %125
125: ; preds = %114, %90
%126 = load i32, i32* %11
%127 = call i32 @close(i32 %126)
br label %128
128: ; preds = %125, %79
br label %129
129: ; preds = %128, %52
br label %130
130: ; preds = %129, %31
%131 = load i32, i32* %6
%132 = call i32 @timestamp(i32 %131)
%133 = getelementptr inbounds [500 x i8], [500 x i8]* %10, i64 0, i64 0
%134 = call i32 @strlen(i8* %133)
store i32 %134, i32* %12
%135 = load i32, i32* %6
%136 = getelementptr inbounds [500 x i8], [500 x i8]* %10, i64 0, i64 0
%137 = load i32, i32* %12
%138 = call i32 @write(i32 %135, i8* %136, i32 %137)
%139 = load i32, i32* %4
%140 = load i32, i32* @SHUT_RDWR
%141 = call i32 @shutdown(i32 %139, i32 %140)
ret void
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32 }
@.str = [17 x i8] c"From %s:%d \22%s\22\09\00"
@.str.1 = [7 x i8] c" HTTP/\00"
@.str.2 = [12 x i8] c" NOT HTTP!\0A\00"
@.str.3 = [5 x i8] c"GET \00"
@.str.4 = [6 x i8] c"HEAD \00"
@.str.5 = [19 x i8] c" UNKNOWN REQUEST!\0A\00"
@.str.6 = [11 x i8] c"index.html\00"
@.str.7 = [10 x i8] c"./webroot\00"
@O_RDONLY = global i32 0
@.str.8 = [16 x i8] c" 404 Not Found\0A\00"
@.str.9 = [25 x i8] c"HTTP/1.0 404 NOT FOUND\0D\0A\00"
@.str.10 = [27 x i8] c"Server: Tiny webserver\0D\0A\0D\0A\00"
@.str.11 = [48 x i8] c"<html><head><title>404 Not Found</title></head>\00"
@.str.12 = [45 x i8] c"<body><h1>URL not found</h1></body></html>\0D\0A\00"
@.str.13 = [9 x i8] c" 200 OK\0A\00"
@.str.14 = [18 x i8] c"HTTP/1.0 200 OK\0D\0A\00"
@.str.15 = [27 x i8] c"getting resource file size\00"
@.str.16 = [39 x i8] c"allocating memory for reading resource\00"
@SHUT_RDWR = global i32 0
define void @handle_connection(i32 %0, %struct.sockaddr_in* nocapture %1, i32 %2) {
%4 = alloca [500 x i8]
%5 = alloca [500 x i8]
%6 = alloca [500 x i8]
%7 = getelementptr inbounds [500 x i8], [500 x i8]* %4, i64 0, i64 0
%8 = getelementptr inbounds [500 x i8], [500 x i8]* %5, i64 0, i64 0
%9 = getelementptr inbounds [500 x i8], [500 x i8]* %6, i64 0, i64 0
%10 = call i32 @recv_line(i32 %0, i8* %7)
%11 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 1
%12 = load i32, i32* %11
%13 = call i8* @inet_ntoa(i32 %12)
%14 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 0
%15 = load i32, i32* %14
%16 = call i32 @ntohs(i32 %15)
%17 = call i32 @sprintf(i8* %9, i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8* %13, i32 %16, i8* %7)
%18 = call i8* @strstr(i8* %7, i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%19 = icmp eq i8* %18, null
br i1 %19, label %20, label %22
20: ; preds = %3
%21 = call i32 (i8*, ...) @strcat(i8* %9, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
br label %77
22: ; preds = %3
store i8 0, i8* %18
%23 = call i64 @strncmp(i8* %7, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i32 4)
%24 = icmp eq i64 %23, 0
%25 = getelementptr inbounds [500 x i8], [500 x i8]* %4, i64 0, i64 4
%26 = select i1 %24, i8* %25, i8* null
%27 = call i64 @strncmp(i8* %7, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0), i32 5)
%28 = icmp eq i64 %27, 0
%29 = getelementptr inbounds [500 x i8], [500 x i8]* %4, i64 0, i64 5
%30 = select i1 %28, i8* %29, i8* %26
%31 = icmp eq i8* %30, null
br i1 %31, label %32, label %34
32: ; preds = %22
%33 = call i32 (i8*, ...) @strcat(i8* %9, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.5, i64 0, i64 0))
br label %77
34: ; preds = %22
%35 = call i32 @strlen(i8* %30)
%36 = add nsw i32 %35, -1
%37 = sext i32 %36 to i64
%38 = getelementptr inbounds i8, i8* %30, i64 %37
%39 = load i8, i8* %38
%40 = icmp eq i8 %39, 47
br i1 %40, label %41, label %43
41: ; preds = %34
%42 = call i32 (i8*, ...) @strcat(i8* %30, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.6, i64 0, i64 0))
br label %43
43: ; preds = %41, %34
%44 = call i32 @strcpy(i8* %8, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.7, i64 0, i64 0))
%45 = call i32 (i8*, ...) @strcat(i8* %8, i8* %30)
%46 = load i32, i32* @O_RDONLY
%47 = call i32 @open(i8* %8, i32 %46, i32 0)
%48 = icmp eq i32 %47, -1
br i1 %48, label %49, label %55
49: ; preds = %43
%50 = call i32 (i8*, ...) @strcat(i8* %9, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.8, i64 0, i64 0))
%51 = call i32 @send_string(i32 %0, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.9, i64 0, i64 0))
%52 = call i32 @send_string(i32 %0, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.10, i64 0, i64 0))
%53 = call i32 @send_string(i32 %0, i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.11, i64 0, i64 0))
%54 = call i32 @send_string(i32 %0, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.12, i64 0, i64 0))
br label %77
55: ; preds = %43
%56 = call i32 (i8*, ...) @strcat(i8* %9, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.13, i64 0, i64 0))
%57 = call i32 @send_string(i32 %0, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.14, i64 0, i64 0))
%58 = call i32 @send_string(i32 %0, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.10, i64 0, i64 0))
%59 = icmp eq i8* %30, %25
br i1 %59, label %60, label %75
60: ; preds = %55
%61 = call i32 @get_file_size(i32 %47)
%62 = icmp eq i32 %61, -1
br i1 %62, label %63, label %65
63: ; preds = %60
%64 = call i32 @fatal(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.15, i64 0, i64 0))
br label %65
65: ; preds = %63, %60
%66 = call i64 @malloc(i32 %61)
%67 = inttoptr i64 %66 to i8*
%68 = icmp eq i64 %66, 0
br i1 %68, label %69, label %71
69: ; preds = %65
%70 = call i32 @fatal(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.16, i64 0, i64 0))
br label %71
71: ; preds = %69, %65
%72 = call i32 @read(i32 %47, i8* %67, i32 %61)
%73 = call i32 @send(i32 %0, i8* %67, i32 %61, i32 0)
%74 = call i32 @free(i8* %67)
br label %75
75: ; preds = %71, %55
%76 = call i32 @close(i32 %47)
br label %77
77: ; preds = %32, %75, %49, %20
%78 = call i32 @timestamp(i32 %2)
%79 = call i32 @strlen(i8* %9)
%80 = call i32 @write(i32 %2, i8* %9, i32 %79)
%81 = load i32, i32* @SHUT_RDWR
%82 = call i32 @shutdown(i32 %0, i32 %81)
ret void
}