#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 int /*<<< orphan*/ echo_msg;

/* Variables and functions */
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ free(void *);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int recv(int, char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ snprintf(char *, int, char *, char *, char *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdout;
int /*<<< orphan*/ strlen(char const *);
int /*<<< orphan*/ write(int, char const *, int /*<<< orphan*/);
void *connection_handler(void *socket_desc) {
  int sock = *(int *)socket_desc;
  char client_msg[1024];
  int read_size = 0;
  const char *message = "server waiting for your msg:\n";
  write(sock, message, strlen(message));
  while ((read_size = recv(sock, client_msg, 1024, 0)) > 0) {
    char echo_msg[1024] = {0};
    snprintf(echo_msg, sizeof(echo_msg), "%s: %s\n", "echo", client_msg);
    echo_msg[1024 - 1] = '\0';
    write(sock, echo_msg, strlen(echo_msg));
    memset(client_msg, 0, 1024);
  }
  if (read_size == 0) {
    fprintf(stdout, "client disconnected.\n");
    fflush(stdout);
  } else if (read_size == -1) {
    fprintf(stderr, "recv failure.\n");
  }
  free(socket_desc);
  return 0;
}


x86 assembly (clang, -O0):
<connection_handler>:
push   %rbp
mov    %rsp,%rbp
sub    $0x840,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0xc(%rbp)
movl   $0x0,-0x414(%rbp)
lea    0xe57(%rip),%rax
mov    %rax,-0x420(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x82c(%rbp)
mov    -0x420(%rbp),%rax
mov    %rax,-0x828(%rbp)
mov    -0x420(%rbp),%rdi
call   1060 <strlen@plt>
mov    -0x82c(%rbp),%edi
mov    -0x828(%rbp),%rsi
mov    %eax,%edx
call   1050 <write@plt>
mov    -0xc(%rbp),%edi
lea    -0x410(%rbp),%rsi
mov    $0x400,%edx
xor    %ecx,%ecx
call   1040 <recv@plt>
mov    %eax,-0x414(%rbp)
cmp    $0x0,%eax
jle    129b <connection_handler+0x11b>
lea    -0x820(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1080 <memset@plt>
lea    -0x820(%rbp),%rdi
lea    -0x410(%rbp),%r8
mov    $0x400,%esi
lea    0xde5(%rip),%rdx
lea    0xde6(%rip),%rcx
call   1070 <snprintf@plt>
movb   $0x0,-0x421(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x83c(%rbp)
lea    -0x820(%rbp),%rax
mov    %rax,-0x838(%rbp)
lea    -0x820(%rbp),%rdi
call   1060 <strlen@plt>
mov    -0x83c(%rbp),%edi
mov    -0x838(%rbp),%rsi
mov    %eax,%edx
call   1050 <write@plt>
lea    -0x410(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1080 <memset@plt>
jmp    11e7 <connection_handler+0x67>
cmpl   $0x0,-0x414(%rbp)
jne    12d0 <connection_handler+0x150>
mov    0x2d29(%rip),%rax
mov    (%rax),%edi
lea    0xd73(%rip),%rsi
call   1090 <fprintf@plt>
mov    0x2d14(%rip),%rax
mov    (%rax),%edi
call   10a0 <fflush@plt>
jmp    12f7 <connection_handler+0x177>
cmpl   $0xffffffff,-0x414(%rbp)
jne    12f2 <connection_handler+0x172>
mov    0x2cfc(%rip),%rax
mov    (%rax),%edi
lea    0xd54(%rip),%rsi
call   1090 <fprintf@plt>
jmp    12f7 <connection_handler+0x177>
mov    -0x8(%rbp),%rdi
call   1030 <free@plt>
xor    %eax,%eax
add    $0x840,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<connection_handler>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x808,%rsp
mov    %rdi,%r14
mov    (%rdi),%r13d
lea    0xe62(%rip),%rbx
mov    %rbx,%rdi
call   1060 <strlen@plt>
mov    %r13d,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1050 <write@plt>
lea    0x400(%rsp),%rsi
mov    %r13d,%edi
mov    $0x400,%edx
xor    %ecx,%ecx
call   1040 <recv@plt>
test   %eax,%eax
jle    1257 <connection_handler+0xd7>
mov    %rsp,%rbx
lea    0xe42(%rip),%r15
lea    0xe43(%rip),%r12
lea    0x400(%rsp),%rbp
nopl   0x0(%rax,%rax,1)
mov    $0x400,%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1080 <memset@plt>
mov    %rbx,%rdi
mov    $0x400,%esi
mov    %r15,%rdx
mov    %r12,%rcx
mov    %rbp,%r8
call   1070 <snprintf@plt>
movb   $0x0,0x3ff(%rsp)
mov    %rbx,%rdi
call   1060 <strlen@plt>
mov    %r13d,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1050 <write@plt>
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1080 <memset@plt>
mov    %r13d,%edi
mov    %rbp,%rsi
mov    $0x400,%edx
xor    %ecx,%ecx
call   1040 <recv@plt>
test   %eax,%eax
jg     11f0 <connection_handler+0x70>
cmp    $0xffffffff,%eax
je     127e <connection_handler+0xfe>
test   %eax,%eax
jne    1293 <connection_handler+0x113>
mov    0x2d71(%rip),%rbx
mov    (%rbx),%edi
lea    0xdbb(%rip),%rsi
call   1090 <fprintf@plt>
mov    (%rbx),%edi
call   10a0 <fflush@plt>
jmp    1293 <connection_handler+0x113>
mov    0x2d5b(%rip),%rax
mov    (%rax),%edi
lea    0xdb3(%rip),%rsi
call   1090 <fprintf@plt>
mov    %r14,%rdi
call   1030 <free@plt>
xor    %eax,%eax
add    $0x808,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [30 x i8] c"server waiting for your msg:\0A\00"
@.str.1 = [8 x i8] c"%s: %s\0A\00"
@.str.2 = [5 x i8] c"echo\00"
@stdout = global i32 0
@.str.3 = [22 x i8] c"client disconnected.\0A\00"
@stderr = global i32 0
@.str.4 = [15 x i8] c"recv failure.\0A\00"
define i8* @connection_handler(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca [1024 x i8]
%5 = alloca i32
%6 = alloca i8*
%7 = alloca [1024 x i8]
store i8* %0, i8** %2
%8 = load i8*, i8** %2
%9 = bitcast i8* %8 to i32*
%10 = load i32, i32* %9
store i32 %10, i32* %3
store i32 0, i32* %5
store i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i8** %6
%11 = load i32, i32* %3
%12 = load i8*, i8** %6
%13 = load i8*, i8** %6
%14 = call i32 @strlen(i8* %13)
%15 = call i32 @write(i32 %11, i8* %12, i32 %14)
br label %16
16: ; preds = %21, %1
%17 = load i32, i32* %3
%18 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%19 = call i32 @recv(i32 %17, i8* %18, i32 1024, i32 0)
store i32 %19, i32* %5
%20 = icmp sgt i32 %19, 0
br i1 %20, label %21, label %34
21: ; preds = %16
%22 = bitcast [1024 x i8]* %7 to i8*
%23 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%24 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%25 = call i32 @snprintf(i8* %23, i32 1024, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i8* %24)
%26 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 1023
store i8 0, i8* %26
%27 = load i32, i32* %3
%28 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%29 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%30 = call i32 @strlen(i8* %29)
%31 = call i32 @write(i32 %27, i8* %28, i32 %30)
%32 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%33 = call i32 @memset(i8* %32, i32 0, i32 1024)
br label %16
34: ; preds = %16
%35 = load i32, i32* %5
%36 = icmp eq i32 %35, 0
br i1 %36, label %37, label %42
37: ; preds = %34
%38 = load i32, i32* @stdout
%39 = call i32 @fprintf(i32 %38, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0))
%40 = load i32, i32* @stdout
%41 = call i32 @fflush(i32 %40)
br label %49
42: ; preds = %34
%43 = load i32, i32* %5
%44 = icmp eq i32 %43, -1
br i1 %44, label %45, label %48
45: ; preds = %42
%46 = load i32, i32* @stderr
%47 = call i32 @fprintf(i32 %46, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0))
br label %48
48: ; preds = %45, %42
br label %49
49: ; preds = %48, %37
%50 = load i8*, i8** %2
%51 = call i32 @free(i8* %50)
ret i8* null
}

LLVM IR (-O2):
@.str = [30 x i8] c"server waiting for your msg:\0A\00"
@.str.1 = [8 x i8] c"%s: %s\0A\00"
@.str.2 = [5 x i8] c"echo\00"
@stdout = global i32 0
@.str.3 = [22 x i8] c"client disconnected.\0A\00"
@stderr = global i32 0
@.str.4 = [15 x i8] c"recv failure.\0A\00"
define noalias i8* @connection_handler(i8* nocapture %0) {
%2 = alloca [1024 x i8]
%3 = alloca [1024 x i8]
%4 = bitcast i8* %0 to i32*
%5 = load i32, i32* %4
%6 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%7 = tail call i32 @strlen(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0))
%8 = tail call i32 @write(i32 %5, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i32 %7)
%9 = call i32 @recv(i32 %5, i8* %6, i32 1024, i32 0)
%10 = icmp sgt i32 %9, 0
br i1 %10, label %11, label %21
11: ; preds = %1
%12 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%13 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 1023
br label %14
14: ; preds = %11, %14
%15 = call i32 @snprintf(i8* %12, i32 1024, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i8* %6)
store i8 0, i8* %13
%16 = call i32 @strlen(i8* %12)
%17 = call i32 @write(i32 %5, i8* %12, i32 %16)
%18 = call i32 @memset(i8* %6, i32 0, i32 1024)
%19 = call i32 @recv(i32 %5, i8* %6, i32 1024, i32 0)
%20 = icmp sgt i32 %19, 0
br i1 %20, label %14, label %21
21: ; preds = %14, %1
%22 = phi i32 [ %9, %1 ], [ %19, %14 ]
switch i32 %22, label %31 [
i32 0, label %23
i32 -1, label %28
]
23: ; preds = %21
%24 = load i32, i32* @stdout
%25 = call i32 @fprintf(i32 %24, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0))
%26 = load i32, i32* @stdout
%27 = call i32 @fflush(i32 %26)
br label %31
28: ; preds = %21
%29 = load i32, i32* @stderr
%30 = call i32 @fprintf(i32 %29, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0))
br label %31
31: ; preds = %21, %28, %23
%32 = call i32 @free(i8* %0)
ret i8* null
}