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

/* Variables and functions */
size_t write(int, void *, size_t);
ssize_t _wp_writen(int fd, void *ptr, size_t n) {
  size_t nleft = n;
  size_t nwritten;
  while (nleft > 0) {
    if ((nwritten = write(fd, ptr, nleft)) < 0) {
      return nwritten;
    }
    nleft -= nwritten;
    ptr += nwritten;
  }
  return n;
}


x86 assembly (clang, -O0):
<_wp_writen>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0xc(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jbe    1181 <_wp_writen+0x71>
mov    -0xc(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   1030 <write@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
jae    1161 <_wp_writen+0x51>
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1189 <_wp_writen+0x79>
mov    -0x30(%rbp),%rcx
mov    -0x28(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x28(%rbp)
mov    -0x30(%rbp),%rax
add    -0x18(%rbp),%rax
mov    %rax,-0x18(%rbp)
jmp    112b <_wp_writen+0x1b>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_wp_writen>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdx,%r14
test   %rdx,%rdx
je     1146 <_wp_writen+0x36>
mov    %rsi,%rbx
mov    %edi,%r15d
mov    %r14,%rbp
nopl   0x0(%rax,%rax,1)
mov    %r15d,%edi
mov    %rbx,%rsi
mov    %rbp,%rdx
call   1030 <write@plt>
add    %rax,%rbx
sub    %rax,%rbp
jne    1130 <_wp_writen+0x20>
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i64 @_wp_writen(i32 %0, i8* %1, i64 %2) {
%4 = alloca i64
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i64
%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* %8
br label %11
11: ; preds = %22, %3
%12 = load i64, i64* %8
%13 = icmp ugt i64 %12, 0
br i1 %13, label %14, label %29
14: ; preds = %11
%15 = load i32, i32* %5
%16 = load i8*, i8** %6
%17 = load i64, i64* %8
%18 = call i64 @write(i32 %15, i8* %16, i64 %17)
store i64 %18, i64* %9
%19 = icmp ult i64 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %14
%21 = load i64, i64* %9
store i64 %21, i64* %4
br label %31
22: ; preds = %14
%23 = load i64, i64* %9
%24 = load i64, i64* %8
%25 = sub i64 %24, %23
store i64 %25, i64* %8
%26 = load i64, i64* %9
%27 = load i8*, i8** %6
%28 = getelementptr i8, i8* %27, i64 %26
store i8* %28, i8** %6
br label %11
29: ; preds = %11
%30 = load i64, i64* %7
store i64 %30, i64* %4
br label %31
31: ; preds = %29, %20
%32 = load i64, i64* %4
ret i64 %32
}

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