#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 ssize_t;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
scalar_t__ EINTR;
scalar_t__ errno;
int /*<<< orphan*/ error(int /*<<< orphan*/, scalar_t__, char *, char const *);
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*/ *);
ssize_t write_file(const char *path, char *buf, size_t len) {
  FILE *ctl_file;
  size_t nwritten;
  while ((ctl_file = fopen(path, "w+")) == NULL && errno == EINTR)
    ;
  if (ctl_file == NULL) {
    error(0, errno, "%s", path);
    return -1;
  }
  nwritten = fwrite(buf, 1, len, ctl_file);
  if (ferror(ctl_file)) {
    error(0, 0, "%s: Error writing file", path);
    return -1;
  }
  fclose(ctl_file);
  return nwritten;
}


x86 assembly (clang, -O0):
<write_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe91(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,%rcx
mov    %rcx,-0x28(%rbp)
xor    %eax,%eax
cmp    $0x0,%rcx
mov    %al,-0x31(%rbp)
jne    11a4 <write_file+0x54>
mov    0x2e4f(%rip),%rax
mov    (%rax),%rax
mov    0x2e3d(%rip),%rcx
cmp    (%rcx),%rax
sete   %al
mov    %al,-0x31(%rbp)
mov    -0x31(%rbp),%al
test   $0x1,%al
jne    11b4 <write_file+0x64>
jmp    11b9 <write_file+0x69>
jmp    1164 <write_file+0x14>
cmpq   $0x0,-0x28(%rbp)
jne    11ec <write_file+0x9c>
mov    0x2e15(%rip),%rax
mov    (%rax),%rsi
mov    -0x10(%rbp),%rcx
xor    %edi,%edi
lea    0xe28(%rip),%rdx
call   1050 <error@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    124b <write_file+0xfb>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rdx
mov    -0x28(%rbp),%rcx
mov    $0x1,%esi
call   1070 <fwrite@plt>
mov    %rax,-0x30(%rbp)
mov    -0x28(%rbp),%rdi
call   1030 <ferror@plt>
cmp    $0x0,%rax
je     123b <write_file+0xeb>
mov    -0x10(%rbp),%rcx
xor    %edi,%edi
xor    %eax,%eax
mov    %eax,%esi
lea    0xddc(%rip),%rdx
call   1050 <error@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    124b <write_file+0xfb>
mov    -0x28(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x30(%rbp),%rax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%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    %rdx,(%rsp)
mov    %rsi,%r12
mov    %rdi,%r14
lea    0xe94(%rip),%r13
mov    0x2e6d(%rip),%rbp
mov    0x2e5e(%rip),%r15
nopw   0x0(%rax,%rax,1)
mov    %r14,%rdi
mov    %r13,%rsi
call   1060 <fopen@plt>
mov    %rax,%rbx
mov    0x0(%rbp),%rsi
test   %rax,%rax
jne    119c <write_file+0x4c>
cmp    (%r15),%rsi
je     1180 <write_file+0x30>
test   %rbx,%rbx
je     11d2 <write_file+0x82>
mov    %r12,%rdi
mov    $0x1,%esi
mov    (%rsp),%rdx
mov    %rbx,%rcx
call   1070 <fwrite@plt>
mov    %rax,%r15
mov    %rbx,%rdi
call   1030 <ferror@plt>
test   %rax,%rax
je     11eb <write_file+0x9b>
lea    0xe3a(%rip),%rdx
xor    %edi,%edi
xor    %esi,%esi
jmp    11db <write_file+0x8b>
lea    0xe2a(%rip),%rdx
xor    %edi,%edi
mov    %r14,%rcx
call   1050 <error@plt>
mov    $0xffffffff,%r15d
jmp    11f3 <write_file+0xa3>
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    %r15d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [3 x i8] c"w+\00"
@errno = global i64 0
@EINTR = global i64 0
@.str.1 = [3 x i8] c"%s\00"
@.str.2 = [23 x i8] c"%s: Error writing file\00"
define i32 @write_file(i8* %0, i8* nocapture %1, i64 %2) {
br label %4
4: ; preds = %4, %3
%5 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%6 = icmp eq i32* %5, null
%7 = load i64, i64* @errno
%8 = load i64, i64* @EINTR
%9 = icmp eq i64 %7, %8
%10 = select i1 %6, i1 %9, i1 false
br i1 %10, label %4, label %11
11: ; preds = %4
br i1 %6, label %12, label %14
12: ; preds = %11
%13 = tail call i32 @error(i32 0, i64 %7, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %23
14: ; preds = %11
%15 = tail call i64 @fwrite(i8* %1, i32 1, i64 %2, i32* %5)
%16 = tail call i64 @ferror(i32* %5)
%17 = icmp eq i64 %16, 0
br i1 %17, label %20, label %18
18: ; preds = %14
%19 = tail call i32 @error(i32 0, i64 0, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i8* %0)
br label %23
20: ; preds = %14
%21 = tail call i32 @fclose(i32* %5)
%22 = trunc i64 %15 to i32
br label %23
23: ; preds = %20, %18, %12
%24 = phi i32 [ -1, %12 ], [ -1, %18 ], [ %22, %20 ]
ret i32 %24
}