#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_FAILURE;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strcmp(char *, char *);
FILE *openfile(char *filename, char *rw) {
  FILE *fp;
  if (!strcmp(rw, "r")) {
    fp = fopen(filename, "r");
    if (fp == NULL) {
      fprintf(stderr, "Error opening file %s\n", filename);
      exit(EXIT_FAILURE);
    }
  } else if (!strcmp(rw, "a")) {
    fp = fopen(filename, "a");
    if (fp == NULL) {
      fprintf(stderr, "Error creating %s\n", filename);
      exit(EXIT_FAILURE);
    }
  } else if (!strcmp(rw, "w")) {
    fp = fopen(filename, "w");
    if (fp == NULL) {
      fprintf(stderr, "Error creating %s\n", filename);
      exit(EXIT_FAILURE);
    }
  }
  return fp;
}


x86 assembly (clang, -O0):
<openfile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xea5(%rip),%rsi
call   1030 <strcmp@plt>
cmp    $0x0,%eax
jne    11b4 <openfile+0x74>
mov    -0x8(%rbp),%rdi
lea    0xe8c(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11af <openfile+0x6f>
mov    0x2e51(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xe66(%rip),%rsi
call   1040 <fprintf@plt>
mov    0x2e30(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>
jmp    1286 <openfile+0x146>
mov    -0x10(%rbp),%rdi
lea    0xe5a(%rip),%rsi
call   1030 <strcmp@plt>
cmp    $0x0,%eax
jne    1218 <openfile+0xd8>
mov    -0x8(%rbp),%rdi
lea    0xe41(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1213 <openfile+0xd3>
mov    0x2ded(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xe1b(%rip),%rsi
call   1040 <fprintf@plt>
mov    0x2dcc(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>
jmp    1281 <openfile+0x141>
mov    -0x10(%rbp),%rdi
lea    0xe0b(%rip),%rsi
call   1030 <strcmp@plt>
cmp    $0x0,%eax
jne    127c <openfile+0x13c>
mov    -0x8(%rbp),%rdi
lea    0xdf2(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1277 <openfile+0x137>
mov    0x2d89(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xdb7(%rip),%rsi
call   1040 <fprintf@plt>
mov    0x2d68(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>
jmp    127c <openfile+0x13c>
jmp    1281 <openfile+0x141>
jmp    1286 <openfile+0x146>
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<openfile>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %rdi,%r14
lea    0xeaf(%rip),%rsi
mov    %rbx,%rdi
call   1030 <strcmp@plt>
test   %eax,%eax
je     118e <openfile+0x4e>
lea    0xeb5(%rip),%rsi
mov    %rbx,%rdi
call   1030 <strcmp@plt>
test   %eax,%eax
je     11b4 <openfile+0x74>
lea    0xeb7(%rip),%rsi
mov    %rbx,%rdi
call   1030 <strcmp@plt>
mov    %eax,%ecx
test   %ecx,%ecx
jne    11c8 <openfile+0x88>
lea    0xea2(%rip),%rsi
jmp    11bb <openfile+0x7b>
lea    0xe6b(%rip),%rsi
mov    %r14,%rdi
call   1050 <fopen@plt>
test   %rax,%rax
jne    11c8 <openfile+0x88>
mov    0x2e37(%rip),%rax
mov    (%rax),%edi
lea    0xe50(%rip),%rsi
jmp    11e0 <openfile+0xa0>
lea    0xe5e(%rip),%rsi
mov    %r14,%rdi
call   1050 <fopen@plt>
test   %rax,%rax
je     11d0 <openfile+0x90>
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
mov    0x2e09(%rip),%rax
mov    (%rax),%edi
lea    0xe3b(%rip),%rsi
mov    %r14,%rdx
call   1040 <fprintf@plt>
mov    0x2de9(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.1 = [23 x i8] c"Error opening file %s\0A\00"
@EXIT_FAILURE = global i32 0
@.str.2 = [2 x i8] c"a\00"
@.str.3 = [19 x i8] c"Error creating %s\0A\00"
@.str.4 = [2 x i8] c"w\00"
define i32* @openfile(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32*
store i8* %0, i8** %3
store i8* %1, i8** %4
%6 = load i8*, i8** %4
%7 = call i32 @strcmp(i8* %6, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%8 = icmp ne i32 %7, 0
br i1 %8, label %21, label %9
9: ; preds = %2
%10 = load i8*, i8** %3
%11 = call i32* @fopen(i8* %10, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %11, i32** %5
%12 = load i32*, i32** %5
%13 = icmp eq i32* %12, null
br i1 %13, label %14, label %20
14: ; preds = %9
%15 = load i32, i32* @stderr
%16 = load i8*, i8** %3
%17 = call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i8* %16)
%18 = load i32, i32* @EXIT_FAILURE
%19 = call i32 @exit(i32 %18)
unreachable
20: ; preds = %9
br label %55
21: ; preds = %2
%22 = load i8*, i8** %4
%23 = call i32 @strcmp(i8* %22, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%24 = icmp ne i32 %23, 0
br i1 %24, label %37, label %25
25: ; preds = %21
%26 = load i8*, i8** %3
%27 = call i32* @fopen(i8* %26, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %27, i32** %5
%28 = load i32*, i32** %5
%29 = icmp eq i32* %28, null
br i1 %29, label %30, label %36
30: ; preds = %25
%31 = load i32, i32* @stderr
%32 = load i8*, i8** %3
%33 = call i32 @fprintf(i32 %31, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0), i8* %32)
%34 = load i32, i32* @EXIT_FAILURE
%35 = call i32 @exit(i32 %34)
unreachable
36: ; preds = %25
br label %54
37: ; preds = %21
%38 = load i8*, i8** %4
%39 = call i32 @strcmp(i8* %38, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%40 = icmp ne i32 %39, 0
br i1 %40, label %53, label %41
41: ; preds = %37
%42 = load i8*, i8** %3
%43 = call i32* @fopen(i8* %42, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
store i32* %43, i32** %5
%44 = load i32*, i32** %5
%45 = icmp eq i32* %44, null
br i1 %45, label %46, label %52
46: ; preds = %41
%47 = load i32, i32* @stderr
%48 = load i8*, i8** %3
%49 = call i32 @fprintf(i32 %47, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0), i8* %48)
%50 = load i32, i32* @EXIT_FAILURE
%51 = call i32 @exit(i32 %50)
unreachable
52: ; preds = %41
br label %53
53: ; preds = %52, %37
br label %54
54: ; preds = %53, %36
br label %55
55: ; preds = %54, %20
%56 = load i32*, i32** %5
ret i32* %56
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.1 = [23 x i8] c"Error opening file %s\0A\00"
@EXIT_FAILURE = global i32 0
@.str.2 = [2 x i8] c"a\00"
@.str.3 = [19 x i8] c"Error creating %s\0A\00"
@.str.4 = [2 x i8] c"w\00"
define noalias i32* @openfile(i8* %0, i8* nocapture %1) {
%3 = tail call i32 @strcmp(i8* dereferenceable(1) %1, i8* dereferenceable(2) getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%4 = icmp eq i32 %3, 0
br i1 %4, label %5, label %13
5: ; preds = %2
%6 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%7 = icmp eq i32* %6, null
br i1 %7, label %8, label %35
8: ; preds = %5
%9 = load i32, i32* @stderr
%10 = tail call i32 @fprintf(i32 %9, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i8* %0)
%11 = load i32, i32* @EXIT_FAILURE
%12 = tail call i32 @exit(i32 %11)
unreachable
13: ; preds = %2
%14 = tail call i32 @strcmp(i8* dereferenceable(1) %1, i8* dereferenceable(2) getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%15 = icmp eq i32 %14, 0
br i1 %15, label %16, label %24
16: ; preds = %13
%17 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%18 = icmp eq i32* %17, null
br i1 %18, label %19, label %35
19: ; preds = %16
%20 = load i32, i32* @stderr
%21 = tail call i32 @fprintf(i32 %20, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0), i8* %0)
%22 = load i32, i32* @EXIT_FAILURE
%23 = tail call i32 @exit(i32 %22)
unreachable
24: ; preds = %13
%25 = tail call i32 @strcmp(i8* dereferenceable(1) %1, i8* dereferenceable(2) getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%26 = icmp eq i32 %25, 0
br i1 %26, label %27, label %35
27: ; preds = %24
%28 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%29 = icmp eq i32* %28, null
br i1 %29, label %30, label %35
30: ; preds = %27
%31 = load i32, i32* @stderr
%32 = tail call i32 @fprintf(i32 %31, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0), i8* %0)
%33 = load i32, i32* @EXIT_FAILURE
%34 = tail call i32 @exit(i32 %33)
unreachable
35: ; preds = %16, %27, %24, %5
%36 = phi i32* [ undef, %24 ], [ %28, %27 ], [ %17, %16 ], [ %6, %5 ]
ret i32* %36
}