#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 scalar_t__ ssize_t;

/* Variables and functions */
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
scalar_t__ read(int, char *, int);
int /*<<< orphan*/ strlen(char *);
scalar_t__ write(int, char *, int /*<<< orphan*/);
void communication(int ns, char *message, char *buf, char *host) {
  ssize_t nread, nwrite;
  for (;;) {
    nwrite = write(ns, message, strlen(message));
    if (nwrite < 0) {
      perror("write");
      close(ns);
      break;
    }
    nread = read(ns, buf, 512);
    if (nread == 0) {
      printf("Fin avec client '%s'\n", host);
      close(ns);
      break;
    } else if (nread < 0) {
      perror("read");
      close(ns);
      break;
    }
    buf[nread] = '\0';
    printf("Message recu '%s'\n", buf);
  }
}


x86 assembly (clang, -O0):
<communication>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    -0x4(%rbp),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x3c(%rbp),%edi
mov    -0x38(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jge    11c4 <communication+0x64>
lea    0xe4e(%rip),%rdi
call   1080 <perror@plt>
mov    -0x4(%rbp),%edi
call   1060 <close@plt>
jmp    124b <communication+0xeb>
mov    -0x4(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    $0x200,%edx
call   1070 <read@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1201 <communication+0xa1>
mov    -0x20(%rbp),%rsi
lea    0xe17(%rip),%rdi
call   1050 <printf@plt>
mov    -0x4(%rbp),%edi
call   1060 <close@plt>
jmp    124b <communication+0xeb>
cmpq   $0x0,-0x28(%rbp)
jge    1225 <communication+0xc5>
lea    0xe09(%rip),%rdi
call   1080 <perror@plt>
mov    -0x4(%rbp),%edi
call   1060 <close@plt>
jmp    124b <communication+0xeb>
jmp    122a <communication+0xca>
mov    -0x18(%rbp),%rax
mov    -0x28(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%rsi
lea    0xde0(%rip),%rdi
call   1050 <printf@plt>
jmp    1177 <communication+0x17>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<communication>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rcx,%r14
mov    %rdx,%rbx
mov    %rsi,%rbp
mov    %edi,%r15d
mov    %rsi,%rdi
call   1040 <strlen@plt>
mov    %r15d,%edi
mov    %rbp,%rsi
mov    %eax,%edx
call   1030 <write@plt>
test   %rax,%rax
js     11e0 <communication+0x80>
lea    0xe8c(%rip),%r12
cs nopw 0x0(%rax,%rax,1)
nop
mov    %r15d,%edi
mov    %rbx,%rsi
mov    $0x200,%edx
call   1070 <read@plt>
test   %rax,%rax
je     11e9 <communication+0x89>
js     1208 <communication+0xa8>
movb   $0x0,(%rbx,%rax,1)
mov    %r12,%rdi
mov    %rbx,%rsi
call   1050 <printf@plt>
mov    %rbp,%rdi
call   1040 <strlen@plt>
mov    %r15d,%edi
mov    %rbp,%rsi
mov    %eax,%edx
call   1030 <write@plt>
test   %rax,%rax
jns    11a0 <communication+0x40>
lea    0xe19(%rip),%rdi
jmp    120f <communication+0xaf>
lea    0xe16(%rip),%rdi
mov    %r14,%rsi
call   1050 <printf@plt>
mov    %r15d,%edi
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1060 <close@plt>
lea    0xe0d(%rip),%rdi
call   1080 <perror@plt>
jmp    11f8 <communication+0x98>

LLVM IR (-O0):
@.str = [6 x i8] c"write\00"
@.str.1 = [22 x i8] c"Fin avec client '%s'\0A\00"
@.str.2 = [5 x i8] c"read\00"
@.str.3 = [19 x i8] c"Message recu '%s'\0A\00"
define void @communication(i32 %0, i8* %1, i8* %2, i8* %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i64
%10 = alloca i64
store i32 %0, i32* %5
store i8* %1, i8** %6
store i8* %2, i8** %7
store i8* %3, i8** %8
br label %11
11: ; preds = %42, %4
%12 = load i32, i32* %5
%13 = load i8*, i8** %6
%14 = load i8*, i8** %6
%15 = call i32 @strlen(i8* %14)
%16 = call i64 @write(i32 %12, i8* %13, i32 %15)
store i64 %16, i64* %10
%17 = load i64, i64* %10
%18 = icmp slt i64 %17, 0
br i1 %18, label %19, label %23
19: ; preds = %11
%20 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%21 = load i32, i32* %5
%22 = call i32 @close(i32 %21)
br label %48
23: ; preds = %11
%24 = load i32, i32* %5
%25 = load i8*, i8** %7
%26 = call i64 @read(i32 %24, i8* %25, i32 512)
store i64 %26, i64* %9
%27 = load i64, i64* %9
%28 = icmp eq i64 %27, 0
br i1 %28, label %29, label %34
29: ; preds = %23
%30 = load i8*, i8** %8
%31 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i8* %30)
%32 = load i32, i32* %5
%33 = call i32 @close(i32 %32)
br label %48
34: ; preds = %23
%35 = load i64, i64* %9
%36 = icmp slt i64 %35, 0
br i1 %36, label %37, label %41
37: ; preds = %34
%38 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%39 = load i32, i32* %5
%40 = call i32 @close(i32 %39)
br label %48
41: ; preds = %34
br label %42
42: ; preds = %41
%43 = load i8*, i8** %7
%44 = load i64, i64* %9
%45 = getelementptr inbounds i8, i8* %43, i64 %44
store i8 0, i8* %45
%46 = load i8*, i8** %7
%47 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0), i8* %46)
br label %11
48: ; preds = %37, %29, %19
ret void
}

LLVM IR (-O2):
@.str = [6 x i8] c"write\00"
@.str.1 = [22 x i8] c"Fin avec client '%s'\0A\00"
@.str.2 = [5 x i8] c"read\00"
@.str.3 = [19 x i8] c"Message recu '%s'\0A\00"
define void @communication(i32 %0, i8* %1, i8* %2, i8* %3) {
%5 = tail call i32 @strlen(i8* %1)
%6 = tail call i64 @write(i32 %0, i8* %1, i32 %5)
%7 = icmp slt i64 %6, 0
br i1 %7, label %8, label %10
8: ; preds = %19, %4
%9 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
br label %25
10: ; preds = %4, %19
%11 = tail call i64 @read(i32 %0, i8* %2, i32 512)
%12 = icmp eq i64 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %10
%14 = tail call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i8* %3)
br label %25
15: ; preds = %10
%16 = icmp slt i64 %11, 0
br i1 %16, label %17, label %19
17: ; preds = %15
%18 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
br label %25
19: ; preds = %15
%20 = getelementptr inbounds i8, i8* %2, i64 %11
store i8 0, i8* %20
%21 = tail call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0), i8* %2)
%22 = tail call i32 @strlen(i8* %1)
%23 = tail call i64 @write(i32 %0, i8* %1, i32 %22)
%24 = icmp slt i64 %23, 0
br i1 %24, label %8, label %10
25: ; preds = %17, %13, %8
%26 = tail call i32 @close(i32 %0)
ret void
}