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

/* Variables and functions */
char *Load(int /*<<< orphan*/ *);
scalar_t__ S_ISDIR(int /*<<< orphan*/);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ printf(char *, char const *, ...);
scalar_t__ stat(char const *, struct stat *);
int load(const char filename[]) {
  char *error;
  struct stat filestat;
  FILE *inputfile;
  if (stat(filename, &filestat) || S_ISDIR(filestat.st_mode) ||
      !(inputfile = fopen(filename, "rt"))) {
    printf("Error opening file %s\n", filename);
    return 0;
  }
  error = Load(inputfile);
  fclose(inputfile);
  if (error) {
    printf("Error reading file %s:\n%s\n", filename, error);
    free(error);
    return 0;
  }
  return 1;
}


x86 assembly (clang, -O0):
<load>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   1080 <stat@plt>
cmp    $0x0,%rax
jne    11c3 <load+0x53>
mov    -0x20(%rbp),%edi
call   1070 <S_ISDIR@plt>
cmp    $0x0,%rax
jne    11c3 <load+0x53>
mov    -0x10(%rbp),%rdi
lea    0xe50(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    11e1 <load+0x71>
mov    -0x10(%rbp),%rsi
lea    0xe35(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1234 <load+0xc4>
mov    -0x28(%rbp),%rdi
call   1040 <Load@plt>
mov    %rax,-0x18(%rbp)
mov    -0x28(%rbp),%rdi
call   1050 <fclose@plt>
cmpq   $0x0,-0x18(%rbp)
je     122d <load+0xbd>
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
lea    0xe09(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
movl   $0x0,-0x4(%rbp)
jmp    1234 <load+0xc4>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<load>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%rbx
lea    0x8(%rsp),%rsi
call   1080 <stat@plt>
test   %rax,%rax
jne    1198 <load+0x28>
mov    0x8(%rsp),%edi
call   1070 <S_ISDIR@plt>
test   %rax,%rax
je     11b6 <load+0x46>
lea    0xe64(%rip),%rdi
xor    %ebp,%ebp
mov    %rbx,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xe43(%rip),%rsi
mov    %rbx,%rdi
call   1090 <fopen@plt>
test   %rax,%rax
je     1198 <load+0x28>
mov    %rax,%rbp
mov    %rax,%rdi
call   1040 <Load@plt>
mov    %rax,%r14
mov    %rbp,%rdi
call   1050 <fclose@plt>
test   %r14,%r14
je     1205 <load+0x95>
lea    0xe2e(%rip),%rdi
xor    %ebp,%ebp
mov    %rbx,%rsi
mov    %r14,%rdx
xor    %eax,%eax
call   1060 <printf@plt>
mov    %r14,%rdi
call   1030 <free@plt>
jmp    11ab <load+0x3b>
mov    $0x1,%ebp
jmp    11ab <load+0x3b>

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [3 x i8] c"rt\00"
@.str.1 = [23 x i8] c"Error opening file %s\0A\00"
@.str.2 = [27 x i8] c"Error reading file %s:\0A%s\0A\00"
define i32 @load(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca %struct.stat
%6 = alloca i32*
store i8* %0, i8** %3
%7 = load i8*, i8** %3
%8 = call i64 @stat(i8* %7, %struct.stat* %5)
%9 = icmp ne i64 %8, 0
br i1 %9, label %19, label %10
10: ; preds = %1
%11 = getelementptr inbounds %struct.stat, %struct.stat* %5, i32 0, i32 0
%12 = load i32, i32* %11
%13 = call i64 @S_ISDIR(i32 %12)
%14 = icmp ne i64 %13, 0
br i1 %14, label %19, label %15
15: ; preds = %10
%16 = load i8*, i8** %3
%17 = call i32* @fopen(i8* %16, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %17, i32** %6
%18 = icmp ne i32* %17, null
br i1 %18, label %22, label %19
19: ; preds = %15, %10, %1
%20 = load i8*, i8** %3
%21 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i8* %20)
store i32 0, i32* %2
br label %36
22: ; preds = %15
%23 = load i32*, i32** %6
%24 = call i8* @Load(i32* %23)
store i8* %24, i8** %4
%25 = load i32*, i32** %6
%26 = call i32 @fclose(i32* %25)
%27 = load i8*, i8** %4
%28 = icmp ne i8* %27, null
br i1 %28, label %29, label %35
29: ; preds = %22
%30 = load i8*, i8** %3
%31 = load i8*, i8** %4
%32 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0), i8* %30, i8* %31)
%33 = load i8*, i8** %4
%34 = call i32 @free(i8* %33)
store i32 0, i32* %2
br label %36
35: ; preds = %22
store i32 1, i32* %2
br label %36
36: ; preds = %35, %29, %19
%37 = load i32, i32* %2
ret i32 %37
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [3 x i8] c"rt\00"
@.str.1 = [23 x i8] c"Error opening file %s\0A\00"
@.str.2 = [27 x i8] c"Error reading file %s:\0A%s\0A\00"
define i32 @load(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 %14
6: ; preds = %1
%7 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
%8 = load i32, i32* %7
%9 = call i64 @S_ISDIR(i32 %8)
%10 = icmp eq i64 %9, 0
br i1 %10, label %11, label %14
11: ; preds = %6
%12 = call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%13 = icmp eq i32* %12, null
br i1 %13, label %14, label %16
14: ; preds = %11, %6, %1
%15 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %23
16: ; preds = %11
%17 = call i8* @Load(i32* %12)
%18 = call i32 @fclose(i32* %12)
%19 = icmp eq i8* %17, null
br i1 %19, label %23, label %20
20: ; preds = %16
%21 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0), i8* %0, i8* %17)
%22 = call i32 @free(i8* %17)
br label %23
23: ; preds = %16, %20, %14
%24 = phi i32 [ 0, %14 ], [ 0, %20 ], [ 1, %16 ]
ret i32 %24
}