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

/* Variables and functions */
int /*<<< orphan*/ LOG_ERR;
int read(int, char *, size_t);
int /*<<< orphan*/ syslog(int /*<<< orphan*/, char *);
int safe_read(int fd, void *pdata, size_t size) {
  char *data = (char *)pdata;
  size_t sizeleft = size;
  while (sizeleft > 0) {
    ssize_t sizedone = read(fd, data, sizeleft);
    if (sizedone == -1) {
      syslog(LOG_ERR, "safe_read(): %m\n");
      return 0;
    }
    if (sizedone == 0)
      return 0;
    sizeleft -= sizedone;
    data += sizedone;
  }
  return 1;
}


x86 assembly (clang, -O0):
<safe_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jbe    11c7 <safe_read+0xa7>
mov    -0x8(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   1030 <read@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0xffffffff,-0x2c(%rbp)
jne    118c <safe_read+0x6c>
mov    0x2e66(%rip),%rax
mov    (%rax),%edi
lea    0xe85(%rip),%rsi
call   1040 <syslog@plt>
movl   $0x0,-0x4(%rbp)
jmp    11ce <safe_read+0xae>
cmpl   $0x0,-0x2c(%rbp)
jne    11a2 <safe_read+0x82>
movl   $0x0,-0x4(%rbp)
jmp    11ce <safe_read+0xae>
movslq -0x2c(%rbp),%rcx
mov    -0x28(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x28(%rbp)
mov    -0x2c(%rbp),%ecx
mov    -0x20(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x20(%rbp)
jmp    1143 <safe_read+0x23>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<safe_read>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    $0x1,%r14d
test   %rdx,%rdx
je     1180 <safe_read+0x60>
mov    %rdx,%rbx
mov    %rsi,%rbp
mov    %edi,%r15d
nopl   0x0(%rax,%rax,1)
mov    %r15d,%edi
mov    %rbp,%rsi
mov    %rbx,%rdx
call   1030 <read@plt>
test   %eax,%eax
je     1163 <safe_read+0x43>
cmp    $0xffffffff,%eax
je     1168 <safe_read+0x48>
cltq
add    %rax,%rbp
sub    %rax,%rbx
jne    1140 <safe_read+0x20>
jmp    1180 <safe_read+0x60>
mov    %eax,%r14d
jmp    1180 <safe_read+0x60>
mov    0x2e69(%rip),%rax
mov    (%rax),%edi
lea    0xe88(%rip),%rsi
call   1040 <syslog@plt>
xor    %r14d,%r14d
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@LOG_ERR = global i32 0
@.str = [17 x i8] c"safe_read(): %m\0A\00"
define i32 @safe_read(i32 %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i8*
%9 = alloca i64
%10 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i64 %2, i64* %7
%11 = load i8*, i8** %6
store i8* %11, i8** %8
%12 = load i64, i64* %7
store i64 %12, i64* %9
br label %13
13: ; preds = %30, %3
%14 = load i64, i64* %9
%15 = icmp ugt i64 %14, 0
br i1 %15, label %16, label %39
16: ; preds = %13
%17 = load i32, i32* %5
%18 = load i8*, i8** %8
%19 = load i64, i64* %9
%20 = call i32 @read(i32 %17, i8* %18, i64 %19)
store i32 %20, i32* %10
%21 = load i32, i32* %10
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %26
23: ; preds = %16
%24 = load i32, i32* @LOG_ERR
%25 = call i32 @syslog(i32 %24, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %4
br label %40
26: ; preds = %16
%27 = load i32, i32* %10
%28 = icmp eq i32 %27, 0
br i1 %28, label %29, label %30
29: ; preds = %26
store i32 0, i32* %4
br label %40
30: ; preds = %26
%31 = load i32, i32* %10
%32 = sext i32 %31 to i64
%33 = load i64, i64* %9
%34 = sub i64 %33, %32
store i64 %34, i64* %9
%35 = load i32, i32* %10
%36 = load i8*, i8** %8
%37 = sext i32 %35 to i64
%38 = getelementptr inbounds i8, i8* %36, i64 %37
store i8* %38, i8** %8
br label %13
39: ; preds = %13
store i32 1, i32* %4
br label %40
40: ; preds = %39, %29, %23
%41 = load i32, i32* %4
ret i32 %41
}

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