#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 *);
scalar_t__ feof(int /*<<< orphan*/ *);
scalar_t__ ferror(int /*<<< orphan*/ *);
scalar_t__ fwrite(char *, int, size_t, int /*<<< orphan*/ *);
void wrbuf(FILE *fd, void *p, size_t sz) {
  size_t n;
  char *buf;
  n = 0;
  buf = p;
  while (n < sz) {
    n += fwrite(buf + n, 1, sz - n, fd);
    if (feof(fd))
      die("Unexpected EOF");
    if (ferror(fd))
      die("Error writing");
  }
}


x86 assembly (clang, -O0):
<wrbuf>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rax
cmp    -0x18(%rbp),%rax
jae    11db <wrbuf+0x9b>
mov    -0x28(%rbp),%rdi
add    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rdx
sub    -0x20(%rbp),%rdx
mov    -0x8(%rbp),%rcx
mov    $0x1,%esi
call   1060 <fwrite@plt>
add    -0x20(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <feof@plt>
cmp    $0x0,%rax
je     11b7 <wrbuf+0x77>
lea    0xe4e(%rip),%rdi
call   1050 <die@plt>
mov    -0x8(%rbp),%rdi
call   1030 <ferror@plt>
cmp    $0x0,%rax
je     11d6 <wrbuf+0x96>
lea    0xe3e(%rip),%rdi
call   1050 <die@plt>
jmp    1164 <wrbuf+0x24>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<wrbuf>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
test   %rdx,%rdx
je     11c2 <wrbuf+0x82>
mov    %rdx,%r15
mov    %rsi,%r14
mov    %rdi,%rbx
xor    %r12d,%r12d
lea    0xe9d(%rip),%r13
jmp    1175 <wrbuf+0x35>
cs nopw 0x0(%rax,%rax,1)
nop
cmp    %r15,%r12
jae    11c2 <wrbuf+0x82>
lea    (%r14,%r12,1),%rdi
mov    %r15,%rdx
sub    %r12,%rdx
mov    $0x1,%esi
mov    %rbx,%rcx
call   1060 <fwrite@plt>
mov    %rax,%rbp
mov    %rbx,%rdi
call   1040 <feof@plt>
test   %rax,%rax
je     11a4 <wrbuf+0x64>
mov    %r13,%rdi
call   1050 <die@plt>
add    %rbp,%r12
mov    %rbx,%rdi
call   1030 <ferror@plt>
test   %rax,%rax
je     1170 <wrbuf+0x30>
lea    0xe54(%rip),%rdi
call   1050 <die@plt>
jmp    1170 <wrbuf+0x30>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [15 x i8] c"Unexpected EOF\00"
@.str.1 = [14 x i8] c"Error writing\00"
define void @wrbuf(i32* %0, i8* %1, i64 %2) {
%4 = alloca i32*
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i64
%8 = alloca i8*
store i32* %0, i32** %4
store i8* %1, i8** %5
store i64 %2, i64* %6
store i64 0, i64* %7
%9 = load i8*, i8** %5
store i8* %9, i8** %8
br label %10
10: ; preds = %36, %3
%11 = load i64, i64* %7
%12 = load i64, i64* %6
%13 = icmp ult i64 %11, %12
br i1 %13, label %14, label %37
14: ; preds = %10
%15 = load i8*, i8** %8
%16 = load i64, i64* %7
%17 = getelementptr inbounds i8, i8* %15, i64 %16
%18 = load i64, i64* %6
%19 = load i64, i64* %7
%20 = sub i64 %18, %19
%21 = load i32*, i32** %4
%22 = call i64 @fwrite(i8* %17, i32 1, i64 %20, i32* %21)
%23 = load i64, i64* %7
%24 = add i64 %23, %22
store i64 %24, i64* %7
%25 = load i32*, i32** %4
%26 = call i64 @feof(i32* %25)
%27 = icmp ne i64 %26, 0
br i1 %27, label %28, label %30
28: ; preds = %14
%29 = call i32 @die(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
br label %30
30: ; preds = %28, %14
%31 = load i32*, i32** %4
%32 = call i64 @ferror(i32* %31)
%33 = icmp ne i64 %32, 0
br i1 %33, label %34, label %36
34: ; preds = %30
%35 = call i32 @die(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
br label %36
36: ; preds = %34, %30
br label %10
37: ; preds = %10
ret void
}

LLVM IR (-O2):
@.str = [15 x i8] c"Unexpected EOF\00"
@.str.1 = [14 x i8] c"Error writing\00"
define void @wrbuf(i32* nocapture %0, i8* nocapture %1, i64 %2) {
%4 = icmp eq i64 %2, 0
br i1 %4, label %22, label %5
5: ; preds = %3, %20
%6 = phi i64 [ %10, %20 ], [ 0, %3 ]
%7 = getelementptr inbounds i8, i8* %1, i64 %6
%8 = sub i64 %2, %6
%9 = tail call i64 @fwrite(i8* %7, i32 1, i64 %8, i32* %0)
%10 = add i64 %9, %6
%11 = tail call i64 @feof(i32* %0)
%12 = icmp eq i64 %11, 0
br i1 %12, label %15, label %13
13: ; preds = %5
%14 = tail call i32 @die(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
br label %15
15: ; preds = %13, %5
%16 = tail call i64 @ferror(i32* %0)
%17 = icmp eq i64 %16, 0
br i1 %17, label %20, label %18
18: ; preds = %15
%19 = tail call i32 @die(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
br label %20
20: ; preds = %18, %15
%21 = icmp ult i64 %10, %2
br i1 %21, label %5, label %22
22: ; preds = %20, %3
ret void
}