#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 */
scalar_t__ EINTR;
scalar_t__ errno;
int /*<<< orphan*/ perror(char *);
int read(int, void *, unsigned int);
int sread(int fd, void *buf, unsigned int len) {
  ssize_t ret;
  while (len > 0) {
    ret = read(fd, buf, len);
    if (ret == -1) {
      if (errno == EINTR)
        continue;
      perror("Error reading fd");
      return -1;
    } else if (ret == 0) {
      return -1;
    }
    buf += ret;
    len -= ret;
  }
  return 0;
}


x86 assembly (clang, -O0):
<sread>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jbe    11cb <sread+0xab>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1030 <read@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    118f <sread+0x6f>
mov    0x2e81(%rip),%rax
mov    (%rax),%rax
mov    0x2e6f(%rip),%rcx
cmp    (%rcx),%rax
jne    1177 <sread+0x57>
jmp    1132 <sread+0x12>
lea    0xe82(%rip),%rdi
call   1040 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11d2 <sread+0xb2>
cmpl   $0x0,-0x18(%rbp)
jne    11a5 <sread+0x85>
movl   $0xffffffff,-0x4(%rbp)
jmp    11d2 <sread+0xb2>
jmp    11aa <sread+0x8a>
mov    -0x18(%rbp),%ecx
mov    -0x10(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x10(%rbp)
mov    -0x18(%rbp),%ecx
mov    -0x14(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x14(%rbp)
jmp    1132 <sread+0x12>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sread>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
xor    %r14d,%r14d
test   %edx,%edx
je     118e <sread+0x6e>
mov    %edx,%ebp
mov    %rsi,%rbx
mov    %edi,%r15d
mov    0x2e9f(%rip),%r12
mov    0x2e90(%rip),%r13
jmp    115a <sread+0x3a>
nopw   0x0(%rax,%rax,1)
mov    (%r12),%rax
cmp    0x0(%r13),%rax
jne    117c <sread+0x5c>
mov    %r15d,%edi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1030 <read@plt>
cmp    $0xffffffff,%eax
je     1150 <sread+0x30>
test   %eax,%eax
je     1188 <sread+0x68>
movslq %eax,%rcx
add    %rcx,%rbx
sub    %eax,%ebp
jne    115a <sread+0x3a>
jmp    118e <sread+0x6e>
lea    0xe7d(%rip),%rdi
call   1040 <perror@plt>
mov    $0xffffffff,%r14d
mov    %r14d,%eax
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
@.str = [17 x i8] c"Error reading fd\00"
define i32 @sread(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i32 %2, i32* %7
br label %9
9: ; preds = %31, %23, %3
%10 = load i32, i32* %7
%11 = icmp ugt i32 %10, 0
br i1 %11, label %12, label %39
12: ; preds = %9
%13 = load i32, i32* %5
%14 = load i8*, i8** %6
%15 = load i32, i32* %7
%16 = call i32 @read(i32 %13, i8* %14, i32 %15)
store i32 %16, i32* %8
%17 = load i32, i32* %8
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %26
19: ; preds = %12
%20 = load i64, i64* @errno
%21 = load i64, i64* @EINTR
%22 = icmp eq i64 %20, %21
br i1 %22, label %23, label %24
23: ; preds = %19
br label %9
24: ; preds = %19
%25 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %4
br label %40
26: ; preds = %12
%27 = load i32, i32* %8
%28 = icmp eq i32 %27, 0
br i1 %28, label %29, label %30
29: ; preds = %26
store i32 -1, i32* %4
br label %40
30: ; preds = %26
br label %31
31: ; preds = %30
%32 = load i32, i32* %8
%33 = load i8*, i8** %6
%34 = sext i32 %32 to i64
%35 = getelementptr i8, i8* %33, i64 %34
store i8* %35, i8** %6
%36 = load i32, i32* %8
%37 = load i32, i32* %7
%38 = sub i32 %37, %36
store i32 %38, i32* %7
br label %9
39: ; preds = %9
store i32 0, i32* %4
br label %40
40: ; preds = %39, %29, %24
%41 = load i32, i32* %4
ret i32 %41
}

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