#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*/ errno;
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
scalar_t__ malloc(int);
int /*<<< orphan*/ printf(char *);
int recv(int, char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ send(int, char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ stdout;
char *strerror(int /*<<< orphan*/);
void *echo_server(void *conf) {
  int *socket, msg_len;
  char *buffer;
  buffer = (char *)malloc(1024);
  socket = (int *)conf;
  msg_len = 1;
  while (msg_len > 0) {
    msg_len = recv(*socket, (char *)buffer, 1024 - 1, 0);
    if (msg_len < 0) {
      printf(strerror(errno));
      fflush(stdout);
      return NULL;
    }
    send(*socket, (char *)buffer, msg_len, 0);
    if (buffer[1] == '\n') {
      printf("Closing client connection. Received empty Message");
      fflush(stdout);
      msg_len = 0;
    }
  }
  return (void *)buffer;
}


x86 assembly (clang, -O0):
<echo_server>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    $0x400,%edi
call   1060 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x18(%rbp)
movl   $0x1,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jle    1232 <echo_server+0xd2>
mov    -0x18(%rbp),%rax
mov    (%rax),%edi
mov    -0x28(%rbp),%rsi
mov    $0x3ff,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    11e7 <echo_server+0x87>
mov    0x2e23(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    %rax,%rdi
call   1050 <printf@plt>
mov    0x2e05(%rip),%rax
mov    (%rax),%edi
call   1070 <fflush@plt>
movq   $0x0,-0x8(%rbp)
jmp    123a <echo_server+0xda>
mov    -0x18(%rbp),%rax
mov    (%rax),%edi
mov    -0x28(%rbp),%rsi
mov    -0x1c(%rbp),%edx
xor    %ecx,%ecx
call   1040 <send@plt>
mov    -0x28(%rbp),%rax
movsbl 0x1(%rax),%eax
cmp    $0xa,%eax
jne    122d <echo_server+0xcd>
lea    0xded(%rip),%rdi
call   1050 <printf@plt>
mov    0x2db9(%rip),%rax
mov    (%rax),%edi
call   1070 <fflush@plt>
movl   $0x0,-0x1c(%rbp)
jmp    1189 <echo_server+0x29>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<echo_server>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r15
mov    $0x400,%edi
call   1060 <malloc@plt>
mov    %rax,%rbx
lea    0xe82(%rip),%r14
xchg   %ax,%ax
mov    (%r15),%edi
mov    %rbx,%rsi
mov    $0x3ff,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
test   %eax,%eax
js     11b3 <echo_server+0x53>
mov    %eax,%ebp
mov    (%r15),%edi
mov    %rbx,%rsi
mov    %eax,%edx
xor    %ecx,%ecx
call   1040 <send@plt>
cmpb   $0xa,0x1(%rbx)
je     11c6 <echo_server+0x66>
test   %ebp,%ebp
jne    1180 <echo_server+0x20>
jmp    11dc <echo_server+0x7c>
mov    0x2e26(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    %rax,%r14
xor    %ebx,%ebx
mov    %r14,%rdi
call   1050 <printf@plt>
mov    0x2e03(%rip),%rax
mov    (%rax),%edi
call   1070 <fflush@plt>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@errno = global i32 0
@stdout = global i32 0
@.str = [50 x i8] c"Closing client connection. Received empty Message\00"
define i8* @echo_server(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca i32
%6 = alloca i8*
store i8* %0, i8** %3
%7 = call i64 @malloc(i32 1024)
%8 = inttoptr i64 %7 to i8*
store i8* %8, i8** %6
%9 = load i8*, i8** %3
%10 = bitcast i8* %9 to i32*
store i32* %10, i32** %4
store i32 1, i32* %5
br label %11
11: ; preds = %42, %1
%12 = load i32, i32* %5
%13 = icmp sgt i32 %12, 0
br i1 %13, label %14, label %43
14: ; preds = %11
%15 = load i32*, i32** %4
%16 = load i32, i32* %15
%17 = load i8*, i8** %6
%18 = call i32 @recv(i32 %16, i8* %17, i32 1023, i32 0)
store i32 %18, i32* %5
%19 = load i32, i32* %5
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %27
21: ; preds = %14
%22 = load i32, i32* @errno
%23 = call i8* @strerror(i32 %22)
%24 = call i32 @printf(i8* %23)
%25 = load i32, i32* @stdout
%26 = call i32 @fflush(i32 %25)
store i8* null, i8** %2
br label %45
27: ; preds = %14
%28 = load i32*, i32** %4
%29 = load i32, i32* %28
%30 = load i8*, i8** %6
%31 = load i32, i32* %5
%32 = call i32 @send(i32 %29, i8* %30, i32 %31, i32 0)
%33 = load i8*, i8** %6
%34 = getelementptr inbounds i8, i8* %33, i64 1
%35 = load i8, i8* %34
%36 = sext i8 %35 to i32
%37 = icmp eq i32 %36, 10
br i1 %37, label %38, label %42
38: ; preds = %27
%39 = call i32 @printf(i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str, i64 0, i64 0))
%40 = load i32, i32* @stdout
%41 = call i32 @fflush(i32 %40)
store i32 0, i32* %5
br label %42
42: ; preds = %38, %27
br label %11
43: ; preds = %11
%44 = load i8*, i8** %6
store i8* %44, i8** %2
br label %45
45: ; preds = %43, %21
%46 = load i8*, i8** %2
ret i8* %46
}

LLVM IR (-O2):
@errno = global i32 0
@stdout = global i32 0
@.str = [50 x i8] c"Closing client connection. Received empty Message\00"
define i8* @echo_server(i8* nocapture %0) {
%2 = tail call i64 @malloc(i32 1024)
%3 = inttoptr i64 %2 to i8*
%4 = bitcast i8* %0 to i32*
%5 = getelementptr inbounds i8, i8* %3, i64 1
br label %6
6: ; preds = %1, %18
%7 = load i32, i32* %4
%8 = tail call i32 @recv(i32 %7, i8* %3, i32 1023, i32 0)
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %13
10: ; preds = %6
%11 = load i32, i32* @errno
%12 = tail call i8* @strerror(i32 %11)
br label %20
13: ; preds = %6
%14 = load i32, i32* %4
%15 = tail call i32 @send(i32 %14, i8* %3, i32 %8, i32 0)
%16 = load i8, i8* %5
%17 = icmp eq i8 %16, 10
br i1 %17, label %20, label %18
18: ; preds = %13
%19 = icmp eq i32 %8, 0
br i1 %19, label %26, label %6
20: ; preds = %13, %10
%21 = phi i8* [ %12, %10 ], [ getelementptr inbounds ([50 x i8], [50 x i8]* @.str, i64 0, i64 0), %13 ]
%22 = phi i8* [ null, %10 ], [ %3, %13 ]
%23 = tail call i32 @printf(i8* dereferenceable(1) %21)
%24 = load i32, i32* @stdout
%25 = tail call i32 @fflush(i32 %24)
br label %26
26: ; preds = %18, %20
%27 = phi i8* [ %22, %20 ], [ %3, %18 ]
ret i8* %27
}