#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*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ fwrite(char *, int, size_t, int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
int io_write(char *path, char *data, size_t length) {
  FILE *fd = NULL;
  fd = fopen(path, "w");
  if (fd == NULL) {
    fprintf(stderr, "Failed to open file: %s\n", path);
    return -1;
  }
  fwrite(data, sizeof(char), length, fd);
  fclose(fd);
  return 0;
}


x86 assembly (clang, -O0):
<io_write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe99(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11a0 <io_write+0x60>
mov    0x2e5e(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe73(%rip),%rsi
call   1040 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11c6 <io_write+0x86>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rdx
mov    -0x28(%rbp),%rcx
mov    $0x1,%esi
call   1060 <fwrite@plt>
mov    -0x28(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<io_write>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%r12
lea    0xea8(%rip),%rsi
call   1050 <fopen@plt>
test   %rax,%rax
je     1184 <io_write+0x44>
mov    %rax,%rbx
mov    %r15,%rdi
mov    $0x1,%esi
mov    %r14,%rdx
mov    %rax,%rcx
call   1060 <fwrite@plt>
mov    %rbx,%rdi
call   1030 <fclose@plt>
xor    %eax,%eax
jmp    11a1 <io_write+0x61>
mov    0x2e55(%rip),%rax
mov    (%rax),%edi
lea    0xe6e(%rip),%rsi
mov    %r12,%rdx
call   1040 <fprintf@plt>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [2 x i8] c"w\00"
@stderr = global i32 0
@.str.1 = [25 x i8] c"Failed to open file: %s\0A\00"
define i32 @io_write(i8* %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32*
store i8* %0, i8** %5
store i8* %1, i8** %6
store i64 %2, i64* %7
store i32* null, i32** %8
%9 = load i8*, i8** %5
%10 = call i32* @fopen(i8* %9, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %10, i32** %8
%11 = load i32*, i32** %8
%12 = icmp eq i32* %11, null
br i1 %12, label %13, label %17
13: ; preds = %3
%14 = load i32, i32* @stderr
%15 = load i8*, i8** %5
%16 = call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i8* %15)
store i32 -1, i32* %4
br label %24
17: ; preds = %3
%18 = load i8*, i8** %6
%19 = load i64, i64* %7
%20 = load i32*, i32** %8
%21 = call i32 @fwrite(i8* %18, i32 1, i64 %19, i32* %20)
%22 = load i32*, i32** %8
%23 = call i32 @fclose(i32* %22)
store i32 0, i32* %4
br label %24
24: ; preds = %17, %13
%25 = load i32, i32* %4
ret i32 %25
}

LLVM IR (-O2):
@.str = [2 x i8] c"w\00"
@stderr = global i32 0
@.str.1 = [25 x i8] c"Failed to open file: %s\0A\00"
define i32 @io_write(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 %9
6: ; preds = %3
%7 = load i32, i32* @stderr
%8 = tail call i32 @fprintf(i32 %7, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %12
9: ; preds = %3
%10 = tail call i32 @fwrite(i8* %1, i32 1, i64 %2, i32* %4)
%11 = tail call i32 @fclose(i32* %4)
br label %12
12: ; preds = %9, %6
%13 = phi i32 [ -1, %6 ], [ 0, %9 ]
ret i32 %13
}