#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__ pthread_t;

/* Variables and functions */
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, unsigned long);
int /*<<< orphan*/ printf(char *, unsigned long, ...);
scalar_t__ pthread_self();
int recv(int, char *, int, int /*<<< orphan*/);
int send(int, char *, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strlen(char *);
void *handle_client(void *socket) {
  char buffer[1024] = {0};
  int *newsockfd_ptr = (int *)socket;
  int newsockfd = *newsockfd_ptr;
  pthread_t thread_id = pthread_self();
  printf("----------\nThread %lu using socket %x\n", (unsigned long)thread_id,
         newsockfd);
  int num_bytes = recv(newsockfd, buffer, 1024 - 1, 0);
  if (num_bytes < 0) {
    fprintf(stderr, "Thread %lu ERROR: recv() failed\n",
            (unsigned long)thread_id);
    return NULL;
  }
  printf("Thread %lu recieved request\n", (unsigned long)thread_id);
  printf("Thread %lu sending response\n", (unsigned long)thread_id);
  num_bytes = send(
      newsockfd,
      "HTTP/1.0 200 OK\r\nServer: LLP2 DummyServer (1.0)\r\nLast-Modified: "
      "Wed, 27 Nov 2016 07:15:56 GMT\r\nContent-Type: text/html\r\nConnection: "
      "Closed\r\n\r\n<html>\r\n  <title>Simple Response</title>\r\n  "
      "<body>\r\n    <img "
      "src=\"https://cdn.tutsplus.com/net/uploads/legacy/511_http/"
      "http_diagram.png\" />\r\n    <br />\r\n    <a "
      "href=\"https://code.tutsplus.com/tutorials/"
      "http-headers-for-dummies--net-8039\">HTTP Headers for Dummies</a>\r\n  "
      "</body>\r\n</html>",
      strlen(
          "HTTP/1.0 200 OK\r\nServer: LLP2 DummyServer (1.0)\r\nLast-Modified: "
          "Wed, 27 Nov 2016 07:15:56 GMT\r\nContent-Type: "
          "text/html\r\nConnection: Closed\r\n\r\n<html>\r\n  <title>Simple "
          "Response</title>\r\n  <body>\r\n    <img "
          "src=\"https://cdn.tutsplus.com/net/uploads/legacy/511_http/"
          "http_diagram.png\" />\r\n    <br />\r\n    <a "
          "href=\"https://code.tutsplus.com/tutorials/"
          "http-headers-for-dummies--net-8039\">HTTP Headers for "
          "Dummies</a>\r\n  </body>\r\n</html>"),
      0);
  if (num_bytes < 0) {
    fprintf(stderr, "Thread %lu ERROR: send() failed\n",
            (unsigned long)thread_id);
    return NULL;
  }
  close(newsockfd);
  return NULL;
}


x86 assembly (clang, -O0):
<handle_client>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
mov    %rdi,-0x10(%rbp)
lea    -0x410(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1070 <memset@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x418(%rbp)
mov    -0x418(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x41c(%rbp)
mov    $0x0,%al
call   10a0 <pthread_self@plt>
mov    %rax,-0x428(%rbp)
mov    -0x428(%rbp),%rsi
mov    -0x41c(%rbp),%edx
lea    0xe22(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x41c(%rbp),%edi
lea    -0x410(%rbp),%rsi
mov    $0x3ff,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,-0x42c(%rbp)
cmpl   $0x0,-0x42c(%rbp)
jge    123a <handle_client+0xba>
mov    0x2dc8(%rip),%rax
mov    (%rax),%edi
mov    -0x428(%rbp),%rdx
lea    0xdff(%rip),%rsi
call   1090 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
jmp    12e1 <handle_client+0x161>
mov    -0x428(%rbp),%rsi
lea    0xe00(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x428(%rbp),%rsi
lea    0xe08(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x41c(%rbp),%eax
mov    %eax,-0x430(%rbp)
lea    0xe0b(%rip),%rdi
call   1040 <strlen@plt>
mov    -0x430(%rbp),%edi
mov    %eax,%edx
lea    0xdf7(%rip),%rsi
xor    %ecx,%ecx
call   1050 <send@plt>
mov    %eax,-0x42c(%rbp)
cmpl   $0x0,-0x42c(%rbp)
jge    12ce <handle_client+0x14e>
mov    0x2d34(%rip),%rax
mov    (%rax),%edi
mov    -0x428(%rbp),%rdx
lea    0xf73(%rip),%rsi
call   1090 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
jmp    12e1 <handle_client+0x161>
mov    -0x41c(%rbp),%edi
call   1080 <close@plt>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x430,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<handle_client>:
push   %rbp
push   %r14
push   %rbx
sub    $0x400,%rsp
mov    %rdi,%rbx
mov    %rsp,%r14
mov    $0x400,%edx
mov    %r14,%rdi
xor    %esi,%esi
call   1070 <memset@plt>
mov    (%rbx),%ebp
xor    %eax,%eax
call   10a0 <pthread_self@plt>
mov    %rax,%rbx
lea    0xe4d(%rip),%rdi
mov    %rax,%rsi
mov    %ebp,%edx
xor    %eax,%eax
call   1060 <printf@plt>
mov    %ebp,%edi
mov    %r14,%rsi
mov    $0x3ff,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
test   %eax,%eax
js     1220 <handle_client+0xa0>
lea    0xe6d(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
lea    0xe79(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
lea    0xe85(%rip),%r14
mov    %r14,%rdi
call   1040 <strlen@plt>
mov    %ebp,%edi
mov    %r14,%rsi
mov    %eax,%edx
xor    %ecx,%ecx
call   1050 <send@plt>
test   %eax,%eax
js     1232 <handle_client+0xb2>
mov    %ebp,%edi
call   1080 <close@plt>
jmp    124a <handle_client+0xca>
mov    0x2db9(%rip),%rax
mov    (%rax),%edi
lea    0xdf7(%rip),%rsi
jmp    1242 <handle_client+0xc2>
mov    0x2da7(%rip),%rax
mov    (%rax),%edi
lea    0xfed(%rip),%rsi
mov    %rbx,%rdx
call   1090 <fprintf@plt>
xor    %eax,%eax
add    $0x400,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [39 x i8] c"----------\0AThread %lu using socket %x\0A\00"
@stderr = global i32 0
@.str.1 = [33 x i8] c"Thread %lu ERROR: recv() failed\0A\00"
@.str.2 = [29 x i8] c"Thread %lu recieved request\0A\00"
@.str.3 = [29 x i8] c"Thread %lu sending response\0A\00"
@.str.4 = [429 x i8] c"HTTP/1.0 200 OK\0D\0AServer: LLP2 DummyServer (1.0)\0D\0ALast-Modified: Wed, 27 Nov 2016 07:15:56 GMT\0D\0AContent-Type: text/html\0D\0AConnection: Closed\0D\0A\0D\0A<html>\0D\0A <title>Simple Response</title>\0D\0A <body>\0D\0A <img src=\22https://cdn.tutsplus.com/net/uploads/legacy/511_http/http_diagram.png\22 />\0D\0A <br />\0D\0A <a href=\22https://code.tutsplus.com/tutorials/http-headers-for-dummies--net-8039\22>HTTP Headers for Dummies</a>\0D\0A </body>\0D\0A</html>\00"
@.str.5 = [33 x i8] c"Thread %lu ERROR: send() failed\0A\00"
define i8* @handle_client(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca [1024 x i8]
%5 = alloca i32*
%6 = alloca i32
%7 = alloca i64
%8 = alloca i32
store i8* %0, i8** %3
%9 = bitcast [1024 x i8]* %4 to i8*
%10 = load i8*, i8** %3
%11 = bitcast i8* %10 to i32*
store i32* %11, i32** %5
%12 = load i32*, i32** %5
%13 = load i32, i32* %12
store i32 %13, i32* %6
%14 = call i64 (...) @pthread_self()
store i64 %14, i64* %7
%15 = load i64, i64* %7
%16 = load i32, i32* %6
%17 = call i32 (i8*, i64, ...) @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str, i64 0, i64 0), i64 %15, i32 %16)
%18 = load i32, i32* %6
%19 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%20 = call i32 @recv(i32 %18, i8* %19, i32 1023, i32 0)
store i32 %20, i32* %8
%21 = load i32, i32* %8
%22 = icmp slt i32 %21, 0
br i1 %22, label %23, label %27
23: ; preds = %1
%24 = load i32, i32* @stderr
%25 = load i64, i64* %7
%26 = call i32 @fprintf(i32 %24, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0), i64 %25)
store i8* null, i8** %2
br label %44
27: ; preds = %1
%28 = load i64, i64* %7
%29 = call i32 (i8*, i64, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0), i64 %28)
%30 = load i64, i64* %7
%31 = call i32 (i8*, i64, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0), i64 %30)
%32 = load i32, i32* %6
%33 = call i32 @strlen(i8* getelementptr inbounds ([429 x i8], [429 x i8]* @.str.4, i64 0, i64 0))
%34 = call i32 @send(i32 %32, i8* getelementptr inbounds ([429 x i8], [429 x i8]* @.str.4, i64 0, i64 0), i32 %33, i32 0)
store i32 %34, i32* %8
%35 = load i32, i32* %8
%36 = icmp slt i32 %35, 0
br i1 %36, label %37, label %41
37: ; preds = %27
%38 = load i32, i32* @stderr
%39 = load i64, i64* %7
%40 = call i32 @fprintf(i32 %38, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.5, i64 0, i64 0), i64 %39)
store i8* null, i8** %2
br label %44
41: ; preds = %27
%42 = load i32, i32* %6
%43 = call i32 @close(i32 %42)
store i8* null, i8** %2
br label %44
44: ; preds = %41, %37, %23
%45 = load i8*, i8** %2
ret i8* %45
}

LLVM IR (-O2):
@.str = [39 x i8] c"----------\0AThread %lu using socket %x\0A\00"
@stderr = global i32 0
@.str.1 = [33 x i8] c"Thread %lu ERROR: recv() failed\0A\00"
@.str.2 = [29 x i8] c"Thread %lu recieved request\0A\00"
@.str.3 = [29 x i8] c"Thread %lu sending response\0A\00"
@.str.4 = [429 x i8] c"HTTP/1.0 200 OK\0D\0AServer: LLP2 DummyServer (1.0)\0D\0ALast-Modified: Wed, 27 Nov 2016 07:15:56 GMT\0D\0AContent-Type: text/html\0D\0AConnection: Closed\0D\0A\0D\0A<html>\0D\0A <title>Simple Response</title>\0D\0A <body>\0D\0A <img src=\22https://cdn.tutsplus.com/net/uploads/legacy/511_http/http_diagram.png\22 />\0D\0A <br />\0D\0A <a href=\22https://code.tutsplus.com/tutorials/http-headers-for-dummies--net-8039\22>HTTP Headers for Dummies</a>\0D\0A </body>\0D\0A</html>\00"
@.str.5 = [33 x i8] c"Thread %lu ERROR: send() failed\0A\00"
define noalias i8* @handle_client(i8* nocapture %0) {
%2 = alloca [1024 x i8]
%3 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%4 = bitcast i8* %0 to i32*
%5 = load i32, i32* %4
%6 = tail call i64 (...) @pthread_self()
%7 = tail call i32 (i8*, i64, ...) @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str, i64 0, i64 0), i64 %6, i32 %5)
%8 = call i32 @recv(i32 %5, i8* %3, i32 1023, i32 0)
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %13
10: ; preds = %1
%11 = load i32, i32* @stderr
%12 = call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0), i64 %6)
br label %24
13: ; preds = %1
%14 = call i32 (i8*, i64, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0), i64 %6)
%15 = call i32 (i8*, i64, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0), i64 %6)
%16 = call i32 @strlen(i8* getelementptr inbounds ([429 x i8], [429 x i8]* @.str.4, i64 0, i64 0))
%17 = call i32 @send(i32 %5, i8* getelementptr inbounds ([429 x i8], [429 x i8]* @.str.4, i64 0, i64 0), i32 %16, i32 0)
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %22
19: ; preds = %13
%20 = load i32, i32* @stderr
%21 = call i32 @fprintf(i32 %20, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.5, i64 0, i64 0), i64 %6)
br label %24
22: ; preds = %13
%23 = call i32 @close(i32 %5)
br label %24
24: ; preds = %22, %19, %10
ret i8* null
}