#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, 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    0xe71(%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    0xe5e(%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    125e <connection_handler+0xee>
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    1200 <connection_handler+0x90>
cmpl   $0x0,-0x10(%rbp)
jne    1287 <connection_handler+0x117>
lea    0xdf0(%rip),%rdi
call   1050 <puts@plt>
mov    0x2d65(%rip),%rax
mov    (%rax),%edi
call   1080 <fflush@plt>
jmp    12a2 <connection_handler+0x132>
cmpl   $0xffffffff,-0x10(%rbp)
jne    129d <connection_handler+0x12d>
lea    0xddb(%rip),%rdi
call   1090 <perror@plt>
jmp    12a2 <connection_handler+0x132>
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   %r14
push   %rbx
sub    $0x7d0,%rsp
mov    %rdi,%r14
mov    (%rdi),%ebp
lea    0xe79(%rip),%rbx
mov    %rbx,%rdi
call   1070 <strlen@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1060 <write@plt>
lea    0xe87(%rip),%rbx
mov    %rbx,%rdi
call   1070 <strlen@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1060 <write@plt>
mov    %rsp,%rsi
mov    %ebp,%edi
mov    $0x7d0,%edx
xor    %ecx,%ecx
call   1040 <recv@plt>
test   %eax,%eax
jle    11f9 <connection_handler+0x89>
mov    %rsp,%rbx
xchg   %ax,%ax
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     11d0 <connection_handler+0x60>
test   %eax,%eax
je     1210 <connection_handler+0xa0>
cmp    $0xffffffff,%eax
jne    122a <connection_handler+0xba>
lea    0xe6a(%rip),%rdi
call   1090 <perror@plt>
jmp    122a <connection_handler+0xba>
lea    0xe48(%rip),%rdi
call   1050 <puts@plt>
mov    0x2dbd(%rip),%rax
mov    (%rax),%edi
call   1080 <fflush@plt>
mov    %r14,%rdi
call   1030 <free@plt>
xor    %eax,%eax
add    $0x7d0,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [41 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 = [20 x i8] c"Client disconnected\00"
@stdout = global i32 0
@.str.3 = [12 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
store i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0), i8** %5
%10 = load i32, i32* %3
%11 = load i8*, i8** %5
%12 = load i8*, i8** %5
%13 = call i32 @strlen(i8* %12)
%14 = call i32 @write(i32 %10, i8* %11, i32 %13)
store i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str.1, i64 0, i64 0), i8** %5
%15 = load i32, i32* %3
%16 = load i8*, i8** %5
%17 = load i8*, i8** %5
%18 = call i32 @strlen(i8* %17)
%19 = call i32 @write(i32 %15, i8* %16, i32 %18)
br label %20
20: ; preds = %25, %1
%21 = load i32, i32* %3
%22 = getelementptr inbounds [2000 x i8], [2000 x i8]* %6, i64 0, i64 0
%23 = call i32 @recv(i32 %21, i8* %22, i32 2000, i32 0)
store i32 %23, i32* %4
%24 = icmp sgt i32 %23, 0
br i1 %24, label %25, label %31
25: ; preds = %20
%26 = load i32, i32* %3
%27 = getelementptr inbounds [2000 x i8], [2000 x i8]* %6, i64 0, i64 0
%28 = getelementptr inbounds [2000 x i8], [2000 x i8]* %6, i64 0, i64 0
%29 = call i32 @strlen(i8* %28)
%30 = call i32 @write(i32 %26, i8* %27, i32 %29)
br label %20
31: ; preds = %20
%32 = load i32, i32* %4
%33 = icmp eq i32 %32, 0
br i1 %33, label %34, label %38
34: ; preds = %31
%35 = call i32 @puts(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0))
%36 = load i32, i32* @stdout
%37 = call i32 @fflush(i32 %36)
br label %44
38: ; preds = %31
%39 = load i32, i32* %4
%40 = icmp eq i32 %39, -1
br i1 %40, label %41, label %43
41: ; preds = %38
%42 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0))
br label %43
43: ; preds = %41, %38
br label %44
44: ; preds = %43, %34
%45 = load i8*, i8** %2
%46 = call i32 @free(i8* %45)
ret i8* null
}

LLVM IR (-O2):
@.str = [41 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 = [20 x i8] c"Client disconnected\00"
@stdout = global i32 0
@.str.3 = [12 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 ([41 x i8], [41 x i8]* @.str, i64 0, i64 0))
%7 = tail call i32 @write(i32 %4, i8* getelementptr inbounds ([41 x i8], [41 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 ([20 x i8], [20 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 ([12 x i8], [12 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
}