#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 hfile_oflags(const char *mode) {
  int rdwr = 0, flags = 0;
  const char *s;
  for (s = mode; *s; s++)
    switch (*s) {
    case 'r':
      rdwr = O_RDONLY;
      break;
    case 'w':
      rdwr = O_WRONLY;
      flags |= O_CREAT | O_TRUNC;
      break;
    case 'a':
      rdwr = O_WRONLY;
      flags |= O_CREAT | O_APPEND;
      break;
    case '+':
      rdwr = O_RDWR;
      break;
    default:
      break;
    }
  return rdwr | flags;
}


x86 assembly (clang, -O0):
<hfile_oflags>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
cmpb   $0x0,(%rax)
je     1205 <hfile_oflags+0x105>
mov    -0x18(%rbp),%rax
movsbl (%rax),%eax
mov    %eax,-0x1c(%rbp)
sub    $0x2b,%eax
je     11d9 <hfile_oflags+0xd9>
jmp    1143 <hfile_oflags+0x43>
mov    -0x1c(%rbp),%eax
sub    $0x61,%eax
je     11b0 <hfile_oflags+0xb0>
jmp    1154 <hfile_oflags+0x54>
mov    -0x1c(%rbp),%eax
sub    $0x72,%eax
je     1176 <hfile_oflags+0x76>
jmp    1165 <hfile_oflags+0x65>
mov    -0x1c(%rbp),%eax
sub    $0x77,%eax
je     1187 <hfile_oflags+0x87>
jmp    11ea <hfile_oflags+0xea>
mov    0x2e53(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xc(%rbp)
jmp    11ef <hfile_oflags+0xef>
mov    0x2e2a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xc(%rbp)
mov    0x2e5e(%rip),%rax
mov    (%rax),%eax
mov    0x2e4d(%rip),%rcx
or     (%rcx),%eax
or     -0x10(%rbp),%eax
mov    %eax,-0x10(%rbp)
jmp    11ef <hfile_oflags+0xef>
mov    0x2e01(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xc(%rbp)
mov    0x2e35(%rip),%rax
mov    (%rax),%eax
mov    0x2de4(%rip),%rcx
or     (%rcx),%eax
or     -0x10(%rbp),%eax
mov    %eax,-0x10(%rbp)
jmp    11ef <hfile_oflags+0xef>
mov    0x2df8(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xc(%rbp)
jmp    11ef <hfile_oflags+0xef>
jmp    11ef <hfile_oflags+0xef>
jmp    11f4 <hfile_oflags+0xf4>
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
jmp    111e <hfile_oflags+0x1e>
mov    -0xc(%rbp),%eax
or     -0x10(%rbp),%eax
pop    %rbp
ret

x86 assembly (clang, -O2):
<hfile_oflags>:
push   %rbx
mov    (%rdi),%bl
test   %bl,%bl
je     11ad <hfile_oflags+0xad>
mov    0x2ec6(%rip),%rax
mov    (%rax),%r8d
mov    0x2e9c(%rip),%rax
mov    (%rax),%ecx
mov    0x2ed3(%rip),%rax
mov    (%rax),%esi
mov    0x2e82(%rip),%rax
mov    (%rax),%r9d
mov    0x2eb8(%rip),%rax
mov    (%rax),%r10d
mov    0x2e8e(%rip),%rax
mov    (%rax),%r11d
add    $0x1,%rdi
xor    %eax,%eax
xor    %edx,%edx
jmp    116b <hfile_oflags+0x6b>
nop
mov    %r8d,%edx
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
movzbl (%rdi),%ebx
add    $0x1,%rdi
test   %bl,%bl
je     11a9 <hfile_oflags+0xa9>
movsbl %bl,%ebx
cmp    $0x71,%ebx
jg     1190 <hfile_oflags+0x90>
cmp    $0x2b,%ebx
je     1150 <hfile_oflags+0x50>
cmp    $0x61,%ebx
jne    1160 <hfile_oflags+0x60>
or     %esi,%eax
or     %r9d,%eax
mov    %ecx,%edx
jmp    1160 <hfile_oflags+0x60>
cs nopw 0x0(%rax,%rax,1)
cmp    $0x77,%ebx
je     11a0 <hfile_oflags+0xa0>
cmp    $0x72,%ebx
jne    1160 <hfile_oflags+0x60>
mov    %r11d,%edx
jmp    1160 <hfile_oflags+0x60>
nop
or     %esi,%eax
or     %r10d,%eax
mov    %ecx,%edx
jmp    1160 <hfile_oflags+0x60>
or     %edx,%eax
pop    %rbx
ret
xor    %eax,%eax
pop    %rbx
ret

LLVM IR (-O0):
@O_RDONLY = global i32 0
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@O_APPEND = global i32 0
@O_RDWR = global i32 0
define i32 @hfile_oflags(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
store i8* %0, i8** %2
store i32 0, i32* %3
store i32 0, i32* %4
%6 = load i8*, i8** %2
store i8* %6, i8** %5
br label %7
7: ; preds = %35, %1
%8 = load i8*, i8** %5
%9 = load i8, i8* %8
%10 = icmp ne i8 %9, 0
br i1 %10, label %11, label %38
11: ; preds = %7
%12 = load i8*, i8** %5
%13 = load i8, i8* %12
%14 = sext i8 %13 to i32
switch i32 %14, label %33 [
i32 114, label %15
i32 119, label %17
i32 97, label %24
i32 43, label %31
]
15: ; preds = %11
%16 = load i32, i32* @O_RDONLY
store i32 %16, i32* %3
br label %34
17: ; preds = %11
%18 = load i32, i32* @O_WRONLY
store i32 %18, i32* %3
%19 = load i32, i32* @O_CREAT
%20 = load i32, i32* @O_TRUNC
%21 = or i32 %19, %20
%22 = load i32, i32* %4
%23 = or i32 %22, %21
store i32 %23, i32* %4
br label %34
24: ; preds = %11
%25 = load i32, i32* @O_WRONLY
store i32 %25, i32* %3
%26 = load i32, i32* @O_CREAT
%27 = load i32, i32* @O_APPEND
%28 = or i32 %26, %27
%29 = load i32, i32* %4
%30 = or i32 %29, %28
store i32 %30, i32* %4
br label %34
31: ; preds = %11
%32 = load i32, i32* @O_RDWR
store i32 %32, i32* %3
br label %34
33: ; preds = %11
br label %34
34: ; preds = %33, %31, %24, %17, %15
br label %35
35: ; preds = %34
%36 = load i8*, i8** %5
%37 = getelementptr inbounds i8, i8* %36, i32 1
store i8* %37, i8** %5
br label %7
38: ; preds = %7
%39 = load i32, i32* %3
%40 = load i32, i32* %4
%41 = or i32 %39, %40
ret i32 %41
}

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