#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*/ STDIN_FILENO;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ perror(char *);
int read(int /*<<< orphan*/, void *, int);
int sa_read(void *buffer, int size) {
  int n;
  while (size) {
    if ((n = read(STDIN_FILENO, buffer, size)) < 0) {
      perror("read");
      exit(2);
    }
    if (!n)
      return 1;
    size -= n;
    buffer = (char *)buffer + n;
  }
  return 0;
}


x86 assembly (clang, -O0):
<sa_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     11b5 <sa_read+0x85>
mov    0x2e90(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1030 <read@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jge    1180 <sa_read+0x50>
lea    0xe8f(%rip),%rdi
call   1040 <perror@plt>
mov    $0x2,%edi
call   1050 <exit@plt>
cmpl   $0x0,-0x18(%rbp)
jne    1196 <sa_read+0x66>
movl   $0x1,-0x4(%rbp)
jmp    11bc <sa_read+0x8c>
mov    -0x18(%rbp),%ecx
mov    -0x14(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
add    %rcx,%rax
mov    %rax,-0x10(%rbp)
jmp    113f <sa_read+0xf>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sa_read>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
xor    %r14d,%r14d
test   %esi,%esi
je     1174 <sa_read+0x44>
mov    %esi,%ebp
mov    %rdi,%rbx
mov    0x2e96(%rip),%r15
nopw   0x0(%rax,%rax,1)
mov    (%r15),%edi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1030 <read@plt>
test   %eax,%eax
js     1182 <sa_read+0x52>
je     116e <sa_read+0x3e>
mov    %eax,%ecx
add    %rcx,%rbx
sub    %eax,%ebp
jne    1150 <sa_read+0x20>
jmp    1174 <sa_read+0x44>
mov    $0x1,%r14d
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe77(%rip),%rdi
call   1040 <perror@plt>
mov    $0x2,%edi
call   1050 <exit@plt>

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

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