#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 /*<<< orphan*/ progressbar_error(char *, int /*<<< orphan*/);
int read(int, void *, int);
int /*<<< orphan*/ strerror(scalar_t__);
int read_bytes(int fd, void *buff, int bytes) {
  int res, count;
  for (count = 0; count < bytes; count += res) {
    res = read(fd, buff + count, bytes - count);
    if (res < 1) {
      if (res == 0)
        goto bytes_read;
      else if (errno != EINTR) {
        do {
          progressbar_error("Read failed because %s\n", strerror(errno));
        } while (0);
        return -1;
      } else
        res = 0;
    }
  }
bytes_read:
  return count;
}


x86 assembly (clang, -O0):
<read_bytes>:
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    -0x1c(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    11f6 <read_bytes+0xc6>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
movslq -0x1c(%rbp),%rax
add    %rax,%rsi
mov    -0x14(%rbp),%edx
sub    -0x1c(%rbp),%edx
call   1030 <read@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x1,-0x18(%rbp)
jge    11e3 <read_bytes+0xb3>
cmpl   $0x0,-0x18(%rbp)
jne    118a <read_bytes+0x5a>
jmp    11fb <read_bytes+0xcb>
mov    0x2e4f(%rip),%rax
mov    (%rax),%rax
mov    0x2e3d(%rip),%rcx
cmp    (%rcx),%rax
je     11d2 <read_bytes+0xa2>
jmp    11a9 <read_bytes+0x79>
mov    0x2e30(%rip),%rax
mov    (%rax),%rdi
call   1050 <strerror@plt>
mov    %eax,%esi
lea    0xe3f(%rip),%rdi
call   1040 <progressbar_error@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1201 <read_bytes+0xd1>
movl   $0x0,-0x18(%rbp)
jmp    11de <read_bytes+0xae>
jmp    11e3 <read_bytes+0xb3>
jmp    11e8 <read_bytes+0xb8>
mov    -0x18(%rbp),%eax
add    -0x1c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
jmp    1149 <read_bytes+0x19>
jmp    11fb <read_bytes+0xcb>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%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
test   %edx,%edx
jle    11a5 <read_bytes+0x75>
mov    %edx,%ebp
mov    %rsi,%r14
mov    %edi,%r15d
xor    %ebx,%ebx
mov    0x2e90(%rip),%r12
mov    0x2e81(%rip),%r13
jmp    1166 <read_bytes+0x36>
nopl   0x0(%rax)
add    %eax,%ebx
cmp    %ebp,%ebx
jge    11a7 <read_bytes+0x77>
movslq %ebx,%rax
lea    (%r14,%rax,1),%rsi
mov    %ebp,%edx
sub    %eax,%edx
mov    %r15d,%edi
call   1030 <read@plt>
test   %eax,%eax
jg     1160 <read_bytes+0x30>
je     11a7 <read_bytes+0x77>
mov    (%r12),%rdi
xor    %eax,%eax
cmp    0x0(%r13),%rdi
je     1160 <read_bytes+0x30>
call   1050 <strerror@plt>
lea    0xe69(%rip),%rdi
mov    %eax,%esi
call   1040 <progressbar_error@plt>
mov    $0xffffffff,%ebx
jmp    11a7 <read_bytes+0x77>
xor    %ebx,%ebx
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
@EINTR = global i64 0
@.str = [24 x i8] c"Read failed because %s\0A\00"
define i32 @read_bytes(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 = %44, %3
%11 = load i32, i32* %9
%12 = load i32, i32* %7
%13 = icmp slt i32 %11, %12
br i1 %13, label %14, label %48
14: ; preds = %10
%15 = load i32, i32* %5
%16 = load i8*, i8** %6
%17 = load i32, i32* %9
%18 = sext i32 %17 to i64
%19 = getelementptr i8, i8* %16, i64 %18
%20 = load i32, i32* %7
%21 = load i32, i32* %9
%22 = sub nsw i32 %20, %21
%23 = call i32 @read(i32 %15, i8* %19, i32 %22)
store i32 %23, i32* %8
%24 = load i32, i32* %8
%25 = icmp slt i32 %24, 1
br i1 %25, label %26, label %43
26: ; preds = %14
%27 = load i32, i32* %8
%28 = icmp eq i32 %27, 0
br i1 %28, label %29, label %30
29: ; preds = %26
br label %49
30: ; preds = %26
%31 = load i64, i64* @errno
%32 = load i64, i64* @EINTR
%33 = icmp ne i64 %31, %32
br i1 %33, label %34, label %40
34: ; preds = %30
br label %35
35: ; preds = %34
%36 = load i64, i64* @errno
%37 = call i32 @strerror(i64 %36)
%38 = call i32 @progressbar_error(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i32 %37)
br label %39
39: ; preds = %35
store i32 -1, i32* %4
br label %51
40: ; preds = %30
store i32 0, i32* %8
br label %41
41: ; preds = %40
br label %42
42: ; preds = %41
br label %43
43: ; preds = %42, %14
br label %44
44: ; preds = %43
%45 = load i32, i32* %8
%46 = load i32, i32* %9
%47 = add nsw i32 %46, %45
store i32 %47, i32* %9
br label %10
48: ; preds = %10
br label %49
49: ; preds = %48, %29
%50 = load i32, i32* %9
store i32 %50, i32* %4
br label %51
51: ; preds = %49, %39
%52 = load i32, i32* %4
ret i32 %52
}

LLVM IR (-O2):
@errno = global i64 0
@EINTR = global i64 0
@.str = [24 x i8] c"Read failed because %s\0A\00"
define i32 @read_bytes(i32 %0, i8* nocapture %1, i32 %2) {
%4 = icmp sgt i32 %2, 0
br i1 %4, label %5, label %25
5: ; preds = %3, %21
%6 = phi i32 [ %23, %21 ], [ 0, %3 ]
%7 = sext i32 %6 to i64
%8 = getelementptr i8, i8* %1, i64 %7
%9 = sub nsw i32 %2, %6
%10 = tail call i32 @read(i32 %0, i8* %8, i32 %9)
%11 = icmp slt i32 %10, 1
br i1 %11, label %12, label %21
12: ; preds = %5
%13 = icmp eq i32 %10, 0
br i1 %13, label %25, label %14
14: ; preds = %12
%15 = load i64, i64* @errno
%16 = load i64, i64* @EINTR
%17 = icmp eq i64 %15, %16
br i1 %17, label %21, label %18
18: ; preds = %14
%19 = tail call i32 @strerror(i64 %15)
%20 = tail call i32 @progressbar_error(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i32 %19)
br label %25
21: ; preds = %14, %5
%22 = phi i32 [ %10, %5 ], [ 0, %14 ]
%23 = add nsw i32 %22, %6
%24 = icmp slt i32 %23, %2
br i1 %24, label %5, label %25
25: ; preds = %21, %12, %3, %18
%26 = phi i32 [ -1, %18 ], [ 0, %3 ], [ %23, %21 ], [ %6, %12 ]
ret i32 %26
}