#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 char uint8_t;
typedef scalar_t__ uint32_t;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
scalar_t__ fwrite(char *, int, scalar_t__, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, char *);
int WriteFile(uint8_t *filename, char *data, uint32_t size) {
  int ret = 0;
  FILE *f = fopen(filename, "w");
  if (!f) {
    printf("Can't open %s\n", filename);
    return 1;
  }
  uint32_t n = fwrite(data, 1, size, f);
  if (n < size) {
    printf("Can't write %s\n", filename);
    ret = 1;
  }
  fclose(f);
  return ret;
}


x86 assembly (clang, -O0):
<WriteFile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe9a(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    1196 <WriteFile+0x56>
mov    -0x10(%rbp),%rsi
lea    0xe7d(%rip),%rdi
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    11e4 <WriteFile+0xa4>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rdx
mov    -0x30(%rbp),%rcx
mov    $0x1,%esi
call   1060 <fwrite@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
cmp    -0x20(%rbp),%rax
jge    11d5 <WriteFile+0x95>
mov    -0x10(%rbp),%rsi
lea    0xe48(%rip),%rdi
call   1040 <printf@plt>
movl   $0x1,-0x24(%rbp)
mov    -0x30(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<WriteFile>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdx,%r15
mov    %rsi,%rbp
mov    %rdi,%r14
lea    0xea9(%rip),%rsi
call   1050 <fopen@plt>
test   %rax,%rax
je     119c <WriteFile+0x5c>
mov    %rax,%rbx
mov    %rbp,%rdi
mov    $0x1,%esi
mov    %r15,%rdx
mov    %rax,%rcx
call   1060 <fwrite@plt>
xor    %ebp,%ebp
cmp    %r15,%rax
jge    1192 <WriteFile+0x52>
lea    0xe8c(%rip),%rdi
mov    %r14,%rsi
call   1040 <printf@plt>
mov    $0x1,%ebp
mov    %rbx,%rdi
call   1030 <fclose@plt>
jmp    11b0 <WriteFile+0x70>
lea    0xe5f(%rip),%rdi
mov    %r14,%rsi
call   1040 <printf@plt>
mov    $0x1,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [2 x i8] c"w\00"
@.str.1 = [15 x i8] c"Can't open %s\0A\00"
@.str.2 = [16 x i8] c"Can't write %s\0A\00"
define i32 @WriteFile(i8* %0, i8* nocapture %1, i64 %2) {
%4 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%5 = icmp eq i32* %4, null
br i1 %5, label %6, label %8
6: ; preds = %3
%7 = tail call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %16
8: ; preds = %3
%9 = tail call i64 @fwrite(i8* %1, i32 1, i64 %2, i32* %4)
%10 = icmp slt i64 %9, %2
br i1 %10, label %11, label %13
11: ; preds = %8
%12 = tail call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i8* %0)
br label %13
13: ; preds = %11, %8
%14 = phi i32 [ 1, %11 ], [ 0, %8 ]
%15 = tail call i32 @fclose(i32* %4)
br label %16
16: ; preds = %13, %6
%17 = phi i32 [ %14, %13 ], [ 1, %6 ]
ret i32 %17
}