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

/* Variables and functions */
int dup(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ error(int, int /*<<< orphan*/, char *, char const *,
                         char const *);
int /*<<< orphan*/ *fdopen(int, char const *);
int open(char const *, int, int /*<<< orphan*/);
int /*<<< orphan*/ strcmp(char const *, char *);
FILE *xfdopen(const char *name, int flags, mode_t open_mode, const char *mode) {
  FILE *fp;
  int fd;
  if (!strcmp(name, "-"))
    fd = dup((*mode == 'r') ? 0 : 1);
  else
    fd = open(name, flags, open_mode);
  if (fd == -1)
    error(1, errno, "open %s (%s)", name, mode);
  fp = fdopen(fd, mode);
  if (fp == NULL)
    error(1, errno, "fopen %s (%s)", name, mode);
  return fp;
}


x86 assembly (clang, -O0):
<xfdopen>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    %rcx,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe8f(%rip),%rsi
call   1040 <strcmp@plt>
cmp    $0x0,%eax
jne    11a0 <xfdopen+0x50>
mov    -0x18(%rbp),%rax
movsbl (%rax),%ecx
mov    $0x1,%edi
xor    %eax,%eax
cmp    $0x72,%ecx
cmove  %eax,%edi
call   1030 <dup@plt>
mov    %eax,-0x24(%rbp)
jmp    11b2 <xfdopen+0x62>
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
mov    -0x10(%rbp),%edx
call   1070 <open@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    11de <xfdopen+0x8e>
mov    0x2e1d(%rip),%rax
mov    (%rax),%esi
mov    -0x8(%rbp),%rcx
mov    -0x18(%rbp),%r8
mov    $0x1,%edi
lea    0xe2a(%rip),%rdx
call   1060 <error@plt>
mov    -0x24(%rbp),%edi
mov    -0x18(%rbp),%rsi
call   1050 <fdopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    121b <xfdopen+0xcb>
mov    0x2de0(%rip),%rax
mov    (%rax),%esi
mov    -0x8(%rbp),%rcx
mov    -0x18(%rbp),%r8
mov    $0x1,%edi
lea    0xdec(%rip),%rdx
call   1060 <error@plt>
mov    -0x20(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<xfdopen>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rcx,%r14
mov    %edx,%ebp
mov    %esi,%ebx
mov    %rdi,%r15
lea    0xe98(%rip),%rsi
call   1040 <strcmp@plt>
test   %eax,%eax
je     117f <xfdopen+0x2f>
mov    %r15,%rdi
mov    %ebx,%esi
mov    %ebp,%edx
call   1070 <open@plt>
jmp    118e <xfdopen+0x3e>
xor    %edi,%edi
cmpb   $0x72,(%r14)
setne  %dil
call   1030 <dup@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
jne    11b5 <xfdopen+0x65>
mov    0x2e44(%rip),%rax
mov    (%rax),%esi
lea    0xe5e(%rip),%rdx
mov    $0x1,%edi
mov    %r15,%rcx
mov    %r14,%r8
call   1060 <error@plt>
mov    %ebp,%edi
mov    %r14,%rsi
call   1050 <fdopen@plt>
mov    %rax,%rbp
test   %rax,%rax
jne    11e7 <xfdopen+0x97>
mov    0x2e12(%rip),%rax
mov    (%rax),%esi
lea    0xe2b(%rip),%rdx
mov    $0x1,%edi
mov    %r15,%rcx
mov    %r14,%r8
call   1060 <error@plt>
mov    %rbp,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c"-\00"
@errno = global i32 0
@.str.1 = [13 x i8] c"open %s (%s)\00"
@.str.2 = [14 x i8] c"fopen %s (%s)\00"
define i32* @xfdopen(i8* %0, i32 %1, i32 %2, i8* %3) {
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i32*
%10 = alloca i32
store i8* %0, i8** %5
store i32 %1, i32* %6
store i32 %2, i32* %7
store i8* %3, i8** %8
%11 = load i8*, i8** %5
%12 = call i32 @strcmp(i8* %11, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%13 = icmp ne i32 %12, 0
br i1 %13, label %22, label %14
14: ; preds = %4
%15 = load i8*, i8** %8
%16 = load i8, i8* %15
%17 = sext i8 %16 to i32
%18 = icmp eq i32 %17, 114
%19 = zext i1 %18 to i64
%20 = select i1 %18, i32 0, i32 1
%21 = call i32 @dup(i32 %20)
store i32 %21, i32* %10
br label %27
22: ; preds = %4
%23 = load i8*, i8** %5
%24 = load i32, i32* %6
%25 = load i32, i32* %7
%26 = call i32 @open(i8* %23, i32 %24, i32 %25)
store i32 %26, i32* %10
br label %27
27: ; preds = %22, %14
%28 = load i32, i32* %10
%29 = icmp eq i32 %28, -1
br i1 %29, label %30, label %35
30: ; preds = %27
%31 = load i32, i32* @errno
%32 = load i8*, i8** %5
%33 = load i8*, i8** %8
%34 = call i32 @error(i32 1, i32 %31, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i8* %32, i8* %33)
br label %35
35: ; preds = %30, %27
%36 = load i32, i32* %10
%37 = load i8*, i8** %8
%38 = call i32* @fdopen(i32 %36, i8* %37)
store i32* %38, i32** %9
%39 = load i32*, i32** %9
%40 = icmp eq i32* %39, null
br i1 %40, label %41, label %46
41: ; preds = %35
%42 = load i32, i32* @errno
%43 = load i8*, i8** %5
%44 = load i8*, i8** %8
%45 = call i32 @error(i32 1, i32 %42, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i8* %43, i8* %44)
br label %46
46: ; preds = %41, %35
%47 = load i32*, i32** %9
ret i32* %47
}

LLVM IR (-O2):
@.str = [2 x i8] c"-\00"
@errno = global i32 0
@.str.1 = [13 x i8] c"open %s (%s)\00"
@.str.2 = [14 x i8] c"fopen %s (%s)\00"
define noalias i32* @xfdopen(i8* %0, i32 %1, i32 %2, i8* %3) {
%5 = tail call i32 @strcmp(i8* dereferenceable(1) %0, i8* dereferenceable(2) getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%6 = icmp eq i32 %5, 0
br i1 %6, label %7, label %12
7: ; preds = %4
%8 = load i8, i8* %3
%9 = icmp ne i8 %8, 114
%10 = zext i1 %9 to i32
%11 = tail call i32 @dup(i32 %10)
br label %14
12: ; preds = %4
%13 = tail call i32 @open(i8* %0, i32 %1, i32 %2)
br label %14
14: ; preds = %12, %7
%15 = phi i32 [ %13, %12 ], [ %11, %7 ]
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %20
17: ; preds = %14
%18 = load i32, i32* @errno
%19 = tail call i32 @error(i32 1, i32 %18, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i8* %0, i8* %3)
br label %20
20: ; preds = %17, %14
%21 = tail call i32* @fdopen(i32 %15, i8* %3)
%22 = icmp eq i32* %21, null
br i1 %22, label %23, label %26
23: ; preds = %20
%24 = load i32, i32* @errno
%25 = tail call i32 @error(i32 1, i32 %24, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i8* %0, i8* %3)
br label %26
26: ; preds = %23, %20
ret i32* %21
}