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

/* Variables and functions */
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fmt_err_exit(char *, ...);
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
size_t strlen(char const *);
int write(int, char const *, size_t);
void write_file(int fd, const char *buff) {
  size_t to_write;
  ssize_t written;
  to_write = strlen(buff);
  if ((written = write(fd, buff, to_write)) == -1)
    fmt_err_exit("output file: %s", strerror(errno));
  else if (written != (ssize_t)to_write)
    fmt_err_exit("output file: resource limit reached");
}


x86 assembly (clang, -O0):
<write_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x18(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1030 <write@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0xffffffff,%eax
jne    119b <write_file+0x5b>
mov    0x2e61(%rip),%rax
mov    (%rax),%edi
call   1050 <strerror@plt>
mov    %eax,%esi
lea    0xe71(%rip),%rdi
mov    $0x0,%al
call   1060 <fmt_err_exit@plt>
jmp    11bd <write_file+0x7d>
mov    -0x1c(%rbp),%eax
mov    -0x18(%rbp),%rcx
cmp    %ecx,%eax
je     11b8 <write_file+0x78>
lea    0xe5f(%rip),%rdi
mov    $0x0,%al
call   1060 <fmt_err_exit@plt>
jmp    11bd <write_file+0x7d>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_file>:
push   %rbp
push   %r14
push   %rbx
mov    %rsi,%rbx
mov    %edi,%ebp
mov    %rsi,%rdi
call   1040 <strlen@plt>
mov    %rax,%r14
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %rax,%rdx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
je     1170 <write_file+0x30>
cmp    %r14d,%eax
jne    1192 <write_file+0x52>
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    0x2e69(%rip),%rax
mov    (%rax),%edi
call   1050 <strerror@plt>
lea    0xe7b(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %rbp
jmp    1060 <fmt_err_exit@plt>
lea    0xe77(%rip),%rdi
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %rbp
jmp    1060 <fmt_err_exit@plt>

LLVM IR (-O0):
@.str = [16 x i8] c"output file: %s\00"
@errno = global i32 0
@.str.1 = [36 x i8] c"output file: resource limit reached\00"
define void @write_file(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i32
store i32 %0, i32* %3
store i8* %1, i8** %4
%7 = load i8*, i8** %4
%8 = call i64 @strlen(i8* %7)
store i64 %8, i64* %5
%9 = load i32, i32* %3
%10 = load i8*, i8** %4
%11 = load i64, i64* %5
%12 = call i32 @write(i32 %9, i8* %10, i64 %11)
store i32 %12, i32* %6
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %18
14: ; preds = %2
%15 = load i32, i32* @errno
%16 = call i32 @strerror(i32 %15)
%17 = call i32 (i8*, ...) @fmt_err_exit(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %16)
br label %26
18: ; preds = %2
%19 = load i32, i32* %6
%20 = load i64, i64* %5
%21 = trunc i64 %20 to i32
%22 = icmp ne i32 %19, %21
br i1 %22, label %23, label %25
23: ; preds = %18
%24 = call i32 (i8*, ...) @fmt_err_exit(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.1, i64 0, i64 0))
br label %25
25: ; preds = %23, %18
br label %26
26: ; preds = %25, %14
ret void
}

LLVM IR (-O2):
@.str = [16 x i8] c"output file: %s\00"
@errno = global i32 0
@.str.1 = [36 x i8] c"output file: resource limit reached\00"
define void @write_file(i32 %0, i8* nocapture %1) {
%3 = tail call i64 @strlen(i8* dereferenceable(1) %1)
%4 = tail call i32 @write(i32 %0, i8* %1, i64 %3)
%5 = icmp eq i32 %4, -1
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 (i8*, ...) @fmt_err_exit(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %8)
br label %15
10: ; preds = %2
%11 = trunc i64 %3 to i32
%12 = icmp eq i32 %4, %11
br i1 %12, label %15, label %13
13: ; preds = %10
%14 = tail call i32 (i8*, ...) @fmt_err_exit(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.1, i64 0, i64 0))
br label %15
15: ; preds = %10, %13, %6
ret void
}