#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*/ assert_file_exist(char *);
int /*<<< orphan*/ fatal(char *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int fread(char *, int, int, int /*<<< orphan*/ *);
scalar_t__ fseek(int /*<<< orphan*/ *, long, int /*<<< orphan*/);
char *readchars_file(char *fn, long offset, int *nread) {
  assert_file_exist(fn);
  FILE *fd = fopen(fn, "r");
  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
call   1040 <assert_file_exist@plt>
mov    -0x8(%rbp),%rdi
lea    0xe68(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    0x2e28(%rip),%rax
mov    (%rax),%edx
call   1070 <fseek@plt>
mov    0x2e2a(%rip),%rcx
cmp    (%rcx),%rax
jne    11d3 <readchars_file+0x63>
lea    0xe34(%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     1218 <readchars_file+0xa8>
lea    0xdfb(%rip),%rdi
call   1090 <fatal@plt>
mov    -0x20(%rbp),%rdi
call   1050 <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,%r12
mov    %rdi,%rbx
call   1040 <assert_file_exist@plt>
lea    0xe73(%rip),%rsi
mov    %rbx,%rdi
call   1080 <fopen@plt>
mov    %rax,%r15
mov    0x2e39(%rip),%rax
mov    (%rax),%edx
mov    %r15,%rdi
mov    %r12,%rsi
call   1070 <fseek@plt>
mov    0x2e35(%rip),%rcx
cmp    (%rcx),%rax
jne    11c4 <readchars_file+0x54>
lea    0xe43(%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    %r15,%rcx
call   1030 <fread@plt>
cmp    (%r14),%eax
je     11f3 <readchars_file+0x83>
lea    0xe20(%rip),%rdi
call   1090 <fatal@plt>
mov    %r15,%rdi
call   1050 <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"
@SEEK_SET = global i32 0
@EOF = global i64 0
@.str.1 = [12 x i8] c"seek failed\00"
@.str.2 = [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 @assert_file_exist(i8* %10)
%12 = load i8*, i8** %4
%13 = call i32* @fopen(i8* %12, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %13, i32** %7
%14 = load i32*, i32** %7
%15 = load i64, i64* %5
%16 = load i32, i32* @SEEK_SET
%17 = call i64 @fseek(i32* %14, i64 %15, i32 %16)
%18 = load i64, i64* @EOF
%19 = icmp eq i64 %17, %18
br i1 %19, label %20, label %22
20: ; preds = %3
%21 = call i32 @fatal(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
br label %22
22: ; preds = %20, %3
%23 = load i32*, i32** %6
%24 = load i32, i32* %23
%25 = call i8* @MALLOC(i32 %24)
store i8* %25, i8** %8
%26 = load i8*, i8** %8
%27 = load i32*, i32** %6
%28 = load i32, i32* %27
%29 = load i32*, i32** %7
%30 = call i32 @fread(i8* %26, i32 1, i32 %28, i32* %29)
store i32 %30, i32* %9
%31 = load i32, i32* %9
%32 = load i32*, i32** %6
%33 = load i32, i32* %32
%34 = icmp ne i32 %31, %33
br i1 %34, label %35, label %37
35: ; preds = %22
%36 = call i32 @fatal(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
br label %37
37: ; preds = %35, %22
%38 = load i32*, i32** %7
%39 = call i32 @fclose(i32* %38)
%40 = load i8*, i8** %8
ret i8* %40
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@SEEK_SET = global i32 0
@EOF = global i64 0
@.str.1 = [12 x i8] c"seek failed\00"
@.str.2 = [13 x i8] c"fread failed\00"
define i8* @readchars_file(i8* %0, i64 %1, i32* nocapture %2) {
%4 = tail call i32 @assert_file_exist(i8* %0)
%5 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%6 = load i32, i32* @SEEK_SET
%7 = tail call i64 @fseek(i32* %5, i64 %1, i32 %6)
%8 = load i64, i64* @EOF
%9 = icmp eq i64 %7, %8
br i1 %9, label %10, label %12
10: ; preds = %3
%11 = tail call i32 @fatal(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
br label %12
12: ; preds = %10, %3
%13 = load i32, i32* %2
%14 = tail call i8* @MALLOC(i32 %13)
%15 = load i32, i32* %2
%16 = tail call i32 @fread(i8* %14, i32 1, i32 %15, i32* %5)
%17 = load i32, i32* %2
%18 = icmp eq i32 %16, %17
br i1 %18, label %21, label %19
19: ; preds = %12
%20 = tail call i32 @fatal(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
br label %21
21: ; preds = %19, %12
%22 = tail call i32 @fclose(i32* %5)
ret i8* %14
}