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

/* Variables and functions */
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ *fopen(char *, char *);
size_t fread(char *, int, int, int /*<<< orphan*/);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
int /*<<< orphan*/ fwrite(char *, int, size_t, int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ stdin;
int /*<<< orphan*/ *stdout;
int /*<<< orphan*/ strcmp(char *, char *);
int /*<<< orphan*/ *tmpfile();
void set_file(int out, char *fn, FILE **dest) {
  if ((!strcmp(fn, "-"))) {
    if (out) {
      *dest = stdout;
    } else {
      char b[4096];
      size_t n = 0;
      FILE *tmpf = tmpfile();
      if (!tmpf)
        perror("tmpfile");
      while ((n = fread(b, sizeof(*b), sizeof(b), stdin)) > 0)
        fwrite(b, sizeof(*b), n, tmpf);
      fseek(tmpf, 0, SEEK_SET);
      *dest = tmpf;
    }
  } 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    $0x1030,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe5f(%rip),%rsi
call   1050 <strcmp@plt>
cmp    $0x0,%eax
jne    1285 <set_file+0x105>
cmpl   $0x0,-0x4(%rbp)
je     11cf <set_file+0x4f>
mov    0x2e10(%rip),%rax
mov    (%rax),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
jmp    1280 <set_file+0x100>
movq   $0x0,-0x1028(%rbp)
mov    $0x0,%al
call   1040 <tmpfile@plt>
mov    %rax,-0x1030(%rbp)
cmpq   $0x0,-0x1030(%rbp)
jne    1202 <set_file+0x82>
lea    0xe05(%rip),%rdi
call   1080 <perror@plt>
jmp    1207 <set_file+0x87>
lea    -0x1020(%rbp),%rdi
mov    0x2dd3(%rip),%rax
mov    (%rax),%ecx
mov    $0x1,%esi
mov    $0x1000,%edx
call   1030 <fread@plt>
mov    %rax,-0x1028(%rbp)
cmp    $0x0,%rax
jbe    125b <set_file+0xdb>
lea    -0x1020(%rbp),%rdi
mov    -0x1028(%rbp),%rdx
mov    -0x1030(%rbp),%rcx
mov    $0x1,%esi
call   10a0 <fwrite@plt>
jmp    1207 <set_file+0x87>
mov    -0x1030(%rbp),%rdi
mov    0x2d6f(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1060 <fseek@plt>
mov    -0x1030(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
jmp    12b0 <set_file+0x130>
mov    -0x10(%rbp),%rdi
mov    -0x4(%rbp),%ecx
lea    0xd79(%rip),%rsi
lea    0xd70(%rip),%rax
cmp    $0x0,%ecx
cmovne %rax,%rsi
call   1070 <fopen@plt>
mov    %rax,%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x18(%rbp),%rax
cmpq   $0x0,(%rax)
jne    12d4 <set_file+0x154>
lea    0xd49(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
add    $0x1030,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<set_file>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x1008,%rsp
mov    %rdx,%r14
mov    %rsi,%rbx
mov    %edi,%ebp
lea    0xe64(%rip),%rsi
mov    %rbx,%rdi
call   1050 <strcmp@plt>
test   %eax,%eax
je     11e1 <set_file+0x61>
test   %ebp,%ebp
lea    0xe5b(%rip),%rax
lea    0xe52(%rip),%rsi
cmove  %rax,%rsi
mov    %rbx,%rdi
call   1070 <fopen@plt>
mov    %rax,%rbx
mov    %rax,(%r14)
test   %rbx,%rbx
je     127c <set_file+0xfc>
add    $0x1008,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
test   %ebp,%ebp
je     11f1 <set_file+0x71>
mov    0x2de4(%rip),%rax
mov    (%rax),%rbx
jmp    1270 <set_file+0xf0>
xor    %eax,%eax
call   1040 <tmpfile@plt>
mov    %rax,%rbx
test   %rax,%rax
je     1292 <set_file+0x112>
mov    0x2ddd(%rip),%rbp
mov    0x0(%rbp),%ecx
mov    %rsp,%rdi
mov    $0x1,%esi
mov    $0x1000,%edx
call   1030 <fread@plt>
test   %rax,%rax
je     125d <set_file+0xdd>
mov    %rsp,%r15
nopl   0x0(%rax,%rax,1)
mov    %r15,%rdi
mov    $0x1,%esi
mov    %rax,%rdx
mov    %rbx,%rcx
call   10a0 <fwrite@plt>
mov    0x0(%rbp),%ecx
mov    %r15,%rdi
mov    $0x1,%esi
mov    $0x1000,%edx
call   1030 <fread@plt>
test   %rax,%rax
jne    1230 <set_file+0xb0>
mov    0x2d74(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1060 <fseek@plt>
mov    %rbx,(%r14)
test   %rbx,%rbx
jne    11d3 <set_file+0x53>
lea    0xd8b(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
lea    0xd69(%rip),%rdi
call   1080 <perror@plt>
jmp    1204 <set_file+0x84>

LLVM IR (-O0):
@.str = [2 x i8] c"-\00"
@stdout = global i32* null
@.str.1 = [8 x i8] c"tmpfile\00"
@stdin = global i32 0
@SEEK_SET = global i32 0
@.str.2 = [2 x i8] c"w\00"
@.str.3 = [2 x i8] c"r\00"
@.str.4 = [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**
%7 = alloca [4096 x i8]
%8 = alloca i64
%9 = alloca i32*
store i32 %0, i32* %4
store i8* %1, i8** %5
store i32** %2, i32*** %6
%10 = load i8*, i8** %5
%11 = call i32 @strcmp(i8* %10, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%12 = icmp ne i32 %11, 0
br i1 %12, label %43, label %13
13: ; preds = %3
%14 = load i32, i32* %4
%15 = icmp ne i32 %14, 0
br i1 %15, label %16, label %19
16: ; preds = %13
%17 = load i32*, i32** @stdout
%18 = load i32**, i32*** %6
store i32* %17, i32** %18
br label %42
19: ; preds = %13
store i64 0, i64* %8
%20 = call i32* (...) @tmpfile()
store i32* %20, i32** %9
%21 = load i32*, i32** %9
%22 = icmp ne i32* %21, null
br i1 %22, label %25, label %23
23: ; preds = %19
%24 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
br label %25
25: ; preds = %23, %19
br label %26
26: ; preds = %31, %25
%27 = getelementptr inbounds [4096 x i8], [4096 x i8]* %7, i64 0, i64 0
%28 = load i32, i32* @stdin
%29 = call i64 @fread(i8* %27, i32 1, i32 4096, i32 %28)
store i64 %29, i64* %8
%30 = icmp ugt i64 %29, 0
br i1 %30, label %31, label %36
31: ; preds = %26
%32 = getelementptr inbounds [4096 x i8], [4096 x i8]* %7, i64 0, i64 0
%33 = load i64, i64* %8
%34 = load i32*, i32** %9
%35 = call i32 @fwrite(i8* %32, i32 1, i64 %33, i32* %34)
br label %26
36: ; preds = %26
%37 = load i32*, i32** %9
%38 = load i32, i32* @SEEK_SET
%39 = call i32 @fseek(i32* %37, i32 0, i32 %38)
%40 = load i32*, i32** %9
%41 = load i32**, i32*** %6
store i32* %40, i32** %41
br label %42
42: ; preds = %36, %16
br label %51
43: ; preds = %3
%44 = load i8*, i8** %5
%45 = load i32, i32* %4
%46 = icmp ne i32 %45, 0
%47 = zext i1 %46 to i64
%48 = select i1 %46, 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.3, i64 0, i64 0)
%49 = call i32* @fopen(i8* %44, i8* %48)
%50 = load i32**, i32*** %6
store i32* %49, i32** %50
br label %51
51: ; preds = %43, %42
%52 = load i32**, i32*** %6
%53 = load i32*, i32** %52
%54 = icmp ne i32* %53, null
br i1 %54, label %58, label %55
55: ; preds = %51
%56 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0))
%57 = call i32 @exit(i32 1)
unreachable
58: ; preds = %51
ret void
}

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