#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*/ fail(char *, ...);
int /*<<< orphan*/ *fopen(char const *, char const *);
int /*<<< orphan*/ *stderr;
int /*<<< orphan*/ *stdin;
int /*<<< orphan*/ *stdout;
scalar_t__ strcmp(char *, char const *);
FILE *xfopen(const char *s, const char *p) {
  FILE *f;
  if (!s)
    fail("trying to open a file with NULL name");
  if (0 == strcmp("-", s)) {
    if (0 == strcmp("w", p))
      return stdout;
    else if (0 == strcmp("r", p))
      return stdin;
    else
      fail("unknown fopen mode \"%s\"", p);
  }
  if (0 == strcmp("--", s) && 0 == strcmp("w", p))
    return stderr;
  f = fopen(s, p);
  if (f == NULL)
    fail("can not open file \"%s\" in mode \"%s\", // (%s)", s, p);
  return f;
}


x86 assembly (clang, -O0):
<xfopen>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1159 <xfopen+0x29>
lea    0xeae(%rip),%rdi
mov    $0x0,%al
call   1030 <fail@plt>
mov    -0x10(%rbp),%rsi
lea    0xede(%rip),%rdi
call   1040 <strcmp@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
jne    11f5 <xfopen+0xc5>
mov    -0x18(%rbp),%rsi
lea    0xea3(%rip),%rdi
call   1040 <strcmp@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
jne    11a8 <xfopen+0x78>
mov    0x2e34(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x8(%rbp)
jmp    127e <xfopen+0x14e>
mov    -0x18(%rbp),%rsi
lea    0xe74(%rip),%rdi
call   1040 <strcmp@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
jne    11d9 <xfopen+0xa9>
mov    0x2e1b(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x8(%rbp)
jmp    127e <xfopen+0x14e>
mov    -0x18(%rbp),%rsi
lea    0xe45(%rip),%rdi
mov    $0x0,%al
call   1030 <fail@plt>
jmp    11f0 <xfopen+0xc0>
jmp    11f5 <xfopen+0xc5>
mov    -0x10(%rbp),%rsi
lea    0xe41(%rip),%rdi
call   1040 <strcmp@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
jne    1244 <xfopen+0x114>
mov    -0x18(%rbp),%rsi
lea    0xe07(%rip),%rdi
call   1040 <strcmp@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
jne    1244 <xfopen+0x114>
mov    0x2da0(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x8(%rbp)
jmp    127e <xfopen+0x14e>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1276 <xfopen+0x146>
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
lea    0xdd5(%rip),%rdi
mov    $0x0,%al
call   1030 <fail@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<xfopen>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%r14
mov    %rdi,%r15
test   %rdi,%rdi
jne    114e <xfopen+0x1e>
lea    0xeb9(%rip),%rdi
xor    %eax,%eax
call   1030 <fail@plt>
lea    0xeed(%rip),%rdi
mov    %r15,%rsi
call   1040 <strcmp@plt>
test   %rax,%rax
je     11b5 <xfopen+0x85>
lea    0xed8(%rip),%rdi
mov    %r15,%rsi
call   1040 <strcmp@plt>
test   %rax,%rax
jne    118a <xfopen+0x5a>
lea    0xea8(%rip),%rdi
mov    %r14,%rsi
call   1040 <strcmp@plt>
test   %rax,%rax
je     11fc <xfopen+0xcc>
mov    %r15,%rdi
mov    %r14,%rsi
call   1050 <fopen@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    120f <xfopen+0xdf>
lea    0xea0(%rip),%rdi
xor    %ebx,%ebx
mov    %r15,%rsi
mov    %r14,%rdx
xor    %eax,%eax
call   1030 <fail@plt>
jmp    120f <xfopen+0xdf>
lea    0xe69(%rip),%rdi
mov    %r14,%rsi
call   1040 <strcmp@plt>
test   %rax,%rax
je     11f3 <xfopen+0xc3>
lea    0xe57(%rip),%rdi
mov    %r14,%rsi
call   1040 <strcmp@plt>
test   %rax,%rax
je     1205 <xfopen+0xd5>
lea    0xe45(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1030 <fail@plt>
jmp    1162 <xfopen+0x32>
mov    0x2dd6(%rip),%rax
jmp    120c <xfopen+0xdc>
mov    0x2dd5(%rip),%rax
jmp    120c <xfopen+0xdc>
mov    0x2ddc(%rip),%rax
mov    (%rax),%rbx
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [37 x i8] c"trying to open a file with NULL name\00"
@.str.1 = [2 x i8] c"-\00"
@.str.2 = [2 x i8] c"w\00"
@stdout = global i32* null
@.str.3 = [2 x i8] c"r\00"
@stdin = global i32* null
@.str.4 = [24 x i8] c"unknown fopen mode \22%s\22\00"
@.str.5 = [3 x i8] c"--\00"
@stderr = global i32* null
@.str.6 = [45 x i8] c"can not open file \22%s\22 in mode \22%s\22, // (%s)\00"
define i32* @xfopen(i8* %0, i8* %1) {
%3 = alloca i32*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32*
store i8* %0, i8** %4
store i8* %1, i8** %5
%7 = load i8*, i8** %4
%8 = icmp ne i8* %7, null
br i1 %8, label %11, label %9
9: ; preds = %2
%10 = call i32 (i8*, ...) @fail(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0))
br label %11
11: ; preds = %9, %2
%12 = load i8*, i8** %4
%13 = call i64 @strcmp(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i8* %12)
%14 = icmp eq i64 0, %13
br i1 %14, label %15, label %32
15: ; preds = %11
%16 = load i8*, i8** %5
%17 = call i64 @strcmp(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), i8* %16)
%18 = icmp eq i64 0, %17
br i1 %18, label %19, label %21
19: ; preds = %15
%20 = load i32*, i32** @stdout
store i32* %20, i32** %3
br label %54
21: ; preds = %15
%22 = load i8*, i8** %5
%23 = call i64 @strcmp(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0), i8* %22)
%24 = icmp eq i64 0, %23
br i1 %24, label %25, label %27
25: ; preds = %21
%26 = load i32*, i32** @stdin
store i32* %26, i32** %3
br label %54
27: ; preds = %21
%28 = load i8*, i8** %5
%29 = call i32 (i8*, ...) @fail(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.4, i64 0, i64 0), i8* %28)
br label %30
30: ; preds = %27
br label %31
31: ; preds = %30
br label %32
32: ; preds = %31, %11
%33 = load i8*, i8** %4
%34 = call i64 @strcmp(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i8* %33)
%35 = icmp eq i64 0, %34
br i1 %35, label %36, label %42
36: ; preds = %32
%37 = load i8*, i8** %5
%38 = call i64 @strcmp(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), i8* %37)
%39 = icmp eq i64 0, %38
br i1 %39, label %40, label %42
40: ; preds = %36
%41 = load i32*, i32** @stderr
store i32* %41, i32** %3
br label %54
42: ; preds = %36, %32
%43 = load i8*, i8** %4
%44 = load i8*, i8** %5
%45 = call i32* @fopen(i8* %43, i8* %44)
store i32* %45, i32** %6
%46 = load i32*, i32** %6
%47 = icmp eq i32* %46, null
br i1 %47, label %48, label %52
48: ; preds = %42
%49 = load i8*, i8** %4
%50 = load i8*, i8** %5
%51 = call i32 (i8*, ...) @fail(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.6, i64 0, i64 0), i8* %49, i8* %50)
br label %52
52: ; preds = %48, %42
%53 = load i32*, i32** %6
store i32* %53, i32** %3
br label %54
54: ; preds = %52, %40, %25, %19
%55 = load i32*, i32** %3
ret i32* %55
}

