#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*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
char *malloc(int);
char *realloc(char *, int);
int /*<<< orphan*/ strcpy(char *, char *);
int strlen(char *);
int leeFile(char *file, char **template) {
  char *buf;
  FILE *in = fopen(file, "r");
  if (!in)
    return (-1);
  buf = malloc(1);
  strcpy(buf, "");
  while (1) {
    buf = realloc(buf, strlen(buf) + 1001);
    if (!fgets(buf + strlen(buf), 1000, in))
      break;
  }
  fclose(in);
  *template = buf;
  return (strlen(buf));
}


x86 assembly (clang, -O0):
<leeFile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe75(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11ab <leeFile+0x3b>
movl   $0xffffffff,-0x4(%rbp)
jmp    124a <leeFile+0xda>
mov    $0x1,%edi
call   1070 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
lea    0xe3d(%rip),%rsi
call   1030 <strcpy@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x20(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x38(%rbp),%rdi
mov    %eax,%esi
add    $0x3e9,%esi
call   1080 <realloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x20(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x30(%rbp),%rdi
cltq
add    %rax,%rdi
mov    -0x28(%rbp),%rdx
mov    $0x3e8,%esi
call   1060 <fgets@plt>
cmp    $0x0,%eax
jne    1225 <leeFile+0xb5>
jmp    122a <leeFile+0xba>
jmp    11c9 <leeFile+0x59>
mov    -0x28(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x20(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x20(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<leeFile>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%r14
lea    0xe81(%rip),%rsi
call   1090 <fopen@plt>
test   %rax,%rax
je     1200 <leeFile+0x90>
mov    %rax,%r15
mov    $0x1,%edi
call   1070 <malloc@plt>
mov    %rax,%rbx
lea    0xe61(%rip),%rsi
mov    %rax,%rdi
call   1030 <strcpy@plt>
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rdi
call   1050 <strlen@plt>
lea    0x3e9(%rax),%esi
mov    %rbx,%rdi
call   1080 <realloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
call   1050 <strlen@plt>
movslq %eax,%rdi
add    %rbx,%rdi
mov    $0x3e8,%esi
mov    %r15,%rdx
call   1060 <fgets@plt>
test   %eax,%eax
jne    11b0 <leeFile+0x40>
mov    %r15,%rdi
call   1040 <fclose@plt>
mov    %rbx,(%r14)
mov    %rbx,%rdi
pop    %rbx
pop    %r14
pop    %r15
jmp    1050 <strlen@plt>
mov    $0xffffffff,%eax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@.str.1 = [1 x i8] zeroinitializer
define i32 @leeFile(i8* %0, i8** %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i32*
store i8* %0, i8** %4
store i8** %1, i8*** %5
%8 = load i8*, i8** %4
%9 = call i32* @fopen(i8* %8, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %9, i32** %7
%10 = load i32*, i32** %7
%11 = icmp ne i32* %10, null
br i1 %11, label %13, label %12
12: ; preds = %2
store i32 -1, i32* %3
br label %40
13: ; preds = %2
%14 = call i8* @malloc(i32 1)
store i8* %14, i8** %6
%15 = load i8*, i8** %6
%16 = call i32 @strcpy(i8* %15, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.1, i64 0, i64 0))
br label %17
17: ; preds = %13, %32
%18 = load i8*, i8** %6
%19 = load i8*, i8** %6
%20 = call i32 @strlen(i8* %19)
%21 = add nsw i32 %20, 1001
%22 = call i8* @realloc(i8* %18, i32 %21)
store i8* %22, i8** %6
%23 = load i8*, i8** %6
%24 = load i8*, i8** %6
%25 = call i32 @strlen(i8* %24)
%26 = sext i32 %25 to i64
%27 = getelementptr inbounds i8, i8* %23, i64 %26
%28 = load i32*, i32** %7
%29 = call i32 @fgets(i8* %27, i32 1000, i32* %28)
%30 = icmp ne i32 %29, 0
br i1 %30, label %32, label %31
31: ; preds = %17
br label %33
32: ; preds = %17
br label %17
33: ; preds = %31
%34 = load i32*, i32** %7
%35 = call i32 @fclose(i32* %34)
%36 = load i8*, i8** %6
%37 = load i8**, i8*** %5
store i8* %36, i8** %37
%38 = load i8*, i8** %6
%39 = call i32 @strlen(i8* %38)
store i32 %39, i32* %3
br label %40
40: ; preds = %33, %12
%41 = load i32, i32* %3
ret i32 %41
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@.str.1 = [1 x i8] zeroinitializer
define i32 @leeFile(i8* nocapture %0, i8** nocapture writeonly %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 %21, label %5
5: ; preds = %2
%6 = tail call dereferenceable_or_null(1) i8* @malloc(i32 1)
%7 = tail call i32 @strcpy(i8* %6, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.1, i64 0, i64 0))
br label %8
8: ; preds = %8, %5
%9 = phi i8* [ %6, %5 ], [ %12, %8 ]
%10 = tail call i32 @strlen(i8* %9)
%11 = add nsw i32 %10, 1001
%12 = tail call i8* @realloc(i8* %9, i32 %11)
%13 = tail call i32 @strlen(i8* %12)
%14 = sext i32 %13 to i64
%15 = getelementptr inbounds i8, i8* %12, i64 %14
%16 = tail call i32 @fgets(i8* %15, i32 1000, i32* %3)
%17 = icmp eq i32 %16, 0
br i1 %17, label %18, label %8
18: ; preds = %8
%19 = tail call i32 @fclose(i32* %3)
store i8* %12, i8** %1
%20 = tail call i32 @strlen(i8* %12)
br label %21
21: ; preds = %2, %18
%22 = phi i32 [ %20, %18 ], [ -1, %2 ]
ret i32 %22
}