#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*/ exit(int);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ *stdin;
int /*<<< orphan*/ *stdout;
int /*<<< orphan*/ strcmp(char *, char *);
void set_file(int out, char *fn, FILE **dest) {
  if ((!strcmp(fn, "-"))) {
    *dest = out ? stdout : stdin;
  } else {
    *dest = fopen(fn, out ? "w" : "r");
  }
  if (!*dest) {
    perror("fopen");
    exit(1);
  }
}


x86 assembly (clang, -O0):
<set_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xea2(%rip),%rsi
call   1030 <strcmp@plt>
cmp    $0x0,%eax
jne    11a7 <set_file+0x67>
cmpl   $0x0,-0x4(%rbp)
je     1189 <set_file+0x49>
mov    0x2e5b(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x20(%rbp)
jmp    1197 <set_file+0x57>
mov    0x2e58(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
jmp    11d2 <set_file+0x92>
mov    -0x10(%rbp),%rdi
mov    -0x4(%rbp),%ecx
lea    0xe4f(%rip),%rsi
lea    0xe46(%rip),%rax
cmp    $0x0,%ecx
cmovne %rax,%rsi
call   1040 <fopen@plt>
mov    %rax,%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x18(%rbp),%rax
cmpq   $0x0,(%rax)
jne    11f6 <set_file+0xb6>
lea    0xe1f(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<set_file>:
push   %rbp
push   %r14
push   %rbx
mov    %rdx,%r14
mov    %rsi,%rbx
mov    %edi,%ebp
lea    0xead(%rip),%rsi
mov    %rbx,%rdi
call   1030 <strcmp@plt>
test   %eax,%eax
je     117d <set_file+0x3d>
test   %ebp,%ebp
lea    0xe9c(%rip),%rax
lea    0xe93(%rip),%rsi
cmove  %rax,%rsi
mov    %rbx,%rdi
call   1040 <fopen@plt>
jmp    1194 <set_file+0x54>
test   %ebp,%ebp
jne    118a <set_file+0x4a>
mov    0x2e60(%rip),%rax
jmp    1191 <set_file+0x51>
mov    0x2e47(%rip),%rax
mov    (%rax),%rax
mov    %rax,(%r14)
test   %rax,%rax
je     11a1 <set_file+0x61>
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xe5e(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@.str = [2 x i8] c"-\00"
@stdout = global i32* null
@stdin = global i32* null
@.str.1 = [2 x i8] c"w\00"
@.str.2 = [2 x i8] c"r\00"
@.str.3 = [6 x i8] c"fopen\00"
define void @set_file(i32 %0, i8* %1, i32** %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32**
store i32 %0, i32* %4
store i8* %1, i8** %5
store i32** %2, i32*** %6
%7 = load i8*, i8** %5
%8 = call i32 @strcmp(i8* %7, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%9 = icmp ne i32 %8, 0
br i1 %9, label %20, label %10
10: ; preds = %3
%11 = load i32, i32* %4
%12 = icmp ne i32 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %10
%14 = load i32*, i32** @stdout
br label %17
15: ; preds = %10
%16 = load i32*, i32** @stdin
br label %17
17: ; preds = %15, %13
%18 = phi i32* [ %14, %13 ], [ %16, %15 ]
%19 = load i32**, i32*** %6
store i32* %18, i32** %19
br label %28
20: ; preds = %3
%21 = load i8*, i8** %5
%22 = load i32, i32* %4
%23 = icmp ne i32 %22, 0
%24 = zext i1 %23 to i64
%25 = select i1 %23, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0)
%26 = call i32* @fopen(i8* %21, i8* %25)
%27 = load i32**, i32*** %6
store i32* %26, i32** %27
br label %28
28: ; preds = %20, %17
%29 = load i32**, i32*** %6
%30 = load i32*, i32** %29
%31 = icmp ne i32* %30, null
br i1 %31, label %35, label %32
32: ; preds = %28
%33 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
%34 = call i32 @exit(i32 1)
unreachable
35: ; preds = %28
ret void
}

LLVM IR (-O2):
@.str = [2 x i8] c"-\00"
@stdout = global i32* null
@stdin = global i32* null
@.str.1 = [2 x i8] c"w\00"
@.str.2 = [2 x i8] c"r\00"
@.str.3 = [6 x i8] c"fopen\00"
define void @set_file(i32 %0, i8* nocapture %1, i32** nocapture writeonly %2) {
%4 = tail call i32 @strcmp(i8* dereferenceable(1) %1, i8* dereferenceable(2) getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%5 = icmp eq i32 %4, 0
%6 = icmp eq i32 %0, 0
br i1 %5, label %7, label %11
7: ; preds = %3
%8 = load i32*, i32** @stdout
%9 = load i32*, i32** @stdin
%10 = select i1 %6, i32* %9, i32* %8
br label %14
11: ; preds = %3
%12 = select i1 %6, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0)
%13 = tail call i32* @fopen(i8* %1, i8* %12)
br label %14
14: ; preds = %11, %7
%15 = phi i32* [ %10, %7 ], [ %13, %11 ]
store i32* %15, i32** %2
%16 = icmp eq i32* %15, null
br i1 %16, label %17, label %20
17: ; preds = %14
%18 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
%19 = tail call i32 @exit(i32 1)
unreachable
20: ; preds = %14
ret void
}