#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 int /*<<< orphan*/ ssize_t;

/* Variables and functions */
scalar_t__ EINTR;
scalar_t__ errno;
int /*<<< orphan*/ read(int const, unsigned char *const, size_t const);
ssize_t safe_read_partial(const int fd, void *const buf_,
                          const size_t max_count) {
  unsigned char *const buf = (unsigned char *)buf_;
  ssize_t readnb;
  while ((readnb = read(fd, buf, max_count)) < (ssize_t)0 && errno == EINTR)
    ;
  return readnb;
}


x86 assembly (clang, -O0):
<safe_read_partial>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1030 <read@plt>
mov    %eax,%ecx
mov    %ecx,-0x24(%rbp)
xor    %eax,%eax
cmp    $0x0,%ecx
mov    %al,-0x25(%rbp)
jge    1168 <safe_read_partial+0x58>
mov    0x2e8b(%rip),%rax
mov    (%rax),%rax
mov    0x2e79(%rip),%rcx
cmp    (%rcx),%rax
sete   %al
mov    %al,-0x25(%rbp)
mov    -0x25(%rbp),%al
test   $0x1,%al
jne    1178 <safe_read_partial+0x68>
jmp    117d <safe_read_partial+0x6d>
jmp    112b <safe_read_partial+0x1b>
mov    -0x24(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<safe_read_partial>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rdx,%r14
mov    %rsi,%rbx
mov    %edi,%ebp
mov    0x2eb9(%rip),%r15
mov    0x2eaa(%rip),%r12
xchg   %ax,%ax
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %r14,%rdx
call   1030 <read@plt>
test   %eax,%eax
jns    114a <safe_read_partial+0x3a>
mov    (%r12),%rcx
cmp    %rcx,(%r15)
je     1130 <safe_read_partial+0x20>
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@errno = global i64 0
@EINTR = global i64 0
define i32 @safe_read_partial(i32 %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i8*
%8 = alloca i32
store i32 %0, i32* %4
store i8* %1, i8** %5
store i64 %2, i64* %6
%9 = load i8*, i8** %5
store i8* %9, i8** %7
br label %10
10: ; preds = %22, %3
%11 = load i32, i32* %4
%12 = load i8*, i8** %7
%13 = load i64, i64* %6
%14 = call i32 @read(i32 %11, i8* %12, i64 %13)
store i32 %14, i32* %8
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %20
16: ; preds = %10
%17 = load i64, i64* @errno
%18 = load i64, i64* @EINTR
%19 = icmp eq i64 %17, %18
br label %20
20: ; preds = %16, %10
%21 = phi i1 [ false, %10 ], [ %19, %16 ]
br i1 %21, label %22, label %23
22: ; preds = %20
br label %10
23: ; preds = %20
%24 = load i32, i32* %8
ret i32 %24
}

LLVM IR (-O2):
@errno = global i64 0
@EINTR = global i64 0
define i32 @safe_read_partial(i32 %0, i8* nocapture %1, i64 %2) {
br label %4
4: ; preds = %4, %3
%5 = tail call i32 @read(i32 %0, i8* %1, i64 %2)
%6 = icmp slt i32 %5, 0
%7 = load i64, i64* @errno
%8 = load i64, i64* @EINTR
%9 = icmp eq i64 %7, %8
%10 = select i1 %6, i1 %9, i1 false
br i1 %10, label %4, label %11
11: ; preds = %4
ret i32 %5
}