#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 write(int, char const *, size_t);
ssize_t xwrite(int fd, const char *s, size_t nmemb) {
  ssize_t r;
  size_t n;
  n = nmemb;
  do {
    r = write(fd, s, n);
    if (r == -1)
      return r;
    n -= r;
    s += r;
  } while (n);
  return nmemb;
}


x86 assembly (clang, -O0):
<xwrite>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   1030 <write@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    1153 <xwrite+0x43>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1185 <xwrite+0x75>
movslq -0x1c(%rbp),%rcx
mov    -0x28(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x28(%rbp)
mov    -0x1c(%rbp),%ecx
mov    -0x10(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    112b <xwrite+0x1b>
mov    -0x18(%rbp),%rax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<xwrite>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rsi,%rbx
mov    %edi,%r15d
mov    %rdx,%rbp
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %r15d,%edi
mov    %rbx,%rsi
mov    %rbp,%rdx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
je     114f <xwrite+0x3f>
cltq
add    %rax,%rbx
sub    %rax,%rbp
jne    1130 <xwrite+0x20>
jmp    1155 <xwrite+0x45>
mov    $0xffffffff,%r14d
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @xwrite(i32 %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32
%9 = alloca i64
store i32 %0, i32* %5
store i8* %1, i8** %6
store i64 %2, i64* %7
%10 = load i64, i64* %7
store i64 %10, i64* %9
br label %11
11: ; preds = %29, %3
%12 = load i32, i32* %5
%13 = load i8*, i8** %6
%14 = load i64, i64* %9
%15 = call i32 @write(i32 %12, i8* %13, i64 %14)
store i32 %15, i32* %8
%16 = load i32, i32* %8
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %20
18: ; preds = %11
%19 = load i32, i32* %8
store i32 %19, i32* %4
br label %35
20: ; preds = %11
%21 = load i32, i32* %8
%22 = sext i32 %21 to i64
%23 = load i64, i64* %9
%24 = sub i64 %23, %22
store i64 %24, i64* %9
%25 = load i32, i32* %8
%26 = load i8*, i8** %6
%27 = sext i32 %25 to i64
%28 = getelementptr inbounds i8, i8* %26, i64 %27
store i8* %28, i8** %6
br label %29
29: ; preds = %20
%30 = load i64, i64* %9
%31 = icmp ne i64 %30, 0
br i1 %31, label %11, label %32
32: ; preds = %29
%33 = load i64, i64* %7
%34 = trunc i64 %33 to i32
store i32 %34, i32* %4
br label %35
35: ; preds = %32, %18
%36 = load i32, i32* %4
ret i32 %36
}

LLVM IR (-O2):
define i32 @xwrite(i32 %0, i8* nocapture %1, i64 %2) {
br label %4
4: ; preds = %9, %3
%5 = phi i8* [ %1, %3 ], [ %12, %9 ]
%6 = phi i64 [ %2, %3 ], [ %11, %9 ]
%7 = tail call i32 @write(i32 %0, i8* %5, i64 %6)
%8 = icmp eq i32 %7, -1
br i1 %8, label %16, label %9
9: ; preds = %4
%10 = sext i32 %7 to i64
%11 = sub i64 %6, %10
%12 = getelementptr inbounds i8, i8* %5, i64 %10
%13 = icmp eq i64 %11, 0
br i1 %13, label %14, label %4
14: ; preds = %9
%15 = trunc i64 %2 to i32
br label %16
16: ; preds = %4, %14
%17 = phi i32 [ %15, %14 ], [ -1, %4 ]
ret i32 %17
}