#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 */
typedef void FILE;

/* Variables and functions */
int /*<<< orphan*/ fatal_error_with_errno(char *, char const *, char *);
void *fopen(char const *, char const *);
void *stdout;
scalar_t__ strchr(char const *, char);
scalar_t__ strcmp(char const *, char *);
void *xfopen(const char *filename, const char *mode) {
  if (strcmp(filename, "-") == 0)
    return stdout;
  FILE *fp = fopen(filename, mode);
  if (!fp) {
    fatal_error_with_errno("Could not open the file \"%s\" for %s", filename,
                           strchr(mode, 'w') ? "writing" : "reading");
  }
  return fp;
}


x86 assembly (clang, -O0):
<xfopen>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xea5(%rip),%rsi
call   1050 <strcmp@plt>
cmp    $0x0,%rax
jne    117d <xfopen+0x3d>
mov    0x2e6f(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x8(%rbp)
jmp    11e0 <xfopen+0xa0>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11d8 <xfopen+0x98>
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
mov    $0x77,%esi
call   1030 <strchr@plt>
mov    -0x28(%rbp),%rsi
mov    %rax,%rcx
lea    0xe71(%rip),%rdx
lea    0xe62(%rip),%rax
cmp    $0x0,%rcx
cmovne %rax,%rdx
lea    0xe2f(%rip),%rdi
call   1040 <fatal_error_with_errno@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<xfopen>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rbx
lea    0xeaf(%rip),%rsi
call   1050 <strcmp@plt>
test   %rax,%rax
je     11a0 <xfopen+0x60>
mov    %rbx,%rdi
mov    %r14,%rsi
call   1060 <fopen@plt>
test   %rax,%rax
jne    11aa <xfopen+0x6a>
mov    %r14,%rdi
mov    $0x77,%esi
call   1030 <strchr@plt>
test   %rax,%rax
lea    0xeac(%rip),%rax
lea    0xe9d(%rip),%rdx
cmove  %rax,%rdx
lea    0xe6e(%rip),%rdi
mov    %rbx,%rsi
call   1040 <fatal_error_with_errno@plt>
xor    %eax,%eax
jmp    11aa <xfopen+0x6a>
mov    0x2e39(%rip),%rax
mov    (%rax),%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [2 x i8] c"-\00"
@stdout = global i8* null
@.str.1 = [36 x i8] c"Could not open the file \22%s\22 for %s\00"
@.str.2 = [8 x i8] c"writing\00"
@.str.3 = [8 x i8] c"reading\00"
define i8* @xfopen(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
store i8* %0, i8** %4
store i8* %1, i8** %5
%7 = load i8*, i8** %4
%8 = call i64 @strcmp(i8* %7, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%9 = icmp eq i64 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %2
%11 = load i8*, i8** @stdout
store i8* %11, i8** %3
br label %28
12: ; preds = %2
%13 = load i8*, i8** %4
%14 = load i8*, i8** %5
%15 = call i8* @fopen(i8* %13, i8* %14)
store i8* %15, i8** %6
%16 = load i8*, i8** %6
%17 = icmp ne i8* %16, null
br i1 %17, label %26, label %18
18: ; preds = %12
%19 = load i8*, i8** %4
%20 = load i8*, i8** %5
%21 = call i64 @strchr(i8* %20, i8 signext 119)
%22 = icmp ne i64 %21, 0
%23 = zext i1 %22 to i64
%24 = select i1 %22, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0)
%25 = call i32 @fatal_error_with_errno(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.1, i64 0, i64 0), i8* %19, i8* %24)
br label %26
26: ; preds = %18, %12
%27 = load i8*, i8** %6
store i8* %27, i8** %3
br label %28
28: ; preds = %26, %10
%29 = load i8*, i8** %3
ret i8* %29
}

LLVM IR (-O2):
@.str = [2 x i8] c"-\00"
@stdout = global i8* null
@.str.1 = [36 x i8] c"Could not open the file \22%s\22 for %s\00"
@.str.2 = [8 x i8] c"writing\00"
@.str.3 = [8 x i8] c"reading\00"
define i8* @xfopen(i8* %0, i8* %1) {
%3 = tail call i64 @strcmp(i8* %0, 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 %7
5: ; preds = %2
%6 = load i8*, i8** @stdout
br label %15
7: ; preds = %2
%8 = tail call i8* @fopen(i8* %0, i8* %1)
%9 = icmp eq i8* %8, null
br i1 %9, label %10, label %15
10: ; preds = %7
%11 = tail call i64 @strchr(i8* %1, i8 signext 119)
%12 = icmp eq i64 %11, 0
%13 = select i1 %12, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0)
%14 = tail call i32 @fatal_error_with_errno(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.1, i64 0, i64 0), i8* %0, i8* %13)
br label %15
15: ; preds = %7, %10, %5
%16 = phi i8* [ %6, %5 ], [ null, %10 ], [ %8, %7 ]
ret i8* %16
}