#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);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *);
size_t fwrite(void const *, int, size_t, int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
void write_file(const void *data, size_t len, const char *filename) {
  FILE *file;
  size_t wrote_len;
  file = fopen(filename, "wb");
  if (!file) {
    fprintf(stderr, "ERROR: Can't write to: %s\n", filename);
    exit(1);
  }
  wrote_len = fwrite(data, 1, len, file);
  if (wrote_len != len) {
    fprintf(stderr, "ERROR: Failed writting to: %s\n", filename);
    fclose(file);
    exit(1);
  }
  fclose(file);
}


x86 assembly (clang, -O0):
<write_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
lea    0xe91(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11a6 <write_file+0x56>
mov    0x2e56(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rdx
lea    0xe6c(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    -0x20(%rbp),%rcx
mov    $0x1,%esi
call   1070 <fwrite@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x10(%rbp),%rax
je     11fa <write_file+0xaa>
mov    0x2e0b(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rdx
lea    0xe3c(%rip),%rsi
call   1040 <fprintf@plt>
mov    -0x20(%rbp),%rdi
call   1030 <fclose@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
mov    -0x20(%rbp),%rdi
call   1030 <fclose@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_file>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%r12
lea    0xe98(%rip),%rsi
mov    %rdx,%rdi
call   1050 <fopen@plt>
test   %rax,%rax
je     11a3 <write_file+0x53>
mov    %rax,%rbx
mov    %r12,%rdi
mov    $0x1,%esi
mov    %r15,%rdx
mov    %rax,%rcx
call   1070 <fwrite@plt>
cmp    %r15,%rax
jne    11c5 <write_file+0x75>
mov    %rbx,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
jmp    1030 <fclose@plt>
mov    0x2e36(%rip),%rax
mov    (%rax),%edi
lea    0xe50(%rip),%rsi
mov    %r14,%rdx
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
mov    0x2e14(%rip),%rax
mov    (%rax),%edi
lea    0xe49(%rip),%rsi
mov    %r14,%rdx
call   1040 <fprintf@plt>
mov    %rbx,%rdi
call   1030 <fclose@plt>
mov    $0x1,%edi
call   1060 <exit@plt>

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

LLVM IR (-O2):
@.str = [3 x i8] c"wb\00"
@stderr = global i32 0
@.str.1 = [27 x i8] c"ERROR: Can't write to: %s\0A\00"
@.str.2 = [31 x i8] c"ERROR: Failed writting to: %s\0A\00"
define void @write_file(i8* nocapture %0, i64 %1, i8* %2) {
%4 = tail call i32* @fopen(i8* %2, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%5 = icmp eq i32* %4, null
br i1 %5, label %6, label %10
6: ; preds = %3
%7 = load i32, i32* @stderr
%8 = tail call i32 @fprintf(i32 %7, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i8* %2)
%9 = tail call i32 @exit(i32 1)
unreachable
10: ; preds = %3
%11 = tail call i64 @fwrite(i8* %0, i32 1, i64 %1, i32* %4)
%12 = icmp eq i64 %11, %1
br i1 %12, label %18, label %13
13: ; preds = %10
%14 = load i32, i32* @stderr
%15 = tail call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0), i8* %2)
%16 = tail call i32 @fclose(i32* %4)
%17 = tail call i32 @exit(i32 1)
unreachable
18: ; preds = %10
%19 = tail call i32 @fclose(i32* %4)
ret void
}