#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*/ fflush(int /*<<< orphan*/);
int /*<<< orphan*/ free(void *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ puts(char *);
int recv(int, char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ stdout;
int /*<<< orphan*/ strlen(char *);
int /*<<< orphan*/ write(int, char *, int /*<<< orphan*/);
void *connection_handler(void *socket_desc) {
  int sock = *(int *)socket_desc;
  int read_size;
  char *message;
  char client_message[2000] = {};
  message = "Greetings! I am your connection handler.\n";
  write(sock, message, strlen(message));
  message = "Now type something and I shall repeat what you type.\n";
  write(sock, message, strlen(message));
  while ((read_size = recv(sock, client_message, 2000, 0)) > 0) {
    write(sock, client_message, strlen(client_message));
  }
  if (read_size == 0) {
    puts("Client disconnected.");
    fflush(stdout);
  } else if (read_size == -1) {
    perror("recv failed.");
  }
  free(socket_desc);
  return 0;
}


x86 assembly (clang, -O0):
<connection_handler>:
push   %rbp
mov    %rsp,%rbp
sub    $0x820,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0xc(%rbp)
lea    -0x7f0(%rbp),%rdi
xor    %esi,%esi
mov    $0x7d0,%edx
call   1080 <memset@plt>
lea    0xe4e(%rip),%rax
mov    %rax,-0x18(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x80c(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x808(%rbp)
mov    -0x18(%rbp),%rdi
call   1070 <strlen@plt>
mov    -0x80c(%rbp),%edi
mov    -0x808(%rbp),%rsi
mov    %eax,%edx
call   1060 <write@plt>
lea    0xe3c(%rip),%rax
mov    %rax,-0x18(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x7fc(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x7f8(%rbp)
mov    -0x18(%rbp),%rdi
call   1070 <strlen@plt>
mov    -0x7fc(%rbp),%edi
mov    -0x7f8(%rbp),%rsi
mov    %eax,%edx
call   1060 <write@plt>
mov    -0xc(%rbp),%edi
lea    -0x7f0(%rbp),%rsi
mov    $0x7d0,%edx
xor    %ecx,%ecx
call   1040 <recv@plt>
mov    %eax,-0x10(%rbp)
cmp    $0x0,%eax
jle    1281 <connection_handler+0x101>
mov    -0xc(%rbp),%eax
mov    %eax,-0x81c(%rbp)
lea    -0x7f0(%rbp),%rax
mov    %rax,-0x818(%rbp)
lea    -0x7f0(%rbp),%rdi
call   1070 <strlen@plt>
mov    -0x81c(%rbp),%edi
mov    -0x818(%rbp),%rsi
mov    %eax,%edx
call   1060 <write@plt>
jmp    1223 <connection_handler+0xa3>
cmpl   $0x0,-0x10(%rbp)
jne    12aa <connection_handler+0x12a>
lea    0xdce(%rip),%rdi
call   1050 <puts@plt>
mov    0x2d42(%rip),%rax
mov    (%rax),%edi
call   1090 <fflush@plt>
jmp    12c5 <connection_handler+0x145>
cmpl   $0xffffffff,-0x10(%rbp)
jne    12c0 <connection_handler+0x140>
lea    0xdba(%rip),%rdi
call   10a0 <perror@plt>
jmp    12c5 <connection_handler+0x145>
mov    -0x8(%rbp),%rdi
call   1030 <free@plt>
xor    %eax,%eax
add    $0x820,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<connection_handler>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x7d8,%rsp
mov    %rdi,%r14
mov    (%rdi),%ebp
mov    %rsp,%r15
mov    $0x7d0,%edx
mov    %r15,%rdi
xor    %esi,%esi
call   1080 <memset@plt>
lea    0xe55(%rip),%rbx
mov    %rbx,%rdi
call   1070 <strlen@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1060 <write@plt>
lea    0xe64(%rip),%rbx
mov    %rbx,%rdi
call   1070 <strlen@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1060 <write@plt>
mov    %ebp,%edi
mov    %r15,%rsi
mov    $0x7d0,%edx
xor    %ecx,%ecx
call   1040 <recv@plt>
test   %eax,%eax
jle    1229 <connection_handler+0xa9>
mov    %rsp,%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %rbx,%rdi
call   1070 <strlen@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1060 <write@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x7d0,%edx
xor    %ecx,%ecx
call   1040 <recv@plt>
test   %eax,%eax
jg     1200 <connection_handler+0x80>
test   %eax,%eax
je     1240 <connection_handler+0xc0>
cmp    $0xffffffff,%eax
jne    125a <connection_handler+0xda>
lea    0xe3c(%rip),%rdi
call   10a0 <perror@plt>
jmp    125a <connection_handler+0xda>
lea    0xe19(%rip),%rdi
call   1050 <puts@plt>
mov    0x2d8d(%rip),%rax
mov    (%rax),%edi
call   1090 <fflush@plt>
mov    %r14,%rdi
call   1030 <free@plt>
xor    %eax,%eax
add    $0x7d8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [42 x i8] c"Greetings! I am your connection handler.\0A\00"
@.str.1 = [54 x i8] c"Now type something and I shall repeat what you type.\0A\00"
@.str.2 = [21 x i8] c"Client disconnected.\00"
@stdout = global i32 0
@.str.3 = [13 x i8] c"recv failed.\00"
define i8* @connection_handler(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca [2000 x i8]
store i8* %0, i8** %2
%7 = load i8*, i8** %2
%8 = bitcast i8* %7 to i32*
%9 = load i32, i32* %8
store i32 %9, i32* %3
%10 = bitcast [2000 x i8]* %6 to i8*
store i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str, i64 0, i64 0), i8** %5
%11 = load i32, i32* %3
%12 = load i8*, i8** %5
%13 = load i8*, i8** %5
%14 = call i32 @strlen(i8* %13)
%15 = call i32 @write(i32 %11, i8* %12, i32 %14)
store i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str.1, i64 0, i64 0), i8** %5
%16 = load i32, i32* %3
%17 = load i8*, i8** %5
%18 = load i8*, i8** %5
%19 = call i32 @strlen(i8* %18)
%20 = call i32 @write(i32 %16, i8* %17, i32 %19)
br label %21
21: ; preds = %26, %1
%22 = load i32, i32* %3
%23 = getelementptr inbounds [2000 x i8], [2000 x i8]* %6, i64 0, i64 0
%24 = call i32 @recv(i32 %22, i8* %23, i32 2000, i32 0)
store i32 %24, i32* %4
%25 = icmp sgt i32 %24, 0
br i1 %25, label %26, label %32
26: ; preds = %21
%27 = load i32, i32* %3
%28 = getelementptr inbounds [2000 x i8], [2000 x i8]* %6, i64 0, i64 0
%29 = getelementptr inbounds [2000 x i8], [2000 x i8]* %6, i64 0, i64 0
%30 = call i32 @strlen(i8* %29)
%31 = call i32 @write(i32 %27, i8* %28, i32 %30)
br label %21
32: ; preds = %21
%33 = load i32, i32* %4
%34 = icmp eq i32 %33, 0
br i1 %34, label %35, label %39
35: ; preds = %32
%36 = call i32 @puts(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
%37 = load i32, i32* @stdout
%38 = call i32 @fflush(i32 %37)
br label %45
39: ; preds = %32
%40 = load i32, i32* %4
%41 = icmp eq i32 %40, -1
br i1 %41, label %42, label %44
42: ; preds = %39
%43 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
br label %44
44: ; preds = %42, %39
br label %45
45: ; preds = %44, %35
%46 = load i8*, i8** %2
%47 = call i32 @free(i8* %46)
ret i8* null
}

LLVM IR (-O2):
@.str = [42 x i8] c"Greetings! I am your connection handler.\0A\00"
@.str.1 = [54 x i8] c"Now type something and I shall repeat what you type.\0A\00"
@.str.2 = [21 x i8] c"Client disconnected.\00"
@stdout = global i32 0
@.str.3 = [13 x i8] c"recv failed.\00"
define noalias i8* @connection_handler(i8* nocapture %0) {
%2 = alloca [2000 x i8]
%3 = bitcast i8* %0 to i32*
%4 = load i32, i32* %3
%5 = getelementptr inbounds [2000 x i8], [2000 x i8]* %2, i64 0, i64 0
%6 = tail call i32 @strlen(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str, i64 0, i64 0))
%7 = tail call i32 @write(i32 %4, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str, i64 0, i64 0), i32 %6)
%8 = tail call i32 @strlen(i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str.1, i64 0, i64 0))
%9 = tail call i32 @write(i32 %4, i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str.1, i64 0, i64 0), i32 %8)
%10 = call i32 @recv(i32 %4, i8* %5, i32 2000, i32 0)
%11 = icmp sgt i32 %10, 0
br i1 %11, label %12, label %17
12: ; preds = %1, %12
%13 = call i32 @strlen(i8* %5)
%14 = call i32 @write(i32 %4, i8* %5, i32 %13)
%15 = call i32 @recv(i32 %4, i8* %5, i32 2000, i32 0)
%16 = icmp sgt i32 %15, 0
br i1 %16, label %12, label %17
17: ; preds = %12, %1
%18 = phi i32 [ %10, %1 ], [ %15, %12 ]
switch i32 %18, label %25 [
i32 0, label %19
i32 -1, label %23
]
19: ; preds = %17
%20 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
%21 = load i32, i32* @stdout
%22 = call i32 @fflush(i32 %21)
br label %25
23: ; preds = %17
%24 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
br label %25
25: ; preds = %17, %23, %19
%26 = call i32 @free(i8* %0)
ret i8* null
}