#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__ ECONNRESET;
scalar_t__ EINTR;
scalar_t__ errno;
int read(int, unsigned char *, int);
int read_fill(int fd, unsigned char *buf, int len) {
  int i;
  unsigned int got = 0;
  do {
    if ((i = read(fd, buf + got, len - got)) <= 0) {
      if (i == 0 || (i < 0 && errno == ECONNRESET)) {
        return -2;
      }
      if (errno != EINTR) {
        return -1;
      }
      i = 0;
    }
    got += i;
  } while (got < len);
  return len;
}


x86 assembly (clang, -O0):
<read_fill>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x1c(%rbp),%eax
add    %rax,%rsi
mov    -0x14(%rbp),%edx
sub    -0x1c(%rbp),%edx
call   1030 <read@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jg     11b4 <read_fill+0xa4>
cmpl   $0x0,-0x18(%rbp)
je     117b <read_fill+0x6b>
cmpl   $0x0,-0x18(%rbp)
jge    1187 <read_fill+0x77>
mov    0x2e70(%rip),%rax
mov    (%rax),%rax
mov    0x2e6e(%rip),%rcx
cmp    (%rcx),%rax
jne    1187 <read_fill+0x77>
movl   $0xfffffffe,-0x4(%rbp)
jmp    11cf <read_fill+0xbf>
mov    0x2e4a(%rip),%rax
mov    (%rax),%rax
mov    0x2e38(%rip),%rcx
cmp    (%rcx),%rax
je     11ad <read_fill+0x9d>
movl   $0xffffffff,-0x4(%rbp)
jmp    11cf <read_fill+0xbf>
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
add    -0x1c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x14(%rbp),%eax
jb     1129 <read_fill+0x19>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_fill>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edx,%ebx
mov    %rsi,%r14
mov    %edi,%r15d
xor    %ebp,%ebp
mov    0x2eac(%rip),%r12
mov    0x2ead(%rip),%r13
jmp    1146 <read_fill+0x36>
cs nopw 0x0(%rax,%rax,1)
nop
add    %eax,%ebp
cmp    %ebx,%ebp
jae    1181 <read_fill+0x71>
mov    %ebp,%esi
add    %r14,%rsi
mov    %ebx,%edx
sub    %ebp,%edx
mov    %r15d,%edi
call   1030 <read@plt>
test   %eax,%eax
jg     1140 <read_fill+0x30>
mov    $0xfffffffe,%eax
je     1183 <read_fill+0x73>
mov    (%r12),%rcx
cmp    0x0(%r13),%rcx
je     1183 <read_fill+0x73>
xor    %eax,%eax
mov    0x2e5b(%rip),%rdx
cmp    (%rdx),%rcx
je     1140 <read_fill+0x30>
mov    $0xffffffff,%eax
jmp    1183 <read_fill+0x73>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@errno = global i64 0
@ECONNRESET = global i64 0
@EINTR = global i64 0
define i32 @read_fill(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i32 %2, i32* %7
store i32 0, i32* %9
br label %10
10: ; preds = %42, %3
%11 = load i32, i32* %5
%12 = load i8*, i8** %6
%13 = load i32, i32* %9
%14 = zext i32 %13 to i64
%15 = getelementptr inbounds i8, i8* %12, i64 %14
%16 = load i32, i32* %7
%17 = load i32, i32* %9
%18 = sub i32 %16, %17
%19 = call i32 @read(i32 %11, i8* %15, i32 %18)
store i32 %19, i32* %8
%20 = icmp sle i32 %19, 0
br i1 %20, label %21, label %38
21: ; preds = %10
%22 = load i32, i32* %8
%23 = icmp eq i32 %22, 0
br i1 %23, label %31, label %24
24: ; preds = %21
%25 = load i32, i32* %8
%26 = icmp slt i32 %25, 0
br i1 %26, label %27, label %32
27: ; preds = %24
%28 = load i64, i64* @errno
%29 = load i64, i64* @ECONNRESET
%30 = icmp eq i64 %28, %29
br i1 %30, label %31, label %32
31: ; preds = %27, %21
store i32 -2, i32* %4
br label %48
32: ; preds = %27, %24
%33 = load i64, i64* @errno
%34 = load i64, i64* @EINTR
%35 = icmp ne i64 %33, %34
br i1 %35, label %36, label %37
36: ; preds = %32
store i32 -1, i32* %4
br label %48
37: ; preds = %32
store i32 0, i32* %8
br label %38
38: ; preds = %37, %10
%39 = load i32, i32* %8
%40 = load i32, i32* %9
%41 = add i32 %40, %39
store i32 %41, i32* %9
br label %42
42: ; preds = %38
%43 = load i32, i32* %9
%44 = load i32, i32* %7
%45 = icmp ult i32 %43, %44
br i1 %45, label %10, label %46
46: ; preds = %42
%47 = load i32, i32* %7
store i32 %47, i32* %4
br label %48
48: ; preds = %46, %36, %31
%49 = load i32, i32* %4
ret i32 %49
}

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