#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*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fwrite(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(int /*<<< orphan*/ *);
int /*<<< orphan*/ *stdout;
int /*<<< orphan*/ strcmp(char *, char *);
int write_file(char *filename, char *buf, int size) {
  FILE *file;
  if (!strcmp(filename, "-"))
    file = stdout;
  else
    file = fopen(filename, "w");
  if (!file) {
    perror(NULL);
    return 0;
  }
  fwrite(buf, size, 1, file);
  fclose(file);
  return 1;
}


x86 assembly (clang, -O0):
<write_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe92(%rip),%rsi
call   1040 <strcmp@plt>
cmp    $0x0,%eax
jne    118f <write_file+0x3f>
mov    0x2e5d(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x28(%rbp)
jmp    11a3 <write_file+0x53>
mov    -0x10(%rbp),%rdi
lea    0xe68(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11c3 <write_file+0x73>
xor    %eax,%eax
mov    %eax,%edi
call   1060 <perror@plt>
movl   $0x0,-0x4(%rbp)
jmp    11e8 <write_file+0x98>
mov    -0x18(%rbp),%rdi
mov    -0x1c(%rbp),%esi
mov    -0x28(%rbp),%rcx
mov    $0x1,%edx
call   1070 <fwrite@plt>
mov    -0x28(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_file>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%r14d
mov    %rsi,%r15
mov    %rdi,%rbx
lea    0xe99(%rip),%rsi
call   1040 <strcmp@plt>
test   %eax,%eax
je     11b4 <write_file+0x64>
lea    0xe8b(%rip),%rsi
mov    %rbx,%rdi
call   1050 <fopen@plt>
mov    %rax,%rbx
test   %rbx,%rbx
je     11c3 <write_file+0x73>
mov    $0x1,%ebp
mov    %r15,%rdi
mov    %r14d,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1070 <fwrite@plt>
mov    %rbx,%rdi
call   1030 <fclose@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2e25(%rip),%rax
mov    (%rax),%rbx
test   %rbx,%rbx
jne    1187 <write_file+0x37>
xor    %ebp,%ebp
xor    %edi,%edi
call   1060 <perror@plt>
jmp    11a7 <write_file+0x57>

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

LLVM IR (-O2):
@.str = [2 x i8] c"-\00"
@stdout = global i32* null
@.str.1 = [2 x i8] c"w\00"
define i32 @write_file(i8* nocapture %0, i8* nocapture %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 %10
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))
br label %10
10: ; preds = %8, %6
%11 = phi i32* [ %9, %8 ], [ %7, %6 ]
%12 = icmp eq i32* %11, null
br i1 %12, label %13, label %15
13: ; preds = %10
%14 = tail call i32 @perror(i32* null)
br label %18
15: ; preds = %10
%16 = tail call i32 @fwrite(i8* %1, i32 %2, i32 1, i32* %11)
%17 = tail call i32 @fclose(i32* %11)
br label %18
18: ; preds = %15, %13
%19 = phi i32 [ 1, %15 ], [ 0, %13 ]
ret i32 %19
}