#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 */
scalar_t__ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ free(char *);
char *ic_read_slurp_file(int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ puts(char *);
char *ic_read_slurp_filename(char *filename) {
  FILE *file;
  char *buffer;
  file = fopen(filename, "r");
  if (!file) {
    printf("ERROR: fopen failed in read_slurp_filename for file '%s'\n",
           filename);
    perror("read_slurp_filename::fopen");
    return 0;
  }
  buffer = ic_read_slurp_file(file);
  if (!buffer) {
    puts("Error: read_slurp_filename::read_slurp_file failed");
    fclose(file);
    return 0;
  }
  if (fclose(file)) {
    puts("Error: read_slurp_filename::fclose failed");
    free(buffer);
    return 0;
  }
  return buffer;
}


x86 assembly (clang, -O0):
<ic_read_slurp_filename>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe79(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11c4 <ic_read_slurp_filename+0x54>
mov    -0x10(%rbp),%rsi
lea    0xe5c(%rip),%rdi
call   1070 <printf@plt>
lea    0xe8a(%rip),%rdi
call   1090 <perror@plt>
movq   $0x0,-0x8(%rbp)
jmp    123b <ic_read_slurp_filename+0xcb>
mov    -0x18(%rbp),%rdi
call   1040 <ic_read_slurp_file@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11fe <ic_read_slurp_filename+0x8e>
lea    0xe74(%rip),%rdi
call   1050 <puts@plt>
mov    -0x18(%rbp),%rdi
call   1060 <fclose@plt>
movq   $0x0,-0x8(%rbp)
jmp    123b <ic_read_slurp_filename+0xcb>
mov    -0x18(%rbp),%rdi
call   1060 <fclose@plt>
cmp    $0x0,%rax
je     1233 <ic_read_slurp_filename+0xc3>
lea    0xe72(%rip),%rdi
call   1050 <puts@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    123b <ic_read_slurp_filename+0xcb>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ic_read_slurp_filename>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
lea    0xe82(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     11e0 <ic_read_slurp_filename+0x70>
mov    %rax,%rbx
mov    %rax,%rdi
call   1040 <ic_read_slurp_file@plt>
test   %rax,%rax
je     11be <ic_read_slurp_filename+0x4e>
mov    %rax,%r14
mov    %rbx,%rdi
call   1060 <fclose@plt>
test   %rax,%rax
je     11d5 <ic_read_slurp_filename+0x65>
lea    0xedb(%rip),%rdi
call   1050 <puts@plt>
mov    %r14,%rdi
call   1030 <free@plt>
jmp    11d2 <ic_read_slurp_filename+0x62>
lea    0xe92(%rip),%rdi
call   1050 <puts@plt>
mov    %rbx,%rdi
call   1060 <fclose@plt>
xor    %r14d,%r14d
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
lea    0xe1b(%rip),%rdi
mov    %r14,%rsi
call   1070 <printf@plt>
lea    0xe46(%rip),%rdi
call   1090 <perror@plt>
jmp    11d2 <ic_read_slurp_filename+0x62>

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@.str.1 = [58 x i8] c"ERROR: fopen failed in read_slurp_filename for file '%s'\0A\00"
@.str.2 = [27 x i8] c"read_slurp_filename::fopen\00"
@.str.3 = [51 x i8] c"Error: read_slurp_filename::read_slurp_file failed\00"
@.str.4 = [42 x i8] c"Error: read_slurp_filename::fclose failed\00"
define i8* @ic_read_slurp_filename(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca i8*
store i8* %0, i8** %3
%6 = load i8*, i8** %3
%7 = call i32* @fopen(i8* %6, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %7, i32** %4
%8 = load i32*, i32** %4
%9 = icmp ne i32* %8, null
br i1 %9, label %14, label %10
10: ; preds = %1
%11 = load i8*, i8** %3
%12 = call i32 @printf(i8* getelementptr inbounds ([58 x i8], [58 x i8]* @.str.1, i64 0, i64 0), i8* %11)
%13 = call i32 @perror(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0))
store i8* null, i8** %2
br label %33
14: ; preds = %1
%15 = load i32*, i32** %4
%16 = call i8* @ic_read_slurp_file(i32* %15)
store i8* %16, i8** %5
%17 = load i8*, i8** %5
%18 = icmp ne i8* %17, null
br i1 %18, label %23, label %19
19: ; preds = %14
%20 = call i32 @puts(i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.3, i64 0, i64 0))
%21 = load i32*, i32** %4
%22 = call i64 @fclose(i32* %21)
store i8* null, i8** %2
br label %33
23: ; preds = %14
%24 = load i32*, i32** %4
%25 = call i64 @fclose(i32* %24)
%26 = icmp ne i64 %25, 0
br i1 %26, label %27, label %31
27: ; preds = %23
%28 = call i32 @puts(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.4, i64 0, i64 0))
%29 = load i8*, i8** %5
%30 = call i32 @free(i8* %29)
store i8* null, i8** %2
br label %33
31: ; preds = %23
%32 = load i8*, i8** %5
store i8* %32, i8** %2
br label %33
33: ; preds = %31, %27, %19, %10
%34 = load i8*, i8** %2
ret i8* %34
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@.str.1 = [58 x i8] c"ERROR: fopen failed in read_slurp_filename for file '%s'\0A\00"
@.str.2 = [27 x i8] c"read_slurp_filename::fopen\00"
@.str.3 = [51 x i8] c"Error: read_slurp_filename::read_slurp_file failed\00"
@.str.4 = [42 x i8] c"Error: read_slurp_filename::fclose failed\00"
define i8* @ic_read_slurp_filename(i8* %0) {
%2 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%3 = icmp eq i32* %2, null
br i1 %3, label %4, label %7
4: ; preds = %1
%5 = tail call i32 @printf(i8* getelementptr inbounds ([58 x i8], [58 x i8]* @.str.1, i64 0, i64 0), i8* %0)
%6 = tail call i32 @perror(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0))
br label %19
7: ; preds = %1
%8 = tail call i8* @ic_read_slurp_file(i32* %2)
%9 = icmp eq i8* %8, null
br i1 %9, label %10, label %13
10: ; preds = %7
%11 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([51 x i8], [51 x i8]* @.str.3, i64 0, i64 0))
%12 = tail call i64 @fclose(i32* %2)
br label %19
13: ; preds = %7
%14 = tail call i64 @fclose(i32* %2)
%15 = icmp eq i64 %14, 0
br i1 %15, label %19, label %16
16: ; preds = %13
%17 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([42 x i8], [42 x i8]* @.str.4, i64 0, i64 0))
%18 = tail call i32 @free(i8* %8)
br label %19
19: ; preds = %13, %16, %10, %4
%20 = phi i8* [ null, %16 ], [ null, %10 ], [ null, %4 ], [ %8, %13 ]
ret i8* %20
}