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

/* Variables and functions */
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, ...);
int lseek(int, int, int /*<<< orphan*/);
scalar_t__ malloc(int);
int read(int, char *, int);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
char *saveRead(int file, off_t offset, off_t len, char *name) {
  char *tmp;
  int count;
  off_t off;
  if ((off = lseek(file, offset, SEEK_SET)) < 0) {
    fprintf(stderr, "%s: fseek: %s\n", name, strerror(errno));
    exit(1);
  }
  if (!(tmp = (char *)malloc(len))) {
    fprintf(stderr, "%s: Can't allocate %ld bytes.\n", name, len);
    exit(1);
  }
  count = read(file, tmp, len);
  if (count != len) {
    fprintf(stderr, "%s: read: %s.\n", name,
            count ? strerror(errno) : "End of file reached");
    exit(1);
  }
  return tmp;
}


x86 assembly (clang, -O0):
<saveRead>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %edx,-0xc(%rbp)
mov    %rcx,-0x18(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x8(%rbp),%esi
mov    0x2e4e(%rip),%rax
mov    (%rax),%edx
call   1030 <lseek@plt>
mov    %eax,-0x28(%rbp)
cmp    $0x0,%eax
jge    11d9 <saveRead+0x79>
mov    0x2e44(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    0x2e28(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x34(%rbp),%edi
mov    -0x30(%rbp),%rdx
mov    %rax,%rcx
lea    0xe38(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0xc(%rbp),%edi
call   1060 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    1217 <saveRead+0xb7>
mov    0x2dea(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rdx
mov    -0xc(%rbp),%ecx
lea    0xe09(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0x4(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    -0xc(%rbp),%edx
call   1040 <read@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0xc(%rbp),%eax
je     129d <saveRead+0x13d>
mov    0x2da4(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x44(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x40(%rbp)
cmpl   $0x0,-0x24(%rbp)
je     126a <saveRead+0x10a>
mov    0x2d7e(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    %rax,-0x50(%rbp)
jmp    127a <saveRead+0x11a>
lea    0xdcc(%rip),%rax
mov    %rax,-0x50(%rbp)
jmp    127a <saveRead+0x11a>
mov    -0x40(%rbp),%rdx
mov    -0x44(%rbp),%edi
mov    -0x50(%rbp),%rcx
lea    0xda2(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0x20(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<saveRead>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rcx,%r14
mov    %edx,%r15d
mov    %edi,%ebx
mov    0x2e5a(%rip),%rax
mov    (%rax),%edx
call   1030 <lseek@plt>
test   %eax,%eax
js     11b1 <saveRead+0x51>
mov    %r15d,%edi
call   1060 <malloc@plt>
test   %rax,%rax
je     11d9 <saveRead+0x79>
mov    %rax,%rbp
mov    %ebx,%edi
mov    %rax,%rsi
mov    %r15d,%edx
call   1040 <read@plt>
cmp    %r15d,%eax
jne    1200 <saveRead+0xa0>
mov    %rbp,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2e28(%rip),%rax
mov    (%rax),%ebx
mov    0x2e17(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xe31(%rip),%rsi
mov    %ebx,%edi
mov    %r14,%rdx
mov    %rax,%rcx
jmp    1233 <saveRead+0xd3>
mov    0x2e00(%rip),%rax
mov    (%rax),%edi
lea    0xe26(%rip),%rsi
mov    %r14,%rdx
mov    %r15d,%ecx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    0x2dd9(%rip),%rcx
mov    (%rcx),%ebx
test   %eax,%eax
jne    1216 <saveRead+0xb6>
lea    0xe29(%rip),%rcx
jmp    1227 <saveRead+0xc7>
mov    0x2dbb(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    %rax,%rcx
lea    0xe00(%rip),%rsi
mov    %ebx,%edi
mov    %r14,%rdx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@SEEK_SET = global i32 0
@stderr = global i32 0
@.str = [15 x i8] c"%s: fseek: %s\0A\00"
@errno = global i32 0
@.str.1 = [31 x i8] c"%s: Can't allocate %ld bytes.\0A\00"
@.str.2 = [15 x i8] c"%s: read: %s.\0A\00"
@.str.3 = [20 x i8] c"End of file reached\00"
define i8* @saveRead(i32 %0, i32 %1, i32 %2, i8* %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i32
%11 = alloca i32
store i32 %0, i32* %5
store i32 %1, i32* %6
store i32 %2, i32* %7
store i8* %3, i8** %8
%12 = load i32, i32* %5
%13 = load i32, i32* %6
%14 = load i32, i32* @SEEK_SET
%15 = call i32 @lseek(i32 %12, i32 %13, i32 %14)
store i32 %15, i32* %11
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %24
17: ; preds = %4
%18 = load i32, i32* @stderr
%19 = load i8*, i8** %8
%20 = load i32, i32* @errno
%21 = call i8* @strerror(i32 %20)
%22 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %18, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* %19, i8* %21)
%23 = call i32 @exit(i32 1)
unreachable
24: ; preds = %4
%25 = load i32, i32* %7
%26 = call i64 @malloc(i32 %25)
%27 = inttoptr i64 %26 to i8*
store i8* %27, i8** %9
%28 = icmp ne i8* %27, null
br i1 %28, label %35, label %29
29: ; preds = %24
%30 = load i32, i32* @stderr
%31 = load i8*, i8** %8
%32 = load i32, i32* %7
%33 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %30, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0), i8* %31, i32 %32)
%34 = call i32 @exit(i32 1)
unreachable
35: ; preds = %24
%36 = load i32, i32* %5
%37 = load i8*, i8** %9
%38 = load i32, i32* %7
%39 = call i32 @read(i32 %36, i8* %37, i32 %38)
store i32 %39, i32* %10
%40 = load i32, i32* %10
%41 = load i32, i32* %7
%42 = icmp ne i32 %40, %41
br i1 %42, label %43, label %56
43: ; preds = %35
%44 = load i32, i32* @stderr
%45 = load i8*, i8** %8
%46 = load i32, i32* %10
%47 = icmp ne i32 %46, 0
br i1 %47, label %48, label %51
48: ; preds = %43
%49 = load i32, i32* @errno
%50 = call i8* @strerror(i32 %49)
br label %52
51: ; preds = %43
br label %52
52: ; preds = %51, %48
%53 = phi i8* [ %50, %48 ], [ getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0), %51 ]
%54 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %44, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i8* %45, i8* %53)
%55 = call i32 @exit(i32 1)
unreachable
56: ; preds = %35
%57 = load i8*, i8** %9
ret i8* %57
}

LLVM IR (-O2):
@SEEK_SET = global i32 0
@stderr = global i32 0
@.str = [15 x i8] c"%s: fseek: %s\0A\00"
@errno = global i32 0
@.str.1 = [31 x i8] c"%s: Can't allocate %ld bytes.\0A\00"
@.str.2 = [15 x i8] c"%s: read: %s.\0A\00"
@.str.3 = [20 x i8] c"End of file reached\00"
define i8* @saveRead(i32 %0, i32 %1, i32 %2, i8* %3) {
%5 = load i32, i32* @SEEK_SET
%6 = tail call i32 @lseek(i32 %0, i32 %1, i32 %5)
%7 = icmp slt i32 %6, 0
br i1 %7, label %8, label %14
8: ; preds = %4
%9 = load i32, i32* @stderr
%10 = load i32, i32* @errno
%11 = tail call i8* @strerror(i32 %10)
%12 = tail call i32 (i32, i8*, i8*, ...) @fprintf(i32 %9, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* %3, i8* %11)
%13 = tail call i32 @exit(i32 1)
unreachable
14: ; preds = %4
%15 = tail call i64 @malloc(i32 %2)
%16 = inttoptr i64 %15 to i8*
%17 = icmp eq i64 %15, 0
br i1 %17, label %18, label %22
18: ; preds = %14
%19 = load i32, i32* @stderr
%20 = tail call i32 (i32, i8*, i8*, ...) @fprintf(i32 %19, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0), i8* %3, i32 %2)
%21 = tail call i32 @exit(i32 1)
unreachable
22: ; preds = %14
%23 = tail call i32 @read(i32 %0, i8* %16, i32 %2)
%24 = icmp eq i32 %23, %2
br i1 %24, label %35, label %25
25: ; preds = %22
%26 = load i32, i32* @stderr
%27 = icmp eq i32 %23, 0
br i1 %27, label %31, label %28
28: ; preds = %25
%29 = load i32, i32* @errno
%30 = tail call i8* @strerror(i32 %29)
br label %31
31: ; preds = %25, %28
%32 = phi i8* [ %30, %28 ], [ getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0), %25 ]
%33 = tail call i32 (i32, i8*, i8*, ...) @fprintf(i32 %26, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i8* %3, i8* %32)
%34 = tail call i32 @exit(i32 1)
unreachable
35: ; preds = %22
ret i8* %16
}