#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 TcpdaemonServerEnvironment {
  int dummy;
};
typedef int /*<<< orphan*/ http_buffer;

/* Variables and functions */
int /*<<< orphan*/ memset(char *, int, int);
int recv(int, char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ send(int, char *, int, int /*<<< orphan*/);
int sprintf(char *, char *);
scalar_t__ strstr(char *, char *);
int tcpmain(struct TcpdaemonServerEnvironment *p_env, int sock, void *p_addr) {
  char http_buffer[4096 + 1];
  int http_len;
  int len;
  memset(http_buffer, 0x00, sizeof(http_buffer));
  http_len = 0;
  while (sizeof(http_buffer) - 1 - http_len > 0) {
    len = recv(sock, http_buffer + http_len, sizeof(http_buffer) - 1 - http_len,
               0);
    if (len == 0)
      return 0;
    if (len == -1)
      return -1;
    if (strstr(http_buffer, "\r\n\r\n"))
      break;
    http_len += len;
  }
  if (sizeof(http_buffer) - 1 - http_len <= 0) {
    return -1;
  }
  memset(http_buffer, 0x00, sizeof(http_buffer));
  http_len = 0;
  http_len = sprintf(
      http_buffer,
      "HTTP/1.0 200 OK\r\nContent-length: 17\r\n\r\nHello Tcpdaemon\r\n");
  send(sock, http_buffer, http_len, 0);
  return 0;
}


x86 assembly (clang, -O0):
<tcpmain>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1040,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
lea    -0x1030(%rbp),%rdi
xor    %esi,%esi
mov    $0x1001,%edx
call   1050 <memset@plt>
movl   $0x0,-0x1034(%rbp)
movslq -0x1034(%rbp),%rcx
mov    $0x1000,%eax
sub    %rcx,%rax
cmp    $0x0,%rax
jbe    1239 <tcpmain+0xe9>
mov    -0x14(%rbp),%edi
lea    -0x1030(%rbp),%rsi
movslq -0x1034(%rbp),%rax
add    %rax,%rsi
movslq -0x1034(%rbp),%rcx
mov    $0x1000,%eax
sub    %rcx,%rax
mov    %eax,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,-0x1038(%rbp)
cmpl   $0x0,-0x1038(%rbp)
jne    11e7 <tcpmain+0x97>
movl   $0x0,-0x4(%rbp)
jmp    12b2 <tcpmain+0x162>
cmpl   $0xffffffff,-0x1038(%rbp)
jne    1200 <tcpmain+0xb0>
movl   $0xffffffff,-0x4(%rbp)
jmp    12b2 <tcpmain+0x162>
lea    -0x1030(%rbp),%rdi
lea    0xdf2(%rip),%rsi
call   1070 <strstr@plt>
cmp    $0x0,%rax
je     1222 <tcpmain+0xd2>
jmp    1239 <tcpmain+0xe9>
mov    -0x1038(%rbp),%eax
add    -0x1034(%rbp),%eax
mov    %eax,-0x1034(%rbp)
jmp    1183 <tcpmain+0x33>
movslq -0x1034(%rbp),%rcx
mov    $0x1000,%eax
sub    %rcx,%rax
cmp    $0x0,%rax
ja     125e <tcpmain+0x10e>
movl   $0xffffffff,-0x4(%rbp)
jmp    12b2 <tcpmain+0x162>
lea    -0x1030(%rbp),%rdi
xor    %esi,%esi
mov    $0x1001,%edx
call   1050 <memset@plt>
movl   $0x0,-0x1034(%rbp)
lea    -0x1030(%rbp),%rdi
lea    0xd7c(%rip),%rsi
call   1060 <sprintf@plt>
mov    %eax,-0x1034(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x1030(%rbp),%rsi
mov    -0x1034(%rbp),%edx
xor    %ecx,%ecx
call   1040 <send@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x1040,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tcpmain>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x1010,%rsp
mov    %esi,%r12d
mov    %rsp,%r14
xor    %ebx,%ebx
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x1001,%edx
call   1050 <memset@plt>
lea    0xe93(%rip),%r15
nopl   (%rax)
movslq %ebx,%rax
mov    $0x1000,%edx
sub    %eax,%edx
lea    (%rsp,%rax,1),%rsi
mov    %r12d,%edi
xor    %ecx,%ecx
call   1030 <recv@plt>
cmp    $0xffffffff,%eax
je     11ad <tcpmain+0x6d>
mov    %eax,%ebp
test   %eax,%eax
je     1209 <tcpmain+0xc9>
mov    %r14,%rdi
mov    %r15,%rsi
call   1060 <strstr@plt>
test   %rax,%rax
jne    11b4 <tcpmain+0x74>
add    %ebp,%ebx
cmp    $0x1000,%ebx
jne    1170 <tcpmain+0x30>
mov    $0xffffffff,%ebp
jmp    1209 <tcpmain+0xc9>
mov    %rsp,%r14
xor    %ebp,%ebp
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x1001,%edx
call   1050 <memset@plt>
movups 0xe36(%rip),%xmm0
movaps %xmm0,(%rsp)
movups 0xe3b(%rip),%xmm0
movaps %xmm0,0x10(%rsp)
movups 0xe3f(%rip),%xmm0
movaps %xmm0,0x20(%rsp)
movups 0xe3c(%rip),%xmm0
movups %xmm0,0x29(%rsp)
mov    %r12d,%edi
mov    %r14,%rsi
mov    $0x38,%edx
xor    %ecx,%ecx
call   1040 <send@plt>
mov    %ebp,%eax
add    $0x1010,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.TcpdaemonServerEnvironment = type { i32 }
@.str = [5 x i8] c"\0D\0A\0D\0A\00"
@.str.1 = [57 x i8] c"HTTP/1.0 200 OK\0D\0AContent-length: 17\0D\0A\0D\0AHello Tcpdaemon\0D\0A\00"
define i32 @tcpmain(%struct.TcpdaemonServerEnvironment* %0, i32 %1, i8* %2) {
%4 = alloca i32
%5 = alloca %struct.TcpdaemonServerEnvironment*
%6 = alloca i32
%7 = alloca i8*
%8 = alloca [4097 x i8]
%9 = alloca i32
%10 = alloca i32
store %struct.TcpdaemonServerEnvironment* %0, %struct.TcpdaemonServerEnvironment** %5
store i32 %1, i32* %6
store i8* %2, i8** %7
%11 = getelementptr inbounds [4097 x i8], [4097 x i8]* %8, i64 0, i64 0
%12 = call i32 @memset(i8* %11, i32 0, i32 4097)
store i32 0, i32* %9
br label %13
13: ; preds = %41, %3
%14 = load i32, i32* %9
%15 = sext i32 %14 to i64
%16 = sub i64 4096, %15
%17 = icmp ugt i64 %16, 0
br i1 %17, label %18, label %45
18: ; preds = %13
%19 = load i32, i32* %6
%20 = getelementptr inbounds [4097 x i8], [4097 x i8]* %8, i64 0, i64 0
%21 = load i32, i32* %9
%22 = sext i32 %21 to i64
%23 = getelementptr inbounds i8, i8* %20, i64 %22
%24 = load i32, i32* %9
%25 = sext i32 %24 to i64
%26 = sub i64 4096, %25
%27 = trunc i64 %26 to i32
%28 = call i32 @recv(i32 %19, i8* %23, i32 %27, i32 0)
store i32 %28, i32* %10
%29 = load i32, i32* %10
%30 = icmp eq i32 %29, 0
br i1 %30, label %31, label %32
31: ; preds = %18
store i32 0, i32* %4
br label %60
32: ; preds = %18
%33 = load i32, i32* %10
%34 = icmp eq i32 %33, -1
br i1 %34, label %35, label %36
35: ; preds = %32
store i32 -1, i32* %4
br label %60
36: ; preds = %32
%37 = getelementptr inbounds [4097 x i8], [4097 x i8]* %8, i64 0, i64 0
%38 = call i64 @strstr(i8* %37, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%39 = icmp ne i64 %38, 0
br i1 %39, label %40, label %41
40: ; preds = %36
br label %45
41: ; preds = %36
%42 = load i32, i32* %10
%43 = load i32, i32* %9
%44 = add nsw i32 %43, %42
store i32 %44, i32* %9
br label %13
45: ; preds = %40, %13
%46 = load i32, i32* %9
%47 = sext i32 %46 to i64
%48 = sub i64 4096, %47
%49 = icmp ule i64 %48, 0
br i1 %49, label %50, label %51
50: ; preds = %45
store i32 -1, i32* %4
br label %60
51: ; preds = %45
%52 = getelementptr inbounds [4097 x i8], [4097 x i8]* %8, i64 0, i64 0
%53 = call i32 @memset(i8* %52, i32 0, i32 4097)
store i32 0, i32* %9
%54 = getelementptr inbounds [4097 x i8], [4097 x i8]* %8, i64 0, i64 0
%55 = call i32 @sprintf(i8* %54, i8* getelementptr inbounds ([57 x i8], [57 x i8]* @.str.1, i64 0, i64 0))
store i32 %55, i32* %9
%56 = load i32, i32* %6
%57 = getelementptr inbounds [4097 x i8], [4097 x i8]* %8, i64 0, i64 0
%58 = load i32, i32* %9
%59 = call i32 @send(i32 %56, i8* %57, i32 %58, i32 0)
store i32 0, i32* %4
br label %60
60: ; preds = %51, %50, %35, %31
%61 = load i32, i32* %4
ret i32 %61
}

LLVM IR (-O2):
%struct.TcpdaemonServerEnvironment = type { i32 }
@.str = [5 x i8] c"\0D\0A\0D\0A\00"
@.str.1 = [57 x i8] c"HTTP/1.0 200 OK\0D\0AContent-length: 17\0D\0A\0D\0AHello Tcpdaemon\0D\0A\00"
define i32 @tcpmain(%struct.TcpdaemonServerEnvironment* nocapture readnone %0, i32 %1, i8* nocapture readnone %2) {
%4 = alloca [4097 x i8]
%5 = getelementptr inbounds [4097 x i8], [4097 x i8]* %4, i64 0, i64 0
%6 = call i32 @memset(i8* %5, i32 0, i32 4097)
br label %9
7: ; preds = %15
%8 = icmp eq i32 %18, 4096
br i1 %8, label %22, label %9
9: ; preds = %3, %7
%10 = phi i32 [ 0, %3 ], [ %18, %7 ]
%11 = sext i32 %10 to i64
%12 = sub i32 4096, %10
%13 = getelementptr inbounds [4097 x i8], [4097 x i8]* %4, i64 0, i64 %11
%14 = call i32 @recv(i32 %1, i8* %13, i32 %12, i32 0)
switch i32 %14, label %15 [
i32 0, label %22
i32 -1, label %24
]
15: ; preds = %9
%16 = call i64 @strstr(i8* %5, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%17 = icmp eq i64 %16, 0
%18 = add nsw i32 %14, %10
br i1 %17, label %7, label %19
19: ; preds = %15
%20 = call i32 @memset(i8* %5, i32 0, i32 4097)
%21 = call i32 @send(i32 %1, i8* %5, i32 56, i32 0)
br label %24
22: ; preds = %9, %7
%23 = phi i32 [ -1, %7 ], [ %14, %9 ]
br label %24
24: ; preds = %9, %22, %19
%25 = phi i32 [ 0, %19 ], [ %23, %22 ], [ -1, %9 ]
ret i32 %25
}