#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 scalar_t__ ssize_t;

/* Variables and functions */
int /*<<< orphan*/ O_WRONLY;
scalar_t__ close(int);
int /*<<< orphan*/ die(char *);
int /*<<< orphan*/ fail_errno_except_eintr(char *);
int open(char const *, int /*<<< orphan*/);
scalar_t__ strlen(char const *);
scalar_t__ write(int, char const *, scalar_t__);
void write_file(const char *filename, const char *content) {
  int fd = open(filename, O_WRONLY);
  ssize_t n;
  while ((int)(n = write(fd, content, strlen(content))) == -1)
    fail_errno_except_eintr("n = write(fd, content, strlen(content))");
  ;
  if (n < strlen(content)) {
    die("incomplete write");
  }
  while ((int)(close(fd)) == -1)
    fail_errno_except_eintr("close(fd)");
  ;
}


x86 assembly (clang, -O0):
<write_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
mov    0x2e5d(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x2c(%rbp),%edi
mov    -0x28(%rbp),%rsi
mov    %rax,%rdx
call   1040 <write@plt>
mov    %rax,-0x20(%rbp)
cmp    $0xffffffff,%eax
jne    11c9 <write_file+0x69>
lea    0xe41(%rip),%rdi
call   1030 <fail_errno_except_eintr@plt>
jmp    1185 <write_file+0x25>
mov    -0x20(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
mov    %rax,%rcx
mov    -0x38(%rbp),%rax
cmp    %rcx,%rax
jge    11f6 <write_file+0x96>
lea    0xe37(%rip),%rdi
call   1080 <die@plt>
jmp    11fb <write_file+0x9b>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
cmp    $0xffffffff,%eax
jne    121d <write_file+0xbd>
lea    0xe26(%rip),%rdi
call   1030 <fail_errno_except_eintr@plt>
jmp    11fb <write_file+0x9b>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_file>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    0x2e67(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,%r15d
mov    %rbx,%rdi
call   1050 <strlen@plt>
mov    %r15d,%edi
mov    %rbx,%rsi
mov    %rax,%rdx
call   1040 <write@plt>
mov    %rax,%rbp
cmp    $0xffffffff,%ebp
jne    11c6 <write_file+0x66>
lea    0xe60(%rip),%r14
mov    %r14,%rdi
call   1030 <fail_errno_except_eintr@plt>
mov    %rbx,%rdi
call   1050 <strlen@plt>
mov    %r15d,%edi
mov    %rbx,%rsi
mov    %rax,%rdx
call   1040 <write@plt>
mov    %rax,%rbp
cmp    $0xffffffff,%ebp
je     11a0 <write_file+0x40>
mov    %rbx,%rdi
call   1050 <strlen@plt>
cmp    %rax,%rbp
jge    11df <write_file+0x7f>
lea    0xe4e(%rip),%rdi
call   1080 <die@plt>
mov    %r15d,%edi
call   1060 <close@plt>
cmp    $0xffffffff,%eax
jne    1215 <write_file+0xb5>
lea    0xe46(%rip),%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %rbx,%rdi
call   1030 <fail_errno_except_eintr@plt>
mov    %r15d,%edi
call   1060 <close@plt>
cmp    $0xffffffff,%eax
je     1200 <write_file+0xa0>
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@O_WRONLY = global i32 0
@.str = [40 x i8] c"n = write(fd, content, strlen(content))\00"
@.str.1 = [17 x i8] c"incomplete write\00"
@.str.2 = [10 x i8] c"close(fd)\00"
define void @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 = load i32, i32* @O_WRONLY
%9 = call i32 @open(i8* %7, i32 %8)
store i32 %9, i32* %5
br label %10
10: ; preds = %18, %2
%11 = load i32, i32* %5
%12 = load i8*, i8** %4
%13 = load i8*, i8** %4
%14 = call i64 @strlen(i8* %13)
%15 = call i64 @write(i32 %11, i8* %12, i64 %14)
store i64 %15, i64* %6
%16 = trunc i64 %15 to i32
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %20
18: ; preds = %10
%19 = call i32 @fail_errno_except_eintr(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0))
br label %10
20: ; preds = %10
%21 = load i64, i64* %6
%22 = load i8*, i8** %4
%23 = call i64 @strlen(i8* %22)
%24 = icmp slt i64 %21, %23
br i1 %24, label %25, label %27
25: ; preds = %20
%26 = call i32 @die(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
br label %27
27: ; preds = %25, %20
br label %28
28: ; preds = %33, %27
%29 = load i32, i32* %5
%30 = call i64 @close(i32 %29)
%31 = trunc i64 %30 to i32
%32 = icmp eq i32 %31, -1
br i1 %32, label %33, label %35
33: ; preds = %28
%34 = call i32 @fail_errno_except_eintr(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0))
br label %28
35: ; preds = %28
ret void
}

LLVM IR (-O2):
@O_WRONLY = global i32 0
@.str = [40 x i8] c"n = write(fd, content, strlen(content))\00"
@.str.1 = [17 x i8] c"incomplete write\00"
@.str.2 = [10 x i8] c"close(fd)\00"
define void @write_file(i8* nocapture %0, i8* nocapture %1) {
%3 = load i32, i32* @O_WRONLY
%4 = tail call i32 @open(i8* %0, i32 %3)
%5 = tail call i64 @strlen(i8* dereferenceable(1) %1)
%6 = tail call i64 @write(i32 %4, i8* %1, i64 %5)
%7 = trunc i64 %6 to i32
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %15
9: ; preds = %2, %9
%10 = tail call i32 @fail_errno_except_eintr(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0))
%11 = tail call i64 @strlen(i8* dereferenceable(1) %1)
%12 = tail call i64 @write(i32 %4, i8* %1, i64 %11)
%13 = trunc i64 %12 to i32
%14 = icmp eq i32 %13, -1
br i1 %14, label %9, label %15
15: ; preds = %9, %2
%16 = phi i64 [ %6, %2 ], [ %12, %9 ]
%17 = tail call i64 @strlen(i8* dereferenceable(1) %1)
%18 = icmp slt i64 %16, %17
br i1 %18, label %19, label %21
19: ; preds = %15
%20 = tail call i32 @die(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
br label %21
21: ; preds = %19, %15
%22 = tail call i64 @close(i32 %4)
%23 = trunc i64 %22 to i32
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %30
25: ; preds = %21, %25
%26 = tail call i32 @fail_errno_except_eintr(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0))
%27 = tail call i64 @close(i32 %4)
%28 = trunc i64 %27 to i32
%29 = icmp eq i32 %28, -1
br i1 %29, label %25, label %30
30: ; preds = %25, %21
ret void
}