#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;
scalar_t__ errno;
int read(int, unsigned char *, int);
int read_bytes(int fd, unsigned char *c, int count) {
  int rc;
  int bytes_left = count;
  do {
    rc = read(fd, &c[count - bytes_left], bytes_left);
    if (rc < 0 && errno == EINTR)
      continue;
    if (rc < 0)
      break;
    bytes_left -= rc;
  } while (bytes_left > 0);
  return rc;
}


x86 assembly (clang, -O0):
<read_bytes>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%eax
sub    -0x1c(%rbp),%eax
cltq
add    %rax,%rsi
mov    -0x1c(%rbp),%edx
call   1030 <read@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    116e <read_bytes+0x5e>
mov    0x2e8a(%rip),%rax
mov    (%rax),%rax
mov    0x2e78(%rip),%rcx
cmp    (%rcx),%rax
jne    116e <read_bytes+0x5e>
jmp    1188 <read_bytes+0x78>
cmpl   $0x0,-0x18(%rbp)
jge    117d <read_bytes+0x6d>
jmp    1192 <read_bytes+0x82>
mov    -0x18(%rbp),%ecx
mov    -0x1c(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jg     1128 <read_bytes+0x18>
mov    -0x18(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_bytes>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edx,%ebp
mov    %rsi,%r14
mov    %edi,%r15d
mov    0x2eb6(%rip),%r12
mov    0x2ea7(%rip),%r13
mov    %edx,%ebx
jmp    1146 <read_bytes+0x36>
cs nopw 0x0(%rax,%rax,1)
nop
sub    %eax,%ebx
test   %ebx,%ebx
jle    1168 <read_bytes+0x58>
mov    %ebp,%eax
sub    %ebx,%eax
movslq %eax,%rsi
add    %r14,%rsi
mov    %r15d,%edi
mov    %ebx,%edx
call   1030 <read@plt>
test   %eax,%eax
jns    1140 <read_bytes+0x30>
mov    (%r12),%rcx
cmp    0x0(%r13),%rcx
je     1142 <read_bytes+0x32>
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
define i32 @read_bytes(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i32 %0, i32* %4
store i8* %1, i8** %5
store i32 %2, i32* %6
%9 = load i32, i32* %6
store i32 %9, i32* %8
br label %10
10: ; preds = %35, %3
%11 = load i32, i32* %4
%12 = load i8*, i8** %5
%13 = load i32, i32* %6
%14 = load i32, i32* %8
%15 = sub nsw i32 %13, %14
%16 = sext i32 %15 to i64
%17 = getelementptr inbounds i8, i8* %12, i64 %16
%18 = load i32, i32* %8
%19 = call i32 @read(i32 %11, i8* %17, i32 %18)
store i32 %19, i32* %7
%20 = load i32, i32* %7
%21 = icmp slt i32 %20, 0
br i1 %21, label %22, label %27
22: ; preds = %10
%23 = load i64, i64* @errno
%24 = load i64, i64* @EINTR
%25 = icmp eq i64 %23, %24
br i1 %25, label %26, label %27
26: ; preds = %22
br label %35
27: ; preds = %22, %10
%28 = load i32, i32* %7
%29 = icmp slt i32 %28, 0
br i1 %29, label %30, label %31
30: ; preds = %27
br label %38
31: ; preds = %27
%32 = load i32, i32* %7
%33 = load i32, i32* %8
%34 = sub nsw i32 %33, %32
store i32 %34, i32* %8
br label %35
35: ; preds = %31, %26
%36 = load i32, i32* %8
%37 = icmp sgt i32 %36, 0
br i1 %37, label %10, label %38
38: ; preds = %35, %30
%39 = load i32, i32* %7
ret i32 %39
}

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