#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 */
int EOF;
int /*<<< orphan*/ SEEK_END;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int fgetc(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
int ftell(int /*<<< orphan*/ *);
char *malloc(int);
int /*<<< orphan*/ sprintf(char *, char *, char const *, char const *);
int /*<<< orphan*/ stderr;
char *readFile_sub(const char *prefix, const char *suffix, int begin, int end) {
  char fname[1024];
  sprintf(fname, "%s%s", prefix, suffix);
  FILE *fi = fopen(fname, "rb");
  fseek(fi, 0, SEEK_END);
  int fis = ftell(fi);
  char *fid = malloc(fis + 1);
  fseek(fi, 0, SEEK_SET);
  int fis2 = 0;
  int i = 0;
  for (;;) {
    int c = fgetc(fi);
    if (c == EOF)
      break;
    if (c == '\0') {
      fprintf(stderr, "NUL DETECTED IN %s\n", fname);
      exit(1);
    }
    if ((i >= begin) && (i < end)) {
      fid[fis2++] = c;
    }
    i++;
  }
  fid[fis2] = '\0';
  fclose(fi);
  return fid;
}


x86 assembly (clang, -O0):
<readFile_sub>:
push   %rbp
mov    %rsp,%rbp
sub    $0x450,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
lea    -0x420(%rbp),%rdi
mov    -0x8(%rbp),%rdx
mov    -0x10(%rbp),%rcx
lea    0xe41(%rip),%rsi
call   10a0 <sprintf@plt>
lea    -0x420(%rbp),%rdi
lea    0xe33(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x428(%rbp)
mov    -0x428(%rbp),%rdi
mov    0x2dfc(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1080 <fseek@plt>
mov    -0x428(%rbp),%rdi
call   1060 <ftell@plt>
mov    %eax,-0x42c(%rbp)
mov    -0x42c(%rbp),%edi
add    $0x1,%edi
call   1070 <malloc@plt>
mov    %rax,-0x438(%rbp)
mov    -0x428(%rbp),%rdi
mov    0x2d9e(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1080 <fseek@plt>
movl   $0x0,-0x43c(%rbp)
movl   $0x0,-0x440(%rbp)
mov    -0x428(%rbp),%rdi
call   1040 <fgetc@plt>
mov    %eax,-0x444(%rbp)
mov    -0x444(%rbp),%eax
mov    0x2d7a(%rip),%rcx
cmp    (%rcx),%eax
jne    1273 <readFile_sub+0xe3>
jmp    12fe <readFile_sub+0x16e>
cmpl   $0x0,-0x444(%rbp)
jne    12a6 <readFile_sub+0x116>
mov    0x2d49(%rip),%rax
mov    (%rax),%edi
lea    -0x420(%rbp),%rdx
lea    0xd71(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    -0x440(%rbp),%eax
cmp    -0x14(%rbp),%eax
jl     12ea <readFile_sub+0x15a>
mov    -0x440(%rbp),%eax
cmp    -0x18(%rbp),%eax
jge    12ea <readFile_sub+0x15a>
mov    -0x444(%rbp),%eax
mov    %al,%dl
mov    -0x438(%rbp),%rax
mov    -0x43c(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0x43c(%rbp)
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x440(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x440(%rbp)
jmp    1247 <readFile_sub+0xb7>
mov    -0x438(%rbp),%rax
movslq -0x43c(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x428(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x438(%rbp),%rax
add    $0x450,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readFile_sub>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x418,%rsp
mov    %ecx,0xc(%rsp)
mov    %edx,%r12d
mov    %rsi,%rcx
mov    %rdi,%rdx
lea    0xe4b(%rip),%rsi
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
call   10a0 <sprintf@plt>
lea    0xe3c(%rip),%rsi
mov    %rbx,%rdi
call   1090 <fopen@plt>
mov    %rax,%rbx
mov    0x2e0d(%rip),%rax
mov    (%rax),%edx
xor    %ebp,%ebp
mov    %rbx,%rdi
xor    %esi,%esi
call   1080 <fseek@plt>
mov    %rbx,%rdi
call   1060 <ftell@plt>
lea    0x1(%rax),%edi
call   1070 <malloc@plt>
mov    %rax,%r15
mov    0x2dc5(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1080 <fseek@plt>
mov    %rbx,%rdi
call   1040 <fgetc@plt>
mov    0x2dc2(%rip),%r13
cmp    0x0(%r13),%eax
jne    1249 <readFile_sub+0xb9>
movslq %ebp,%rax
movb   $0x0,(%r15,%rax,1)
mov    %rbx,%rdi
call   1030 <fclose@plt>
mov    %r15,%rax
add    $0x418,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
xor    %ebp,%ebp
xor    %r14d,%r14d
jmp    1262 <readFile_sub+0xd2>
add    $0x1,%r14d
mov    %rbx,%rdi
call   1040 <fgetc@plt>
cmp    0x0(%r13),%eax
je     1224 <readFile_sub+0x94>
test   %eax,%eax
je     127e <readFile_sub+0xee>
cmp    %r12d,%r14d
jl     1250 <readFile_sub+0xc0>
cmp    0xc(%rsp),%r14d
jge    1250 <readFile_sub+0xc0>
movslq %ebp,%rcx
add    $0x1,%ebp
mov    %al,(%r15,%rcx,1)
jmp    1250 <readFile_sub+0xc0>
mov    0x2d4b(%rip),%rax
mov    (%rax),%edi
lea    0xd7a(%rip),%rsi
lea    0x10(%rsp),%rdx
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
@.str = [5 x i8] c"%s%s\00"
@.str.1 = [3 x i8] c"rb\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
@EOF = global i32 0
@stderr = global i32 0
@.str.2 = [20 x i8] c"NUL DETECTED IN %s\0A\00"
define i8* @readFile_sub(i8* %0, i8* %1, i32 %2, i32 %3) {
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca [1024 x i8]
%10 = alloca i32*
%11 = alloca i32
%12 = alloca i8*
%13 = alloca i32
%14 = alloca i32
%15 = alloca i32
store i8* %0, i8** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
store i32 %3, i32* %8
%16 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%17 = load i8*, i8** %5
%18 = load i8*, i8** %6
%19 = call i32 @sprintf(i8* %16, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* %17, i8* %18)
%20 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%21 = call i32* @fopen(i8* %20, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %21, i32** %10
%22 = load i32*, i32** %10
%23 = load i32, i32* @SEEK_END
%24 = call i32 @fseek(i32* %22, i32 0, i32 %23)
%25 = load i32*, i32** %10
%26 = call i32 @ftell(i32* %25)
store i32 %26, i32* %11
%27 = load i32, i32* %11
%28 = add nsw i32 %27, 1
%29 = call i8* @malloc(i32 %28)
store i8* %29, i8** %12
%30 = load i32*, i32** %10
%31 = load i32, i32* @SEEK_SET
%32 = call i32 @fseek(i32* %30, i32 0, i32 %31)
store i32 0, i32* %13
store i32 0, i32* %14
br label %33
33: ; preds = %64, %4
%34 = load i32*, i32** %10
%35 = call i32 @fgetc(i32* %34)
store i32 %35, i32* %15
%36 = load i32, i32* %15
%37 = load i32, i32* @EOF
%38 = icmp eq i32 %36, %37
br i1 %38, label %39, label %40
39: ; preds = %33
br label %67
40: ; preds = %33
%41 = load i32, i32* %15
%42 = icmp eq i32 %41, 0
br i1 %42, label %43, label %48
43: ; preds = %40
%44 = load i32, i32* @stderr
%45 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%46 = call i32 @fprintf(i32 %44, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i8* %45)
%47 = call i32 @exit(i32 1)
unreachable
48: ; preds = %40
%49 = load i32, i32* %14
%50 = load i32, i32* %7
%51 = icmp sge i32 %49, %50
br i1 %51, label %52, label %64
52: ; preds = %48
%53 = load i32, i32* %14
%54 = load i32, i32* %8
%55 = icmp slt i32 %53, %54
br i1 %55, label %56, label %64
56: ; preds = %52
%57 = load i32, i32* %15
%58 = trunc i32 %57 to i8
%59 = load i8*, i8** %12
%60 = load i32, i32* %13
%61 = add nsw i32 %60, 1
store i32 %61, i32* %13
%62 = sext i32 %60 to i64
%63 = getelementptr inbounds i8, i8* %59, i64 %62
store i8 %58, i8* %63
br label %64
64: ; preds = %56, %52, %48
%65 = load i32, i32* %14
%66 = add nsw i32 %65, 1
store i32 %66, i32* %14
br label %33
67: ; preds = %39
%68 = load i8*, i8** %12
%69 = load i32, i32* %13
%70 = sext i32 %69 to i64
%71 = getelementptr inbounds i8, i8* %68, i64 %70
store i8 0, i8* %71
%72 = load i32*, i32** %10
%73 = call i32 @fclose(i32* %72)
%74 = load i8*, i8** %12
ret i8* %74
}

LLVM IR (-O2):
@.str = [5 x i8] c"%s%s\00"
@.str.1 = [3 x i8] c"rb\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
@EOF = global i32 0
@stderr = global i32 0
@.str.2 = [20 x i8] c"NUL DETECTED IN %s\0A\00"
define noalias i8* @readFile_sub(i8* %0, i8* %1, i32 %2, i32 %3) {
%5 = alloca [1024 x i8]
%6 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%7 = call i32 @sprintf(i8* %6, i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %1)
%8 = call i32* @fopen(i8* %6, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%9 = load i32, i32* @SEEK_END
%10 = tail call i32 @fseek(i32* %8, i32 0, i32 %9)
%11 = tail call i32 @ftell(i32* %8)
%12 = add nsw i32 %11, 1
%13 = tail call i8* @malloc(i32 %12)
%14 = load i32, i32* @SEEK_SET
%15 = tail call i32 @fseek(i32* %8, i32 0, i32 %14)
%16 = tail call i32 @fgetc(i32* %8)
%17 = load i32, i32* @EOF
%18 = icmp eq i32 %16, %17
br i1 %18, label %43, label %19
19: ; preds = %4, %37
%20 = phi i32 [ %40, %37 ], [ %16, %4 ]
%21 = phi i32 [ %38, %37 ], [ 0, %4 ]
%22 = phi i32 [ %39, %37 ], [ 0, %4 ]
%23 = icmp eq i32 %20, 0
br i1 %23, label %24, label %28
24: ; preds = %19
%25 = load i32, i32* @stderr
%26 = call i32 @fprintf(i32 %25, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i8* %6)
%27 = call i32 @exit(i32 1)
unreachable
28: ; preds = %19
%29 = icmp sge i32 %22, %2
%30 = icmp slt i32 %22, %3
%31 = and i1 %29, %30
br i1 %31, label %32, label %37
32: ; preds = %28
%33 = trunc i32 %20 to i8
%34 = add nsw i32 %21, 1
%35 = sext i32 %21 to i64
%36 = getelementptr inbounds i8, i8* %13, i64 %35
store i8 %33, i8* %36
br label %37
37: ; preds = %28, %32
%38 = phi i32 [ %34, %32 ], [ %21, %28 ]
%39 = add nuw nsw i32 %22, 1
%40 = tail call i32 @fgetc(i32* %8)
%41 = load i32, i32* @EOF
%42 = icmp eq i32 %40, %41
br i1 %42, label %43, label %19
43: ; preds = %37, %4
%44 = phi i32 [ 0, %4 ], [ %38, %37 ]
%45 = sext i32 %44 to i64
%46 = getelementptr inbounds i8, i8* %13, i64 %45
store i8 0, i8* %46
%47 = tail call i32 @fclose(i32* %8)
ret i8* %13
}