#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 scalar_t__ ssize_t;

/* Variables and functions */
int /*<<< orphan*/ abort();
int /*<<< orphan*/ fputs(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
scalar_t__ read(int /*<<< orphan*/, unsigned char *, size_t);
int /*<<< orphan*/ stderr;
int read_n(unsigned char *buf, size_t count) {
  size_t left = count;
  while (left) {
    ssize_t ret = read(0, buf, left);
    if (ret < 0) {
      perror("read");
      abort();
    } else if (ret == 0 && left < count) {
      fputs("short read\n", stderr);
      abort();
    } else if (ret == 0) {
      return 0;
    }
    left -= ret;
  }
  return 1;
}


x86 assembly (clang, -O0):
<read_n>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     11fe <read_n+0xbe>
mov    -0x10(%rbp),%rsi
mov    -0x20(%rbp),%rdx
xor    %edi,%edi
call   1050 <read@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jge    1194 <read_n+0x54>
lea    0xe78(%rip),%rdi
call   1060 <perror@plt>
mov    $0x0,%al
call   1030 <abort@plt>
cmpq   $0x0,-0x28(%rbp)
jne    11c9 <read_n+0x89>
mov    -0x20(%rbp),%rax
cmp    -0x18(%rbp),%rax
jae    11c9 <read_n+0x89>
mov    0x2e2c(%rip),%rax
mov    (%rax),%esi
lea    0xe48(%rip),%rdi
call   1040 <fputs@plt>
mov    $0x0,%al
call   1030 <abort@plt>
cmpq   $0x0,-0x28(%rbp)
jne    11e0 <read_n+0xa0>
movl   $0x0,-0x4(%rbp)
jmp    1205 <read_n+0xc5>
jmp    11e5 <read_n+0xa5>
jmp    11ea <read_n+0xaa>
mov    -0x28(%rbp),%rcx
mov    -0x20(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x20(%rbp)
jmp    1158 <read_n+0x18>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_n>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%r14
mov    %rdi,%r15
mov    %rsi,%rbx
jmp    1158 <read_n+0x18>
sub    %rax,%rbx
test   %rax,%rax
je     1199 <read_n+0x59>
test   %rbx,%rbx
je     1192 <read_n+0x52>
xor    %edi,%edi
mov    %r15,%rsi
mov    %rbx,%rdx
call   1050 <read@plt>
test   %rax,%rax
js     11a1 <read_n+0x61>
jne    1150 <read_n+0x10>
cmp    %r14,%rbx
jae    1150 <read_n+0x10>
mov    0x2e63(%rip),%rax
mov    (%rax),%esi
lea    0xe7f(%rip),%rdi
call   1040 <fputs@plt>
xor    %eax,%eax
call   1030 <abort@plt>
mov    $0x1,%eax
jmp    119b <read_n+0x5b>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %r15
ret
lea    0xe58(%rip),%rdi
call   1060 <perror@plt>
xor    %eax,%eax
call   1030 <abort@plt>

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

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