#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;
typedef int /*<<< orphan*/ fd_set;

/* Variables and functions */
scalar_t__ EAGAIN;
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
scalar_t__ errno;
int read(int, unsigned char *, size_t);
scalar_t__ select(int, int /*<<< orphan*/ *, int /*<<< orphan*/,
                  int /*<<< orphan*/, int /*<<< orphan*/);
ssize_t io_read_sync(int fd, void *buffer, size_t len) {
  fd_set fds;
  ssize_t r;
  unsigned char *b = buffer;
  while (len > 0) {
    FD_ZERO(&fds);
    FD_SET(fd, &fds);
    if (select(fd + 1, &fds, 0, 0, 0) < 0)
      return -3;
    r = read(fd, b, len);
    if (r < 0 && errno != EAGAIN) {
      return -3;
    } else if (r > 0) {
      b += r;
      len -= r;
    } else {
      break;
    }
  }
  return b - (unsigned char *)buffer;
}


x86 assembly (clang, -O0):
<io_read_sync>:
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,-0x28(%rbp)
cmpq   $0x0,-0x18(%rbp)
jbe    122a <io_read_sync+0xea>
lea    -0x1c(%rbp),%rdi
call   1040 <FD_ZERO@plt>
mov    -0x8(%rbp),%edi
lea    -0x1c(%rbp),%rsi
call   1060 <FD_SET@plt>
mov    -0x8(%rbp),%edi
add    $0x1,%edi
lea    -0x1c(%rbp),%rsi
xor    %r8d,%r8d
mov    %r8d,%edx
mov    %r8d,%ecx
call   1050 <select@plt>
cmp    $0x0,%rax
jge    11a9 <io_read_sync+0x69>
movl   $0xfffffffd,-0x4(%rbp)
jmp    1238 <io_read_sync+0xf8>
mov    -0x8(%rbp),%edi
mov    -0x28(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1030 <read@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    11ec <io_read_sync+0xac>
mov    0x2e13(%rip),%rax
mov    (%rax),%rax
mov    0x2df9(%rip),%rcx
cmp    (%rcx),%rax
je     11ec <io_read_sync+0xac>
movl   $0xfffffffd,-0x4(%rbp)
jmp    1238 <io_read_sync+0xf8>
cmpl   $0x0,-0x20(%rbp)
jle    121b <io_read_sync+0xdb>
mov    -0x20(%rbp),%ecx
mov    -0x28(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x28(%rbp)
movslq -0x20(%rbp),%rcx
mov    -0x18(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x18(%rbp)
jmp    1220 <io_read_sync+0xe0>
jmp    122a <io_read_sync+0xea>
jmp    1225 <io_read_sync+0xe5>
jmp    115b <io_read_sync+0x1b>
mov    -0x28(%rbp),%rax
mov    -0x10(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<io_read_sync>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%r15
mov    %rsi,%rax
mov    %rsi,0x8(%rsp)
test   %rdx,%rdx
je     11e0 <io_read_sync+0xa0>
mov    %rdx,%rbx
mov    %edi,%r12d
lea    0x1(%r12),%r13d
lea    0x14(%rsp),%rbp
mov    0x8(%rsp),%r15
mov    $0xfffffffd,%r14d
nopl   (%rax)
mov    %rbp,%rdi
call   1040 <FD_ZERO@plt>
mov    %r12d,%edi
mov    %rbp,%rsi
call   1060 <FD_SET@plt>
mov    %r13d,%edi
mov    %rbp,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
xor    %r8d,%r8d
call   1050 <select@plt>
test   %rax,%rax
js     11e8 <io_read_sync+0xa8>
mov    %r12d,%edi
mov    %r15,%rsi
mov    %rbx,%rdx
call   1030 <read@plt>
test   %eax,%eax
js     11ca <io_read_sync+0x8a>
je     11e0 <io_read_sync+0xa0>
mov    %eax,%eax
add    %rax,%r15
sub    %rax,%rbx
jne    1180 <io_read_sync+0x40>
jmp    11e0 <io_read_sync+0xa0>
mov    0x2e0f(%rip),%rax
mov    (%rax),%rax
mov    0x2df5(%rip),%rcx
cmp    (%rcx),%rax
jne    11e8 <io_read_sync+0xa8>
sub    0x8(%rsp),%r15d
mov    %r15d,%r14d
mov    %r14d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@errno = global i64 0
@EAGAIN = global i64 0
define i32 @io_read_sync(i32 %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32
%9 = alloca i32
%10 = alloca i8*
store i32 %0, i32* %5
store i8* %1, i8** %6
store i64 %2, i64* %7
%11 = load i8*, i8** %6
store i8* %11, i8** %10
br label %12
12: ; preds = %50, %3
%13 = load i64, i64* %7
%14 = icmp ugt i64 %13, 0
br i1 %14, label %15, label %51
15: ; preds = %12
%16 = call i32 @FD_ZERO(i32* %8)
%17 = load i32, i32* %5
%18 = call i32 @FD_SET(i32 %17, i32* %8)
%19 = load i32, i32* %5
%20 = add nsw i32 %19, 1
%21 = call i64 @select(i32 %20, i32* %8, i32 0, i32 0, i32 0)
%22 = icmp slt i64 %21, 0
br i1 %22, label %23, label %24
23: ; preds = %15
store i32 -3, i32* %4
br label %58
24: ; preds = %15
%25 = load i32, i32* %5
%26 = load i8*, i8** %10
%27 = load i64, i64* %7
%28 = call i32 @read(i32 %25, i8* %26, i64 %27)
store i32 %28, i32* %9
%29 = load i32, i32* %9
%30 = icmp slt i32 %29, 0
br i1 %30, label %31, label %36
31: ; preds = %24
%32 = load i64, i64* @errno
%33 = load i64, i64* @EAGAIN
%34 = icmp ne i64 %32, %33
br i1 %34, label %35, label %36
35: ; preds = %31
store i32 -3, i32* %4
br label %58
36: ; preds = %31, %24
%37 = load i32, i32* %9
%38 = icmp sgt i32 %37, 0
br i1 %38, label %39, label %48
39: ; preds = %36
%40 = load i32, i32* %9
%41 = load i8*, i8** %10
%42 = sext i32 %40 to i64
%43 = getelementptr inbounds i8, i8* %41, i64 %42
store i8* %43, i8** %10
%44 = load i32, i32* %9
%45 = sext i32 %44 to i64
%46 = load i64, i64* %7
%47 = sub i64 %46, %45
store i64 %47, i64* %7
br label %49
48: ; preds = %36
br label %51
49: ; preds = %39
br label %50
50: ; preds = %49
br label %12
51: ; preds = %48, %12
%52 = load i8*, i8** %10
%53 = load i8*, i8** %6
%54 = ptrtoint i8* %52 to i64
%55 = ptrtoint i8* %53 to i64
%56 = sub i64 %54, %55
%57 = trunc i64 %56 to i32
store i32 %57, i32* %4
br label %58
58: ; preds = %51, %35, %23
%59 = load i32, i32* %4
ret i32 %59
}

LLVM IR (-O2):
@errno = global i64 0
@EAGAIN = global i64 0
define i32 @io_read_sync(i32 %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = bitcast i32* %4 to i8*
%6 = icmp eq i64 %2, 0
br i1 %6, label %30, label %7
7: ; preds = %3
%8 = add nsw i32 %0, 1
br label %9
9: ; preds = %7, %25
%10 = phi i8* [ %1, %7 ], [ %27, %25 ]
%11 = phi i64 [ %2, %7 ], [ %28, %25 ]
%12 = call i32 @FD_ZERO(i32* %4)
%13 = call i32 @FD_SET(i32 %0, i32* %4)
%14 = call i64 @select(i32 %8, i32* %4, i32 0, i32 0, i32 0)
%15 = icmp slt i64 %14, 0
br i1 %15, label %36, label %16
16: ; preds = %9
%17 = call i32 @read(i32 %0, i8* %10, i64 %11)
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %23
19: ; preds = %16
%20 = load i64, i64* @errno
%21 = load i64, i64* @EAGAIN
%22 = icmp eq i64 %20, %21
br i1 %22, label %30, label %36
23: ; preds = %16
%24 = icmp eq i32 %17, 0
br i1 %24, label %30, label %25
25: ; preds = %23
%26 = zext i32 %17 to i64
%27 = getelementptr inbounds i8, i8* %10, i64 %26
%28 = sub i64 %11, %26
%29 = icmp eq i64 %28, 0
br i1 %29, label %30, label %9
30: ; preds = %25, %23, %3, %19
%31 = phi i8* [ %10, %19 ], [ %1, %3 ], [ %27, %25 ], [ %10, %23 ]
%32 = ptrtoint i8* %31 to i64
%33 = ptrtoint i8* %1 to i64
%34 = sub i64 %32, %33
%35 = trunc i64 %34 to i32
br label %36
36: ; preds = %9, %19, %30
%37 = phi i32 [ %35, %30 ], [ -3, %19 ], [ -3, %9 ]
ret i32 %37
}