#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 */
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ puts(char *);
int read(int, unsigned char *, int);
int read_bytes(int fd, int n, unsigned char *b) {
  int t = 0, l;
  while (t < n) {
    l = read(fd, b + t, n - t);
    if (l <= 0) {
      if (l == -1)
        perror("read");
      puts("read_bytes(): error reading data");
      return -1;
    }
    t += l;
  }
  return t;
}


x86 assembly (clang, -O0):
<read_bytes>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    11b7 <read_bytes+0x87>
mov    -0x8(%rbp),%edi
mov    -0x18(%rbp),%rsi
movslq -0x1c(%rbp),%rax
add    %rax,%rsi
mov    -0xc(%rbp),%edx
sub    -0x1c(%rbp),%edx
call   1040 <read@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jg     11a9 <read_bytes+0x79>
cmpl   $0xffffffff,-0x20(%rbp)
jne    1191 <read_bytes+0x61>
lea    0xe74(%rip),%rdi
call   1050 <perror@plt>
lea    0xe6d(%rip),%rdi
call   1030 <puts@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11bd <read_bytes+0x8d>
mov    -0x20(%rbp),%eax
add    -0x1c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
jmp    1149 <read_bytes+0x19>
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   %rbx
push   %rax
test   %esi,%esi
jle    116f <read_bytes+0x3f>
mov    %rdx,%r14
mov    %esi,%ebp
mov    %edi,%r15d
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nop
movslq %ebx,%rax
lea    (%r14,%rax,1),%rsi
mov    %ebp,%edx
sub    %eax,%edx
mov    %r15d,%edi
call   1040 <read@plt>
test   %eax,%eax
jle    1173 <read_bytes+0x43>
add    %eax,%ebx
cmp    %ebp,%ebx
jl     1150 <read_bytes+0x20>
jmp    1189 <read_bytes+0x59>
xor    %ebx,%ebx
jmp    1189 <read_bytes+0x59>
cmp    $0xffffffff,%eax
je     1196 <read_bytes+0x66>
lea    0xe86(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe63(%rip),%rdi
call   1050 <perror@plt>
jmp    1178 <read_bytes+0x48>

LLVM IR (-O0):
@.str = [5 x i8] c"read\00"
@.str.1 = [33 x i8] c"read_bytes(): error reading data\00"
define i32 @read_bytes(i32 %0, i32 %1, i8* %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
store i32 %0, i32* %5
store i32 %1, i32* %6
store i8* %2, i8** %7
store i32 0, i32* %8
br label %10
10: ; preds = %33, %3
%11 = load i32, i32* %8
%12 = load i32, i32* %6
%13 = icmp slt i32 %11, %12
br i1 %13, label %14, label %37
14: ; preds = %10
%15 = load i32, i32* %5
%16 = load i8*, i8** %7
%17 = load i32, i32* %8
%18 = sext i32 %17 to i64
%19 = getelementptr inbounds i8, i8* %16, i64 %18
%20 = load i32, i32* %6
%21 = load i32, i32* %8
%22 = sub nsw i32 %20, %21
%23 = call i32 @read(i32 %15, i8* %19, i32 %22)
store i32 %23, i32* %9
%24 = load i32, i32* %9
%25 = icmp sle i32 %24, 0
br i1 %25, label %26, label %33
26: ; preds = %14
%27 = load i32, i32* %9
%28 = icmp eq i32 %27, -1
br i1 %28, label %29, label %31
29: ; preds = %26
%30 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %31
31: ; preds = %29, %26
%32 = call i32 @puts(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %4
br label %39
33: ; preds = %14
%34 = load i32, i32* %9
%35 = load i32, i32* %8
%36 = add nsw i32 %35, %34
store i32 %36, i32* %8
br label %10
37: ; preds = %10
%38 = load i32, i32* %8
store i32 %38, i32* %4
br label %39
39: ; preds = %37, %31
%40 = load i32, i32* %4
ret i32 %40
}

LLVM IR (-O2):
@.str = [5 x i8] c"read\00"
@.str.1 = [33 x i8] c"read_bytes(): error reading data\00"
define i32 @read_bytes(i32 %0, i32 %1, i8* nocapture %2) {
%4 = icmp sgt i32 %1, 0
br i1 %4, label %5, label %21
5: ; preds = %3, %18
%6 = phi i32 [ %19, %18 ], [ 0, %3 ]
%7 = sext i32 %6 to i64
%8 = getelementptr inbounds i8, i8* %2, i64 %7
%9 = sub nsw i32 %1, %6
%10 = tail call i32 @read(i32 %0, i8* %8, i32 %9)
%11 = icmp slt i32 %10, 1
br i1 %11, label %12, label %18
12: ; preds = %5
%13 = icmp eq i32 %10, -1
br i1 %13, label %14, label %16
14: ; preds = %12
%15 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %16
16: ; preds = %14, %12
%17 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0))
br label %21
18: ; preds = %5
%19 = add nsw i32 %10, %6
%20 = icmp slt i32 %19, %1
br i1 %20, label %5, label %21
21: ; preds = %18, %3, %16
%22 = phi i32 [ -1, %16 ], [ 0, %3 ], [ %19, %18 ]
ret i32 %22
}