#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 */
int O_APPEND;
int O_CLOEXEC;
int O_CREAT;
int O_EXCL;
int O_RDONLY;
int O_RDWR;
int O_TRUNC;
int O_WRONLY;
scalar_t__ strchr(char const *, char);
int __fmodeflags(const char *mode) {
  int flags;
  if (strchr(mode, '+'))
    flags = O_RDWR;
  else if (*mode == 'r')
    flags = O_RDONLY;
  else
    flags = O_WRONLY;
  if (strchr(mode, 'x'))
    flags |= O_EXCL;
  if (strchr(mode, 'e'))
    flags |= O_CLOEXEC;
  if (*mode != 'r')
    flags |= O_CREAT;
  if (*mode == 'w')
    flags |= O_TRUNC;
  if (*mode == 'a')
    flags |= O_APPEND;
  return flags;
}


x86 assembly (clang, -O0):
<__fmodeflags>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
mov    $0x2b,%esi
call   1030 <strchr@plt>
cmp    $0x0,%rax
je     1145 <__fmodeflags+0x35>
mov    0x2e9d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xc(%rbp)
jmp    1177 <__fmodeflags+0x67>
mov    -0x8(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x72,%eax
jne    1166 <__fmodeflags+0x56>
mov    0x2e74(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xc(%rbp)
jmp    1172 <__fmodeflags+0x62>
mov    0x2e43(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xc(%rbp)
jmp    1177 <__fmodeflags+0x67>
mov    -0x8(%rbp),%rdi
mov    $0x78,%esi
call   1030 <strchr@plt>
cmp    $0x0,%rax
je     119e <__fmodeflags+0x8e>
mov    0x2e2a(%rip),%rax
mov    (%rax),%eax
or     -0xc(%rbp),%eax
mov    %eax,-0xc(%rbp)
mov    -0x8(%rbp),%rdi
mov    $0x65,%esi
call   1030 <strchr@plt>
cmp    $0x0,%rax
je     11c5 <__fmodeflags+0xb5>
mov    0x2de3(%rip),%rax
mov    (%rax),%eax
or     -0xc(%rbp),%eax
mov    %eax,-0xc(%rbp)
mov    -0x8(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x72,%eax
je     11e4 <__fmodeflags+0xd4>
mov    0x2e1c(%rip),%rax
mov    (%rax),%eax
or     -0xc(%rbp),%eax
mov    %eax,-0xc(%rbp)
mov    -0x8(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x77,%eax
jne    1203 <__fmodeflags+0xf3>
mov    0x2df5(%rip),%rax
mov    (%rax),%eax
or     -0xc(%rbp),%eax
mov    %eax,-0xc(%rbp)
mov    -0x8(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x61,%eax
jne    1222 <__fmodeflags+0x112>
mov    0x2d8e(%rip),%rax
mov    (%rax),%eax
or     -0xc(%rbp),%eax
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<__fmodeflags>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%rbx
mov    $0x2b,%esi
call   1030 <strchr@plt>
test   %rax,%rax
je     1132 <__fmodeflags+0x22>
mov    0x2ea8(%rip),%rax
jmp    1147 <__fmodeflags+0x37>
cmpb   $0x72,(%rbx)
jne    1140 <__fmodeflags+0x30>
mov    0x2e92(%rip),%rax
jmp    1147 <__fmodeflags+0x37>
mov    0x2e69(%rip),%rax
mov    (%rax),%r15d
mov    %rbx,%rdi
mov    $0x78,%esi
call   1030 <strchr@plt>
xor    %r14d,%r14d
mov    $0x0,%ebp
test   %rax,%rax
je     116d <__fmodeflags+0x5d>
mov    0x2e55(%rip),%rax
mov    (%rax),%ebp
mov    %rbx,%rdi
mov    $0x65,%esi
call   1030 <strchr@plt>
mov    $0x0,%edx
test   %rax,%rax
je     118d <__fmodeflags+0x7d>
mov    0x2e15(%rip),%rax
mov    (%rax),%edx
or     %r15d,%ebp
mov    (%rbx),%al
mov    $0x0,%ecx
cmp    $0x72,%al
je     11a4 <__fmodeflags+0x94>
mov    0x2e56(%rip),%rcx
mov    (%rcx),%ecx
or     %edx,%ebp
mov    $0x0,%edx
cmp    $0x77,%al
jne    11b8 <__fmodeflags+0xa8>
mov    0x2e3a(%rip),%rdx
mov    (%rdx),%edx
or     %edx,%ecx
or     %ecx,%ebp
cmp    $0x61,%al
jne    11ca <__fmodeflags+0xba>
mov    0x2de1(%rip),%rax
mov    (%rax),%r14d
or     %r14d,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@O_RDWR = global i32 0
@O_RDONLY = global i32 0
@O_WRONLY = global i32 0
@O_EXCL = global i32 0
@O_CLOEXEC = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@O_APPEND = global i32 0
define i32 @__fmodeflags(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
store i8* %0, i8** %2
%4 = load i8*, i8** %2
%5 = call i64 @strchr(i8* %4, i8 signext 43)
%6 = icmp ne i64 %5, 0
br i1 %6, label %7, label %9
7: ; preds = %1
%8 = load i32, i32* @O_RDWR
store i32 %8, i32* %3
br label %19
9: ; preds = %1
%10 = load i8*, i8** %2
%11 = load i8, i8* %10
%12 = sext i8 %11 to i32
%13 = icmp eq i32 %12, 114
br i1 %13, label %14, label %16
14: ; preds = %9
%15 = load i32, i32* @O_RDONLY
store i32 %15, i32* %3
br label %18
16: ; preds = %9
%17 = load i32, i32* @O_WRONLY
store i32 %17, i32* %3
br label %18
18: ; preds = %16, %14
br label %19
19: ; preds = %18, %7
%20 = load i8*, i8** %2
%21 = call i64 @strchr(i8* %20, i8 signext 120)
%22 = icmp ne i64 %21, 0
br i1 %22, label %23, label %27
23: ; preds = %19
%24 = load i32, i32* @O_EXCL
%25 = load i32, i32* %3
%26 = or i32 %25, %24
store i32 %26, i32* %3
br label %27
27: ; preds = %23, %19
%28 = load i8*, i8** %2
%29 = call i64 @strchr(i8* %28, i8 signext 101)
%30 = icmp ne i64 %29, 0
br i1 %30, label %31, label %35
31: ; preds = %27
%32 = load i32, i32* @O_CLOEXEC
%33 = load i32, i32* %3
%34 = or i32 %33, %32
store i32 %34, i32* %3
br label %35
35: ; preds = %31, %27
%36 = load i8*, i8** %2
%37 = load i8, i8* %36
%38 = sext i8 %37 to i32
%39 = icmp ne i32 %38, 114
br i1 %39, label %40, label %44
40: ; preds = %35
%41 = load i32, i32* @O_CREAT
%42 = load i32, i32* %3
%43 = or i32 %42, %41
store i32 %43, i32* %3
br label %44
44: ; preds = %40, %35
%45 = load i8*, i8** %2
%46 = load i8, i8* %45
%47 = sext i8 %46 to i32
%48 = icmp eq i32 %47, 119
br i1 %48, label %49, label %53
49: ; preds = %44
%50 = load i32, i32* @O_TRUNC
%51 = load i32, i32* %3
%52 = or i32 %51, %50
store i32 %52, i32* %3
br label %53
53: ; preds = %49, %44
%54 = load i8*, i8** %2
%55 = load i8, i8* %54
%56 = sext i8 %55 to i32
%57 = icmp eq i32 %56, 97
br i1 %57, label %58, label %62
58: ; preds = %53
%59 = load i32, i32* @O_APPEND
%60 = load i32, i32* %3
%61 = or i32 %60, %59
store i32 %61, i32* %3
br label %62
62: ; preds = %58, %53
%63 = load i32, i32* %3
ret i32 %63
}

LLVM IR (-O2):
@O_RDWR = global i32 0
@O_RDONLY = global i32 0
@O_WRONLY = global i32 0
@O_EXCL = global i32 0
@O_CLOEXEC = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@O_APPEND = global i32 0
define i32 @__fmodeflags(i8* %0) {
%2 = tail call i64 @strchr(i8* %0, i8 signext 43)
%3 = icmp eq i64 %2, 0
br i1 %3, label %4, label %8
4: ; preds = %1
%5 = load i8, i8* %0
%6 = icmp eq i8 %5, 114
%7 = select i1 %6, i32* @O_RDONLY, i32* @O_WRONLY
br label %8
8: ; preds = %4, %1
%9 = phi i32* [ @O_RDWR, %1 ], [ %7, %4 ]
%10 = load i32, i32* %9
%11 = tail call i64 @strchr(i8* %0, i8 signext 120)
%12 = icmp eq i64 %11, 0
%13 = load i32, i32* @O_EXCL
%14 = select i1 %12, i32 0, i32 %13
%15 = or i32 %14, %10
%16 = tail call i64 @strchr(i8* %0, i8 signext 101)
%17 = icmp eq i64 %16, 0
%18 = load i32, i32* @O_CLOEXEC
%19 = select i1 %17, i32 0, i32 %18
%20 = or i32 %15, %19
%21 = load i8, i8* %0
%22 = icmp eq i8 %21, 114
%23 = load i32, i32* @O_CREAT
%24 = select i1 %22, i32 0, i32 %23
%25 = or i32 %20, %24
%26 = icmp eq i8 %21, 119
%27 = load i32, i32* @O_TRUNC
%28 = select i1 %26, i32 %27, i32 0
%29 = or i32 %25, %28
%30 = icmp eq i8 %21, 97
%31 = load i32, i32* @O_APPEND
%32 = select i1 %30, i32 %31, i32 0
%33 = or i32 %29, %32
ret i32 %33
}