#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*/ close(int);
int /*<<< orphan*/ printf(char *, char *);
int read(int, char *, int);
int snprintf(char *, int, char *);
int /*<<< orphan*/ write(int, char *, int);
int connection_handler(int connection_fd) {
  int nbytes;
  char buffer[256];
  nbytes = read(connection_fd, buffer, 256);
  buffer[nbytes] = 0;
  printf("MESSAGE FROM CLIENT: %s\n", buffer);
  nbytes = snprintf(buffer, 256, "hello from the server");
  write(connection_fd, buffer, nbytes);
  close(connection_fd);
  return 0;
}


x86 assembly (clang, -O0):
<connection_handler>:
push   %rbp
mov    %rsp,%rbp
sub    $0x110,%rsp
mov    %edi,-0x4(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x110(%rbp),%rsi
mov    $0x100,%edx
call   1070 <read@plt>
mov    %eax,-0x8(%rbp)
movslq -0x8(%rbp),%rax
movb   $0x0,-0x110(%rbp,%rax,1)
lea    -0x110(%rbp),%rsi
lea    0xe71(%rip),%rdi
call   1040 <printf@plt>
lea    -0x110(%rbp),%rdi
mov    $0x100,%esi
lea    0xe72(%rip),%rdx
call   1050 <snprintf@plt>
mov    %eax,-0x8(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x110(%rbp),%rsi
mov    -0x8(%rbp),%edx
call   1030 <write@plt>
mov    -0x4(%rbp),%edi
call   1060 <close@plt>
xor    %eax,%eax
add    $0x110,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<connection_handler>:
push   %r14
push   %rbx
sub    $0x108,%rsp
mov    %edi,%ebx
mov    %rsp,%r14
mov    %r14,%rsi
mov    $0x100,%edx
call   1070 <read@plt>
cltq
movb   $0x0,(%rsp,%rax,1)
lea    0xe87(%rip),%rdi
mov    %r14,%rsi
call   1040 <printf@plt>
lea    0xe91(%rip),%rdx
mov    %r14,%rdi
mov    $0x100,%esi
call   1050 <snprintf@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    %ebx,%edi
call   1060 <close@plt>
xor    %eax,%eax
add    $0x108,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [25 x i8] c"MESSAGE FROM CLIENT: %s\0A\00"
@.str.1 = [22 x i8] c"hello from the server\00"
define i32 @connection_handler(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca [256 x i8]
store i32 %0, i32* %2
%5 = load i32, i32* %2
%6 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%7 = call i32 @read(i32 %5, i8* %6, i32 256)
store i32 %7, i32* %3
%8 = load i32, i32* %3
%9 = sext i32 %8 to i64
%10 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 %9
store i8 0, i8* %10
%11 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%12 = call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i8* %11)
%13 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%14 = call i32 @snprintf(i8* %13, i32 256, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
store i32 %14, i32* %3
%15 = load i32, i32* %2
%16 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%17 = load i32, i32* %3
%18 = call i32 @write(i32 %15, i8* %16, i32 %17)
%19 = load i32, i32* %2
%20 = call i32 @close(i32 %19)
ret i32 0
}

LLVM IR (-O2):
@.str = [25 x i8] c"MESSAGE FROM CLIENT: %s\0A\00"
@.str.1 = [22 x i8] c"hello from the server\00"
define i32 @connection_handler(i32 %0) {
%2 = alloca [256 x i8]
%3 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%4 = call i32 @read(i32 %0, i8* %3, i32 256)
%5 = sext i32 %4 to i64
%6 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 %5
store i8 0, i8* %6
%7 = call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i8* %3)
%8 = call i32 @snprintf(i8* %3, i32 256, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
%9 = call i32 @write(i32 %0, i8* %3, i32 %8)
%10 = call i32 @close(i32 %0)
ret i32 0
}