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

/* Variables and functions */
scalar_t__ EAGAIN;
int /*<<< orphan*/ close(int);
scalar_t__ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ perror(char *);
size_t read(int, char *, int);
int /*<<< orphan*/ stderr;
int handle_client(int client) {
  char buffer[1024];
  ssize_t amount = read(client, buffer, 1023);
  if (amount < 0 && errno == EAGAIN) {
    return 0;
  }
  if (amount < 0) {
    perror("read from client");
    return -1;
  } else if (amount == 0) {
    close(client);
    return -1;
  }
  buffer[amount] = 0;
  fprintf(stderr, "%s\n", buffer);
  return 0;
}


x86 assembly (clang, -O0):
<handle_client>:
push   %rbp
mov    %rsp,%rbp
sub    $0x420,%rsp
mov    %edi,-0x8(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x410(%rbp),%rsi
mov    $0x3ff,%edx
call   1040 <read@plt>
mov    %rax,-0x418(%rbp)
cmpq   $0x0,-0x418(%rbp)
jae    119d <handle_client+0x5d>
mov    0x2e5a(%rip),%rax
mov    (%rax),%rax
mov    0x2e40(%rip),%rcx
cmp    (%rcx),%rax
jne    119d <handle_client+0x5d>
movl   $0x0,-0x4(%rbp)
jmp    121c <handle_client+0xdc>
cmpq   $0x0,-0x418(%rbp)
jae    11c3 <handle_client+0x83>
lea    0xe4e(%rip),%rdi
call   1060 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    121c <handle_client+0xdc>
cmpq   $0x0,-0x418(%rbp)
jne    11e5 <handle_client+0xa5>
mov    -0x8(%rbp),%edi
call   1030 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    121c <handle_client+0xdc>
jmp    11ea <handle_client+0xaa>
mov    -0x418(%rbp),%rax
movb   $0x0,-0x410(%rbp,%rax,1)
mov    0x2de0(%rip),%rax
mov    (%rax),%edi
lea    -0x410(%rbp),%rdx
lea    0xe01(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x420,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<handle_client>:
push   %rbx
sub    $0x400,%rsp
mov    %edi,%ebx
mov    %rsp,%rsi
mov    $0x3ff,%edx
call   1040 <read@plt>
test   %rax,%rax
je     116c <handle_client+0x3c>
movb   $0x0,(%rsp,%rax,1)
mov    0x2e89(%rip),%rax
mov    (%rax),%edi
lea    0xea0(%rip),%rsi
mov    %rsp,%rdx
call   1050 <fprintf@plt>
xor    %eax,%eax
jmp    1178 <handle_client+0x48>
mov    %ebx,%edi
call   1030 <close@plt>
mov    $0xffffffff,%eax
add    $0x400,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@errno = global i64 0
@EAGAIN = global i64 0
@.str = [17 x i8] c"read from client\00"
@stderr = global i32 0
@.str.1 = [4 x i8] c"%s\0A\00"
define i32 @handle_client(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca [1024 x i8]
%5 = alloca i64
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 i64 @read(i32 %6, i8* %7, i32 1023)
store i64 %8, i64* %5
%9 = load i64, i64* %5
%10 = icmp ult i64 %9, 0
br i1 %10, label %11, label %16
11: ; preds = %1
%12 = load i64, i64* @errno
%13 = load i64, i64* @EAGAIN
%14 = icmp eq i64 %12, %13
br i1 %14, label %15, label %16
15: ; preds = %11
store i32 0, i32* %2
br label %34
16: ; preds = %11, %1
%17 = load i64, i64* %5
%18 = icmp ult i64 %17, 0
br i1 %18, label %19, label %21
19: ; preds = %16
%20 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %2
br label %34
21: ; preds = %16
%22 = load i64, i64* %5
%23 = icmp eq i64 %22, 0
br i1 %23, label %24, label %27
24: ; preds = %21
%25 = load i32, i32* %3
%26 = call i32 @close(i32 %25)
store i32 -1, i32* %2
br label %34
27: ; preds = %21
br label %28
28: ; preds = %27
%29 = load i64, i64* %5
%30 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 %29
store i8 0, i8* %30
%31 = load i32, i32* @stderr
%32 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%33 = call i32 @fprintf(i32 %31, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* %32)
store i32 0, i32* %2
br label %34
34: ; preds = %28, %24, %19, %15
%35 = load i32, i32* %2
ret i32 %35
}

LLVM IR (-O2):
@errno = global i64 0
@EAGAIN = global i64 0
@stderr = global i32 0
@.str.1 = [4 x i8] c"%s\0A\00"
define i32 @handle_client(i32 %0) {
%2 = alloca [1024 x i8]
%3 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%4 = call i64 @read(i32 %0, i8* %3, i32 1023)
%5 = icmp eq i64 %4, 0
br i1 %5, label %6, label %8
6: ; preds = %1
%7 = tail call i32 @close(i32 %0)
br label %12
8: ; preds = %1
%9 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 %4
store i8 0, i8* %9
%10 = load i32, i32* @stderr
%11 = call i32 @fprintf(i32 %10, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* %3)
br label %12
12: ; preds = %8, %6
%13 = phi i32 [ -1, %6 ], [ 0, %8 ]
ret i32 %13
}