#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);
scalar_t__ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ perror(char const *);
int /*<<< orphan*/ *stdout;
int /*<<< orphan*/ strcmp(char const *, char *);
int /*<<< orphan*/ write_binary(int /*<<< orphan*/ *);
int /*<<< orphan*/ write_hex(int /*<<< orphan*/ *);
int /*<<< orphan*/ write_rom(int /*<<< orphan*/ *);
void write_file(const char *name, int binary, int hex) {
  FILE *file;
  if (!strcmp(name, "-"))
    file = stdout;
  else {
    file = fopen(name, "w");
    if (!file) {
      perror(name);
      exit(1);
    }
  }
  if (binary)
    write_binary(file);
  else if (!hex)
    write_rom(file);
  else
    write_hex(file);
  if (fclose(file)) {
    perror(name);
    exit(1);
  }
}


x86 assembly (clang, -O0):
<write_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe63(%rip),%rsi
call   1050 <strcmp@plt>
cmp    $0x0,%eax
jne    11be <write_file+0x3e>
mov    0x2e2e(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x18(%rbp)
jmp    11f5 <write_file+0x75>
mov    -0x8(%rbp),%rdi
lea    0xe39(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11f0 <write_file+0x70>
mov    -0x8(%rbp),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
jmp    11f5 <write_file+0x75>
cmpl   $0x0,-0xc(%rbp)
je     120d <write_file+0x8d>
mov    -0x18(%rbp),%rdi
call   1060 <write_binary@plt>
jmp    1233 <write_file+0xb3>
cmpl   $0x0,-0x10(%rbp)
jne    1225 <write_file+0xa5>
mov    -0x18(%rbp),%rdi
call   1070 <write_rom@plt>
jmp    122e <write_file+0xae>
mov    -0x18(%rbp),%rdi
call   1030 <write_hex@plt>
jmp    1233 <write_file+0xb3>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
cmp    $0x0,%rax
je     1259 <write_file+0xd9>
mov    -0x8(%rbp),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_file>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%r14d
mov    %esi,%ebx
mov    %rdi,%r15
lea    0xe6a(%rip),%rsi
call   1050 <strcmp@plt>
test   %eax,%eax
je     11b8 <write_file+0x38>
lea    0xe5c(%rip),%rsi
mov    %r15,%rdi
call   1080 <fopen@plt>
mov    %rax,%rbp
test   %rax,%rax
jne    11c2 <write_file+0x42>
jmp    11f9 <write_file+0x79>
mov    0x2e21(%rip),%rax
mov    (%rax),%rbp
mov    %rbp,%rdi
test   %ebx,%ebx
je     11d0 <write_file+0x50>
call   1060 <write_binary@plt>
jmp    11e1 <write_file+0x61>
test   %r14d,%r14d
je     11dc <write_file+0x5c>
call   1030 <write_hex@plt>
jmp    11e1 <write_file+0x61>
call   1070 <write_rom@plt>
mov    %rbp,%rdi
call   1040 <fclose@plt>
test   %rax,%rax
jne    11f9 <write_file+0x79>
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %r15,%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@.str = [2 x i8] c"-\00"
@stdout = global i32* null
@.str.1 = [2 x i8] c"w\00"
define void @write_file(i8* %0, i32 %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32*
store i8* %0, i8** %4
store i32 %1, i32* %5
store i32 %2, i32* %6
%8 = load i8*, i8** %4
%9 = call i32 @strcmp(i8* %8, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%10 = icmp ne i32 %9, 0
br i1 %10, label %13, label %11
11: ; preds = %3
%12 = load i32*, i32** @stdout
store i32* %12, i32** %7
br label %23
13: ; preds = %3
%14 = load i8*, i8** %4
%15 = call i32* @fopen(i8* %14, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %15, i32** %7
%16 = load i32*, i32** %7
%17 = icmp ne i32* %16, null
br i1 %17, label %22, label %18
18: ; preds = %13
%19 = load i8*, i8** %4
%20 = call i32 @perror(i8* %19)
%21 = call i32 @exit(i32 1)
unreachable
22: ; preds = %13
br label %23
23: ; preds = %22, %11
%24 = load i32, i32* %5
%25 = icmp ne i32 %24, 0
br i1 %25, label %26, label %29
26: ; preds = %23
%27 = load i32*, i32** %7
%28 = call i32 @write_binary(i32* %27)
br label %39
29: ; preds = %23
%30 = load i32, i32* %6
%31 = icmp ne i32 %30, 0
br i1 %31, label %35, label %32
32: ; preds = %29
%33 = load i32*, i32** %7
%34 = call i32 @write_rom(i32* %33)
br label %38
35: ; preds = %29
%36 = load i32*, i32** %7
%37 = call i32 @write_hex(i32* %36)
br label %38
38: ; preds = %35, %32
br label %39
39: ; preds = %38, %26
%40 = load i32*, i32** %7
%41 = call i64 @fclose(i32* %40)
%42 = icmp ne i64 %41, 0
br i1 %42, label %43, label %47
43: ; preds = %39
%44 = load i8*, i8** %4
%45 = call i32 @perror(i8* %44)
%46 = call i32 @exit(i32 1)
unreachable
47: ; preds = %39
ret void
}

LLVM IR (-O2):
@.str = [2 x i8] c"-\00"
@stdout = global i32* null
@.str.1 = [2 x i8] c"w\00"
define void @write_file(i8* nocapture %0, i32 %1, i32 %2) {
%4 = tail call i32 @strcmp(i8* dereferenceable(1) %0, i8* dereferenceable(2) getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%5 = icmp eq i32 %4, 0
br i1 %5, label %6, label %8
6: ; preds = %3
%7 = load i32*, i32** @stdout
br label %14
8: ; preds = %3
%9 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%10 = icmp eq i32* %9, null
br i1 %10, label %11, label %14
11: ; preds = %8
%12 = tail call i32 @perror(i8* %0)
%13 = tail call i32 @exit(i32 1)
unreachable
14: ; preds = %8, %6
%15 = phi i32* [ %9, %8 ], [ %7, %6 ]
%16 = icmp eq i32 %1, 0
br i1 %16, label %19, label %17
17: ; preds = %14
%18 = tail call i32 @write_binary(i32* %15)
br label %25
19: ; preds = %14
%20 = icmp eq i32 %2, 0
br i1 %20, label %21, label %23
21: ; preds = %19
%22 = tail call i32 @write_rom(i32* %15)
br label %25
23: ; preds = %19
%24 = tail call i32 @write_hex(i32* %15)
br label %25
25: ; preds = %21, %23, %17
%26 = tail call i64 @fclose(i32* %15)
%27 = icmp eq i64 %26, 0
br i1 %27, label %31, label %28
28: ; preds = %25
%29 = tail call i32 @perror(i8* %0)
%30 = tail call i32 @exit(i32 1)
unreachable
31: ; preds = %25
ret void
}