#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*/ errx(int, char *, char const *, long);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *, char *);
int lseek(int, int, int /*<<< orphan*/);
char *malloc(int);
int read(int, char *, int);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
char *save_read(int file, off_t offset, off_t len, const 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 = malloc(len)) == NULL)
    errx(1, "%s: Can't allocate %ld bytes.", name, (long)len);
  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):
<save_read>:
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    0x2e3e(%rip),%rax
mov    (%rax),%edx
call   1040 <lseek@plt>
mov    %eax,-0x28(%rbp)
cmp    $0x0,%eax
jge    11e7 <save_read+0x77>
mov    0x2e34(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    0x2e18(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0x34(%rbp),%edi
mov    -0x30(%rbp),%rdx
mov    %rax,%rcx
lea    0xe28(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0xc(%rbp),%edi
call   1070 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    1216 <save_read+0xa6>
mov    -0x18(%rbp),%rdx
movslq -0xc(%rbp),%rcx
mov    $0x1,%edi
lea    0xdfe(%rip),%rsi
call   1030 <errx@plt>
mov    -0x4(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    -0xc(%rbp),%edx
call   1050 <read@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0xc(%rbp),%eax
je     129a <save_read+0x12a>
mov    0x2da5(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x44(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x40(%rbp)
cmpl   $0x0,-0x24(%rbp)
je     1269 <save_read+0xf9>
mov    0x2d7f(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    %rax,-0x50(%rbp)
jmp    1279 <save_read+0x109>
lea    0xdcc(%rip),%rax
mov    %rax,-0x50(%rbp)
jmp    1279 <save_read+0x109>
mov    -0x40(%rbp),%rdx
mov    -0x44(%rbp),%edi
mov    -0x50(%rbp),%rcx
lea    0xda2(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x20(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<save_read>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rcx,%r14
mov    %edx,%ebp
mov    %edi,%r15d
mov    0x2e4a(%rip),%rax
mov    (%rax),%edx
call   1040 <lseek@plt>
test   %eax,%eax
js     11d6 <save_read+0x66>
mov    %ebp,%edi
call   1070 <malloc@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    11b7 <save_read+0x47>
movslq %ebp,%rcx
lea    0xe65(%rip),%rsi
mov    $0x1,%edi
mov    %r14,%rdx
call   1030 <errx@plt>
mov    %r15d,%edi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1050 <read@plt>
cmp    %ebp,%eax
jne    11fe <save_read+0x8e>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2e03(%rip),%rax
mov    (%rax),%ebx
mov    0x2df2(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xe0c(%rip),%rsi
mov    %ebx,%edi
mov    %r14,%rdx
mov    %rax,%rcx
jmp    1231 <save_read+0xc1>
mov    0x2ddb(%rip),%rcx
mov    (%rcx),%ebx
test   %eax,%eax
jne    1214 <save_read+0xa4>
lea    0xe2a(%rip),%rcx
jmp    1225 <save_read+0xb5>
mov    0x2dbd(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    %rax,%rcx
lea    0xe01(%rip),%rsi
mov    %ebx,%edi
mov    %r14,%rdx
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1080 <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 = [30 x i8] c"%s: Can't allocate %ld bytes.\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* @save_read(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 @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 i8* @malloc(i32 %25)
store i8* %26, i8** %9
%27 = icmp eq i8* %26, null
br i1 %27, label %28, label %33
28: ; preds = %24
%29 = load i8*, i8** %8
%30 = load i32, i32* %7
%31 = sext i32 %30 to i64
%32 = call i32 @errx(i32 1, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0), i8* %29, i64 %31)
br label %33
33: ; preds = %28, %24
%34 = load i32, i32* %5
%35 = load i8*, i8** %9
%36 = load i32, i32* %7
%37 = call i32 @read(i32 %34, i8* %35, i32 %36)
store i32 %37, i32* %10
%38 = load i32, i32* %10
%39 = load i32, i32* %7
%40 = icmp ne i32 %38, %39
br i1 %40, label %41, label %54
41: ; preds = %33
%42 = load i32, i32* @stderr
%43 = load i8*, i8** %8
%44 = load i32, i32* %10
%45 = icmp ne i32 %44, 0
br i1 %45, label %46, label %49
46: ; preds = %41
%47 = load i32, i32* @errno
%48 = call i8* @strerror(i32 %47)
br label %50
49: ; preds = %41
br label %50
50: ; preds = %49, %46
%51 = phi i8* [ %48, %46 ], [ getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0), %49 ]
%52 = call i32 @fprintf(i32 %42, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i8* %43, i8* %51)
%53 = call i32 @exit(i32 1)
unreachable
54: ; preds = %33
%55 = load i8*, i8** %9
ret i8* %55
}

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 = [30 x i8] c"%s: Can't allocate %ld bytes.\00"
@.str.2 = [15 x i8] c"%s: read: %s.\0A\00"
@.str.3 = [20 x i8] c"End of file reached\00"
define noalias i8* @save_read(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 @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 i8* @malloc(i32 %2)
%16 = icmp eq i8* %15, null
br i1 %16, label %17, label %20
17: ; preds = %14
%18 = sext i32 %2 to i64
%19 = tail call i32 @errx(i32 1, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0), i8* %3, i64 %18)
br label %20
20: ; preds = %17, %14
%21 = tail call i32 @read(i32 %0, i8* %15, i32 %2)
%22 = icmp eq i32 %21, %2
br i1 %22, label %33, label %23
23: ; preds = %20
%24 = load i32, i32* @stderr
%25 = icmp eq i32 %21, 0
br i1 %25, label %29, label %26
26: ; preds = %23
%27 = load i32, i32* @errno
%28 = tail call i8* @strerror(i32 %27)
br label %29
29: ; preds = %23, %26
%30 = phi i8* [ %28, %26 ], [ getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0), %23 ]
%31 = tail call i32 @fprintf(i32 %24, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i8* %3, i8* %30)
%32 = tail call i32 @exit(i32 1)
unreachable
33: ; preds = %20
ret i8* %15
}