#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*/ SEEK_END;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
int ftell(int /*<<< orphan*/ *);
scalar_t__ malloc(int);
int /*<<< orphan*/ rewind(int /*<<< orphan*/ *);
char *get_log_contents() {
  FILE *fpg = fopen("test.log", "r");
  if (fpg) {
    fseek(fpg, 0, SEEK_END);
    int string_size = ftell(fpg);
    rewind(fpg);
    char *buff = (char *)malloc(sizeof(char) * (string_size + 1));
    int read_size = fread(buff, sizeof(char), string_size, fpg);
    buff[string_size] = '\0';
    if (string_size != read_size) {
      buff[0] = '\0';
    }
    fclose(fpg);
    return buff;
  }
  return NULL;
}


x86 assembly (clang, -O0):
<get_log_contents>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
lea    0xe81(%rip),%rdi
lea    0xe83(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1227 <get_log_contents+0xb7>
mov    -0x10(%rbp),%rdi
mov    0x2e43(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1080 <fseek@plt>
mov    -0x10(%rbp),%rdi
call   1060 <ftell@plt>
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <rewind@plt>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
cltq
shl    $0x0,%rax
mov    %eax,%edi
call   1070 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x14(%rbp),%edx
mov    -0x10(%rbp),%rcx
mov    $0x1,%esi
call   1030 <fread@plt>
mov    %eax,-0x24(%rbp)
mov    -0x20(%rbp),%rax
movslq -0x14(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x14(%rbp),%eax
cmp    -0x24(%rbp),%eax
je     1211 <get_log_contents+0xa1>
mov    -0x20(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x10(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    122f <get_log_contents+0xbf>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_log_contents>:
push   %r15
push   %r14
push   %rbx
lea    0xe84(%rip),%rdi
lea    0xe86(%rip),%rsi
call   1090 <fopen@plt>
test   %rax,%rax
je     11ef <get_log_contents+0x7f>
mov    %rax,%rbx
mov    0x2e51(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1080 <fseek@plt>
mov    %rbx,%rdi
call   1060 <ftell@plt>
mov    %eax,%r15d
mov    %rbx,%rdi
call   1050 <rewind@plt>
lea    0x1(%r15),%edi
call   1070 <malloc@plt>
mov    %rax,%r14
mov    %rax,%rdi
mov    $0x1,%esi
mov    %r15d,%edx
mov    %rbx,%rcx
call   1030 <fread@plt>
movslq %r15d,%rcx
movb   $0x0,(%r14,%rcx,1)
cmp    %eax,%ecx
je     11e5 <get_log_contents+0x75>
movb   $0x0,(%r14)
mov    %rbx,%rdi
call   1040 <fclose@plt>
jmp    11f2 <get_log_contents+0x82>
xor    %r14d,%r14d
mov    %r14,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [9 x i8] c"test.log\00"
@.str.1 = [2 x i8] c"r\00"
@SEEK_END = global i32 0
define i8* @get_log_contents() {
%1 = alloca i8*
%2 = alloca i32*
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = call i32* @fopen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %6, i32** %2
%7 = load i32*, i32** %2
%8 = icmp ne i32* %7, null
br i1 %8, label %9, label %42
9: ; preds = %0
%10 = load i32*, i32** %2
%11 = load i32, i32* @SEEK_END
%12 = call i32 @fseek(i32* %10, i32 0, i32 %11)
%13 = load i32*, i32** %2
%14 = call i32 @ftell(i32* %13)
store i32 %14, i32* %3
%15 = load i32*, i32** %2
%16 = call i32 @rewind(i32* %15)
%17 = load i32, i32* %3
%18 = add nsw i32 %17, 1
%19 = sext i32 %18 to i64
%20 = mul i64 1, %19
%21 = trunc i64 %20 to i32
%22 = call i64 @malloc(i32 %21)
%23 = inttoptr i64 %22 to i8*
store i8* %23, i8** %4
%24 = load i8*, i8** %4
%25 = load i32, i32* %3
%26 = load i32*, i32** %2
%27 = call i32 @fread(i8* %24, i32 1, i32 %25, i32* %26)
store i32 %27, i32* %5
%28 = load i8*, i8** %4
%29 = load i32, i32* %3
%30 = sext i32 %29 to i64
%31 = getelementptr inbounds i8, i8* %28, i64 %30
store i8 0, i8* %31
%32 = load i32, i32* %3
%33 = load i32, i32* %5
%34 = icmp ne i32 %32, %33
br i1 %34, label %35, label %38
35: ; preds = %9
%36 = load i8*, i8** %4
%37 = getelementptr inbounds i8, i8* %36, i64 0
store i8 0, i8* %37
br label %38
38: ; preds = %35, %9
%39 = load i32*, i32** %2
%40 = call i32 @fclose(i32* %39)
%41 = load i8*, i8** %4
store i8* %41, i8** %1
br label %43
42: ; preds = %0
store i8* null, i8** %1
br label %43
43: ; preds = %42, %38
%44 = load i8*, i8** %1
ret i8* %44
}

LLVM IR (-O2):
@.str = [9 x i8] c"test.log\00"
@.str.1 = [2 x i8] c"r\00"
@SEEK_END = global i32 0
define i8* @get_log_contents() {
%1 = tail call i32* @fopen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%2 = icmp eq i32* %1, null
br i1 %2, label %18, label %3
3: ; preds = %0
%4 = load i32, i32* @SEEK_END
%5 = tail call i32 @fseek(i32* %1, i32 0, i32 %4)
%6 = tail call i32 @ftell(i32* %1)
%7 = tail call i32 @rewind(i32* %1)
%8 = add nsw i32 %6, 1
%9 = tail call i64 @malloc(i32 %8)
%10 = inttoptr i64 %9 to i8*
%11 = tail call i32 @fread(i8* %10, i32 1, i32 %6, i32* %1)
%12 = sext i32 %6 to i64
%13 = getelementptr inbounds i8, i8* %10, i64 %12
store i8 0, i8* %13
%14 = icmp eq i32 %6, %11
br i1 %14, label %16, label %15
15: ; preds = %3
store i8 0, i8* %10
br label %16
16: ; preds = %15, %3
%17 = tail call i32 @fclose(i32* %1)
br label %18
18: ; preds = %0, %16
%19 = phi i8* [ %10, %16 ], [ null, %0 ]
ret i8* %19
}