#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 */
struct stat {
  scalar_t__ st_size;
  int /*<<< orphan*/ st_mode;
};
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ S_ISREG(int /*<<< orphan*/);
char *SafeMalloc(scalar_t__);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int fread(char *, int, scalar_t__, int /*<<< orphan*/ *);
scalar_t__ stat(char const *, struct stat *);
char *ReadFile(const char *zFilename) {
  struct stat sStat;
  FILE *pIn;
  char *zBuf;
  int n;
  if (stat(zFilename, &sStat) != 0 || !S_ISREG(sStat.st_mode)) {
    return 0;
  }
  pIn = fopen(zFilename, "r");
  if (pIn == 0) {
    return 0;
  }
  zBuf = SafeMalloc(sStat.st_size + 1);
  n = fread(zBuf, 1, sStat.st_size, pIn);
  zBuf[n] = 0;
  fclose(pIn);
  return zBuf;
}


x86 assembly (clang, -O0):
<ReadFile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   1050 <stat@plt>
cmp    $0x0,%rax
jne    1194 <ReadFile+0x34>
mov    -0x18(%rbp),%edi
call   1060 <S_ISREG@plt>
cmp    $0x0,%eax
jne    11a1 <ReadFile+0x41>
movq   $0x0,-0x8(%rbp)
jmp    1214 <ReadFile+0xb4>
mov    -0x10(%rbp),%rdi
lea    0xe54(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11cd <ReadFile+0x6d>
movq   $0x0,-0x8(%rbp)
jmp    1214 <ReadFile+0xb4>
mov    -0x20(%rbp),%rdi
add    $0x1,%rdi
call   1070 <SafeMalloc@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x20(%rbp),%rdx
mov    -0x28(%rbp),%rcx
mov    $0x1,%esi
call   1030 <fread@plt>
mov    %eax,-0x34(%rbp)
mov    -0x30(%rbp),%rax
movslq -0x34(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x28(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ReadFile>:
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rdi,%rbx
lea    0x8(%rsp),%rsi
call   1050 <stat@plt>
test   %rax,%rax
je     1186 <ReadFile+0x26>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret
mov    0x10(%rsp),%edi
call   1060 <S_ISREG@plt>
test   %eax,%eax
je     1179 <ReadFile+0x19>
lea    0xe66(%rip),%rsi
mov    %rbx,%rdi
call   1080 <fopen@plt>
test   %rax,%rax
je     1179 <ReadFile+0x19>
mov    %rax,%r14
mov    0x8(%rsp),%rdi
add    $0x1,%rdi
call   1070 <SafeMalloc@plt>
mov    %rax,%rbx
mov    0x8(%rsp),%rdx
mov    %rax,%rdi
mov    $0x1,%esi
mov    %r14,%rcx
call   1030 <fread@plt>
cltq
movb   $0x0,(%rbx,%rax,1)
mov    %r14,%rdi
call   1040 <fclose@plt>
jmp    117b <ReadFile+0x1b>

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

LLVM IR (-O2):
%struct.stat = type { i64, i32 }
@.str = [2 x i8] c"r\00"
define i8* @ReadFile(i8* %0) {
%2 = alloca %struct.stat
%3 = bitcast %struct.stat* %2 to i8*
%4 = call i64 @stat(i8* %0, %struct.stat* %2)
%5 = icmp eq i64 %4, 0
br i1 %5, label %6, label %24
6: ; preds = %1
%7 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 1
%8 = load i32, i32* %7
%9 = call i32 @S_ISREG(i32 %8)
%10 = icmp eq i32 %9, 0
br i1 %10, label %24, label %11
11: ; preds = %6
%12 = call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%13 = icmp eq i32* %12, null
br i1 %13, label %24, label %14
14: ; preds = %11
%15 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
%16 = load i64, i64* %15
%17 = add nsw i64 %16, 1
%18 = call i8* @SafeMalloc(i64 %17)
%19 = load i64, i64* %15
%20 = call i32 @fread(i8* %18, i32 1, i64 %19, i32* %12)
%21 = sext i32 %20 to i64
%22 = getelementptr inbounds i8, i8* %18, i64 %21
store i8 0, i8* %22
%23 = call i32 @fclose(i32* %12)
br label %24
24: ; preds = %11, %1, %6, %14
%25 = phi i8* [ %18, %14 ], [ null, %6 ], [ null, %1 ], [ null, %11 ]
ret i8* %25
}