#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 /*<<< orphan*/ SEEK_END;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
int ftell(int /*<<< orphan*/ *);
scalar_t__ malloc(int);
int /*<<< orphan*/ perror(char *);
int read_template(char *filename, char **template_text) {
  FILE *file;
  int length, rc;
  file = fopen(filename, "r");
  if (file == NULL) {
    perror(filename);
    return -1;
  }
  fseek(file, 0, SEEK_END);
  length = ftell(file);
  fseek(file, 0, SEEK_SET);
  *template_text = (char *)malloc(length + 1);
  rc = fread(*template_text, 1, length, file);
  if (rc == 0 && length > 0) {
    perror(filename);
    fclose(file);
    free(*template_text);
    *template_text = NULL;
    return -1;
  }
  (*template_text)[rc] = '\0';
  return 0;
}


x86 assembly (clang, -O0):
<read_template>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe65(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11c4 <read_template+0x44>
mov    -0x10(%rbp),%rdi
call   10a0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1287 <read_template+0x107>
mov    -0x20(%rbp),%rdi
mov    0x2e19(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1080 <fseek@plt>
mov    -0x20(%rbp),%rdi
call   1060 <ftell@plt>
mov    %eax,-0x24(%rbp)
mov    -0x20(%rbp),%rdi
mov    0x2de9(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1080 <fseek@plt>
mov    -0x24(%rbp),%edi
add    $0x1,%edi
call   1070 <malloc@plt>
mov    %rax,%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
mov    -0x24(%rbp),%edx
mov    -0x20(%rbp),%rcx
mov    $0x1,%esi
call   1040 <fread@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jne    1271 <read_template+0xf1>
cmpl   $0x0,-0x24(%rbp)
jle    1271 <read_template+0xf1>
mov    -0x10(%rbp),%rdi
call   10a0 <perror@plt>
mov    -0x20(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
call   1030 <free@plt>
mov    -0x18(%rbp),%rax
movq   $0x0,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    1287 <read_template+0x107>
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
movslq -0x28(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_template>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rsi,%r15
mov    %rdi,%r14
lea    0xe6b(%rip),%rsi
call   1090 <fopen@plt>
test   %rax,%rax
je     120f <read_template+0x8f>
mov    %rax,%rbx
mov    0x2e3f(%rip),%rax
mov    (%rax),%edx
xor    %r12d,%r12d
mov    %rbx,%rdi
xor    %esi,%esi
call   1080 <fseek@plt>
mov    %rbx,%rdi
call   1060 <ftell@plt>
mov    %eax,%ebp
mov    0x2e0f(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1080 <fseek@plt>
lea    0x1(%rbp),%edi
call   1070 <malloc@plt>
mov    %rax,(%r15)
mov    %rax,%rdi
mov    $0x1,%esi
mov    %ebp,%edx
mov    %rbx,%rcx
call   1040 <fread@plt>
test   %ebp,%ebp
jle    11fa <read_template+0x7a>
test   %eax,%eax
je     121f <read_template+0x9f>
mov    (%r15),%rcx
cltq
movb   $0x0,(%rcx,%rax,1)
mov    %r12d,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %r14,%rdi
call   10a0 <perror@plt>
mov    $0xffffffff,%r12d
jmp    1203 <read_template+0x83>
mov    %r14,%rdi
call   10a0 <perror@plt>
mov    %rbx,%rdi
call   1050 <fclose@plt>
mov    (%r15),%rdi
call   1030 <free@plt>
movq   $0x0,(%r15)
mov    $0xffffffff,%r12d
jmp    1203 <read_template+0x83>

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
define i32 @read_template(i8* %0, i8** %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8**
%6 = alloca i32*
%7 = alloca i32
%8 = alloca i32
store i8* %0, i8** %4
store i8** %1, i8*** %5
%9 = load i8*, i8** %4
%10 = call i32* @fopen(i8* %9, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %10, i32** %6
%11 = load i32*, i32** %6
%12 = icmp eq i32* %11, null
br i1 %12, label %13, label %16
13: ; preds = %2
%14 = load i8*, i8** %4
%15 = call i32 @perror(i8* %14)
store i32 -1, i32* %3
br label %55
16: ; preds = %2
%17 = load i32*, i32** %6
%18 = load i32, i32* @SEEK_END
%19 = call i32 @fseek(i32* %17, i32 0, i32 %18)
%20 = load i32*, i32** %6
%21 = call i32 @ftell(i32* %20)
store i32 %21, i32* %7
%22 = load i32*, i32** %6
%23 = load i32, i32* @SEEK_SET
%24 = call i32 @fseek(i32* %22, i32 0, i32 %23)
%25 = load i32, i32* %7
%26 = add nsw i32 %25, 1
%27 = call i64 @malloc(i32 %26)
%28 = inttoptr i64 %27 to i8*
%29 = load i8**, i8*** %5
store i8* %28, i8** %29
%30 = load i8**, i8*** %5
%31 = load i8*, i8** %30
%32 = load i32, i32* %7
%33 = load i32*, i32** %6
%34 = call i32 @fread(i8* %31, i32 1, i32 %32, i32* %33)
store i32 %34, i32* %8
%35 = load i32, i32* %8
%36 = icmp eq i32 %35, 0
br i1 %36, label %37, label %49
37: ; preds = %16
%38 = load i32, i32* %7
%39 = icmp sgt i32 %38, 0
br i1 %39, label %40, label %49
40: ; preds = %37
%41 = load i8*, i8** %4
%42 = call i32 @perror(i8* %41)
%43 = load i32*, i32** %6
%44 = call i32 @fclose(i32* %43)
%45 = load i8**, i8*** %5
%46 = load i8*, i8** %45
%47 = call i32 @free(i8* %46)
%48 = load i8**, i8*** %5
store i8* null, i8** %48
store i32 -1, i32* %3
br label %55
49: ; preds = %37, %16
%50 = load i8**, i8*** %5
%51 = load i8*, i8** %50
%52 = load i32, i32* %8
%53 = sext i32 %52 to i64
%54 = getelementptr inbounds i8, i8* %51, i64 %53
store i8 0, i8* %54
store i32 0, i32* %3
br label %55
55: ; preds = %49, %40, %13
%56 = load i32, i32* %3
ret i32 %56
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
define i32 @read_template(i8* nocapture %0, i8** nocapture %1) {
%3 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%4 = icmp eq i32* %3, null
br i1 %4, label %5, label %7
5: ; preds = %2
%6 = tail call i32 @perror(i8* %0)
br label %29
7: ; preds = %2
%8 = load i32, i32* @SEEK_END
%9 = tail call i32 @fseek(i32* %3, i32 0, i32 %8)
%10 = tail call i32 @ftell(i32* %3)
%11 = load i32, i32* @SEEK_SET
%12 = tail call i32 @fseek(i32* %3, i32 0, i32 %11)
%13 = add nsw i32 %10, 1
%14 = tail call i64 @malloc(i32 %13)
%15 = inttoptr i64 %14 to i8*
store i8* %15, i8** %1
%16 = tail call i32 @fread(i8* %15, i32 1, i32 %10, i32* %3)
%17 = icmp eq i32 %16, 0
%18 = icmp sgt i32 %10, 0
%19 = and i1 %18, %17
br i1 %19, label %20, label %25
20: ; preds = %7
%21 = tail call i32 @perror(i8* %0)
%22 = tail call i32 @fclose(i32* %3)
%23 = load i8*, i8** %1
%24 = tail call i32 @free(i8* %23)
store i8* null, i8** %1
br label %29
25: ; preds = %7
%26 = load i8*, i8** %1
%27 = sext i32 %16 to i64
%28 = getelementptr inbounds i8, i8* %26, i64 %27
store i8 0, i8* %28
br label %29
29: ; preds = %25, %20, %5
%30 = phi i32 [ -1, %5 ], [ -1, %20 ], [ 0, %25 ]
ret i32 %30
}