#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*/ *);
char *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ printf(char *, char *);
char *readFile(char *filename, char *mode) {
  FILE *file;
  char buffer[1024] = "", *str;
  file = fopen(filename, mode);
  if (file == NULL) {
    printf("error: cannot open the file %s\n", filename);
    return "";
  }
  str = fgets(buffer, 1024, file);
  fclose(file);
  return str;
}


x86 assembly (clang, -O0):
<readFile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
lea    -0x420(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1050 <memset@plt>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11b2 <readFile+0x62>
mov    -0x10(%rbp),%rsi
lea    0xe63(%rip),%rdi
call   1040 <printf@plt>
lea    0xe76(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    11e2 <readFile+0x92>
lea    -0x420(%rbp),%rdi
mov    -0x20(%rbp),%rdx
mov    $0x400,%esi
call   1060 <fgets@plt>
mov    %rax,-0x428(%rbp)
mov    -0x20(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x428(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x430,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readFile>:
push   %r14
push   %rbx
sub    $0x408,%rsp
mov    %rsi,%rbx
mov    %rdi,%r14
mov    %rsp,%rdi
mov    $0x400,%edx
xor    %esi,%esi
call   1050 <memset@plt>
mov    %r14,%rdi
mov    %rbx,%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     119f <readFile+0x4f>
mov    %rax,%rbx
mov    %rsp,%rdi
mov    $0x400,%esi
mov    %rax,%rdx
call   1060 <fgets@plt>
mov    %rax,%r14
mov    %rbx,%rdi
call   1030 <fclose@plt>
jmp    11b5 <readFile+0x65>
lea    0xe5a(%rip),%rdi
mov    %r14,%rsi
call   1040 <printf@plt>
lea    0xe6a(%rip),%r14
mov    %r14,%rax
add    $0x408,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [32 x i8] c"error: cannot open the file %s\0A\00"
@.str.1 = [1 x i8] zeroinitializer
define i8* @readFile(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32*
%7 = alloca [1024 x i8]
%8 = alloca i8*
store i8* %0, i8** %4
store i8* %1, i8** %5
%9 = bitcast [1024 x i8]* %7 to i8*
%10 = load i8*, i8** %4
%11 = load i8*, i8** %5
%12 = call i32* @fopen(i8* %10, i8* %11)
store i32* %12, i32** %6
%13 = load i32*, i32** %6
%14 = icmp eq i32* %13, null
br i1 %14, label %15, label %18
15: ; preds = %2
%16 = load i8*, i8** %4
%17 = call i32 @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), i8* %16)
store i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.1, i64 0, i64 0), i8** %3
br label %25
18: ; preds = %2
%19 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%20 = load i32*, i32** %6
%21 = call i8* @fgets(i8* %19, i32 1024, i32* %20)
store i8* %21, i8** %8
%22 = load i32*, i32** %6
%23 = call i32 @fclose(i32* %22)
%24 = load i8*, i8** %8
store i8* %24, i8** %3
br label %25
25: ; preds = %18, %15
%26 = load i8*, i8** %3
ret i8* %26
}

LLVM IR (-O2):
@.str = [32 x i8] c"error: cannot open the file %s\0A\00"
@.str.1 = [1 x i8] zeroinitializer
define i8* @readFile(i8* %0, i8* nocapture %1) {
%3 = alloca [1024 x i8]
%4 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%5 = tail call i32* @fopen(i8* %0, i8* %1)
%6 = icmp eq i32* %5, null
br i1 %6, label %7, label %9
7: ; preds = %2
%8 = tail call i32 @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %12
9: ; preds = %2
%10 = call i8* @fgets(i8* %4, i32 1024, i32* %5)
%11 = call i32 @fclose(i32* %5)
br label %12
12: ; preds = %9, %7
%13 = phi i8* [ getelementptr inbounds ([1 x i8], [1 x i8]* @.str.1, i64 0, i64 0), %7 ], [ %10, %9 ]
ret i8* %13
}