#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 */

/* Variables and functions */
scalar_t__ is_file_exists(char const *);
scalar_t__ strcmp(char const *, char *);
char fopen_flag(const char *restrict filename, const char *restrict mode) {
  int read_mode = 0;
  int write_mode = 0;
  char flag = 'U';
  if (strcmp(mode, "r"))
    read_mode = 1;
  else if (strcmp(mode, "r+")) {
    read_mode = 1;
  } else if (strcmp(mode, "w"))
    write_mode = 1;
  else if (strcmp(mode, "w+"))
    write_mode = 1;
  else if (strcmp(mode, "a")) {
    read_mode = 1;
    write_mode = 1;
  } else if (strcmp(mode, "a+")) {
    read_mode = 1;
    write_mode = 1;
  }
  if (write_mode && read_mode) {
    if (is_file_exists(filename)) {
      flag = 'B';
    } else {
      flag = 'W';
    }
  } else if (write_mode) {
    flag = 'W';
  } else if (read_mode) {
    flag = 'R';
  }
  return flag;
}


x86 assembly (clang, -O0):
<fopen_flag>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
movb   $0x55,-0x19(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xeb3(%rip),%rsi
call   1030 <strcmp@plt>
cmp    $0x0,%rax
je     1168 <fopen_flag+0x48>
movl   $0x1,-0x14(%rbp)
jmp    1248 <fopen_flag+0x128>
mov    -0x10(%rbp),%rdi
lea    0xe8f(%rip),%rsi
call   1030 <strcmp@plt>
cmp    $0x0,%rax
je     118e <fopen_flag+0x6e>
movl   $0x1,-0x14(%rbp)
jmp    1243 <fopen_flag+0x123>
mov    -0x10(%rbp),%rdi
lea    0xe6c(%rip),%rsi
call   1030 <strcmp@plt>
cmp    $0x0,%rax
je     11b4 <fopen_flag+0x94>
movl   $0x1,-0x18(%rbp)
jmp    123e <fopen_flag+0x11e>
mov    -0x10(%rbp),%rdi
lea    0xe48(%rip),%rsi
call   1030 <strcmp@plt>
cmp    $0x0,%rax
je     11da <fopen_flag+0xba>
movl   $0x1,-0x18(%rbp)
jmp    1239 <fopen_flag+0x119>
mov    -0x10(%rbp),%rdi
lea    0xe25(%rip),%rsi
call   1030 <strcmp@plt>
cmp    $0x0,%rax
je     1207 <fopen_flag+0xe7>
movl   $0x1,-0x14(%rbp)
movl   $0x1,-0x18(%rbp)
jmp    1234 <fopen_flag+0x114>
mov    -0x10(%rbp),%rdi
lea    0xdfa(%rip),%rsi
call   1030 <strcmp@plt>
cmp    $0x0,%rax
je     122f <fopen_flag+0x10f>
movl   $0x1,-0x14(%rbp)
movl   $0x1,-0x18(%rbp)
jmp    1234 <fopen_flag+0x114>
jmp    1239 <fopen_flag+0x119>
jmp    123e <fopen_flag+0x11e>
jmp    1243 <fopen_flag+0x123>
jmp    1248 <fopen_flag+0x128>
cmpl   $0x0,-0x18(%rbp)
je     1281 <fopen_flag+0x161>
cmpl   $0x0,-0x14(%rbp)
je     1281 <fopen_flag+0x161>
mov    -0x8(%rbp),%rdi
call   1040 <is_file_exists@plt>
cmp    $0x0,%rax
je     1278 <fopen_flag+0x158>
movb   $0x42,-0x19(%rbp)
jmp    127c <fopen_flag+0x15c>
movb   $0x57,-0x19(%rbp)
jmp    12ac <fopen_flag+0x18c>
cmpl   $0x0,-0x18(%rbp)
je     1294 <fopen_flag+0x174>
movb   $0x57,-0x19(%rbp)
jmp    12a7 <fopen_flag+0x187>
cmpl   $0x0,-0x14(%rbp)
je     12a2 <fopen_flag+0x182>
movb   $0x52,-0x19(%rbp)
jmp    12a7 <fopen_flag+0x187>
jmp    12ac <fopen_flag+0x18c>
movsbl -0x19(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fopen_flag>:
push   %rbp
push   %r14
push   %rbx
mov    %rsi,%rbx
mov    %rdi,%r14
lea    0xecf(%rip),%rsi
mov    %rbx,%rdi
call   1030 <strcmp@plt>
mov    $0x52,%bpl
test   %rax,%rax
jne    11c0 <fopen_flag+0xa0>
lea    0xeba(%rip),%rsi
mov    %rbx,%rdi
call   1030 <strcmp@plt>
test   %rax,%rax
jne    11c0 <fopen_flag+0xa0>
lea    0xea9(%rip),%rsi
mov    %rbx,%rdi
call   1030 <strcmp@plt>
mov    $0x57,%bpl
test   %rax,%rax
jne    11c0 <fopen_flag+0xa0>
lea    0xe94(%rip),%rsi
mov    %rbx,%rdi
call   1030 <strcmp@plt>
test   %rax,%rax
jne    11c0 <fopen_flag+0xa0>
lea    0xe83(%rip),%rsi
mov    %rbx,%rdi
call   1030 <strcmp@plt>
test   %rax,%rax
jne    11a8 <fopen_flag+0x88>
lea    0xe71(%rip),%rsi
mov    %rbx,%rdi
call   1030 <strcmp@plt>
test   %rax,%rax
je     11c7 <fopen_flag+0xa7>
mov    %r14,%rdi
call   1040 <is_file_exists@plt>
test   %rax,%rax
mov    $0x57,%eax
mov    $0x42,%ebp
cmove  %eax,%ebp
mov    %ebp,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    $0x55,%bpl
jmp    11c0 <fopen_flag+0xa0>

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@.str.1 = [3 x i8] c"r+\00"
@.str.2 = [2 x i8] c"w\00"
@.str.3 = [3 x i8] c"w+\00"
@.str.4 = [2 x i8] c"a\00"
@.str.5 = [3 x i8] c"a+\00"
define signext i8 @fopen_flag(i8* noalias %0, i8* noalias %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8
store i8* %0, i8** %3
store i8* %1, i8** %4
store i32 0, i32* %5
store i32 0, i32* %6
store i8 85, i8* %7
%8 = load i8*, i8** %4
%9 = call i64 @strcmp(i8* %8, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%10 = icmp ne i64 %9, 0
br i1 %10, label %11, label %12
11: ; preds = %2
store i32 1, i32* %5
br label %42
12: ; preds = %2
%13 = load i8*, i8** %4
%14 = call i64 @strcmp(i8* %13, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%15 = icmp ne i64 %14, 0
br i1 %15, label %16, label %17
16: ; preds = %12
store i32 1, i32* %5
br label %41
17: ; preds = %12
%18 = load i8*, i8** %4
%19 = call i64 @strcmp(i8* %18, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%20 = icmp ne i64 %19, 0
br i1 %20, label %21, label %22
21: ; preds = %17
store i32 1, i32* %6
br label %40
22: ; preds = %17
%23 = load i8*, i8** %4
%24 = call i64 @strcmp(i8* %23, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
%25 = icmp ne i64 %24, 0
br i1 %25, label %26, label %27
26: ; preds = %22
store i32 1, i32* %6
br label %39
27: ; preds = %22
%28 = load i8*, i8** %4
%29 = call i64 @strcmp(i8* %28, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%30 = icmp ne i64 %29, 0
br i1 %30, label %31, label %32
31: ; preds = %27
store i32 1, i32* %5
store i32 1, i32* %6
br label %38
32: ; preds = %27
%33 = load i8*, i8** %4
%34 = call i64 @strcmp(i8* %33, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0))
%35 = icmp ne i64 %34, 0
br i1 %35, label %36, label %37
36: ; preds = %32
store i32 1, i32* %5
store i32 1, i32* %6
br label %37
37: ; preds = %36, %32
br label %38
38: ; preds = %37, %31
br label %39
39: ; preds = %38, %26
br label %40
40: ; preds = %39, %21
br label %41
41: ; preds = %40, %16
br label %42
42: ; preds = %41, %11
%43 = load i32, i32* %6
%44 = icmp ne i32 %43, 0
br i1 %44, label %45, label %55
45: ; preds = %42
%46 = load i32, i32* %5
%47 = icmp ne i32 %46, 0
br i1 %47, label %48, label %55
48: ; preds = %45
%49 = load i8*, i8** %3
%50 = call i64 @is_file_exists(i8* %49)
%51 = icmp ne i64 %50, 0
br i1 %51, label %52, label %53
52: ; preds = %48
store i8 66, i8* %7
br label %54
53: ; preds = %48
store i8 87, i8* %7
br label %54
54: ; preds = %53, %52
br label %65
55: ; preds = %45, %42
%56 = load i32, i32* %6
%57 = icmp ne i32 %56, 0
br i1 %57, label %58, label %59
58: ; preds = %55
store i8 87, i8* %7
br label %64
59: ; preds = %55
%60 = load i32, i32* %5
%61 = icmp ne i32 %60, 0
br i1 %61, label %62, label %63
62: ; preds = %59
store i8 82, i8* %7
br label %63
63: ; preds = %62, %59
br label %64
64: ; preds = %63, %58
br label %65
65: ; preds = %64, %54
%66 = load i8, i8* %7
ret i8 %66
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@.str.1 = [3 x i8] c"r+\00"
@.str.2 = [2 x i8] c"w\00"
@.str.3 = [3 x i8] c"w+\00"
@.str.4 = [2 x i8] c"a\00"
@.str.5 = [3 x i8] c"a+\00"
define signext i8 @fopen_flag(i8* noalias %0, i8* noalias %1) {
%3 = tail call i64 @strcmp(i8* %1, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%4 = icmp eq i64 %3, 0
br i1 %4, label %5, label %24
5: ; preds = %2
%6 = tail call i64 @strcmp(i8* %1, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%7 = icmp eq i64 %6, 0
br i1 %7, label %8, label %24
8: ; preds = %5
%9 = tail call i64 @strcmp(i8* %1, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%10 = icmp eq i64 %9, 0
br i1 %10, label %11, label %24
11: ; preds = %8
%12 = tail call i64 @strcmp(i8* %1, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
%13 = icmp eq i64 %12, 0
br i1 %13, label %14, label %24
14: ; preds = %11
%15 = tail call i64 @strcmp(i8* %1, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%16 = icmp eq i64 %15, 0
br i1 %16, label %17, label %20
17: ; preds = %14
%18 = tail call i64 @strcmp(i8* %1, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0))
%19 = icmp eq i64 %18, 0
br i1 %19, label %24, label %20
20: ; preds = %14, %17
%21 = tail call i64 @is_file_exists(i8* %0)
%22 = icmp eq i64 %21, 0
%23 = select i1 %22, i8 87, i8 66
br label %24
24: ; preds = %8, %11, %17, %2, %5, %20
%25 = phi i8 [ %23, %20 ], [ 82, %5 ], [ 82, %2 ], [ 85, %17 ], [ 87, %11 ], [ 87, %8 ]
ret i8 %25
}