#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 const, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ trim(char *);
char *file_get_contents(const char *filename, char *s, const int size) {
  char *res;
  FILE *f;
  if (!(f = fopen(filename, "r")))
    return (NULL);
  res = fgets(s, size, f);
  fclose(f);
  if (res)
    trim(res);
  return (res);
}


x86 assembly (clang, -O0):
<file_get_contents>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xea2(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
jne    117e <file_get_contents+0x3e>
movq   $0x0,-0x8(%rbp)
jmp    11b7 <file_get_contents+0x77>
mov    -0x18(%rbp),%rdi
mov    -0x1c(%rbp),%esi
mov    -0x30(%rbp),%rdx
call   1050 <fgets@plt>
mov    %rax,-0x28(%rbp)
mov    -0x30(%rbp),%rdi
call   1030 <fclose@plt>
cmpq   $0x0,-0x28(%rbp)
je     11af <file_get_contents+0x6f>
mov    -0x28(%rbp),%rdi
call   1040 <trim@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<file_get_contents>:
push   %rbp
push   %r14
push   %rbx
mov    %edx,%r14d
mov    %rsi,%rbx
lea    0xeaf(%rip),%rsi
call   1060 <fopen@plt>
test   %rax,%rax
je     1186 <file_get_contents+0x46>
mov    %rax,%rbp
mov    %rbx,%rdi
mov    %r14d,%esi
mov    %rax,%rdx
call   1050 <fgets@plt>
mov    %rax,%rbx
mov    %rbp,%rdi
call   1030 <fclose@plt>
test   %rbx,%rbx
je     1186 <file_get_contents+0x46>
mov    %rbx,%rdi
call   1040 <trim@plt>
jmp    1188 <file_get_contents+0x48>
xor    %ebx,%ebx
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
define i8* @file_get_contents(i8* %0, i8* %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i32*
store i8* %0, i8** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%10 = load i8*, i8** %5
%11 = call i32* @fopen(i8* %10, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %11, i32** %9
%12 = icmp ne i32* %11, null
br i1 %12, label %14, label %13
13: ; preds = %3
store i8* null, i8** %4
br label %28
14: ; preds = %3
%15 = load i8*, i8** %6
%16 = load i32, i32* %7
%17 = load i32*, i32** %9
%18 = call i8* @fgets(i8* %15, i32 %16, i32* %17)
store i8* %18, i8** %8
%19 = load i32*, i32** %9
%20 = call i32 @fclose(i32* %19)
%21 = load i8*, i8** %8
%22 = icmp ne i8* %21, null
br i1 %22, label %23, label %26
23: ; preds = %14
%24 = load i8*, i8** %8
%25 = call i32 @trim(i8* %24)
br label %26
26: ; preds = %23, %14
%27 = load i8*, i8** %8
store i8* %27, i8** %4
br label %28
28: ; preds = %26, %13
%29 = load i8*, i8** %4
ret i8* %29
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
define i8* @file_get_contents(i8* nocapture %0, i8* %1, i32 %2) {
%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 = %3
%7 = tail call i8* @fgets(i8* %1, i32 %2, i32* %4)
%8 = tail call i32 @fclose(i32* %4)
%9 = icmp eq i8* %7, null
br i1 %9, label %12, label %10
10: ; preds = %6
%11 = tail call i32 @trim(i8* %7)
br label %12
12: ; preds = %6, %10, %3
%13 = phi i8* [ null, %3 ], [ %7, %10 ], [ null, %6 ]
ret i8* %13
}