#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*/ fclose(int /*<<< orphan*/ *);
scalar_t__ ferror(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
scalar_t__ fwrite(void const *, int, unsigned int, int /*<<< orphan*/ *);
void write_file(const char *file, const void *buf, unsigned buflen) {
  FILE *fp = fopen(file, "w");
  size_t size = 0;
  if (fp == NULL)
    die("Couldn't open %s", file);
  while (size < buflen) {
    size += fwrite(buf + size, 1, buflen - size, fp);
    if (ferror(fp))
      die("Error writing %s", file);
  }
  fclose(fp);
}


x86 assembly (clang, -O0):
<write_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe92(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    119a <write_file+0x4a>
mov    -0x8(%rbp),%rsi
lea    0xe6d(%rip),%rdi
call   1060 <die@plt>
jmp    119f <write_file+0x4f>
mov    -0x28(%rbp),%rax
mov    -0x14(%rbp),%ecx
cmp    %rcx,%rax
jae    11fe <write_file+0xae>
mov    -0x10(%rbp),%rdi
add    -0x28(%rbp),%rdi
mov    -0x14(%rbp),%eax
sub    -0x28(%rbp),%rax
mov    %eax,%edx
mov    -0x20(%rbp),%rcx
mov    $0x1,%esi
call   1070 <fwrite@plt>
add    -0x28(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rdi
call   1030 <ferror@plt>
cmp    $0x0,%rax
je     11f9 <write_file+0xa9>
mov    -0x8(%rbp),%rsi
lea    0xe1f(%rip),%rdi
call   1060 <die@plt>
jmp    119f <write_file+0x4f>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_file>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edx,%r13d
mov    %rsi,%r14
lea    0xe98(%rip),%rsi
mov    %rdi,(%rsp)
call   1050 <fopen@plt>
mov    %rax,%rbx
test   %rax,%rax
je     1180 <write_file+0x30>
test   %r13d,%r13d
jne    1195 <write_file+0x45>
jmp    11e3 <write_file+0x93>
lea    0xe7b(%rip),%rdi
mov    (%rsp),%rsi
call   1060 <die@plt>
test   %r13d,%r13d
je     11e3 <write_file+0x93>
mov    %r13d,%r12d
xor    %r15d,%r15d
jmp    11a5 <write_file+0x55>
nopl   (%rax)
cmp    %r12,%r15
jae    11e3 <write_file+0x93>
mov    %r15,%rbp
lea    (%r14,%r15,1),%rdi
mov    %r13d,%edx
sub    %ebp,%edx
mov    $0x1,%esi
mov    %rbx,%rcx
call   1070 <fwrite@plt>
mov    %rax,%r15
add    %rbp,%r15
mov    %rbx,%rdi
call   1030 <ferror@plt>
test   %rax,%rax
je     11a0 <write_file+0x50>
lea    0xe3b(%rip),%rdi
mov    (%rsp),%rsi
call   1060 <die@plt>
jmp    11a0 <write_file+0x50>
mov    %rbx,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <fclose@plt>

LLVM IR (-O0):
@.str = [2 x i8] c"w\00"
@.str.1 = [17 x i8] c"Couldn't open %s\00"
@.str.2 = [17 x i8] c"Error writing %s\00"
define void @write_file(i8* %0, i8* %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32*
%8 = alloca i64
store i8* %0, i8** %4
store i8* %1, i8** %5
store i32 %2, i32* %6
%9 = load i8*, i8** %4
%10 = call i32* @fopen(i8* %9, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %10, i32** %7
store i64 0, i64* %8
%11 = load i32*, i32** %7
%12 = icmp eq i32* %11, null
br i1 %12, label %13, label %16
13: ; preds = %3
%14 = load i8*, i8** %4
%15 = call i32 @die(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i8* %14)
br label %16
16: ; preds = %13, %3
br label %17
17: ; preds = %41, %16
%18 = load i64, i64* %8
%19 = load i32, i32* %6
%20 = zext i32 %19 to i64
%21 = icmp ult i64 %18, %20
br i1 %21, label %22, label %42
22: ; preds = %17
%23 = load i8*, i8** %5
%24 = load i64, i64* %8
%25 = getelementptr i8, i8* %23, i64 %24
%26 = load i32, i32* %6
%27 = zext i32 %26 to i64
%28 = load i64, i64* %8
%29 = sub i64 %27, %28
%30 = trunc i64 %29 to i32
%31 = load i32*, i32** %7
%32 = call i64 @fwrite(i8* %25, i32 1, i32 %30, i32* %31)
%33 = load i64, i64* %8
%34 = add i64 %33, %32
store i64 %34, i64* %8
%35 = load i32*, i32** %7
%36 = call i64 @ferror(i32* %35)
%37 = icmp ne i64 %36, 0
br i1 %37, label %38, label %41
38: ; preds = %22
%39 = load i8*, i8** %4
%40 = call i32 @die(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i8* %39)
br label %41
41: ; preds = %38, %22
br label %17
42: ; preds = %17
%43 = load i32*, i32** %7
%44 = call i32 @fclose(i32* %43)
ret void
}

LLVM IR (-O2):
@.str = [2 x i8] c"w\00"
@.str.1 = [17 x i8] c"Couldn't open %s\00"
@.str.2 = [17 x i8] c"Error writing %s\00"
define void @write_file(i8* %0, i8* nocapture %1, i32 %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 %8
6: ; preds = %3
%7 = tail call i32 @die(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %8
8: ; preds = %6, %3
%9 = zext i32 %2 to i64
%10 = icmp eq i32 %2, 0
br i1 %10, label %24, label %11
11: ; preds = %8, %22
%12 = phi i64 [ %17, %22 ], [ 0, %8 ]
%13 = getelementptr i8, i8* %1, i64 %12
%14 = trunc i64 %12 to i32
%15 = sub i32 %2, %14
%16 = tail call i64 @fwrite(i8* %13, i32 1, i32 %15, i32* %4)
%17 = add i64 %16, %12
%18 = tail call i64 @ferror(i32* %4)
%19 = icmp eq i64 %18, 0
br i1 %19, label %22, label %20
20: ; preds = %11
%21 = tail call i32 @die(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i8* %0)
br label %22
22: ; preds = %20, %11
%23 = icmp ult i64 %17, %9
br i1 %23, label %11, label %24
24: ; preds = %22, %8
%25 = tail call i32 @fclose(i32* %4)
ret void
}