#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*/ buffer;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *const, char *);
char *strdup(char *);
char *file_get(const char *const file) {
  FILE *fp;
  char buffer[4096];
  if ((fp = fopen(file, "r")) == NULL)
    return NULL;
  if (fgets(buffer, sizeof(buffer), fp) == NULL) {
    (void)fclose(fp);
    return NULL;
  }
  (void)fclose(fp);
  return strdup(buffer);
}


x86 assembly (clang, -O0):
<file_get>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1020,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xea6(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    117a <file_get+0x3a>
movq   $0x0,-0x8(%rbp)
jmp    11c8 <file_get+0x88>
lea    -0x1020(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    $0x1000,%esi
call   1040 <fgets@plt>
cmp    $0x0,%rax
jne    11af <file_get+0x6f>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
movq   $0x0,-0x8(%rbp)
jmp    11c8 <file_get+0x88>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
lea    -0x1020(%rbp),%rdi
call   1060 <strdup@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x1020,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<file_get>:
push   %r14
push   %rbx
sub    $0x1008,%rsp
lea    0xeaf(%rip),%rsi
call   1050 <fopen@plt>
test   %rax,%rax
je     1188 <file_get+0x48>
mov    %rax,%rbx
mov    %rsp,%rdi
mov    $0x1000,%esi
mov    %rax,%rdx
call   1040 <fgets@plt>
mov    %rax,%r14
mov    %rbx,%rdi
call   1030 <fclose@plt>
test   %r14,%r14
je     1188 <file_get+0x48>
mov    %rsp,%rdi
call   1060 <strdup@plt>
jmp    118a <file_get+0x4a>
xor    %eax,%eax
add    $0x1008,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
define i8* @file_get(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca [4096 x 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 = icmp eq i32* %7, null
br i1 %8, label %9, label %10
9: ; preds = %1
store i8* null, i8** %2
br label %23
10: ; preds = %1
%11 = getelementptr inbounds [4096 x i8], [4096 x i8]* %5, i64 0, i64 0
%12 = load i32*, i32** %4
%13 = call i32* @fgets(i8* %11, i32 4096, i32* %12)
%14 = icmp eq i32* %13, null
br i1 %14, label %15, label %18
15: ; preds = %10
%16 = load i32*, i32** %4
%17 = call i32 @fclose(i32* %16)
store i8* null, i8** %2
br label %23
18: ; preds = %10
%19 = load i32*, i32** %4
%20 = call i32 @fclose(i32* %19)
%21 = getelementptr inbounds [4096 x i8], [4096 x i8]* %5, i64 0, i64 0
%22 = call i8* @strdup(i8* %21)
store i8* %22, i8** %2
br label %23
23: ; preds = %18, %15, %9
%24 = load i8*, i8** %2
ret i8* %24
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
define noalias i8* @file_get(i8* nocapture %0) {
%2 = alloca [4096 x i8]
%3 = getelementptr inbounds [4096 x i8], [4096 x i8]* %2, i64 0, i64 0
%4 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%5 = icmp eq i32* %4, null
br i1 %5, label %12, label %6
6: ; preds = %1
%7 = call i32* @fgets(i8* %3, i32 4096, i32* %4)
%8 = icmp eq i32* %7, null
%9 = call i32 @fclose(i32* %4)
br i1 %8, label %12, label %10
10: ; preds = %6
%11 = call i8* @strdup(i8* %3)
br label %12
12: ; preds = %6, %1, %10
%13 = phi i8* [ %11, %10 ], [ null, %1 ], [ null, %6 ]
ret i8* %13
}