#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 EAGAIN;
int EINTR;
int errno;
scalar_t__ read(int, char *, size_t);
int ez_net_read(int fd, char *buf, size_t bufsize, ssize_t *nbytes) {
  int ezerrno;
  ssize_t r = read(fd, buf, bufsize);
  if (r == 0) {
    *nbytes = 0;
    ezerrno = errno;
    if (ezerrno == EAGAIN || ezerrno == EINTR)
      return (-EAGAIN);
    else
      return 0;
  } else if (r > 0) {
    *nbytes = r;
    return 0;
  } else {
    *nbytes = 0;
    ezerrno = errno;
    if (ezerrno == EAGAIN || ezerrno == EINTR)
      return (-EAGAIN);
    else
      return -1;
  }
}


x86 assembly (clang, -O0):
<ez_net_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1030 <read@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    11a0 <ez_net_read+0x90>
mov    -0x20(%rbp),%rax
movq   $0x0,(%rax)
mov    0x2e88(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
mov    0x2e61(%rip),%rcx
cmp    (%rcx),%eax
je     1181 <ez_net_read+0x71>
mov    -0x24(%rbp),%eax
mov    0x2e5f(%rip),%rcx
cmp    (%rcx),%eax
jne    1194 <ez_net_read+0x84>
xor    %eax,%eax
mov    0x2e3e(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    1217 <ez_net_read+0x107>
movl   $0x0,-0x4(%rbp)
jmp    1217 <ez_net_read+0x107>
cmpq   $0x0,-0x30(%rbp)
jle    11c2 <ez_net_read+0xb2>
mov    -0x30(%rbp),%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x0,-0x4(%rbp)
jmp    1217 <ez_net_read+0x107>
mov    -0x20(%rbp),%rax
movq   $0x0,(%rax)
mov    0x2e0c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
mov    0x2de5(%rip),%rcx
cmp    (%rcx),%eax
je     11fd <ez_net_read+0xed>
mov    -0x24(%rbp),%eax
mov    0x2de3(%rip),%rcx
cmp    (%rcx),%eax
jne    1210 <ez_net_read+0x100>
xor    %eax,%eax
mov    0x2dc2(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    1217 <ez_net_read+0x107>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ez_net_read>:
push   %rbx
mov    %rcx,%rbx
call   1030 <read@plt>
test   %rax,%rax
je     1127 <ez_net_read+0x17>
jle    1159 <ez_net_read+0x49>
mov    %rax,(%rbx)
xor    %eax,%eax
pop    %rbx
ret
movq   $0x0,(%rbx)
mov    0x2eab(%rip),%rax
mov    (%rax),%ecx
mov    0x2e8a(%rip),%rax
mov    (%rax),%edx
mov    0x2e91(%rip),%rsi
mov    %edx,%edi
neg    %edi
xor    %eax,%eax
cmp    (%rsi),%ecx
cmove  %edi,%eax
cmp    %edx,%ecx
cmove  %edi,%eax
pop    %rbx
ret
movq   $0x0,(%rbx)
mov    0x2e79(%rip),%rax
mov    (%rax),%ecx
mov    0x2e58(%rip),%rax
mov    (%rax),%edx
mov    0x2e5f(%rip),%rax
mov    %edx,%esi
neg    %esi
cmp    (%rax),%ecx
mov    $0xffffffff,%eax
cmove  %esi,%eax
cmp    %edx,%ecx
cmove  %esi,%eax
pop    %rbx
ret

LLVM IR (-O0):
@errno = global i32 0
@EAGAIN = global i32 0
@EINTR = global i32 0
define i32 @ez_net_read(i32 %0, i8* %1, i64 %2, i64* %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i64*
%10 = alloca i32
%11 = alloca i64
store i32 %0, i32* %6
store i8* %1, i8** %7
store i64 %2, i64* %8
store i64* %3, i64** %9
%12 = load i32, i32* %6
%13 = load i8*, i8** %7
%14 = load i64, i64* %8
%15 = call i64 @read(i32 %12, i8* %13, i64 %14)
store i64 %15, i64* %11
%16 = load i64, i64* %11
%17 = icmp eq i64 %16, 0
br i1 %17, label %18, label %32
18: ; preds = %4
%19 = load i64*, i64** %9
store i64 0, i64* %19
%20 = load i32, i32* @errno
store i32 %20, i32* %10
%21 = load i32, i32* %10
%22 = load i32, i32* @EAGAIN
%23 = icmp eq i32 %21, %22
br i1 %23, label %28, label %24
24: ; preds = %18
%25 = load i32, i32* %10
%26 = load i32, i32* @EINTR
%27 = icmp eq i32 %25, %26
br i1 %27, label %28, label %31
28: ; preds = %24, %18
%29 = load i32, i32* @EAGAIN
%30 = sub nsw i32 0, %29
store i32 %30, i32* %5
br label %52
31: ; preds = %24
store i32 0, i32* %5
br label %52
32: ; preds = %4
%33 = load i64, i64* %11
%34 = icmp sgt i64 %33, 0
br i1 %34, label %35, label %38
35: ; preds = %32
%36 = load i64, i64* %11
%37 = load i64*, i64** %9
store i64 %36, i64* %37
store i32 0, i32* %5
br label %52
38: ; preds = %32
%39 = load i64*, i64** %9
store i64 0, i64* %39
%40 = load i32, i32* @errno
store i32 %40, i32* %10
%41 = load i32, i32* %10
%42 = load i32, i32* @EAGAIN
%43 = icmp eq i32 %41, %42
br i1 %43, label %48, label %44
44: ; preds = %38
%45 = load i32, i32* %10
%46 = load i32, i32* @EINTR
%47 = icmp eq i32 %45, %46
br i1 %47, label %48, label %51
48: ; preds = %44, %38
%49 = load i32, i32* @EAGAIN
%50 = sub nsw i32 0, %49
store i32 %50, i32* %5
br label %52
51: ; preds = %44
store i32 -1, i32* %5
br label %52
52: ; preds = %51, %48, %35, %31, %28
%53 = load i32, i32* %5
ret i32 %53
}

LLVM IR (-O2):
@errno = global i32 0
@EAGAIN = global i32 0
@EINTR = global i32 0
define i32 @ez_net_read(i32 %0, i8* nocapture %1, i64 %2, i64* nocapture writeonly %3) {
%5 = tail call i64 @read(i32 %0, i8* %1, i64 %2)
%6 = icmp eq i64 %5, 0
br i1 %6, label %7, label %16
7: ; preds = %4
store i64 0, i64* %3
%8 = load i32, i32* @errno
%9 = load i32, i32* @EAGAIN
%10 = icmp eq i32 %8, %9
%11 = load i32, i32* @EINTR
%12 = icmp eq i32 %8, %11
%13 = select i1 %10, i1 true, i1 %12
%14 = sub nsw i32 0, %9
%15 = select i1 %13, i32 %14, i32 0
br label %28
16: ; preds = %4
%17 = icmp sgt i64 %5, 0
br i1 %17, label %18, label %19
18: ; preds = %16
store i64 %5, i64* %3
br label %28
19: ; preds = %16
store i64 0, i64* %3
%20 = load i32, i32* @errno
%21 = load i32, i32* @EAGAIN
%22 = icmp eq i32 %20, %21
%23 = load i32, i32* @EINTR
%24 = icmp eq i32 %20, %23
%25 = select i1 %22, i1 true, i1 %24
%26 = sub nsw i32 0, %21
%27 = select i1 %25, i32 %26, i32 -1
br label %28
28: ; preds = %19, %7, %18
%29 = phi i32 [ 0, %18 ], [ %15, %7 ], [ %27, %19 ]
ret i32 %29
}