#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 EAGAIN;
int /*<<< orphan*/ err(int, char *);
int read(int, void *, size_t);
size_t wrap_read(void *ptr, size_t bytes, int stream) {
  int result = read(stream, ptr, bytes);
  if (result < 0) {
    if (result == EAGAIN) {
      return 0;
    } else {
      err(1, "read");
    }
  }
  return result;
}


x86 assembly (clang, -O0):
<wrap_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    -0x1c(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1030 <read@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    1185 <wrap_read+0x65>
mov    -0x20(%rbp),%eax
mov    0x2e7e(%rip),%rcx
cmp    (%rcx),%eax
jne    116f <wrap_read+0x4f>
movq   $0x0,-0x8(%rbp)
jmp    118d <wrap_read+0x6d>
mov    $0x1,%edi
lea    0xe85(%rip),%rsi
call   1040 <err@plt>
jmp    1185 <wrap_read+0x65>
movslq -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<wrap_read>:
push   %rbx
mov    %edx,%eax
mov    %rsi,%rdx
mov    %rdi,%rsi
mov    %eax,%edi
call   1030 <read@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    1156 <wrap_read+0x36>
mov    0x2e9b(%rip),%rax
cmp    (%rax),%ebx
jne    1145 <wrap_read+0x25>
xor    %eax,%eax
pop    %rbx
ret
lea    0xeb4(%rip),%rsi
mov    $0x1,%edi
call   1040 <err@plt>
movslq %ebx,%rax
pop    %rbx
ret

LLVM IR (-O0):
@EAGAIN = global i32 0
@.str = [5 x i8] c"read\00"
define i64 @wrap_read(i8* %0, i64 %1, i32 %2) {
%4 = alloca i64
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i32
%8 = alloca i32
store i8* %0, i8** %5
store i64 %1, i64* %6
store i32 %2, i32* %7
%9 = load i32, i32* %7
%10 = load i8*, i8** %5
%11 = load i64, i64* %6
%12 = call i32 @read(i32 %9, i8* %10, i64 %11)
store i32 %12, i32* %8
%13 = load i32, i32* %8
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %23
15: ; preds = %3
%16 = load i32, i32* %8
%17 = load i32, i32* @EAGAIN
%18 = icmp eq i32 %16, %17
br i1 %18, label %19, label %20
19: ; preds = %15
store i64 0, i64* %4
br label %26
20: ; preds = %15
%21 = call i32 @err(i32 1, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %22
22: ; preds = %20
br label %23
23: ; preds = %22, %3
%24 = load i32, i32* %8
%25 = sext i32 %24 to i64
store i64 %25, i64* %4
br label %26
26: ; preds = %23, %19
%27 = load i64, i64* %4
ret i64 %27
}

LLVM IR (-O2):
@EAGAIN = global i32 0
@.str = [5 x i8] c"read\00"
define i64 @wrap_read(i8* nocapture %0, i64 %1, i32 %2) {
%4 = tail call i32 @read(i32 %2, i8* %0, i64 %1)
%5 = icmp slt i32 %4, 0
br i1 %5, label %6, label %11
6: ; preds = %3
%7 = load i32, i32* @EAGAIN
%8 = icmp eq i32 %4, %7
br i1 %8, label %13, label %9
9: ; preds = %6
%10 = tail call i32 @err(i32 1, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %11
11: ; preds = %9, %3
%12 = sext i32 %4 to i64
br label %13
13: ; preds = %6, %11
%14 = phi i64 [ %12, %11 ], [ 0, %6 ]
ret i64 %14
}