#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*/ exit(int);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ sprintf(char *, char *, char *);
FILE *openfile_or_exit(char *filename, char *mode, int exitcode) {
  FILE *fileout;
  char error_str[256];
  fileout = fopen(filename, mode);
  if (!fileout) {
    sprintf(error_str, "Error opening file %s", filename);
    perror(error_str);
    exit(exitcode);
  }
  return fileout;
}


x86 assembly (clang, -O0):
<openfile_or_exit>:
push   %rbp
mov    %rsp,%rbp
sub    $0x120,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1030 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    119d <openfile_or_exit+0x5d>
lea    -0x120(%rbp),%rdi
mov    -0x8(%rbp),%rdx
lea    0xe7c(%rip),%rsi
call   1050 <sprintf@plt>
lea    -0x120(%rbp),%rdi
call   1040 <perror@plt>
mov    -0x14(%rbp),%edi
call   1060 <exit@plt>
mov    -0x20(%rbp),%rax
add    $0x120,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<openfile_or_exit>:
push   %rbp
push   %r14
push   %rbx
sub    $0x100,%rsp
mov    %edx,%ebp
mov    %rdi,%rbx
call   1030 <fopen@plt>
test   %rax,%rax
je     1166 <openfile_or_exit+0x26>
add    $0x100,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xe93(%rip),%rsi
mov    %rsp,%r14
mov    %r14,%rdi
mov    %rbx,%rdx
call   1050 <sprintf@plt>
mov    %r14,%rdi
call   1040 <perror@plt>
mov    %ebp,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@.str = [22 x i8] c"Error opening file %s\00"
define i32* @openfile_or_exit(i8* %0, i8* %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32*
%8 = alloca [256 x i8]
store i8* %0, i8** %4
store i8* %1, i8** %5
store i32 %2, i32* %6
%9 = load i8*, i8** %4
%10 = load i8*, i8** %5
%11 = call i32* @fopen(i8* %9, i8* %10)
store i32* %11, i32** %7
%12 = load i32*, i32** %7
%13 = icmp ne i32* %12, null
br i1 %13, label %22, label %14
14: ; preds = %3
%15 = getelementptr inbounds [256 x i8], [256 x i8]* %8, i64 0, i64 0
%16 = load i8*, i8** %4
%17 = call i32 @sprintf(i8* %15, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8* %16)
%18 = getelementptr inbounds [256 x i8], [256 x i8]* %8, i64 0, i64 0
%19 = call i32 @perror(i8* %18)
%20 = load i32, i32* %6
%21 = call i32 @exit(i32 %20)
unreachable
22: ; preds = %3
%23 = load i32*, i32** %7
ret i32* %23
}

LLVM IR (-O2):
@.str = [22 x i8] c"Error opening file %s\00"
define noalias i32* @openfile_or_exit(i8* %0, i8* nocapture %1, i32 %2) {
%4 = alloca [256 x i8]
%5 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%6 = tail call i32* @fopen(i8* %0, i8* %1)
%7 = icmp eq i32* %6, null
br i1 %7, label %8, label %12
8: ; preds = %3
%9 = call i32 @sprintf(i8* %5, i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8* %0)
%10 = call i32 @perror(i8* %5)
%11 = tail call i32 @exit(i32 %2)
unreachable
12: ; preds = %3
ret i32* %6
}