#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 */
struct sockaddr {
  int dummy;
};
typedef scalar_t__ ssize_t;
typedef int /*<<< orphan*/ socklen_t;

/* Variables and functions */
int /*<<< orphan*/ NI_MAXHOST;
int /*<<< orphan*/ NI_NUMERICHOST;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
char *gai_strerror(int);
int getnameinfo(struct sockaddr const *, int /*<<< orphan*/, char *,
                int /*<<< orphan*/, int /*<<< orphan*/ *, int /*<<< orphan*/,
                int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
scalar_t__ read(int, char *, int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strlen(char *);
scalar_t__ write(int, char *, int /*<<< orphan*/);
void communication(const struct sockaddr *sa, socklen_t fromlen, char *host,
                   int ns) {
  int s;
  ssize_t nread, nwrite;
  char buf[512];
  char *message = "Message a envoyer: ";
  s = getnameinfo(sa, fromlen, host, NI_MAXHOST, NULL, 0, NI_NUMERICHOST);
  if (s == 0)
    printf("Debut avec client '%s'\n", host);
  else
    fprintf(stderr, "getnameinfo: %s\n", gai_strerror(s));
  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    $0x260,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
lea    0xe50(%rip),%rax
mov    %rax,-0x238(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
mov    -0x18(%rbp),%rdx
mov    0x2e07(%rip),%rax
mov    (%rax),%ecx
mov    0x2e06(%rip),%rax
mov    (%rax),%eax
xor    %r8d,%r8d
xor    %r9d,%r9d
mov    %eax,(%rsp)
call   1080 <getnameinfo@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jne    1204 <communication+0x74>
mov    -0x18(%rbp),%rsi
lea    0xe1a(%rip),%rdi
call   1050 <printf@plt>
jmp    1230 <communication+0xa0>
mov    0x2dd5(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x23c(%rbp)
mov    -0x20(%rbp),%edi
call   1060 <gai_strerror@plt>
mov    -0x23c(%rbp),%edi
mov    %rax,%rdx
lea    0xe01(%rip),%rsi
call   10a0 <fprintf@plt>
jmp    1235 <communication+0xa5>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x24c(%rbp)
mov    -0x238(%rbp),%rax
mov    %rax,-0x248(%rbp)
mov    -0x238(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x24c(%rbp),%edi
mov    -0x248(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jge    1294 <communication+0x104>
lea    0xdbb(%rip),%rdi
call   10b0 <perror@plt>
mov    -0x1c(%rbp),%edi
call   1070 <close@plt>
jmp    1321 <communication+0x191>
mov    -0x1c(%rbp),%edi
lea    -0x230(%rbp),%rsi
mov    $0x200,%edx
call   1090 <read@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    12d4 <communication+0x144>
mov    -0x18(%rbp),%rsi
lea    0xd81(%rip),%rdi
call   1050 <printf@plt>
mov    -0x1c(%rbp),%edi
call   1070 <close@plt>
jmp    1321 <communication+0x191>
cmpq   $0x0,-0x28(%rbp)
jge    12f8 <communication+0x168>
lea    0xd73(%rip),%rdi
call   10b0 <perror@plt>
mov    -0x1c(%rbp),%edi
call   1070 <close@plt>
jmp    1321 <communication+0x191>
jmp    12fd <communication+0x16d>
mov    -0x28(%rbp),%rax
movb   $0x0,-0x230(%rbp,%rax,1)
lea    -0x230(%rbp),%rsi
lea    0xd47(%rip),%rdi
call   1050 <printf@plt>
jmp    1235 <communication+0xa5>
add    $0x260,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<communication>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x210,%rsp
mov    %ecx,%r12d
mov    %rdx,%r14
mov    0x2e24(%rip),%rax
mov    (%rax),%ecx
mov    0x2e23(%rip),%rax
mov    (%rax),%eax
mov    %eax,(%rsp)
xor    %r8d,%r8d
xor    %r9d,%r9d
call   1080 <getnameinfo@plt>
test   %eax,%eax
je     11ec <communication+0x5c>
mov    0x2e10(%rip),%rcx
mov    (%rcx),%ebx
mov    %eax,%edi
call   1060 <gai_strerror@plt>
lea    0xe4c(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   10a0 <fprintf@plt>
jmp    11fb <communication+0x6b>
lea    0xe21(%rip),%rdi
mov    %r14,%rsi
call   1050 <printf@plt>
lea    0xdfe(%rip),%rbx
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    %r12d,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <write@plt>
test   %rax,%rax
js     1271 <communication+0xe1>
lea    0x10(%rsp),%rbx
lea    0xe36(%rip),%r15
lea    0xdd1(%rip),%rbp
nop
mov    %r12d,%edi
mov    %rbx,%rsi
mov    $0x200,%edx
call   1090 <read@plt>
test   %rax,%rax
je     127a <communication+0xea>
js     12a1 <communication+0x111>
movb   $0x0,0x10(%rsp,%rax,1)
mov    %r15,%rdi
mov    %rbx,%rsi
call   1050 <printf@plt>
mov    %rbp,%rdi
call   1040 <strlen@plt>
mov    %r12d,%edi
mov    %rbp,%rsi
mov    %eax,%edx
call   1030 <write@plt>
test   %rax,%rax
jns    1230 <communication+0xa0>
lea    0xdc5(%rip),%rdi
jmp    12a8 <communication+0x118>
lea    0xdc2(%rip),%rdi
mov    %r14,%rsi
call   1050 <printf@plt>
mov    %r12d,%edi
call   1070 <close@plt>
add    $0x210,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdb1(%rip),%rdi
call   10b0 <perror@plt>
jmp    1289 <communication+0xf9>

LLVM IR (-O0):
%struct.sockaddr = type { i32 }
@.str = [20 x i8] c"Message a envoyer: \00"
@NI_MAXHOST = global i32 0
@NI_NUMERICHOST = global i32 0
@.str.1 = [24 x i8] c"Debut avec client '%s'\0A\00"
@stderr = global i32 0
@.str.2 = [17 x i8] c"getnameinfo: %s\0A\00"
@.str.3 = [6 x i8] c"write\00"
@.str.4 = [22 x i8] c"Fin avec client '%s'\0A\00"
@.str.5 = [5 x i8] c"read\00"
@.str.6 = [19 x i8] c"Message recu '%s'\0A\00"
define void @communication(%struct.sockaddr* %0, i32 %1, i8* %2, i32 %3) {
%5 = alloca %struct.sockaddr*
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i64
%11 = alloca i64
%12 = alloca [512 x i8]
%13 = alloca i8*
store %struct.sockaddr* %0, %struct.sockaddr** %5
store i32 %1, i32* %6
store i8* %2, i8** %7
store i32 %3, i32* %8
store i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8** %13
%14 = load %struct.sockaddr*, %struct.sockaddr** %5
%15 = load i32, i32* %6
%16 = load i8*, i8** %7
%17 = load i32, i32* @NI_MAXHOST
%18 = load i32, i32* @NI_NUMERICHOST
%19 = call i32 @getnameinfo(%struct.sockaddr* %14, i32 %15, i8* %16, i32 %17, i32* null, i32 0, i32 %18)
store i32 %19, i32* %9
%20 = load i32, i32* %9
%21 = icmp eq i32 %20, 0
br i1 %21, label %22, label %25
22: ; preds = %4
%23 = load i8*, i8** %7
%24 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i8* %23)
br label %30
25: ; preds = %4
%26 = load i32, i32* @stderr
%27 = load i32, i32* %9
%28 = call i8* @gai_strerror(i32 %27)
%29 = call i32 @fprintf(i32 %26, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i8* %28)
br label %30
30: ; preds = %25, %22
br label %31
31: ; preds = %62, %30
%32 = load i32, i32* %8
%33 = load i8*, i8** %13
%34 = load i8*, i8** %13
%35 = call i32 @strlen(i8* %34)
%36 = call i64 @write(i32 %32, i8* %33, i32 %35)
store i64 %36, i64* %11
%37 = load i64, i64* %11
%38 = icmp slt i64 %37, 0
br i1 %38, label %39, label %43
39: ; preds = %31
%40 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
%41 = load i32, i32* %8
%42 = call i32 @close(i32 %41)
br label %67
43: ; preds = %31
%44 = load i32, i32* %8
%45 = getelementptr inbounds [512 x i8], [512 x i8]* %12, i64 0, i64 0
%46 = call i64 @read(i32 %44, i8* %45, i32 512)
store i64 %46, i64* %10
%47 = load i64, i64* %10
%48 = icmp eq i64 %47, 0
br i1 %48, label %49, label %54
49: ; preds = %43
%50 = load i8*, i8** %7
%51 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0), i8* %50)
%52 = load i32, i32* %8
%53 = call i32 @close(i32 %52)
br label %67
54: ; preds = %43
%55 = load i64, i64* %10
%56 = icmp slt i64 %55, 0
br i1 %56, label %57, label %61
57: ; preds = %54
%58 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0))
%59 = load i32, i32* %8
%60 = call i32 @close(i32 %59)
br label %67
61: ; preds = %54
br label %62
62: ; preds = %61
%63 = load i64, i64* %10
%64 = getelementptr inbounds [512 x i8], [512 x i8]* %12, i64 0, i64 %63
store i8 0, i8* %64
%65 = getelementptr inbounds [512 x i8], [512 x i8]* %12, i64 0, i64 0
%66 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.6, i64 0, i64 0), i8* %65)
br label %31
67: ; preds = %57, %49, %39
ret void
}

LLVM IR (-O2):
%struct.sockaddr = type { i32 }
@.str = [20 x i8] c"Message a envoyer: \00"
@NI_MAXHOST = global i32 0
@NI_NUMERICHOST = global i32 0
@.str.1 = [24 x i8] c"Debut avec client '%s'\0A\00"
@stderr = global i32 0
@.str.2 = [17 x i8] c"getnameinfo: %s\0A\00"
@.str.3 = [6 x i8] c"write\00"
@.str.4 = [22 x i8] c"Fin avec client '%s'\0A\00"
@.str.5 = [5 x i8] c"read\00"
@.str.6 = [19 x i8] c"Message recu '%s'\0A\00"
define void @communication(%struct.sockaddr* %0, i32 %1, i8* %2, i32 %3) {
%5 = alloca [512 x i8]
%6 = getelementptr inbounds [512 x i8], [512 x i8]* %5, i64 0, i64 0
%7 = load i32, i32* @NI_MAXHOST
%8 = load i32, i32* @NI_NUMERICHOST
%9 = tail call i32 @getnameinfo(%struct.sockaddr* %0, i32 %1, i8* %2, i32 %7, i32* null, i32 0, i32 %8)
%10 = icmp eq i32 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %4
%12 = tail call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i8* %2)
br label %17
13: ; preds = %4
%14 = load i32, i32* @stderr
%15 = tail call i8* @gai_strerror(i32 %9)
%16 = tail call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i8* %15)
br label %17
17: ; preds = %13, %11
%18 = call i32 @strlen(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
%19 = call i64 @write(i32 %3, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i32 %18)
%20 = icmp slt i64 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %32, %17
%22 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
br label %38
23: ; preds = %17, %32
%24 = call i64 @read(i32 %3, i8* %6, i32 512)
%25 = icmp eq i64 %24, 0
br i1 %25, label %26, label %28
26: ; preds = %23
%27 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0), i8* %2)
br label %38
28: ; preds = %23
%29 = icmp slt i64 %24, 0
br i1 %29, label %30, label %32
30: ; preds = %28
%31 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0))
br label %38
32: ; preds = %28
%33 = getelementptr inbounds [512 x i8], [512 x i8]* %5, i64 0, i64 %24
store i8 0, i8* %33
%34 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.6, i64 0, i64 0), i8* %6)
%35 = call i32 @strlen(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
%36 = call i64 @write(i32 %3, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i32 %35)
%37 = icmp slt i64 %36, 0
br i1 %37, label %21, label %23
38: ; preds = %30, %26, %21
%39 = call i32 @close(i32 %3)
ret void
}