#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;
scalar_t__ EINTR;
int SIZE_MAX;
int /*<<< orphan*/ assert(int);
scalar_t__ errno;
size_t read(int const, unsigned char *, size_t);
ssize_t safe_read(const int fd, void *const buf_, size_t size) {
  unsigned char *buf = (unsigned char *)buf_;
  ssize_t readnb;
  assert(size > (size_t)0U);
  assert(size <= (SIZE_MAX / 2 - 1));
  do {
    while ((readnb = read(fd, buf, size)) < (ssize_t)0 &&
           (errno == EINTR || errno == EAGAIN))
      ;
    if (readnb < (ssize_t)0) {
      return readnb;
    }
    if (readnb == (ssize_t)0) {
      break;
    }
    size -= (size_t)readnb;
    buf += readnb;
  } while (size > (ssize_t)0);
  return (ssize_t)(buf - (unsigned char *)buf_);
}


x86 assembly (clang, -O0):
<safe_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0xc(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x20(%rbp)
seta   %al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    0x2e6c(%rip),%rax
mov    (%rax),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
mov    -0x38(%rbp),%rax
sub    $0x1,%ecx
movslq %ecx,%rcx
cmp    %rcx,%rax
setbe  %al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
jmp    1187 <safe_read+0x67>
mov    -0xc(%rbp),%edi
mov    -0x28(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1030 <read@plt>
mov    %rax,%rcx
mov    %rcx,-0x30(%rbp)
xor    %eax,%eax
cmp    $0x0,%rcx
mov    %al,-0x39(%rbp)
jae    11ec <safe_read+0xcc>
mov    0x2e2c(%rip),%rax
mov    (%rax),%rcx
mov    $0x1,%al
mov    0x2e18(%rip),%rdx
cmp    (%rdx),%rcx
mov    %al,-0x3a(%rbp)
je     11e6 <safe_read+0xc6>
mov    0x2e0d(%rip),%rax
mov    (%rax),%rax
mov    0x2de3(%rip),%rcx
cmp    (%rcx),%rax
sete   %al
mov    %al,-0x3a(%rbp)
mov    -0x3a(%rbp),%al
mov    %al,-0x39(%rbp)
mov    -0x39(%rbp),%al
test   $0x1,%al
jne    11fc <safe_read+0xdc>
jmp    1201 <safe_read+0xe1>
jmp    1187 <safe_read+0x67>
cmpq   $0x0,-0x30(%rbp)
jae    1219 <safe_read+0xf9>
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    125e <safe_read+0x13e>
cmpq   $0x0,-0x30(%rbp)
jne    1229 <safe_read+0x109>
jmp    124f <safe_read+0x12f>
mov    -0x30(%rbp),%rcx
mov    -0x20(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x20(%rbp)
mov    -0x30(%rbp),%rax
add    -0x28(%rbp),%rax
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x20(%rbp)
ja     1182 <safe_read+0x62>
mov    -0x28(%rbp),%rax
mov    -0x18(%rbp),%rcx
sub    %rcx,%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<safe_read>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdx,%rbx
mov    %rsi,%r14
mov    %edi,%r15d
xor    %edi,%edi
test   %rdx,%rdx
setne  %dil
call   1040 <assert@plt>
mov    0x2e93(%rip),%rax
mov    (%rax),%eax
mov    %eax,%ecx
shr    $0x1f,%ecx
add    %eax,%ecx
sar    %ecx
add    $0xffffffff,%ecx
movslq %ecx,%rax
xor    %edi,%edi
cmp    %rbx,%rax
setae  %dil
call   1040 <assert@plt>
mov    %r14,%rbp
nopw   0x0(%rax,%rax,1)
mov    %r15d,%edi
mov    %rbp,%rsi
mov    %rbx,%rdx
call   1030 <read@plt>
test   %rax,%rax
je     118b <safe_read+0x6b>
add    %rax,%rbp
sub    %rax,%rbx
jne    1170 <safe_read+0x50>
sub    %r14,%rbp
mov    %rbp,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@SIZE_MAX = global i32 0
@errno = global i64 0
@EINTR = global i64 0
@EAGAIN = global i64 0
define i64 @safe_read(i32 %0, i8* %1, i64 %2) {
%4 = alloca i64
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i8*
%9 = alloca i64
store i32 %0, i32* %5
store i8* %1, i8** %6
store i64 %2, i64* %7
%10 = load i8*, i8** %6
store i8* %10, i8** %8
%11 = load i64, i64* %7
%12 = icmp ugt i64 %11, 0
%13 = zext i1 %12 to i32
%14 = call i32 @assert(i32 %13)
%15 = load i64, i64* %7
%16 = load i32, i32* @SIZE_MAX
%17 = sdiv i32 %16, 2
%18 = sub nsw i32 %17, 1
%19 = sext i32 %18 to i64
%20 = icmp ule i64 %15, %19
%21 = zext i1 %20 to i32
%22 = call i32 @assert(i32 %21)
br label %23
23: ; preds = %59, %3
br label %24
24: ; preds = %42, %23
%25 = load i32, i32* %5
%26 = load i8*, i8** %8
%27 = load i64, i64* %7
%28 = call i64 @read(i32 %25, i8* %26, i64 %27)
store i64 %28, i64* %9
%29 = icmp ult i64 %28, 0
br i1 %29, label %30, label %40
30: ; preds = %24
%31 = load i64, i64* @errno
%32 = load i64, i64* @EINTR
%33 = icmp eq i64 %31, %32
br i1 %33, label %38, label %34
34: ; preds = %30
%35 = load i64, i64* @errno
%36 = load i64, i64* @EAGAIN
%37 = icmp eq i64 %35, %36
br label %38
38: ; preds = %34, %30
%39 = phi i1 [ true, %30 ], [ %37, %34 ]
br label %40
40: ; preds = %38, %24
%41 = phi i1 [ false, %24 ], [ %39, %38 ]
br i1 %41, label %42, label %43
42: ; preds = %40
br label %24
43: ; preds = %40
%44 = load i64, i64* %9
%45 = icmp ult i64 %44, 0
br i1 %45, label %46, label %48
46: ; preds = %43
%47 = load i64, i64* %9
store i64 %47, i64* %4
br label %68
48: ; preds = %43
%49 = load i64, i64* %9
%50 = icmp eq i64 %49, 0
br i1 %50, label %51, label %52
51: ; preds = %48
br label %62
52: ; preds = %48
%53 = load i64, i64* %9
%54 = load i64, i64* %7
%55 = sub i64 %54, %53
store i64 %55, i64* %7
%56 = load i64, i64* %9
%57 = load i8*, i8** %8
%58 = getelementptr inbounds i8, i8* %57, i64 %56
store i8* %58, i8** %8
br label %59
59: ; preds = %52
%60 = load i64, i64* %7
%61 = icmp ugt i64 %60, 0
br i1 %61, label %23, label %62
62: ; preds = %59, %51
%63 = load i8*, i8** %8
%64 = load i8*, i8** %6
%65 = ptrtoint i8* %63 to i64
%66 = ptrtoint i8* %64 to i64
%67 = sub i64 %65, %66
store i64 %67, i64* %4
br label %68
68: ; preds = %62, %46
%69 = load i64, i64* %4
ret i64 %69
}

LLVM IR (-O2):
@SIZE_MAX = global i32 0
@errno = global i64 0
@EINTR = global i64 0
@EAGAIN = global i64 0
define i64 @safe_read(i32 %0, i8* %1, i64 %2) {
%4 = icmp ne i64 %2, 0
%5 = zext i1 %4 to i32
%6 = tail call i32 @assert(i32 %5)
%7 = load i32, i32* @SIZE_MAX
%8 = sdiv i32 %7, 2
%9 = add nsw i32 %8, -1
%10 = sext i32 %9 to i64
%11 = icmp uge i64 %10, %2
%12 = zext i1 %11 to i32
%13 = tail call i32 @assert(i32 %12)
br label %14
14: ; preds = %19, %3
%15 = phi i64 [ %2, %3 ], [ %20, %19 ]
%16 = phi i8* [ %1, %3 ], [ %21, %19 ]
%17 = tail call i64 @read(i32 %0, i8* %16, i64 %15)
%18 = icmp eq i64 %17, 0
br i1 %18, label %23, label %19
19: ; preds = %14
%20 = sub i64 %15, %17
%21 = getelementptr inbounds i8, i8* %16, i64 %17
%22 = icmp eq i64 %20, 0
br i1 %22, label %23, label %14
23: ; preds = %19, %14
%24 = phi i8* [ %16, %14 ], [ %21, %19 ]
%25 = ptrtoint i8* %24 to i64
%26 = ptrtoint i8* %1 to i64
%27 = sub i64 %25, %26
ret i64 %27
}