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

/* Variables and functions */
scalar_t__ EAGAIN;
scalar_t__ EINTR;
scalar_t__ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
scalar_t__ read(int, char *, size_t);
int /*<<< orphan*/ stderr;
char *strerror(scalar_t__);
size_t read_available(int fd, void *buf, size_t len) {
  size_t pos = 0;
  while (pos < len) {
    ssize_t got = read(fd, (char *)buf + pos, len - pos);
    if (got < 0) {
      if (errno == EINTR)
        continue;
      if (errno == EAGAIN)
        break;
      fprintf(stderr,
              "remotethread"
              " WARNING: "
              "read() failed (%s)\n",
              strerror(errno));
      return 0;
    } else if (got == 0) {
      fprintf(stderr, "remotethread"
                      " WARNING: "
                      "unexpected EOF\n");
      return 0;
    }
    pos += got;
  }
  return pos;
}


x86 assembly (clang, -O0):
<read_available>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0xc(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x20(%rbp),%rax
jae    123f <read_available+0x10f>
mov    -0xc(%rbp),%edi
mov    -0x18(%rbp),%rsi
add    -0x28(%rbp),%rsi
mov    -0x20(%rbp),%rdx
sub    -0x28(%rbp),%rdx
call   1030 <read@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jge    11fa <read_available+0xca>
mov    0x2e51(%rip),%rax
mov    (%rax),%rax
mov    0x2e3f(%rip),%rcx
cmp    (%rcx),%rax
jne    119f <read_available+0x6f>
jmp    114b <read_available+0x1b>
mov    0x2e32(%rip),%rax
mov    (%rax),%rax
mov    0x2e10(%rip),%rcx
cmp    (%rcx),%rax
jne    11be <read_available+0x8e>
jmp    123f <read_available+0x10f>
mov    0x2e1b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    0x2e07(%rip),%rax
mov    (%rax),%rdi
call   1050 <strerror@plt>
mov    -0x34(%rbp),%edi
mov    %rax,%rdx
lea    0xe1a(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
jmp    1247 <read_available+0x117>
cmpq   $0x0,-0x30(%rbp)
jne    1229 <read_available+0xf9>
mov    0x2dd4(%rip),%rax
mov    (%rax),%edi
lea    0xe15(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
jmp    1247 <read_available+0x117>
jmp    122e <read_available+0xfe>
mov    -0x30(%rbp),%rax
add    -0x28(%rbp),%rax
mov    %rax,-0x28(%rbp)
jmp    114b <read_available+0x1b>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_available>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
test   %rdx,%rdx
je     1191 <read_available+0x61>
mov    %rdx,%r14
mov    %rsi,%r15
mov    %edi,%ebp
xor    %ebx,%ebx
mov    0x2e87(%rip),%r12
mov    0x2e78(%rip),%r13
jmp    116f <read_available+0x3f>
nopw   0x0(%rax,%rax,1)
mov    (%r12),%rdi
cmp    0x0(%r13),%rdi
jne    11b0 <read_available+0x80>
cmp    %r14,%rbx
jae    11df <read_available+0xaf>
lea    (%r15,%rbx,1),%rsi
mov    %r14,%rdx
sub    %rbx,%rdx
mov    %ebp,%edi
call   1030 <read@plt>
test   %rax,%rax
js     1160 <read_available+0x30>
je     1195 <read_available+0x65>
add    %rax,%rbx
cmp    %r14,%rbx
jb     116f <read_available+0x3f>
jmp    11df <read_available+0xaf>
xor    %ebx,%ebx
jmp    11df <read_available+0xaf>
mov    0x2e44(%rip),%rax
mov    (%rax),%edi
lea    0xe85(%rip),%rsi
xor    %ebx,%ebx
xor    %eax,%eax
call   1040 <fprintf@plt>
jmp    11df <read_available+0xaf>
mov    0x2e09(%rip),%rax
cmp    (%rax),%rdi
je     11df <read_available+0xaf>
mov    0x2e1d(%rip),%rax
mov    (%rax),%ebp
call   1050 <strerror@plt>
lea    0xe2f(%rip),%rsi
xor    %ebx,%ebx
mov    %ebp,%edi
mov    %rax,%rdx
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    %rbx,%rax
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
@EAGAIN = global i64 0
@stderr = global i32 0
@.str = [42 x i8] c"remotethread WARNING: read() failed (%s)\0A\00"
@.str.1 = [38 x i8] c"remotethread WARNING: unexpected EOF\0A\00"
define i64 @read_available(i32 %0, i8* %1, i64 %2) {
%4 = alloca i64
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i64
%9 = alloca i64
store i32 %0, i32* %5
store i8* %1, i8** %6
store i64 %2, i64* %7
store i64 0, i64* %8
br label %10
10: ; preds = %47, %29, %3
%11 = load i64, i64* %8
%12 = load i64, i64* %7
%13 = icmp ult i64 %11, %12
br i1 %13, label %14, label %51
14: ; preds = %10
%15 = load i32, i32* %5
%16 = load i8*, i8** %6
%17 = load i64, i64* %8
%18 = getelementptr inbounds i8, i8* %16, i64 %17
%19 = load i64, i64* %7
%20 = load i64, i64* %8
%21 = sub i64 %19, %20
%22 = call i64 @read(i32 %15, i8* %18, i64 %21)
store i64 %22, i64* %9
%23 = load i64, i64* %9
%24 = icmp slt i64 %23, 0
br i1 %24, label %25, label %40
25: ; preds = %14
%26 = load i64, i64* @errno
%27 = load i64, i64* @EINTR
%28 = icmp eq i64 %26, %27
br i1 %28, label %29, label %30
29: ; preds = %25
br label %10
30: ; preds = %25
%31 = load i64, i64* @errno
%32 = load i64, i64* @EAGAIN
%33 = icmp eq i64 %31, %32
br i1 %33, label %34, label %35
34: ; preds = %30
br label %51
35: ; preds = %30
%36 = load i32, i32* @stderr
%37 = load i64, i64* @errno
%38 = call i8* @strerror(i64 %37)
%39 = call i32 (i32, i8*, ...) @fprintf(i32 %36, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str, i64 0, i64 0), i8* %38)
store i64 0, i64* %4
br label %53
40: ; preds = %14
%41 = load i64, i64* %9
%42 = icmp eq i64 %41, 0
br i1 %42, label %43, label %46
43: ; preds = %40
%44 = load i32, i32* @stderr
%45 = call i32 (i32, i8*, ...) @fprintf(i32 %44, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.1, i64 0, i64 0))
store i64 0, i64* %4
br label %53
46: ; preds = %40
br label %47
47: ; preds = %46
%48 = load i64, i64* %9
%49 = load i64, i64* %8
%50 = add i64 %49, %48
store i64 %50, i64* %8
br label %10
51: ; preds = %34, %10
%52 = load i64, i64* %8
store i64 %52, i64* %4
br label %53
53: ; preds = %51, %43, %35
%54 = load i64, i64* %4
ret i64 %54
}

LLVM IR (-O2):
@errno = global i64 0
@EINTR = global i64 0
@EAGAIN = global i64 0
@stderr = global i32 0
@.str = [42 x i8] c"remotethread WARNING: read() failed (%s)\0A\00"
@.str.1 = [38 x i8] c"remotethread WARNING: unexpected EOF\0A\00"
define i64 @read_available(i32 %0, i8* nocapture %1, i64 %2) {
%4 = icmp eq i64 %2, 0
br i1 %4, label %32, label %5
5: ; preds = %3, %29
%6 = phi i64 [ %30, %29 ], [ 0, %3 ]
%7 = getelementptr inbounds i8, i8* %1, i64 %6
%8 = sub i64 %2, %6
%9 = tail call i64 @read(i32 %0, i8* %7, i64 %8)
%10 = icmp slt i64 %9, 0
br i1 %10, label %11, label %22
11: ; preds = %5
%12 = load i64, i64* @errno
%13 = load i64, i64* @EINTR
%14 = icmp eq i64 %12, %13
br i1 %14, label %29, label %15
15: ; preds = %11
%16 = load i64, i64* @EAGAIN
%17 = icmp eq i64 %12, %16
br i1 %17, label %32, label %18
18: ; preds = %15
%19 = load i32, i32* @stderr
%20 = tail call i8* @strerror(i64 %12)
%21 = tail call i32 (i32, i8*, ...) @fprintf(i32 %19, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str, i64 0, i64 0), i8* %20)
br label %32
22: ; preds = %5
%23 = icmp eq i64 %9, 0
br i1 %23, label %24, label %27
24: ; preds = %22
%25 = load i32, i32* @stderr
%26 = tail call i32 (i32, i8*, ...) @fprintf(i32 %25, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.1, i64 0, i64 0))
br label %32
27: ; preds = %22
%28 = add i64 %9, %6
br label %29
29: ; preds = %11, %27
%30 = phi i64 [ %28, %27 ], [ %6, %11 ]
%31 = icmp ult i64 %30, %2
br i1 %31, label %5, label %32
32: ; preds = %29, %3, %24, %18, %15
%33 = phi i64 [ %6, %15 ], [ 0, %18 ], [ 0, %24 ], [ 0, %3 ], [ %30, %29 ]
ret i64 %33
}