#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 */
scalar_t__ EINTR;
int /*<<< orphan*/ LOG_ERR;
scalar_t__ errno;
int read(int, void *, int);
int /*<<< orphan*/ syslog(int /*<<< orphan*/, char *, int);
int safe_read(int filedes, void *buffer, int size) {
  for (;;) {
    int p = read(filedes, buffer, size);
    if (p < 0 && errno == EINTR) {
      syslog(LOG_ERR, "EINTR while reading from file handle %d - retrying",
             filedes);
      continue;
    }
    return p;
  }
}


x86 assembly (clang, -O0):
<safe_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1030 <read@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    1185 <safe_read+0x65>
mov    0x2e8b(%rip),%rax
mov    (%rax),%rax
mov    0x2e79(%rip),%rcx
cmp    (%rcx),%rax
jne    1185 <safe_read+0x65>
mov    0x2e59(%rip),%rax
mov    (%rax),%edi
mov    -0x4(%rbp),%edx
lea    0xe85(%rip),%rsi
call   1040 <syslog@plt>
jmp    1132 <safe_read+0x12>
mov    -0x18(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<safe_read>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edx,%r14d
mov    %rsi,%rbx
mov    %edi,%ebp
call   1030 <read@plt>
test   %eax,%eax
jns    118e <safe_read+0x6e>
mov    0x2e9d(%rip),%r12
mov    (%r12),%rcx
mov    0x2e8a(%rip),%r13
cmp    0x0(%r13),%rcx
jne    118e <safe_read+0x6e>
lea    0xea5(%rip),%r15
nopl   0x0(%rax,%rax,1)
mov    0x2e61(%rip),%rax
mov    (%rax),%edi
mov    %r15,%rsi
mov    %ebp,%edx
call   1040 <syslog@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %r14d,%edx
call   1030 <read@plt>
test   %eax,%eax
jns    118e <safe_read+0x6e>
mov    (%r12),%rcx
cmp    0x0(%r13),%rcx
je     1160 <safe_read+0x40>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@errno = global i64 0
@EINTR = global i64 0
@LOG_ERR = global i32 0
@.str = [51 x i8] c"EINTR while reading from file handle %d - retrying\00"
define i32 @safe_read(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
store i32 %0, i32* %4
store i8* %1, i8** %5
store i32 %2, i32* %6
br label %8
8: ; preds = %19, %3
%9 = load i32, i32* %4
%10 = load i8*, i8** %5
%11 = load i32, i32* %6
%12 = call i32 @read(i32 %9, i8* %10, i32 %11)
store i32 %12, i32* %7
%13 = load i32, i32* %7
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %23
15: ; preds = %8
%16 = load i64, i64* @errno
%17 = load i64, i64* @EINTR
%18 = icmp eq i64 %16, %17
br i1 %18, label %19, label %23
19: ; preds = %15
%20 = load i32, i32* @LOG_ERR
%21 = load i32, i32* %4
%22 = call i32 @syslog(i32 %20, i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str, i64 0, i64 0), i32 %21)
br label %8
23: ; preds = %15, %8
%24 = load i32, i32* %7
ret i32 %24
}

LLVM IR (-O2):
@errno = global i64 0
@EINTR = global i64 0
@LOG_ERR = global i32 0
@.str = [51 x i8] c"EINTR while reading from file handle %d - retrying\00"
define i32 @safe_read(i32 %0, i8* nocapture %1, i32 %2) {
%4 = tail call i32 @read(i32 %0, i8* %1, i32 %2)
%5 = icmp slt i32 %4, 0
br i1 %5, label %6, label %19
6: ; preds = %3
%7 = load i64, i64* @errno
%8 = load i64, i64* @EINTR
%9 = icmp eq i64 %7, %8
br i1 %9, label %14, label %19
10: ; preds = %14
%11 = load i64, i64* @errno
%12 = load i64, i64* @EINTR
%13 = icmp eq i64 %11, %12
br i1 %13, label %14, label %19
14: ; preds = %6, %10
%15 = load i32, i32* @LOG_ERR
%16 = tail call i32 @syslog(i32 %15, i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str, i64 0, i64 0), i32 %0)
%17 = tail call i32 @read(i32 %0, i8* %1, i32 %2)
%18 = icmp slt i32 %17, 0
br i1 %18, label %10, label %19
19: ; preds = %10, %14, %6, %3
%20 = phi i32 [ %4, %3 ], [ %4, %6 ], [ %17, %14 ], [ %17, %10 ]
ret i32 %20
}