#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 */
int /*<<< orphan*/ S_ISREG(int /*<<< orphan*/);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ file_error(char const *, char *);
int /*<<< orphan*/ *fopen(char const *, char const *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *);
int stat(char const *, struct stat *);
int /*<<< orphan*/ stderr;
FILE *open_file(const char *filename, const char *mode) {
  FILE *f;
  if (mode[0] == 'r') {
    struct stat fileinfo;
    if (stat(filename, &fileinfo) == -1)
      file_error(filename, "checking");
    if (!S_ISREG(fileinfo.st_mode)) {
      fprintf(stderr, "Error, file \"%s\" isn't a regular file\n", filename);
      exit(1);
    }
  }
  f = fopen(filename, mode);
  if (!f)
    file_error(filename, "opening");
  return f;
}


x86 assembly (clang, -O0):
<open_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x72,%eax
jne    11df <open_file+0x7f>
mov    -0x8(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   1040 <stat@plt>
cmp    $0xffffffff,%eax
jne    11a6 <open_file+0x46>
mov    -0x8(%rbp),%rdi
lea    0xe5f(%rip),%rsi
call   1080 <file_error@plt>
mov    -0x20(%rbp),%edi
call   1050 <S_ISREG@plt>
cmp    $0x0,%eax
jne    11da <open_file+0x7a>
mov    0x2e22(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xe3e(%rip),%rsi
call   1030 <fprintf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
jmp    11df <open_file+0x7f>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    120b <open_file+0xab>
mov    -0x8(%rbp),%rdi
lea    0xe2a(%rip),%rsi
call   1080 <file_error@plt>
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_file>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %rdi,%r14
cmpb   $0x72,(%rsi)
jne    119a <open_file+0x3a>
mov    %rsp,%rsi
mov    %r14,%rdi
call   1040 <stat@plt>
cmp    $0xffffffff,%eax
jne    118e <open_file+0x2e>
lea    0xe7a(%rip),%rsi
mov    %r14,%rdi
call   1080 <file_error@plt>
mov    (%rsp),%edi
call   1050 <S_ISREG@plt>
test   %eax,%eax
je     11c7 <open_file+0x67>
mov    %r14,%rdi
mov    %rbx,%rsi
call   1060 <fopen@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    11bc <open_file+0x5c>
lea    0xe7c(%rip),%rsi
mov    %r14,%rdi
call   1080 <file_error@plt>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
mov    0x2e12(%rip),%rax
mov    (%rax),%edi
lea    0xe32(%rip),%rsi
mov    %r14,%rdx
call   1030 <fprintf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [9 x i8] c"checking\00"
@stderr = global i32 0
@.str.1 = [39 x i8] c"Error, file \22%s\22 isn't a regular file\0A\00"
@.str.2 = [8 x i8] c"opening\00"
define i32* @open_file(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca %struct.stat
store i8* %0, i8** %3
store i8* %1, i8** %4
%7 = load i8*, i8** %4
%8 = getelementptr inbounds i8, i8* %7, i64 0
%9 = load i8, i8* %8
%10 = sext i8 %9 to i32
%11 = icmp eq i32 %10, 114
br i1 %11, label %12, label %30
12: ; preds = %2
%13 = load i8*, i8** %3
%14 = call i32 @stat(i8* %13, %struct.stat* %6)
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %19
16: ; preds = %12
%17 = load i8*, i8** %3
%18 = call i32 @file_error(i8* %17, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
br label %19
19: ; preds = %16, %12
%20 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%21 = load i32, i32* %20
%22 = call i32 @S_ISREG(i32 %21)
%23 = icmp ne i32 %22, 0
br i1 %23, label %29, label %24
24: ; preds = %19
%25 = load i32, i32* @stderr
%26 = load i8*, i8** %3
%27 = call i32 @fprintf(i32 %25, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0), i8* %26)
%28 = call i32 @exit(i32 1)
unreachable
29: ; preds = %19
br label %30
30: ; preds = %29, %2
%31 = load i8*, i8** %3
%32 = load i8*, i8** %4
%33 = call i32* @fopen(i8* %31, i8* %32)
store i32* %33, i32** %5
%34 = load i32*, i32** %5
%35 = icmp ne i32* %34, null
br i1 %35, label %39, label %36
36: ; preds = %30
%37 = load i8*, i8** %3
%38 = call i32 @file_error(i8* %37, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
br label %39
39: ; preds = %36, %30
%40 = load i32*, i32** %5
ret i32* %40
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [9 x i8] c"checking\00"
@stderr = global i32 0
@.str.1 = [39 x i8] c"Error, file \22%s\22 isn't a regular file\0A\00"
@.str.2 = [8 x i8] c"opening\00"
define noalias i32* @open_file(i8* %0, i8* nocapture %1) {
%3 = alloca %struct.stat
%4 = load i8, i8* %1
%5 = icmp eq i8 %4, 114
br i1 %5, label %6, label %22
6: ; preds = %2
%7 = bitcast %struct.stat* %3 to i8*
%8 = call i32 @stat(i8* %0, %struct.stat* %3)
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %12
10: ; preds = %6
%11 = tail call i32 @file_error(i8* %0, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
br label %12
12: ; preds = %10, %6
%13 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%14 = load i32, i32* %13
%15 = tail call i32 @S_ISREG(i32 %14)
%16 = icmp eq i32 %15, 0
br i1 %16, label %17, label %21
17: ; preds = %12
%18 = load i32, i32* @stderr
%19 = tail call i32 @fprintf(i32 %18, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0), i8* %0)
%20 = tail call i32 @exit(i32 1)
unreachable
21: ; preds = %12
br label %22
22: ; preds = %21, %2
%23 = tail call i32* @fopen(i8* %0, i8* %1)
%24 = icmp eq i32* %23, null
br i1 %24, label %25, label %27
25: ; preds = %22
%26 = tail call i32 @file_error(i8* %0, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
br label %27
27: ; preds = %25, %22
ret i32* %23
}