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

/* Variables and functions */
int /*<<< orphan*/ assert(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, long,
                           int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int read(int, char *, int);
int /*<<< orphan*/ stderr;
int safe_read(int fd, char *buf, int count) {
  ssize_t r = read(fd, buf, count);
  if (r < 0) {
    assert(r == -1);
    fprintf(stderr, "read() = %li, errno: %i\n", (long int)r, errno);
    fflush(stderr);
    perror("read()");
    exit(1);
  }
  return r;
}


x86 assembly (clang, -O0):
<safe_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1030 <read@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    11e5 <safe_read+0x85>
cmpl   $0xffffffff,-0x18(%rbp)
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
mov    0x2e3a(%rip),%rax
mov    (%rax),%edi
movslq -0x18(%rbp),%rdx
mov    0x2e25(%rip),%rax
mov    (%rax),%ecx
lea    0xe44(%rip),%rsi
call   1050 <fprintf@plt>
mov    0x2e18(%rip),%rax
mov    (%rax),%edi
call   1060 <fflush@plt>
lea    0xe43(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x18(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<safe_read>:
push   %r14
push   %rbx
push   %rax
call   1030 <read@plt>
mov    %eax,%ebx
test   %eax,%eax
js     1179 <safe_read+0x19>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
xor    %edi,%edi
cmp    $0xffffffff,%ebx
sete   %dil
call   1040 <assert@plt>
mov    0x2e52(%rip),%r14
mov    (%r14),%edi
movslq %ebx,%rdx
mov    0x2e3d(%rip),%rax
mov    (%rax),%ecx
lea    0xe5c(%rip),%rsi
call   1050 <fprintf@plt>
mov    (%r14),%edi
call   1060 <fflush@plt>
lea    0xe61(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [25 x i8] c"read() = %li, errno: %i\0A\00"
@errno = global i32 0
@.str.1 = [7 x i8] c"read()\00"
define i32 @safe_read(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
store i32 %0, i32* %4
store i8* %1, i8** %5
store i32 %2, i32* %6
%8 = load i32, i32* %4
%9 = load i8*, i8** %5
%10 = load i32, i32* %6
%11 = call i32 @read(i32 %8, i8* %9, i32 %10)
store i32 %11, i32* %7
%12 = load i32, i32* %7
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %28
14: ; preds = %3
%15 = load i32, i32* %7
%16 = icmp eq i32 %15, -1
%17 = zext i1 %16 to i32
%18 = call i32 @assert(i32 %17)
%19 = load i32, i32* @stderr
%20 = load i32, i32* %7
%21 = sext i32 %20 to i64
%22 = load i32, i32* @errno
%23 = call i32 @fprintf(i32 %19, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i64 %21, i32 %22)
%24 = load i32, i32* @stderr
%25 = call i32 @fflush(i32 %24)
%26 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%27 = call i32 @exit(i32 1)
unreachable
28: ; preds = %3
%29 = load i32, i32* %7
ret i32 %29
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [25 x i8] c"read() = %li, errno: %i\0A\00"
@errno = global i32 0
@.str.1 = [7 x i8] c"read()\00"
define i32 @safe_read(i32 %0, i8* nocapture %1, i32 %2) {
%4 = tail call i32 @read(i32 %0, i8* %1, i32 %2)
%5 = icmp slt i32 %4, 0
br i1 %5, label %6, label %18
6: ; preds = %3
%7 = icmp eq i32 %4, -1
%8 = zext i1 %7 to i32
%9 = tail call i32 @assert(i32 %8)
%10 = load i32, i32* @stderr
%11 = sext i32 %4 to i64
%12 = load i32, i32* @errno
%13 = tail call i32 @fprintf(i32 %10, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i64 %11, i32 %12)
%14 = load i32, i32* @stderr
%15 = tail call i32 @fflush(i32 %14)
%16 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%17 = tail call i32 @exit(i32 1)
unreachable
18: ; preds = %3
ret i32 %4
}