#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*/ exit(int);
int /*<<< orphan*/ perror(char *);
scalar_t__ write(int, void const *, size_t);
ssize_t xwrite(int fd, const void *buf, size_t count) {
  ssize_t r, r2;
  r2 = 0;
  while (count) {
    r = write(fd, buf, count);
    if (r < 0) {
      perror("write");
      exit(1);
    }
    count -= r;
    buf += r;
    r2 += r;
  }
  return r2;
}


x86 assembly (clang, -O0):
<xwrite>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movq   $0x0,-0x28(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     11b7 <xwrite+0x87>
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1030 <write@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jge    118b <xwrite+0x5b>
lea    0xe84(%rip),%rdi
call   1040 <perror@plt>
mov    $0x1,%edi
call   1050 <exit@plt>
mov    -0x20(%rbp),%rcx
mov    -0x18(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x18(%rbp)
mov    -0x20(%rbp),%rax
add    -0x10(%rbp),%rax
mov    %rax,-0x10(%rbp)
mov    -0x20(%rbp),%rax
add    -0x28(%rbp),%rax
mov    %rax,-0x28(%rbp)
jmp    114b <xwrite+0x1b>
mov    -0x28(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<xwrite>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
test   %rdx,%rdx
je     116f <xwrite+0x3f>
mov    %rdx,%r15
mov    %rsi,%rbx
mov    %edi,%ebp
xor    %r14d,%r14d
nopw   0x0(%rax,%rax,1)
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %r15,%rdx
call   1030 <write@plt>
test   %rax,%rax
js     1180 <xwrite+0x50>
add    %rax,%rbx
add    %rax,%r14
sub    %rax,%r15
jne    1150 <xwrite+0x20>
jmp    1172 <xwrite+0x42>
xor    %r14d,%r14d
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe79(%rip),%rdi
call   1040 <perror@plt>
mov    $0x1,%edi
call   1050 <exit@plt>

LLVM IR (-O0):
@.str = [6 x i8] c"write\00"
define i64 @xwrite(i32 %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i64
%8 = alloca i64
store i32 %0, i32* %4
store i8* %1, i8** %5
store i64 %2, i64* %6
store i64 0, i64* %8
br label %9
9: ; preds = %22, %3
%10 = load i64, i64* %6
%11 = icmp ne i64 %10, 0
br i1 %11, label %12, label %32
12: ; preds = %9
%13 = load i32, i32* %4
%14 = load i8*, i8** %5
%15 = load i64, i64* %6
%16 = call i64 @write(i32 %13, i8* %14, i64 %15)
store i64 %16, i64* %7
%17 = load i64, i64* %7
%18 = icmp slt i64 %17, 0
br i1 %18, label %19, label %22
19: ; preds = %12
%20 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%21 = call i32 @exit(i32 1)
unreachable
22: ; preds = %12
%23 = load i64, i64* %7
%24 = load i64, i64* %6
%25 = sub i64 %24, %23
store i64 %25, i64* %6
%26 = load i64, i64* %7
%27 = load i8*, i8** %5
%28 = getelementptr i8, i8* %27, i64 %26
store i8* %28, i8** %5
%29 = load i64, i64* %7
%30 = load i64, i64* %8
%31 = add nsw i64 %30, %29
store i64 %31, i64* %8
br label %9
32: ; preds = %9
%33 = load i64, i64* %8
ret i64 %33
}

LLVM IR (-O2):
@.str = [6 x i8] c"write\00"
define i64 @xwrite(i32 %0, i8* nocapture %1, i64 %2) {
%4 = icmp eq i64 %2, 0
br i1 %4, label %19, label %5
5: ; preds = %3, %14
%6 = phi i64 [ %17, %14 ], [ 0, %3 ]
%7 = phi i8* [ %16, %14 ], [ %1, %3 ]
%8 = phi i64 [ %15, %14 ], [ %2, %3 ]
%9 = tail call i64 @write(i32 %0, i8* %7, i64 %8)
%10 = icmp slt i64 %9, 0
br i1 %10, label %11, label %14
11: ; preds = %5
%12 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%13 = tail call i32 @exit(i32 1)
unreachable
14: ; preds = %5
%15 = sub i64 %8, %9
%16 = getelementptr i8, i8* %7, i64 %9
%17 = add nsw i64 %9, %6
%18 = icmp eq i64 %15, 0
br i1 %18, label %19, label %5
19: ; preds = %14, %3
%20 = phi i64 [ 0, %3 ], [ %17, %14 ]
ret i64 %20
}