#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*/ EXIT_FAILURE;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ perror(char *);
int read(int, char *, int);
int /*<<< orphan*/ stderr;
int read_from_client(int filedes) {
  char buffer[512 + 1];
  int nbytes;
  nbytes = read(filedes, buffer, 512);
  if (nbytes < 0) {
    perror("read");
    exit(EXIT_FAILURE);
  } else if (nbytes == 0)
    return -1;
  else {
    if (buffer[nbytes - 1] == '\n')
      buffer[nbytes - 1] = '\0';
    else
      buffer[nbytes] = '\0';
    fprintf(stderr, "Server: got message: `%s'\n", buffer);
    return 0;
  }
}


x86 assembly (clang, -O0):
<read_from_client>:
push   %rbp
mov    %rsp,%rbp
sub    $0x220,%rsp
mov    %edi,-0x8(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x210(%rbp),%rsi
mov    $0x200,%edx
call   1030 <read@plt>
mov    %eax,-0x214(%rbp)
cmpl   $0x0,-0x214(%rbp)
jge    118f <read_from_client+0x4f>
lea    0xe84(%rip),%rdi
call   1050 <perror@plt>
mov    0x2e50(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>
cmpl   $0x0,-0x214(%rbp)
jne    11a8 <read_from_client+0x68>
movl   $0xffffffff,-0x4(%rbp)
jmp    120e <read_from_client+0xce>
mov    -0x214(%rbp),%eax
sub    $0x1,%eax
cltq
movsbl -0x210(%rbp,%rax,1),%eax
ff
cmp    $0xa,%eax
jne    11dc <read_from_client+0x9c>
mov    -0x214(%rbp),%eax
sub    $0x1,%eax
cltq
movb   $0x0,-0x210(%rbp,%rax,1)
jmp    11eb <read_from_client+0xab>
movslq -0x214(%rbp),%rax
movb   $0x0,-0x210(%rbp,%rax,1)
mov    0x2dee(%rip),%rax
mov    (%rax),%edi
lea    -0x210(%rbp),%rdx
lea    0xe03(%rip),%rsi
call   1040 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x220,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_from_client>:
sub    $0x208,%rsp
mov    %rsp,%rsi
mov    $0x200,%edx
call   1030 <read@plt>
test   %eax,%eax
js     119a <read_from_client+0x5a>
je     118d <read_from_client+0x4d>
lea    -0x1(%rax),%ecx
cmpb   $0xa,(%rsp,%rcx,1)
mov    %eax,%eax
cmove  %rcx,%rax
movb   $0x0,(%rsp,%rax,1)
mov    0x2e6e(%rip),%rax
mov    (%rax),%edi
lea    0xe8a(%rip),%rsi
mov    %rsp,%rdx
call   1040 <fprintf@plt>
xor    %eax,%eax
add    $0x208,%rsp
ret
mov    $0xffffffff,%eax
add    $0x208,%rsp
ret
lea    0xe5f(%rip),%rdi
call   1050 <perror@plt>
mov    0x2e2b(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@.str = [5 x i8] c"read\00"
@EXIT_FAILURE = global i32 0
@stderr = global i32 0
@.str.1 = [27 x i8] c"Server: got message: `%s'\0A\00"
define i32 @read_from_client(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca [513 x i8]
%5 = alloca i32
store i32 %0, i32* %3
%6 = load i32, i32* %3
%7 = getelementptr inbounds [513 x i8], [513 x i8]* %4, i64 0, i64 0
%8 = call i32 @read(i32 %6, i8* %7, i32 512)
store i32 %8, i32* %5
%9 = load i32, i32* %5
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %15
11: ; preds = %1
%12 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%13 = load i32, i32* @EXIT_FAILURE
%14 = call i32 @exit(i32 %13)
unreachable
15: ; preds = %1
%16 = load i32, i32* %5
%17 = icmp eq i32 %16, 0
br i1 %17, label %18, label %19
18: ; preds = %15
store i32 -1, i32* %2
br label %40
19: ; preds = %15
%20 = load i32, i32* %5
%21 = sub nsw i32 %20, 1
%22 = sext i32 %21 to i64
%23 = getelementptr inbounds [513 x i8], [513 x i8]* %4, i64 0, i64 %22
%24 = load i8, i8* %23
%25 = sext i8 %24 to i32
%26 = icmp eq i32 %25, 10
br i1 %26, label %27, label %32
27: ; preds = %19
%28 = load i32, i32* %5
%29 = sub nsw i32 %28, 1
%30 = sext i32 %29 to i64
%31 = getelementptr inbounds [513 x i8], [513 x i8]* %4, i64 0, i64 %30
store i8 0, i8* %31
br label %36
32: ; preds = %19
%33 = load i32, i32* %5
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds [513 x i8], [513 x i8]* %4, i64 0, i64 %34
store i8 0, i8* %35
br label %36
36: ; preds = %32, %27
%37 = load i32, i32* @stderr
%38 = getelementptr inbounds [513 x i8], [513 x i8]* %4, i64 0, i64 0
%39 = call i32 @fprintf(i32 %37, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i8* %38)
store i32 0, i32* %2
br label %40
40: ; preds = %36, %18
%41 = load i32, i32* %2
ret i32 %41
}

LLVM IR (-O2):
@.str = [5 x i8] c"read\00"
@EXIT_FAILURE = global i32 0
@stderr = global i32 0
@.str.1 = [27 x i8] c"Server: got message: `%s'\0A\00"
define i32 @read_from_client(i32 %0) {
%2 = alloca [513 x i8]
%3 = getelementptr inbounds [513 x i8], [513 x i8]* %2, i64 0, i64 0
%4 = call i32 @read(i32 %0, i8* %3, i32 512)
%5 = icmp slt i32 %4, 0
br i1 %5, label %6, label %10
6: ; preds = %1
%7 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%8 = load i32, i32* @EXIT_FAILURE
%9 = tail call i32 @exit(i32 %8)
unreachable
10: ; preds = %1
%11 = icmp eq i32 %4, 0
br i1 %11, label %23, label %12
12: ; preds = %10
%13 = add nsw i32 %4, -1
%14 = zext i32 %13 to i64
%15 = getelementptr inbounds [513 x i8], [513 x i8]* %2, i64 0, i64 %14
%16 = load i8, i8* %15
%17 = icmp eq i8 %16, 10
%18 = zext i32 %4 to i64
%19 = getelementptr inbounds [513 x i8], [513 x i8]* %2, i64 0, i64 %18
%20 = select i1 %17, i8* %15, i8* %19
store i8 0, i8* %20
%21 = load i32, i32* @stderr
%22 = call i32 @fprintf(i32 %21, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i8* %3)
br label %23
23: ; preds = %10, %12
%24 = phi i32 [ 0, %12 ], [ -1, %10 ]
ret i32 %24
}