#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*/ 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*/ stderr;
char *readchars_file(char *fn, long offset, int *nread) {
  FILE *fd = fopen(fn, "r");
  if (!fd) {
    fprintf(stderr, "readchars_file: fn = %s\n", fn);
    fatal("open failed");
  }
  if (fseek(fd, offset, SEEK_SET) == EOF)
    fatal("seek failed");
  char *result = MALLOC(*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    0xe71(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11c8 <readchars_file+0x58>
mov    0x2e2e(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xe4b(%rip),%rsi
call   1050 <fprintf@plt>
lea    0xe58(%rip),%rdi
call   1090 <fatal@plt>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    0x2df9(%rip),%rax
mov    (%rax),%edx
call   1070 <fseek@plt>
mov    0x2e03(%rip),%rcx
cmp    (%rcx),%rax
jne    11fa <readchars_file+0x8a>
lea    0xe32(%rip),%rdi
call   1090 <fatal@plt>
mov    -0x18(%rbp),%rax
mov    (%rax),%edi
call   1060 <MALLOC@plt>
mov    %rax,-0x28(%rbp)
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     123f <readchars_file+0xcf>
lea    0xdf9(%rip),%rdi
call   1090 <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,%r14
mov    %rsi,%r15
mov    %rdi,%rbx
lea    0xe78(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,%r12
test   %rax,%rax
jne    11b9 <readchars_file+0x49>
mov    0x2e3c(%rip),%rax
mov    (%rax),%edi
lea    0xe5d(%rip),%rsi
mov    %rbx,%rdx
call   1050 <fprintf@plt>
lea    0xe67(%rip),%rdi
call   1090 <fatal@plt>
mov    0x2e10(%rip),%rax
mov    (%rax),%edx
mov    %r12,%rdi
mov    %r15,%rsi
call   1070 <fseek@plt>
mov    0x2e14(%rip),%rcx
cmp    (%rcx),%rax
jne    11e5 <readchars_file+0x75>
lea    0xe47(%rip),%rdi
call   1090 <fatal@plt>
mov    (%r14),%edi
call   1060 <MALLOC@plt>
mov    %rax,%rbx
mov    (%r14),%edx
mov    %rax,%rdi
mov    $0x1,%esi
mov    %r12,%rcx
call   1030 <fread@plt>
cmp    (%r14),%eax
je     1214 <readchars_file+0xa4>
lea    0xe24(%rip),%rdi
call   1090 <fatal@plt>
mov    %r12,%rdi
call   1040 <fclose@plt>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.1 = [25 x i8] c"readchars_file: fn = %s\0A\00"
@.str.2 = [12 x i8] c"open failed\00"
@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 ne i32* %12, null
br i1 %13, label %19, label %14
14: ; preds = %3
%15 = load i32, i32* @stderr
%16 = load i8*, i8** %4
%17 = call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i8* %16)
%18 = call i32 @fatal(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
br label %19
19: ; preds = %14, %3
%20 = load i32*, i32** %7
%21 = load i64, i64* %5
%22 = load i32, i32* @SEEK_SET
%23 = call i64 @fseek(i32* %20, i64 %21, i32 %22)
%24 = load i64, i64* @EOF
%25 = icmp eq i64 %23, %24
br i1 %25, label %26, label %28
26: ; preds = %19
%27 = call i32 @fatal(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0))
br label %28
28: ; preds = %26, %19
%29 = load i32*, i32** %6
%30 = load i32, i32* %29
%31 = call i8* @MALLOC(i32 %30)
store i8* %31, i8** %8
%32 = load i8*, i8** %8
%33 = load i32*, i32** %6
%34 = load i32, i32* %33
%35 = load i32*, i32** %7
%36 = call i32 @fread(i8* %32, i32 1, i32 %34, i32* %35)
store i32 %36, i32* %9
%37 = load i32, i32* %9
%38 = load i32*, i32** %6
%39 = load i32, i32* %38
%40 = icmp ne i32 %37, %39
br i1 %40, label %41, label %43
41: ; preds = %28
%42 = call i32 @fatal(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0))
br label %43
43: ; preds = %41, %28
%44 = load i32*, i32** %7
%45 = call i32 @fclose(i32* %44)
%46 = load i8*, i8** %8
ret i8* %46
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.1 = [25 x i8] c"readchars_file: fn = %s\0A\00"
@.str.2 = [12 x i8] c"open failed\00"
@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 %10
6: ; preds = %3
%7 = load i32, i32* @stderr
%8 = tail call i32 @fprintf(i32 %7, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i8* %0)
%9 = tail call i32 @fatal(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
br label %10
10: ; preds = %6, %3
%11 = load i32, i32* @SEEK_SET
%12 = tail call i64 @fseek(i32* %4, i64 %1, i32 %11)
%13 = load i64, i64* @EOF
%14 = icmp eq i64 %12, %13
br i1 %14, label %15, label %17
15: ; preds = %10
%16 = tail call i32 @fatal(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0))
br label %17
17: ; preds = %15, %10
%18 = load i32, i32* %2
%19 = tail call i8* @MALLOC(i32 %18)
%20 = load i32, i32* %2
%21 = tail call i32 @fread(i8* %19, i32 1, i32 %20, i32* %4)
%22 = load i32, i32* %2
%23 = icmp eq i32 %21, %22
br i1 %23, label %26, label %24
24: ; preds = %17
%25 = tail call i32 @fatal(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0))
br label %26
26: ; preds = %24, %17
%27 = tail call i32 @fclose(i32* %4)
ret i8* %19
}