#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 /*<<< orphan*/ FILE;

/* Variables and functions */
scalar_t__ EOF;
char *MALLOC(int);
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fatal(char *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int fread(char *, int, int, int /*<<< orphan*/ *);
scalar_t__ fseek(int /*<<< orphan*/ *, long, int /*<<< orphan*/);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ stderr;
char *readchars_file(char *fn, long offset, int *nread) {
  FILE *fd = fopen(fn, "r");
  if (fd == NULL) {
    fprintf(stderr, "Unable to open file <%s>: ", fn);
    perror("");
    exit(1);
  }
  if (fseek(fd, offset, SEEK_SET) == EOF)
    fatal("seek failed");
  char *result = MALLOC(*nread);
  memset(result, 0, *nread);
  int nbytesread = fread(result, 1, *nread, fd);
  if (nbytesread != *nread)
    fatal("fread failed");
  fclose(fd);
  return result;
}


x86 assembly (clang, -O0):
<readchars_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe41(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1202 <readchars_file+0x62>
mov    0x2dfe(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xe1b(%rip),%rsi
call   1060 <fprintf@plt>
lea    0xe29(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    0x2dbf(%rip),%rax
mov    (%rax),%edx
call   1080 <fseek@plt>
mov    0x2dc9(%rip),%rcx
cmp    (%rcx),%rax
jne    1234 <readchars_file+0x94>
lea    0xdee(%rip),%rdi
call   10c0 <fatal@plt>
mov    -0x18(%rbp),%rax
mov    (%rax),%edi
call   1070 <MALLOC@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1050 <memset@plt>
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rax
mov    (%rax),%edx
mov    -0x20(%rbp),%rcx
mov    $0x1,%esi
call   1030 <fread@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
mov    -0x18(%rbp),%rcx
cmp    (%rcx),%eax
je     128a <readchars_file+0xea>
lea    0xda4(%rip),%rdi
call   10c0 <fatal@plt>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x28(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readchars_file>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r15
mov    %rsi,%r12
mov    %rdi,%rbx
lea    0xe48(%rip),%rsi
call   1090 <fopen@plt>
test   %rax,%rax
je     1248 <readchars_file+0xa8>
mov    %rax,%r14
mov    0x2e00(%rip),%rax
mov    (%rax),%edx
mov    %r14,%rdi
mov    %r12,%rsi
call   1080 <fseek@plt>
mov    0x2e04(%rip),%rcx
cmp    (%rcx),%rax
jne    11f5 <readchars_file+0x55>
lea    0xe2d(%rip),%rdi
call   10c0 <fatal@plt>
mov    (%r15),%edi
call   1070 <MALLOC@plt>
mov    %rax,%rbx
mov    (%r15),%edx
mov    %rax,%rdi
xor    %esi,%esi
call   1050 <memset@plt>
mov    (%r15),%edx
mov    %rbx,%rdi
mov    $0x1,%esi
mov    %r14,%rcx
call   1030 <fread@plt>
cmp    (%r15),%eax
je     1231 <readchars_file+0x91>
lea    0xdfd(%rip),%rdi
call   10c0 <fatal@plt>
mov    %r14,%rdi
call   1040 <fclose@plt>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
mov    0x2d89(%rip),%rax
mov    (%rax),%edi
lea    0xdaa(%rip),%rsi
mov    %rbx,%rdx
call   1060 <fprintf@plt>
lea    0xdb5(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.1 = [27 x i8] c"Unable to open file <%s>: \00"
@.str.2 = [1 x i8] zeroinitializer
@SEEK_SET = global i32 0
@EOF = global i64 0
@.str.3 = [12 x i8] c"seek failed\00"
@.str.4 = [13 x i8] c"fread failed\00"
define i8* @readchars_file(i8* %0, i64 %1, i32* %2) {
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i32*
%7 = alloca i32*
%8 = alloca i8*
%9 = alloca i32
store i8* %0, i8** %4
store i64 %1, i64* %5
store i32* %2, i32** %6
%10 = load i8*, i8** %4
%11 = call i32* @fopen(i8* %10, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %11, i32** %7
%12 = load i32*, i32** %7
%13 = icmp eq i32* %12, null
br i1 %13, label %14, label %20
14: ; preds = %3
%15 = load i32, i32* @stderr
%16 = load i8*, i8** %4
%17 = call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i8* %16)
%18 = call i32 @perror(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.2, i64 0, i64 0))
%19 = call i32 @exit(i32 1)
unreachable
20: ; preds = %3
%21 = load i32*, i32** %7
%22 = load i64, i64* %5
%23 = load i32, i32* @SEEK_SET
%24 = call i64 @fseek(i32* %21, i64 %22, i32 %23)
%25 = load i64, i64* @EOF
%26 = icmp eq i64 %24, %25
br i1 %26, label %27, label %29
27: ; preds = %20
%28 = call i32 @fatal(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0))
br label %29
29: ; preds = %27, %20
%30 = load i32*, i32** %6
%31 = load i32, i32* %30
%32 = call i8* @MALLOC(i32 %31)
store i8* %32, i8** %8
%33 = load i8*, i8** %8
%34 = load i32*, i32** %6
%35 = load i32, i32* %34
%36 = call i32 @memset(i8* %33, i32 0, i32 %35)
%37 = load i8*, i8** %8
%38 = load i32*, i32** %6
%39 = load i32, i32* %38
%40 = load i32*, i32** %7
%41 = call i32 @fread(i8* %37, i32 1, i32 %39, i32* %40)
store i32 %41, i32* %9
%42 = load i32, i32* %9
%43 = load i32*, i32** %6
%44 = load i32, i32* %43
%45 = icmp ne i32 %42, %44
br i1 %45, label %46, label %48
46: ; preds = %29
%47 = call i32 @fatal(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0))
br label %48
48: ; preds = %46, %29
%49 = load i32*, i32** %7
%50 = call i32 @fclose(i32* %49)
%51 = load i8*, i8** %8
ret i8* %51
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.1 = [27 x i8] c"Unable to open file <%s>: \00"
@.str.2 = [1 x i8] zeroinitializer
@SEEK_SET = global i32 0
@EOF = global i64 0
@.str.3 = [12 x i8] c"seek failed\00"
@.str.4 = [13 x i8] c"fread failed\00"
define i8* @readchars_file(i8* %0, i64 %1, i32* nocapture %2) {
%4 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%5 = icmp eq i32* %4, null
br i1 %5, label %6, label %11
6: ; preds = %3
%7 = load i32, i32* @stderr
%8 = tail call i32 @fprintf(i32 %7, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i8* %0)
%9 = tail call i32 @perror(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.2, i64 0, i64 0))
%10 = tail call i32 @exit(i32 1)
unreachable
11: ; preds = %3
%12 = load i32, i32* @SEEK_SET
%13 = tail call i64 @fseek(i32* %4, i64 %1, i32 %12)
%14 = load i64, i64* @EOF
%15 = icmp eq i64 %13, %14
br i1 %15, label %16, label %18
16: ; preds = %11
%17 = tail call i32 @fatal(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0))
br label %18
18: ; preds = %16, %11
%19 = load i32, i32* %2
%20 = tail call i8* @MALLOC(i32 %19)
%21 = load i32, i32* %2
%22 = tail call i32 @memset(i8* %20, i32 0, i32 %21)
%23 = load i32, i32* %2
%24 = tail call i32 @fread(i8* %20, i32 1, i32 %23, i32* %4)
%25 = load i32, i32* %2
%26 = icmp eq i32 %24, %25
br i1 %26, label %29, label %27
27: ; preds = %18
%28 = tail call i32 @fatal(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0))
br label %29
29: ; preds = %27, %18
%30 = tail call i32 @fclose(i32* %4)
ret i8* %20
}