#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*/ close(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int read(int, void *, int);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int sa_fread(int ifd, void *buffer, int size, int mode) {
  int n;
  if ((n = read(ifd, buffer, size)) < 0) {
    fprintf(stderr, ("Error while reading system activity file: %s\n"),
            strerror(errno));
    close(ifd);
    exit(2);
  }
  if (!n && (mode == 0))
    return 1;
  if (n < size) {
    fprintf(stderr, ("End of system activity file unexpected\n"));
    close(ifd);
    exit(2);
  }
  return 0;
}


x86 assembly (clang, -O0):
<sa_fread>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1040 <read@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jge    11c0 <sa_fread+0x70>
mov    0x2e59(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    0x2e45(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    -0x20(%rbp),%edi
mov    %rax,%rdx
lea    0xe59(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x8(%rbp),%edi
call   1030 <close@plt>
mov    $0x2,%edi
call   1060 <exit@plt>
cmpl   $0x0,-0x1c(%rbp)
jne    11e0 <sa_fread+0x90>
cmpl   $0x0,-0x18(%rbp)
jne    11e0 <sa_fread+0x90>
movl   $0x1,-0x4(%rbp)
jmp    121c <sa_fread+0xcc>
mov    -0x1c(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    1215 <sa_fread+0xc5>
mov    0x2ded(%rip),%rax
mov    (%rax),%edi
lea    0xe32(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x8(%rbp),%edi
call   1030 <close@plt>
mov    $0x2,%edi
call   1060 <exit@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sa_fread>:
push   %rbp
push   %r14
push   %rbx
mov    %ecx,%ebx
mov    %edx,%ebp
mov    %edi,%r14d
call   1040 <read@plt>
test   %eax,%eax
js     1193 <sa_fread+0x43>
or     %eax,%ebx
je     1187 <sa_fread+0x37>
xor    %ecx,%ecx
cmp    %ebp,%eax
jge    118c <sa_fread+0x3c>
mov    0x2e6b(%rip),%rax
mov    (%rax),%edi
lea    0xeb0(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
jmp    11bd <sa_fread+0x6d>
mov    $0x1,%ecx
mov    %ecx,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    0x2e46(%rip),%rax
mov    (%rax),%ebx
mov    0x2e35(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
lea    0xe4f(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    %r14d,%edi
call   1030 <close@plt>
mov    $0x2,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [46 x i8] c"Error while reading system activity file: %s\0A\00"
@errno = global i32 0
@.str.1 = [40 x i8] c"End of system activity file unexpected\0A\00"
define i32 @sa_fread(i32 %0, i8* %1, i32 %2, i32 %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i32 %0, i32* %6
store i8* %1, i8** %7
store i32 %2, i32* %8
store i32 %3, i32* %9
%11 = load i32, i32* %6
%12 = load i8*, i8** %7
%13 = load i32, i32* %8
%14 = call i32 @read(i32 %11, i8* %12, i32 %13)
store i32 %14, i32* %10
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %24
16: ; preds = %4
%17 = load i32, i32* @stderr
%18 = load i32, i32* @errno
%19 = call i8* @strerror(i32 %18)
%20 = call i32 (i32, i8*, ...) @fprintf(i32 %17, i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str, i64 0, i64 0), i8* %19)
%21 = load i32, i32* %6
%22 = call i32 @close(i32 %21)
%23 = call i32 @exit(i32 2)
unreachable
24: ; preds = %4
%25 = load i32, i32* %10
%26 = icmp ne i32 %25, 0
br i1 %26, label %31, label %27
27: ; preds = %24
%28 = load i32, i32* %9
%29 = icmp eq i32 %28, 0
br i1 %29, label %30, label %31
30: ; preds = %27
store i32 1, i32* %5
br label %42
31: ; preds = %27, %24
%32 = load i32, i32* %10
%33 = load i32, i32* %8
%34 = icmp slt i32 %32, %33
br i1 %34, label %35, label %41
35: ; preds = %31
%36 = load i32, i32* @stderr
%37 = call i32 (i32, i8*, ...) @fprintf(i32 %36, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.1, i64 0, i64 0))
%38 = load i32, i32* %6
%39 = call i32 @close(i32 %38)
%40 = call i32 @exit(i32 2)
unreachable
41: ; preds = %31
store i32 0, i32* %5
br label %42
42: ; preds = %41, %30
%43 = load i32, i32* %5
ret i32 %43
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [46 x i8] c"Error while reading system activity file: %s\0A\00"
@errno = global i32 0
@.str.1 = [40 x i8] c"End of system activity file unexpected\0A\00"
define i32 @sa_fread(i32 %0, i8* nocapture %1, i32 %2, i32 %3) {
%5 = tail call i32 @read(i32 %0, i8* %1, i32 %2)
%6 = icmp slt i32 %5, 0
br i1 %6, label %7, label %14
7: ; preds = %4
%8 = load i32, i32* @stderr
%9 = load i32, i32* @errno
%10 = tail call i8* @strerror(i32 %9)
%11 = tail call i32 (i32, i8*, ...) @fprintf(i32 %8, i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str, i64 0, i64 0), i8* %10)
%12 = tail call i32 @close(i32 %0)
%13 = tail call i32 @exit(i32 2)
unreachable
14: ; preds = %4
%15 = or i32 %5, %3
%16 = icmp eq i32 %15, 0
br i1 %16, label %24, label %17
17: ; preds = %14
%18 = icmp slt i32 %5, %2
br i1 %18, label %19, label %24
19: ; preds = %17
%20 = load i32, i32* @stderr
%21 = tail call i32 (i32, i8*, ...) @fprintf(i32 %20, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.1, i64 0, i64 0))
%22 = tail call i32 @close(i32 %0)
%23 = tail call i32 @exit(i32 2)
unreachable
24: ; preds = %17, %14
%25 = phi i32 [ 1, %14 ], [ 0, %17 ]
ret i32 %25
}