#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_CREAT;
int O_RDONLY;
int O_RDWR;
int O_TRUNC;
int O_WRONLY;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ *strchr(char const *, char);
int str2mode(const char *mode) {
  int ret;
  const char *second = &mode[1];
  if (*second == 'b')
    second++;
  switch (mode[0]) {
  case 'r':
    ret = O_RDONLY;
    if (*second == '+' || *second == 'w')
      ret = O_RDWR;
    break;
  case 'w':
    ret = O_WRONLY | O_CREAT | O_TRUNC;
    if (*second == '+' || *second == 'r')
      ret = O_RDWR | O_CREAT | O_TRUNC;
    break;
  case 'a':
    ret = O_WRONLY | O_APPEND | O_CREAT;
    if (*second == '+')
      ret = O_RDWR | O_APPEND | O_CREAT;
    break;
  default:
    ret = 0;
    fprintf(stderr, "bad mode passed to str2mode\n");
    exit(1);
  }
  if (strchr(mode, 'b') != NULL)
    ret |= 0;
  return ret;
}


x86 assembly (clang, -O0):
<str2mode>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x62,%eax
jne    1164 <str2mode+0x34>
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%rax
movsbl (%rax),%eax
mov    %eax,-0x1c(%rbp)
sub    $0x61,%eax
je     123c <str2mode+0x10c>
jmp    117c <str2mode+0x4c>
mov    -0x1c(%rbp),%eax
sub    $0x72,%eax
je     119e <str2mode+0x6e>
jmp    118d <str2mode+0x5d>
mov    -0x1c(%rbp),%eax
sub    $0x77,%eax
je     11db <str2mode+0xab>
jmp    128d <str2mode+0x15d>
mov    0x2e2b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xc(%rbp)
mov    -0x18(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2b,%eax
je     11ca <str2mode+0x9a>
mov    -0x18(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x77,%eax
jne    11d6 <str2mode+0xa6>
mov    0x2e07(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xc(%rbp)
jmp    12b3 <str2mode+0x183>
mov    0x2dce(%rip),%rax
mov    (%rax),%eax
mov    0x2e0d(%rip),%rcx
or     (%rcx),%eax
mov    0x2dfc(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0xc(%rbp)
mov    -0x18(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2b,%eax
je     1219 <str2mode+0xe9>
mov    -0x18(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x72,%eax
jne    1237 <str2mode+0x107>
mov    0x2db8(%rip),%rax
mov    (%rax),%eax
mov    0x2dcf(%rip),%rcx
or     (%rcx),%eax
mov    0x2dbe(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0xc(%rbp)
jmp    12b3 <str2mode+0x183>
mov    0x2d6d(%rip),%rax
mov    (%rax),%eax
mov    0x2d5c(%rip),%rcx
or     (%rcx),%eax
mov    0x2da3(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0xc(%rbp)
mov    -0x18(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2b,%eax
jne    1288 <str2mode+0x158>
mov    0x2d67(%rip),%rax
mov    (%rax),%eax
mov    0x2d2e(%rip),%rcx
or     (%rcx),%eax
mov    0x2d75(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0xc(%rbp)
jmp    12b3 <str2mode+0x183>
movl   $0x0,-0xc(%rbp)
mov    0x2d25(%rip),%rax
mov    (%rax),%edi
lea    0xd5c(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1050 <exit@plt>
mov    -0x8(%rbp),%rdi
mov    $0x62,%esi
call   1030 <strchr@plt>
cmp    $0x0,%rax
je     12d4 <str2mode+0x1a4>
mov    -0xc(%rbp),%eax
or     $0x0,%eax
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<str2mode>:
push   %rbx
lea    0x1(%rdi),%rax
lea    0x2(%rdi),%rcx
cmpb   $0x62,0x1(%rdi)
cmove  %rcx,%rax
movsbl (%rdi),%ecx
cmp    $0x61,%ecx
je     116a <str2mode+0x3a>
cmp    $0x77,%ecx
je     118c <str2mode+0x5c>
cmp    $0x72,%ecx
jne    11ef <str2mode+0xbf>
mov    (%rax),%al
cmp    $0x77,%al
je     1161 <str2mode+0x31>
cmp    $0x2b,%al
jne    11cb <str2mode+0x9b>
mov    0x2e70(%rip),%rax
jmp    11d2 <str2mode+0xa2>
mov    0x2e37(%rip),%rcx
mov    0x2e80(%rip),%rdx
mov    (%rdx),%ebx
or     (%rcx),%ebx
cmpb   $0x2b,(%rax)
jne    11a7 <str2mode+0x77>
mov    0x2e50(%rip),%rax
or     (%rax),%ebx
jmp    11e1 <str2mode+0xb1>
mov    (%rax),%cl
mov    0x2e63(%rip),%rax
cmp    $0x2b,%cl
jne    11b2 <str2mode+0x82>
mov    (%rax),%ebx
mov    0x2e4d(%rip),%rax
mov    (%rax),%eax
jmp    11c2 <str2mode+0x92>
mov    0x2e02(%rip),%rax
or     (%rax),%ebx
jmp    11e1 <str2mode+0xb1>
mov    (%rax),%ebx
mov    0x2e35(%rip),%rax
mov    (%rax),%eax
cmp    $0x72,%cl
jne    11d6 <str2mode+0xa6>
mov    0x2e0f(%rip),%rcx
jmp    11dd <str2mode+0xad>
mov    0x2dfe(%rip),%rax
mov    (%rax),%ebx
jmp    11e1 <str2mode+0xb1>
mov    0x2dd3(%rip),%rcx
or     (%rcx),%ebx
or     %eax,%ebx
mov    $0x62,%esi
call   1030 <strchr@plt>
mov    %ebx,%eax
pop    %rbx
ret
mov    0x2dca(%rip),%rax
mov    (%rax),%edi
lea    0xe01(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1050 <exit@plt>

LLVM IR (-O0):
@O_RDONLY = global i32 0
@O_RDWR = global i32 0
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@O_APPEND = global i32 0
@stderr = global i32 0
@.str = [29 x i8] c"bad mode passed to str2mode\0A\00"
define i32 @str2mode(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i8*
store i8* %0, i8** %2
%5 = load i8*, i8** %2
%6 = getelementptr inbounds i8, i8* %5, i64 1
store i8* %6, i8** %4
%7 = load i8*, i8** %4
%8 = load i8, i8* %7
%9 = sext i8 %8 to i32
%10 = icmp eq i32 %9, 98
br i1 %10, label %11, label %14
11: ; preds = %1
%12 = load i8*, i8** %4
%13 = getelementptr inbounds i8, i8* %12, i32 1
store i8* %13, i8** %4
br label %14
14: ; preds = %11, %1
%15 = load i8*, i8** %2
%16 = getelementptr inbounds i8, i8* %15, i64 0
%17 = load i8, i8* %16
%18 = sext i8 %17 to i32
switch i32 %18, label %72 [
i32 114, label %19
i32 119, label %33
i32 97, label %55
]
19: ; preds = %14
%20 = load i32, i32* @O_RDONLY
store i32 %20, i32* %3
%21 = load i8*, i8** %4
%22 = load i8, i8* %21
%23 = sext i8 %22 to i32
%24 = icmp eq i32 %23, 43
br i1 %24, label %30, label %25
25: ; preds = %19
%26 = load i8*, i8** %4
%27 = load i8, i8* %26
%28 = sext i8 %27 to i32
%29 = icmp eq i32 %28, 119
br i1 %29, label %30, label %32
30: ; preds = %25, %19
%31 = load i32, i32* @O_RDWR
store i32 %31, i32* %3
br label %32
32: ; preds = %30, %25
br label %76
33: ; preds = %14
%34 = load i32, i32* @O_WRONLY
%35 = load i32, i32* @O_CREAT
%36 = or i32 %34, %35
%37 = load i32, i32* @O_TRUNC
%38 = or i32 %36, %37
store i32 %38, i32* %3
%39 = load i8*, i8** %4
%40 = load i8, i8* %39
%41 = sext i8 %40 to i32
%42 = icmp eq i32 %41, 43
br i1 %42, label %48, label %43
43: ; preds = %33
%44 = load i8*, i8** %4
%45 = load i8, i8* %44
%46 = sext i8 %45 to i32
%47 = icmp eq i32 %46, 114
br i1 %47, label %48, label %54
48: ; preds = %43, %33
%49 = load i32, i32* @O_RDWR
%50 = load i32, i32* @O_CREAT
%51 = or i32 %49, %50
%52 = load i32, i32* @O_TRUNC
%53 = or i32 %51, %52
store i32 %53, i32* %3
br label %54
54: ; preds = %48, %43
br label %76
55: ; preds = %14
%56 = load i32, i32* @O_WRONLY
%57 = load i32, i32* @O_APPEND
%58 = or i32 %56, %57
%59 = load i32, i32* @O_CREAT
%60 = or i32 %58, %59
store i32 %60, i32* %3
%61 = load i8*, i8** %4
%62 = load i8, i8* %61
%63 = sext i8 %62 to i32
%64 = icmp eq i32 %63, 43
br i1 %64, label %65, label %71
65: ; preds = %55
%66 = load i32, i32* @O_RDWR
%67 = load i32, i32* @O_APPEND
%68 = or i32 %66, %67
%69 = load i32, i32* @O_CREAT
%70 = or i32 %68, %69
store i32 %70, i32* %3
br label %71
71: ; preds = %65, %55
br label %76
72: ; preds = %14
store i32 0, i32* %3
%73 = load i32, i32* @stderr
%74 = call i32 @fprintf(i32 %73, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0))
%75 = call i32 @exit(i32 1)
unreachable
76: ; preds = %71, %54, %32
%77 = load i8*, i8** %2
%78 = call i32* @strchr(i8* %77, i8 signext 98)
%79 = icmp ne i32* %78, null
br i1 %79, label %80, label %83
80: ; preds = %76
%81 = load i32, i32* %3
%82 = or i32 %81, 0
store i32 %82, i32* %3
br label %83
83: ; preds = %80, %76
%84 = load i32, i32* %3
ret i32 %84
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@O_RDWR = global i32 0
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@O_APPEND = global i32 0
@stderr = global i32 0
@.str = [29 x i8] c"bad mode passed to str2mode\0A\00"
define i32 @str2mode(i8* %0) {
%2 = getelementptr inbounds i8, i8* %0, i64 1
%3 = load i8, i8* %2
%4 = icmp eq i8 %3, 98
%5 = getelementptr inbounds i8, i8* %0, i64 2
%6 = select i1 %4, i8* %5, i8* %2
%7 = load i8, i8* %0
%8 = sext i8 %7 to i32
switch i32 %8, label %44 [
i32 114, label %9
i32 119, label %14
i32 97, label %33
]
9: ; preds = %1
%10 = load i8, i8* %6
%11 = load i32, i32* @O_RDONLY
switch i8 %10, label %48 [
i8 119, label %12
i8 43, label %12
]
12: ; preds = %9, %9
%13 = load i32, i32* @O_RDWR
br label %48
14: ; preds = %1
%15 = load i8, i8* %6
%16 = icmp eq i8 %15, 43
br i1 %16, label %17, label %20
17: ; preds = %14
%18 = load i32, i32* @O_CREAT
%19 = load i32, i32* @O_TRUNC
br label %27
20: ; preds = %14
%21 = load i32, i32* @O_WRONLY
%22 = load i32, i32* @O_CREAT
%23 = or i32 %22, %21
%24 = load i32, i32* @O_TRUNC
%25 = or i32 %23, %24
%26 = icmp eq i8 %15, 114
br i1 %26, label %27, label %48
27: ; preds = %17, %20
%28 = phi i32 [ %19, %17 ], [ %24, %20 ]
%29 = phi i32 [ %18, %17 ], [ %22, %20 ]
%30 = load i32, i32* @O_RDWR
%31 = or i32 %29, %30
%32 = or i32 %31, %28
br label %48
33: ; preds = %1
%34 = load i32, i32* @O_WRONLY
%35 = load i32, i32* @O_APPEND
%36 = load i32, i32* @O_CREAT
%37 = or i32 %36, %35
%38 = or i32 %37, %34
%39 = load i8, i8* %6
%40 = icmp eq i8 %39, 43
br i1 %40, label %41, label %48
41: ; preds = %33
%42 = load i32, i32* @O_RDWR
%43 = or i32 %37, %42
br label %48
44: ; preds = %1
%45 = load i32, i32* @stderr
%46 = tail call i32 @fprintf(i32 %45, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0))
%47 = tail call i32 @exit(i32 1)
unreachable
48: ; preds = %9, %33, %41, %20, %27, %12
%49 = phi i32 [ %43, %41 ], [ %38, %33 ], [ %32, %27 ], [ %25, %20 ], [ %13, %12 ], [ %11, %9 ]
%50 = tail call i32* @strchr(i8* %0, i8 signext 98)
ret i32 %49
}