#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 int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fatal(char *, char const *, char const *, ...);
int /*<<< orphan*/ *fopen(char const *, char const *);
int /*<<< orphan*/ *stdin;
int /*<<< orphan*/ *stdout;
int /*<<< orphan*/ *strchr(char const *, char);
scalar_t__ strcmp(char const *, char *);
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
FILE *xfopen(const char *fname, const char *type, const char *errtxt) {
  FILE *f;
  int am_writing = strchr(type, 'w') != NULL;
  if (fname == NULL) {
    fatal("%s must have a filename specified for %s.\n", errtxt,
          am_writing ? "write" : "read");
  }
  if (0 == strcmp(fname, "-"))
    return am_writing ? stdout : stdin;
  f = fopen(fname, type);
  if (NULL == f) {
    fatal("%s cannot open '%s' for %s.  Error was '%s'.\n", errtxt, fname,
          am_writing ? "write" : "read", strerror(errno));
  }
  return f;
}


x86 assembly (clang, -O0):
<xfopen>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
mov    $0x77,%esi
call   1030 <strchr@plt>
cmp    $0x0,%rax
setne  %al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x2c(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11b6 <xfopen+0x66>
mov    -0x20(%rbp),%rsi
mov    -0x2c(%rbp),%ecx
lea    0xe97(%rip),%rdx
lea    0xe8a(%rip),%rax
cmp    $0x0,%ecx
cmovne %rax,%rdx
lea    0xe51(%rip),%rdi
mov    $0x0,%al
call   1070 <fatal@plt>
mov    -0x10(%rbp),%rdi
lea    0xe75(%rip),%rsi
call   1040 <strcmp@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
jne    120c <xfopen+0xbc>
cmpl   $0x0,-0x2c(%rbp)
je     11f1 <xfopen+0xa1>
mov    0x2deb(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x38(%rbp)
jmp    11ff <xfopen+0xaf>
mov    0x2df0(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1288 <xfopen+0x138>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x28(%rbp)
xor    %eax,%eax
cmp    -0x28(%rbp),%rax
jne    1280 <xfopen+0x130>
mov    -0x20(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x2c(%rbp),%edx
lea    0xdee(%rip),%rax
lea    0xde1(%rip),%rcx
cmp    $0x0,%edx
cmovne %rcx,%rax
mov    %rax,-0x40(%rbp)
mov    0x2d7c(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
mov    -0x50(%rbp),%rsi
mov    -0x48(%rbp),%rdx
mov    -0x40(%rbp),%rcx
mov    %eax,%r8d
lea    0xdbf(%rip),%rdi
mov    $0x0,%al
call   1070 <fatal@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<xfopen>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rdx,%r14
mov    %rsi,%r12
mov    %rdi,%r13
mov    %rsi,%rdi
mov    $0x77,%esi
call   1030 <strchr@plt>
mov    %rax,%r15
test   %r13,%r13
jne    119d <xfopen+0x4d>
test   %r15,%r15
lea    0xeb0(%rip),%rax
lea    0xea3(%rip),%rdx
cmove  %rax,%rdx
lea    0xe6d(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1070 <fatal@plt>
lea    0xe92(%rip),%rsi
mov    %r13,%rdi
call   1040 <strcmp@plt>
test   %rax,%rax
je     1205 <xfopen+0xb5>
mov    %r13,%rdi
mov    %r12,%rsi
call   1050 <fopen@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    121d <xfopen+0xcd>
test   %r15,%r15
lea    0xe63(%rip),%rax
lea    0xe56(%rip),%r15
cmove  %rax,%r15
mov    0x2df8(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
lea    0xe4a(%rip),%rdi
xor    %ebx,%ebx
mov    %r14,%rsi
mov    %r13,%rdx
mov    %r15,%rcx
mov    %eax,%r8d
xor    %eax,%eax
call   1070 <fatal@plt>
jmp    121d <xfopen+0xcd>
test   %r15,%r15
jne    1213 <xfopen+0xc3>
mov    0x2dd7(%rip),%rax
jmp    121a <xfopen+0xca>
mov    0x2db6(%rip),%rax
mov    (%rax),%rbx
mov    %rbx,%rax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [43 x i8] c"%s must have a filename specified for %s.\0A\00"
@.str.1 = [6 x i8] c"write\00"
@.str.2 = [5 x i8] c"read\00"
@.str.3 = [2 x i8] c"-\00"
@stdout = global i32* null
@stdin = global i32* null
@.str.4 = [46 x i8] c"%s cannot open '%s' for %s. Error was '%s'.\0A\00"
@errno = global i32 0
define i32* @xfopen(i8* %0, i8* %1, i8* %2) {
%4 = alloca i32*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32*
%9 = alloca i32
store i8* %0, i8** %5
store i8* %1, i8** %6
store i8* %2, i8** %7
%10 = load i8*, i8** %6
%11 = call i32* @strchr(i8* %10, i8 signext 119)
%12 = icmp ne i32* %11, null
%13 = zext i1 %12 to i32
store i32 %13, i32* %9
%14 = load i8*, i8** %5
%15 = icmp eq i8* %14, null
br i1 %15, label %16, label %23
16: ; preds = %3
%17 = load i8*, i8** %7
%18 = load i32, i32* %9
%19 = icmp ne i32 %18, 0
%20 = zext i1 %19 to i64
%21 = select i1 %19, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0)
%22 = call i32 (i8*, i8*, i8*, ...) @fatal(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str, i64 0, i64 0), i8* %17, i8* %21)
br label %23
23: ; preds = %16, %3
%24 = load i8*, i8** %5
%25 = call i64 @strcmp(i8* %24, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%26 = icmp eq i64 0, %25
br i1 %26, label %27, label %36
27: ; preds = %23
%28 = load i32, i32* %9
%29 = icmp ne i32 %28, 0
br i1 %29, label %30, label %32
30: ; preds = %27
%31 = load i32*, i32** @stdout
br label %34
32: ; preds = %27
%33 = load i32*, i32** @stdin
br label %34
34: ; preds = %32, %30
%35 = phi i32* [ %31, %30 ], [ %33, %32 ]
store i32* %35, i32** %4
br label %54
36: ; preds = %23
%37 = load i8*, i8** %5
%38 = load i8*, i8** %6
%39 = call i32* @fopen(i8* %37, i8* %38)
store i32* %39, i32** %8
%40 = load i32*, i32** %8
%41 = icmp eq i32* null, %40
br i1 %41, label %42, label %52
42: ; preds = %36
%43 = load i8*, i8** %7
%44 = load i8*, i8** %5
%45 = load i32, i32* %9
%46 = icmp ne i32 %45, 0
%47 = zext i1 %46 to i64
%48 = select i1 %46, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0)
%49 = load i32, i32* @errno
%50 = call i32 @strerror(i32 %49)
%51 = call i32 (i8*, i8*, i8*, ...) @fatal(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.4, i64 0, i64 0), i8* %43, i8* %44, i8* %48, i32 %50)
br label %52
52: ; preds = %42, %36
%53 = load i32*, i32** %8
store i32* %53, i32** %4
br label %54
54: ; preds = %52, %34
%55 = load i32*, i32** %4
ret i32* %55
}

LLVM IR (-O2):
@.str = [43 x i8] c"%s must have a filename specified for %s.\0A\00"
@.str.1 = [6 x i8] c"write\00"
@.str.2 = [5 x i8] c"read\00"
@.str.3 = [2 x i8] c"-\00"
@stdout = global i32* null
@stdin = global i32* null
@.str.4 = [46 x i8] c"%s cannot open '%s' for %s. Error was '%s'.\0A\00"
@errno = global i32 0
define i32* @xfopen(i8* %0, i8* %1, i8* %2) {
%4 = tail call i32* @strchr(i8* %1, i8 signext 119)
%5 = icmp eq i32* %4, null
%6 = icmp eq i8* %0, null
br i1 %6, label %7, label %10
7: ; preds = %3
%8 = select i1 %5, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0)
%9 = tail call i32 (i8*, i8*, i8*, ...) @fatal(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str, i64 0, i64 0), i8* %2, i8* %8)
br label %10
10: ; preds = %7, %3
%11 = tail call i64 @strcmp(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%12 = icmp eq i64 %11, 0
br i1 %12, label %13, label %17
13: ; preds = %10
%14 = load i32*, i32** @stdout
%15 = load i32*, i32** @stdin
%16 = select i1 %5, i32* %15, i32* %14
br label %25
17: ; preds = %10
%18 = tail call i32* @fopen(i8* %0, i8* %1)
%19 = icmp eq i32* %18, null
br i1 %19, label %20, label %25
20: ; preds = %17
%21 = select i1 %5, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0)
%22 = load i32, i32* @errno
%23 = tail call i32 @strerror(i32 %22)
%24 = tail call i32 (i8*, i8*, i8*, ...) @fatal(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.4, i64 0, i64 0), i8* %2, i8* %0, i8* %21, i32 %23)
br label %25
25: ; preds = %17, %20, %13
%26 = phi i32* [ %16, %13 ], [ null, %20 ], [ %18, %17 ]
ret i32* %26
}