#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*/ fwrite(char *, int, int, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int read(int, char *, int);
int /*<<< orphan*/ stdout;
int read_from_client(int filedes) {
  char buffer[1024];
  int nbytes;
  nbytes = read(filedes, buffer, 1024);
  if (nbytes < 0) {
    perror("read");
    exit(EXIT_FAILURE);
  } else if (nbytes == 0)
    return -1;
  else {
    fwrite(buffer, nbytes, 1, stdout);
    return 0;
  }
}


x86 assembly (clang, -O0):
<read_from_client>:
push   %rbp
mov    %rsp,%rbp
sub    $0x420,%rsp
mov    %edi,-0x8(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x410(%rbp),%rsi
mov    $0x400,%edx
call   1030 <read@plt>
mov    %eax,-0x414(%rbp)
cmpl   $0x0,-0x414(%rbp)
jge    118f <read_from_client+0x4f>
lea    0xe84(%rip),%rdi
call   1040 <perror@plt>
mov    0x2e58(%rip),%rax
mov    (%rax),%edi
call   1050 <exit@plt>
cmpl   $0x0,-0x414(%rbp)
jne    11a8 <read_from_client+0x68>
movl   $0xffffffff,-0x4(%rbp)
jmp    11cf <read_from_client+0x8f>
lea    -0x410(%rbp),%rdi
mov    -0x414(%rbp),%esi
mov    0x2e1c(%rip),%rax
mov    (%rax),%ecx
mov    $0x1,%edx
call   1060 <fwrite@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x420,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_from_client>:
sub    $0x408,%rsp
mov    %rsp,%rsi
mov    $0x400,%edx
call   1030 <read@plt>
test   %eax,%eax
js     1189 <read_from_client+0x49>
je     117c <read_from_client+0x3c>
mov    0x2e77(%rip),%rcx
mov    (%rcx),%ecx
mov    %rsp,%rdi
mov    %eax,%esi
mov    $0x1,%edx
call   1060 <fwrite@plt>
xor    %eax,%eax
add    $0x408,%rsp
ret
mov    $0xffffffff,%eax
add    $0x408,%rsp
ret
lea    0xe70(%rip),%rdi
call   1040 <perror@plt>
mov    0x2e44(%rip),%rax
mov    (%rax),%edi
call   1050 <exit@plt>

LLVM IR (-O0):
@.str = [5 x i8] c"read\00"
@EXIT_FAILURE = global i32 0
@stdout = global i32 0
define i32 @read_from_client(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca [1024 x i8]
%5 = alloca i32
store i32 %0, i32* %3
%6 = load i32, i32* %3
%7 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%8 = call i32 @read(i32 %6, i8* %7, i32 1024)
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 %24
19: ; preds = %15
%20 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%21 = load i32, i32* %5
%22 = load i32, i32* @stdout
%23 = call i32 @fwrite(i8* %20, i32 %21, i32 1, i32 %22)
store i32 0, i32* %2
br label %24
24: ; preds = %19, %18
%25 = load i32, i32* %2
ret i32 %25
}

LLVM IR (-O2):
@.str = [5 x i8] c"read\00"
@EXIT_FAILURE = global i32 0
@stdout = global i32 0
define i32 @read_from_client(i32 %0) {
%2 = alloca [1024 x i8]
%3 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%4 = call i32 @read(i32 %0, i8* %3, i32 1024)
%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 %15, label %12
12: ; preds = %10
%13 = load i32, i32* @stdout
%14 = call i32 @fwrite(i8* %3, i32 %4, i32 1, i32 %13)
br label %15
15: ; preds = %10, %12
%16 = phi i32 [ 0, %12 ], [ -1, %10 ]
ret i32 %16
}