#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*/ _dbg(char *, size_t, ...);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
size_t 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;
  size_t written = 0;
  if (0)
    _dbg("dbg: Writing %d bytes to '%s'...\n", size, filename);
  if (!strcmp(filename, "-"))
    file = stdout;
  else
    file = fopen(filename, "w");
  if (!file) {
    perror(NULL);
    return 0;
  }
  written = fwrite(buf, size, 1, file);
  if (0)
    _dbg("dbg: written %d bytes\n", written * size);
  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)
movq   $0x0,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe8a(%rip),%rsi
call   1040 <strcmp@plt>
cmp    $0x0,%eax
jne    1197 <write_file+0x47>
mov    0x2e55(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x28(%rbp)
jmp    11ab <write_file+0x5b>
mov    -0x10(%rbp),%rdi
lea    0xe60(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11cb <write_file+0x7b>
xor    %eax,%eax
mov    %eax,%edi
call   1060 <perror@plt>
movl   $0x0,-0x4(%rbp)
jmp    11f4 <write_file+0xa4>
mov    -0x18(%rbp),%rdi
mov    -0x1c(%rbp),%esi
mov    -0x28(%rbp),%rcx
mov    $0x1,%edx
call   1070 <fwrite@plt>
mov    %rax,-0x30(%rbp)
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*
%9 = alloca i64
store i8* %0, i8** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
store i64 0, i64* %9
%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 %15, label %13
13: ; preds = %3
%14 = load i32*, i32** @stdout
store i32* %14, i32** %8
br label %18
15: ; preds = %3
%16 = load i8*, i8** %5
%17 = call i32* @fopen(i8* %16, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %17, i32** %8
br label %18
18: ; preds = %15, %13
%19 = load i32*, i32** %8
%20 = icmp ne i32* %19, null
br i1 %20, label %23, label %21
21: ; preds = %18
%22 = call i32 @perror(i32* null)
store i32 0, i32* %4
br label %30
23: ; preds = %18
%24 = load i8*, i8** %6
%25 = load i32, i32* %7
%26 = load i32*, i32** %8
%27 = call i64 @fwrite(i8* %24, i32 %25, i32 1, i32* %26)
store i64 %27, i64* %9
%28 = load i32*, i32** %8
%29 = call i32 @fclose(i32* %28)
store i32 1, i32* %4
br label %30
30: ; preds = %23, %21
%31 = load i32, i32* %4
ret i32 %31
}

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 i64 @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
}