#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*/ die(char *, char const *, int /*<<< orphan*/);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
scalar_t__ ferror(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
size_t fwrite(char *, int, size_t, int /*<<< orphan*/ *);
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
size_t write_file(const char *path, char *buf, size_t len) {
  FILE *ctl_file;
  size_t nwritten;
  ctl_file = fopen(path, "w+");
  if (ctl_file == NULL)
    die("could not open file: %s: %s", path, strerror(errno));
  nwritten = fwrite(buf, 1, len, ctl_file);
  if (ferror(ctl_file))
    die("could not write file: %s: %s", path, strerror(errno));
  fclose(ctl_file);
  return nwritten;
}


x86 assembly (clang, -O0):
<write_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe81(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11bb <write_file+0x5b>
mov    -0x8(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    0x2e3e(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x30(%rbp),%rsi
mov    %eax,%edx
lea    0xe4d(%rip),%rdi
call   1060 <die@plt>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    -0x20(%rbp),%rcx
mov    $0x1,%esi
call   1070 <fwrite@plt>
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rdi
call   1030 <ferror@plt>
cmp    $0x0,%rax
je     1210 <write_file+0xb0>
mov    -0x8(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    0x2de9(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x38(%rbp),%rsi
mov    %eax,%edx
lea    0xe14(%rip),%rdi
call   1060 <die@plt>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x28(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_file>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r15
mov    %rsi,%r12
mov    %rdi,%r14
lea    0xe88(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    11a4 <write_file+0x44>
mov    0x2e54(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xe69(%rip),%rdi
mov    %r14,%rsi
mov    %eax,%edx
call   1060 <die@plt>
mov    %r12,%rdi
mov    $0x1,%esi
mov    %r15,%rdx
mov    %rbx,%rcx
call   1070 <fwrite@plt>
mov    %rax,%r15
mov    %rbx,%rdi
call   1030 <ferror@plt>
test   %rax,%rax
je     11e6 <write_file+0x86>
mov    0x2e12(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xe43(%rip),%rdi
mov    %r14,%rsi
mov    %eax,%edx
call   1060 <die@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    %r15,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [3 x i8] c"w+\00"
@.str.1 = [28 x i8] c"could not open file: %s: %s\00"
@errno = global i32 0
@.str.2 = [29 x i8] c"could not write file: %s: %s\00"
define i64 @write_file(i8* %0, i8* %1, i64 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i32*
%8 = alloca i64
store i8* %0, i8** %4
store i8* %1, i8** %5
store i64 %2, i64* %6
%9 = load i8*, i8** %4
%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 eq i32* %11, null
br i1 %12, label %13, label %18
13: ; preds = %3
%14 = load i8*, i8** %4
%15 = load i32, i32* @errno
%16 = call i32 @strerror(i32 %15)
%17 = call i32 @die(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i8* %14, i32 %16)
br label %18
18: ; preds = %13, %3
%19 = load i8*, i8** %5
%20 = load i64, i64* %6
%21 = load i32*, i32** %7
%22 = call i64 @fwrite(i8* %19, i32 1, i64 %20, i32* %21)
store i64 %22, i64* %8
%23 = load i32*, i32** %7
%24 = call i64 @ferror(i32* %23)
%25 = icmp ne i64 %24, 0
br i1 %25, label %26, label %31
26: ; preds = %18
%27 = load i8*, i8** %4
%28 = load i32, i32* @errno
%29 = call i32 @strerror(i32 %28)
%30 = call i32 @die(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0), i8* %27, i32 %29)
br label %31
31: ; preds = %26, %18
%32 = load i32*, i32** %7
%33 = call i32 @fclose(i32* %32)
%34 = load i64, i64* %8
ret i64 %34
}

LLVM IR (-O2):
@.str = [3 x i8] c"w+\00"
@.str.1 = [28 x i8] c"could not open file: %s: %s\00"
@errno = global i32 0
@.str.2 = [29 x i8] c"could not write file: %s: %s\00"
define i64 @write_file(i8* %0, i8* nocapture %1, i64 %2) {
%4 = tail call i32* @fopen(i8* %0, 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* @errno
%8 = tail call i32 @strerror(i32 %7)
%9 = tail call i32 @die(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i8* %0, i32 %8)
br label %10
10: ; preds = %6, %3
%11 = tail call i64 @fwrite(i8* %1, i32 1, i64 %2, i32* %4)
%12 = tail call i64 @ferror(i32* %4)
%13 = icmp eq i64 %12, 0
br i1 %13, label %18, label %14
14: ; preds = %10
%15 = load i32, i32* @errno
%16 = tail call i32 @strerror(i32 %15)
%17 = tail call i32 @die(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0), i8* %0, i32 %16)
br label %18
18: ; preds = %14, %10
%19 = tail call i32 @fclose(i32* %4)
ret i64 %11
}