#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*/ fflush(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
size_t fwrite(void const *, int, size_t, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, ...);
size_t strlen(char const *);
void write_file(const char *filename, const char *data) {
  FILE *f;
  size_t ret;
  f = fopen(filename, "wb");
  if (!f) {
    printf("failed to open %s\n", "/tmp/duk-test-compile-file-temp.js");
    return;
  }
  ret = fwrite((const void *)data, 1, strlen(data), f);
  fflush(f);
  fclose(f);
  f = NULL;
  if (ret != strlen(data)) {
    printf("failed to write test data fully\n");
    return;
  }
}


x86 assembly (clang, -O0):
<write_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe85(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11a9 <write_file+0x49>
lea    0xe6d(%rip),%rdi
lea    0xe79(%rip),%rsi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    121c <write_file+0xbc>
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x30(%rbp),%rdi
mov    %rax,%rdx
mov    -0x18(%rbp),%rcx
mov    $0x1,%esi
call   1080 <fwrite@plt>
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
call   1060 <fflush@plt>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
movq   $0x0,-0x18(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,%rcx
mov    -0x28(%rbp),%rax
cmp    %rcx,%rax
je     121c <write_file+0xbc>
lea    0xe24(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_file>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%r14
lea    0xe81(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     11cd <write_file+0x5d>
mov    %rax,%rbx
mov    %r14,%rdi
call   1050 <strlen@plt>
mov    %r14,%rdi
mov    $0x1,%esi
mov    %rax,%rdx
mov    %rbx,%rcx
call   1090 <fwrite@plt>
mov    %rax,%r15
mov    %rbx,%rdi
call   1070 <fflush@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    %r14,%rdi
call   1050 <strlen@plt>
cmp    %rax,%r15
jne    11e7 <write_file+0x77>
pop    %rbx
pop    %r14
pop    %r15
ret
lea    0xe2f(%rip),%rdi
lea    0xe3b(%rip),%rsi
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %r15
jmp    1060 <printf@plt>
lea    0xe4b(%rip),%rdi
pop    %rbx
pop    %r14
pop    %r15
jmp    1030 <puts@plt>

LLVM IR (-O0):
@.str = [3 x i8] c"wb\00"
@.str.1 = [19 x i8] c"failed to open %s\0A\00"
@.str.2 = [35 x i8] c"/tmp/duk-test-compile-file-temp.js\00"
@.str.3 = [33 x i8] c"failed to write test data fully\0A\00"
define void @write_file(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca i64
store i8* %0, i8** %3
store i8* %1, i8** %4
%7 = load i8*, i8** %3
%8 = call i32* @fopen(i8* %7, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %8, i32** %5
%9 = load i32*, i32** %5
%10 = icmp ne i32* %9, null
br i1 %10, label %13, label %11
11: ; preds = %2
%12 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.2, i64 0, i64 0))
br label %29
13: ; preds = %2
%14 = load i8*, i8** %4
%15 = load i8*, i8** %4
%16 = call i64 @strlen(i8* %15)
%17 = load i32*, i32** %5
%18 = call i64 @fwrite(i8* %14, i32 1, i64 %16, i32* %17)
store i64 %18, i64* %6
%19 = load i32*, i32** %5
%20 = call i32 @fflush(i32* %19)
%21 = load i32*, i32** %5
%22 = call i32 @fclose(i32* %21)
store i32* null, i32** %5
%23 = load i64, i64* %6
%24 = load i8*, i8** %4
%25 = call i64 @strlen(i8* %24)
%26 = icmp ne i64 %23, %25
br i1 %26, label %27, label %29
27: ; preds = %13
%28 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.3, i64 0, i64 0))
br label %29
29: ; preds = %11, %27, %13
ret void
}

LLVM IR (-O2):
@.str = [3 x i8] c"wb\00"
@.str.1 = [19 x i8] c"failed to open %s\0A\00"
@.str.2 = [35 x i8] c"/tmp/duk-test-compile-file-temp.js\00"
@str = [32 x i8] c"failed to write test data fully\00"
define void @write_file(i8* nocapture %0, i8* nocapture %1) {
%3 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%4 = icmp eq i32* %3, null
br i1 %4, label %5, label %7
5: ; preds = %2
%6 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.2, i64 0, i64 0))
br label %16
7: ; preds = %2
%8 = tail call i64 @strlen(i8* dereferenceable(1) %1)
%9 = tail call i64 @fwrite(i8* %1, i32 1, i64 %8, i32* %3)
%10 = tail call i32 @fflush(i32* %3)
%11 = tail call i32 @fclose(i32* %3)
%12 = tail call i64 @strlen(i8* dereferenceable(1) %1)
%13 = icmp eq i64 %9, %12
br i1 %13, label %16, label %14
14: ; preds = %7
%15 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @str, i64 0, i64 0))
br label %16
16: ; preds = %7, %14, %5
ret void
}