#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 /*<<< orphan*/ errnoThrow(int);
void *fopen(char const *, char *);
void *Sys_FileOpen(const char *name, unsigned mode) {
  char *modestr[] = {"r", "rb", "r+", "r+b", "w", "wb", "BAD"};
  void *handle;
  if (mode > 5)
    mode = 6;
  handle = fopen(name, modestr[mode]);
  errnoThrow(handle == 0);
  return handle;
}


x86 assembly (clang, -O0):
<Sys_FileOpen>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
lea    -0x50(%rbp),%rdi
lea    0x2c96(%rip),%rsi
mov    $0x38,%edx
call   1030 <memcpy@plt>
cmpl   $0x5,-0xc(%rbp)
jbe    1165 <Sys_FileOpen+0x35>
movl   $0x6,-0xc(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%eax
mov    -0x50(%rbp,%rax,8),%rsi
call   1040 <fopen@plt>
mov    %rax,-0x58(%rbp)
cmpq   $0x0,-0x58(%rbp)
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1050 <errnoThrow@plt>
mov    -0x58(%rbp),%rax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<Sys_FileOpen>:
push   %rbx
cmp    $0x6,%esi
mov    $0x6,%eax
cmovb  %esi,%eax
lea    0xee5(%rip),%rcx
movslq (%rcx,%rax,4),%rsi
add    %rcx,%rsi
call   1030 <fopen@plt>
mov    %rax,%rbx
xor    %edi,%edi
test   %rax,%rax
sete   %dil
call   1040 <errnoThrow@plt>
mov    %rbx,%rax
pop    %rbx
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@.str.1 = [3 x i8] c"rb\00"
@.str.2 = [3 x i8] c"r+\00"
@.str.3 = [4 x i8] c"r+b\00"
@.str.4 = [2 x i8] c"w\00"
@.str.5 = [3 x i8] c"wb\00"
@.str.6 = [4 x i8] c"BAD\00"
@__const.Sys_FileOpen.modestr = [7 x i8*] [i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i32 0, i32 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i32 0, i32 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i32 0, i32 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i32 0, i32 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i32 0, i32 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i32 0, i32 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.6, i32 0, i32 0)]
define i8* @Sys_FileOpen(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca [7 x i8*]
%6 = alloca i8*
store i8* %0, i8** %3
store i32 %1, i32* %4
%7 = bitcast [7 x i8*]* %5 to i8*
%8 = load i32, i32* %4
%9 = icmp ugt i32 %8, 5
br i1 %9, label %10, label %11
10: ; preds = %2
store i32 6, i32* %4
br label %11
11: ; preds = %10, %2
%12 = load i8*, i8** %3
%13 = load i32, i32* %4
%14 = zext i32 %13 to i64
%15 = getelementptr inbounds [7 x i8*], [7 x i8*]* %5, i64 0, i64 %14
%16 = load i8*, i8** %15
%17 = call i8* @fopen(i8* %12, i8* %16)
store i8* %17, i8** %6
%18 = load i8*, i8** %6
%19 = icmp eq i8* %18, null
%20 = zext i1 %19 to i32
%21 = call i32 @errnoThrow(i32 %20)
%22 = load i8*, i8** %6
ret i8* %22
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@.str.1 = [3 x i8] c"rb\00"
@.str.2 = [3 x i8] c"r+\00"
@.str.3 = [4 x i8] c"r+b\00"
@.str.4 = [2 x i8] c"w\00"
@.str.5 = [3 x i8] c"wb\00"
@.str.6 = [4 x i8] c"BAD\00"
@reltable.Sys_FileOpen = [7 x i32] [i32 trunc (i64 sub (i64 ptrtoint ([2 x i8]* @.str to i64), i64 ptrtoint ([7 x i32]* @reltable.Sys_FileOpen to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([3 x i8]* @.str.1 to i64), i64 ptrtoint ([7 x i32]* @reltable.Sys_FileOpen to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([3 x i8]* @.str.2 to i64), i64 ptrtoint ([7 x i32]* @reltable.Sys_FileOpen to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([4 x i8]* @.str.3 to i64), i64 ptrtoint ([7 x i32]* @reltable.Sys_FileOpen to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([2 x i8]* @.str.4 to i64), i64 ptrtoint ([7 x i32]* @reltable.Sys_FileOpen to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([3 x i8]* @.str.5 to i64), i64 ptrtoint ([7 x i32]* @reltable.Sys_FileOpen to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([4 x i8]* @.str.6 to i64), i64 ptrtoint ([7 x i32]* @reltable.Sys_FileOpen to i64)) to i32)]
define noalias i8* @Sys_FileOpen(i8* nocapture %0, i32 %1) {
%3 = icmp ult i32 %1, 6
%4 = select i1 %3, i32 %1, i32 6
%5 = zext i32 %4 to i64
%6 = shl i64 %5, 2
%7 = call i8* @llvm.load.relative.i64(i8* bitcast ([7 x i32]* @reltable.Sys_FileOpen to i8*), i64 %6)
%8 = tail call i8* @fopen(i8* %0, i8* %7)
%9 = icmp eq i8* %8, null
%10 = zext i1 %9 to i32
%11 = tail call i32 @errnoThrow(i32 %10)
ret i8* %8
}