#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*/ *);
scalar_t__ fgetsnolfs(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
scalar_t__ malloc(scalar_t__);
int /*<<< orphan*/ strcat(char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
scalar_t__ strlen(char *);
char *readfile(char *fn) {
  FILE *f;
  char buf[1024], nbuf[10240], *b;
  long s;
  f = fopen(fn, "r");
  if (!f)
    return NULL;
  nbuf[0] = 0;
  while (fgetsnolfs(buf, 1024, f)) {
    strcat(nbuf, buf);
    strcat(nbuf, "\n");
  }
  fclose(f);
  b = (char *)malloc(strlen(nbuf) + 1);
  strcpy(b, nbuf);
  return b;
}


x86 assembly (clang, -O0):
<readfile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x2c30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe76(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11ab <readfile+0x3b>
movq   $0x0,-0x8(%rbp)
jmp    1242 <readfile+0xd2>
movb   $0x0,-0x2c20(%rbp)
lea    -0x420(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    $0x400,%esi
call   1060 <fgetsnolfs@plt>
cmp    $0x0,%rax
je     11fc <readfile+0x8c>
lea    -0x2c20(%rbp),%rdi
lea    -0x420(%rbp),%rsi
call   1090 <strcat@plt>
lea    -0x2c20(%rbp),%rdi
lea    0xe10(%rip),%rsi
call   1090 <strcat@plt>
jmp    11b2 <readfile+0x42>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
lea    -0x2c20(%rbp),%rdi
call   1050 <strlen@plt>
mov    %rax,%rdi
add    $0x1,%rdi
call   1070 <malloc@plt>
mov    %rax,-0x2c28(%rbp)
mov    -0x2c28(%rbp),%rdi
lea    -0x2c20(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x2c28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x2c30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readfile>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x2c08,%rsp
lea    0xe7b(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     122c <readfile+0xbc>
mov    %rax,%r14
movb   $0x0,0x400(%rsp)
mov    %rsp,%rdi
mov    $0x400,%esi
mov    %rax,%rdx
call   1060 <fgetsnolfs@plt>
test   %rax,%rax
je     11fb <readfile+0x8b>
lea    0x400(%rsp),%r15
mov    %rsp,%rbx
lea    0xe3d(%rip),%r12
cs nopw 0x0(%rax,%rax,1)
nop
mov    %r15,%rdi
mov    %rbx,%rsi
call   1090 <strcat@plt>
mov    %r15,%rdi
mov    %r12,%rsi
call   1090 <strcat@plt>
mov    %rbx,%rdi
mov    $0x400,%esi
mov    %r14,%rdx
call   1060 <fgetsnolfs@plt>
test   %rax,%rax
jne    11d0 <readfile+0x60>
mov    %r14,%rdi
call   1040 <fclose@plt>
lea    0x400(%rsp),%r14
mov    %r14,%rdi
call   1050 <strlen@plt>
lea    0x1(%rax),%rdi
call   1070 <malloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r14,%rsi
call   1030 <strcpy@plt>
jmp    122e <readfile+0xbe>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x2c08,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@.str.1 = [2 x i8] c"\0A\00"
define i8* @readfile(i8* nocapture %0) {
%2 = alloca [1024 x i8]
%3 = alloca [10240 x i8]
%4 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%5 = getelementptr inbounds [10240 x i8], [10240 x i8]* %3, i64 0, i64 0
%6 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%7 = icmp eq i32* %6, null
br i1 %7, label %23, label %8
8: ; preds = %1
store i8 0, i8* %5
%9 = call i64 @fgetsnolfs(i8* %4, i32 1024, i32* %6)
%10 = icmp eq i64 %9, 0
br i1 %10, label %16, label %11
11: ; preds = %8, %11
%12 = call i32 @strcat(i8* %5, i8* %4)
%13 = call i32 @strcat(i8* %5, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%14 = call i64 @fgetsnolfs(i8* %4, i32 1024, i32* %6)
%15 = icmp eq i64 %14, 0
br i1 %15, label %16, label %11
16: ; preds = %11, %8
%17 = call i32 @fclose(i32* %6)
%18 = call i64 @strlen(i8* %5)
%19 = add nsw i64 %18, 1
%20 = call i64 @malloc(i64 %19)
%21 = inttoptr i64 %20 to i8*
%22 = call i32 @strcpy(i8* %21, i8* %5)
br label %23
23: ; preds = %1, %16
%24 = phi i8* [ %21, %16 ], [ null, %1 ]
ret i8* %24
}