#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 /*<<< orphan*/ uint8_t;

/* Variables and functions */
int /*<<< orphan*/ perror(char *);
int read(int, int /*<<< orphan*/ *, int);
int read_n(int fd, uint8_t *buffer, int len) {
  int cnt = 0;
  while (len) {
    int ret = read(fd, buffer, len);
    if (ret < 0) {
      perror("input read()");
      return ret;
    }
    if (ret == 0)
      return cnt;
    cnt += ret;
    buffer += ret;
    len -= ret;
  }
  return cnt;
}


x86 assembly (clang, -O0):
<read_n>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     11b9 <read_n+0x99>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1030 <read@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    1176 <read_n+0x56>
lea    0xe9a(%rip),%rdi
call   1040 <perror@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11bf <read_n+0x9f>
cmpl   $0x0,-0x1c(%rbp)
jne    118b <read_n+0x6b>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11bf <read_n+0x9f>
mov    -0x1c(%rbp),%eax
add    -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x1c(%rbp),%ecx
mov    -0x10(%rbp),%rax
movslq %ecx,%rcx
shl    $0x2,%rcx
add    %rcx,%rax
mov    %rax,-0x10(%rbp)
mov    -0x1c(%rbp),%ecx
mov    -0x14(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x14(%rbp)
jmp    1139 <read_n+0x19>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_n>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
test   %edx,%edx
je     1166 <read_n+0x46>
mov    %edx,%r12d
mov    %rsi,%rbx
mov    %edi,%r15d
xor    %r14d,%r14d
nopl   0x0(%rax,%rax,1)
mov    %r15d,%edi
mov    %rbx,%rsi
mov    %r12d,%edx
call   1030 <read@plt>
mov    %eax,%ebp
test   %eax,%eax
js     1175 <read_n+0x55>
je     1169 <read_n+0x49>
add    %ebp,%r14d
mov    %ebp,%eax
lea    (%rbx,%rax,4),%rbx
sub    %ebp,%r12d
jne    1140 <read_n+0x20>
jmp    1169 <read_n+0x49>
xor    %r14d,%r14d
mov    %r14d,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe84(%rip),%rdi
call   1040 <perror@plt>
mov    %ebp,%r14d
jmp    1169 <read_n+0x49>

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

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