LLVM IR (-O2):
@.str = [37 x i8] c"trying to open a file with NULL name\00"
@.str.1 = [2 x i8] c"-\00"
@.str.2 = [2 x i8] c"w\00"
@stdout = global i32* null
@.str.3 = [2 x i8] c"r\00"
@stdin = global i32* null
@.str.4 = [24 x i8] c"unknown fopen mode \22%s\22\00"
@.str.5 = [3 x i8] c"--\00"
@stderr = global i32* null
@.str.6 = [45 x i8] c"can not open file \22%s\22 in mode \22%s\22, // (%s)\00"
define i32* @xfopen(i8* %0, i8* %1) {
%3 = icmp eq i8* %0, null
br i1 %3, label %4, label %6
4: ; preds = %2
%5 = tail call i32 (i8*, ...) @fail(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0))
br label %6
6: ; preds = %4, %2
%7 = tail call i64 @strcmp(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i8* %0)
%8 = icmp eq i64 %7, 0
br i1 %8, label %9, label %21
9: ; preds = %6
%10 = tail call i64 @strcmp(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), i8* %1)
%11 = icmp eq i64 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %9
%13 = load i32*, i32** @stdout
br label %34
14: ; preds = %9
%15 = tail call i64 @strcmp(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0), i8* %1)
%16 = icmp eq i64 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %14
%18 = load i32*, i32** @stdin
br label %34
19: ; preds = %14
%20 = tail call i32 (i8*, ...) @fail(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.4, i64 0, i64 0), i8* %1)
br label %21
21: ; preds = %19, %6
%22 = tail call i64 @strcmp(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i8* %0)
%23 = icmp eq i64 %22, 0
br i1 %23, label %24, label %29
24: ; preds = %21
%25 = tail call i64 @strcmp(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), i8* %1)
%26 = icmp eq i64 %25, 0
br i1 %26, label %27, label %29
27: ; preds = %24
%28 = load i32*, i32** @stderr
br label %34
29: ; preds = %24, %21
%30 = tail call i32* @fopen(i8* %0, i8* %1)
%31 = icmp eq i32* %30, null
br i1 %31, label %32, label %34
32: ; preds = %29
%33 = tail call i32 (i8*, ...) @fail(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.6, i64 0, i64 0), i8* %0, i8* %1)
br label %34
34: ; preds = %29, %32, %27, %17, %12
%35 = phi i32* [ %13, %12 ], [ %18, %17 ], [ %28, %27 ], [ null, %32 ], [ %30, %29 ]
ret i32* %35
}