#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*/ chaz_Util_die(char *, char const *, int /*<<< orphan*/);
int /*<<< orphan*/ errno;
scalar_t__ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ fwrite(char const *, int, size_t, int /*<<< orphan*/ *);
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
size_t strlen(char const *);
void chaz_Util_write_file(const char *filename, const char *content) {
  FILE *fh = fopen(filename, "w+");
  size_t content_len = strlen(content);
  if (fh == NULL) {
    chaz_Util_die("Couldn't open '%s': %s", filename, strerror(errno));
  }
  fwrite(content, sizeof(char), content_len, fh);
  if (fclose(fh)) {
    chaz_Util_die("Error when closing '%s': %s", filename, strerror(errno));
  }
}


x86 assembly (clang, -O0):
<chaz_Util_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   1050 <fopen@plt>
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11c4 <chaz_Util_write_file+0x64>
mov    -0x8(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    0x2e35(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    -0x28(%rbp),%rsi
mov    %eax,%edx
lea    0xe44(%rip),%rdi
call   1080 <chaz_Util_die@plt>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rdx
mov    -0x18(%rbp),%rcx
mov    $0x1,%esi
call   1060 <fwrite@plt>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
cmp    $0x0,%rax
je     1215 <chaz_Util_write_file+0xb5>
mov    -0x8(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    0x2de4(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    -0x30(%rbp),%rsi
mov    %eax,%edx
lea    0xe0a(%rip),%rdi
call   1080 <chaz_Util_die@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<chaz_Util_write_file>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r15
mov    %rdi,%r14
lea    0xe8b(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,%rbx
mov    %r15,%rdi
call   1040 <strlen@plt>
mov    %rax,%r12
test   %rbx,%rbx
jne    11ac <chaz_Util_write_file+0x4c>
mov    0x2e4c(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
lea    0xe61(%rip),%rdi
mov    %r14,%rsi
mov    %eax,%edx
call   1080 <chaz_Util_die@plt>
mov    %r15,%rdi
mov    $0x1,%esi
mov    %r12,%rdx
mov    %rbx,%rcx
call   1060 <fwrite@plt>
mov    %rbx,%rdi
call   1030 <fclose@plt>
test   %rax,%rax
je     11f6 <chaz_Util_write_file+0x96>
mov    0x2e0d(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
lea    0xe39(%rip),%rdi
mov    %r14,%rsi
mov    %eax,%edx
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
jmp    1080 <chaz_Util_die@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [3 x i8] c"w+\00"
@.str.1 = [23 x i8] c"Couldn't open '%s': %s\00"
@errno = global i32 0
@.str.2 = [28 x i8] c"Error when closing '%s': %s\00"
define void @chaz_Util_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 i8*, i8** %4
%10 = call i64 @strlen(i8* %9)
store i64 %10, i64* %6
%11 = load i32*, i32** %5
%12 = icmp eq i32* %11, null
br i1 %12, label %13, label %18
13: ; preds = %2
%14 = load i8*, i8** %3
%15 = load i32, i32* @errno
%16 = call i32 @strerror(i32 %15)
%17 = call i32 @chaz_Util_die(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i8* %14, i32 %16)
br label %18
18: ; preds = %13, %2
%19 = load i8*, i8** %4
%20 = load i64, i64* %6
%21 = load i32*, i32** %5
%22 = call i32 @fwrite(i8* %19, i32 1, i64 %20, i32* %21)
%23 = load i32*, i32** %5
%24 = call i64 @fclose(i32* %23)
%25 = icmp ne i64 %24, 0
br i1 %25, label %26, label %31
26: ; preds = %18
%27 = load i8*, i8** %3
%28 = load i32, i32* @errno
%29 = call i32 @strerror(i32 %28)
%30 = call i32 @chaz_Util_die(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0), i8* %27, i32 %29)
br label %31
31: ; preds = %26, %18
ret void
}

LLVM IR (-O2):
@.str = [3 x i8] c"w+\00"
@.str.1 = [23 x i8] c"Couldn't open '%s': %s\00"
@errno = global i32 0
@.str.2 = [28 x i8] c"Error when closing '%s': %s\00"
define void @chaz_Util_write_file(i8* %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 = tail call i64 @strlen(i8* dereferenceable(1) %1)
%5 = icmp eq i32* %3, null
br i1 %5, label %6, label %10
6: ; preds = %2
%7 = load i32, i32* @errno
%8 = tail call i32 @strerror(i32 %7)
%9 = tail call i32 @chaz_Util_die(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i8* %0, i32 %8)
br label %10
10: ; preds = %6, %2
%11 = tail call i32 @fwrite(i8* %1, i32 1, i64 %4, i32* %3)
%12 = tail call i64 @fclose(i32* %3)
%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 @chaz_Util_die(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0), i8* %0, i32 %16)
br label %18
18: ; preds = %14, %10
ret void
}