#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 */

/* Variables and functions */
scalar_t__ write(int, char *, size_t);
size_t write_n_bytes(int fd, char *buff, size_t size) {
  size_t i = 0;
  size_t wrote = 0;
  for (;;) {
    i = (size_t)write(fd, &buff[wrote], size - wrote);
    if (i <= 0)
      return i;
    wrote += i;
    if (wrote == size)
      return wrote;
  }
}


x86 assembly (clang, -O0):
<write_n_bytes>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0xc(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
mov    -0xc(%rbp),%edi
mov    -0x18(%rbp),%rsi
add    -0x30(%rbp),%rsi
mov    -0x20(%rbp),%rdx
sub    -0x30(%rbp),%rdx
call   1030 <write@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
ja     1167 <write_n_bytes+0x57>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1193 <write_n_bytes+0x83>
mov    -0x28(%rbp),%rax
add    -0x30(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
cmp    -0x20(%rbp),%rax
jne    118e <write_n_bytes+0x7e>
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1193 <write_n_bytes+0x83>
jmp    1133 <write_n_bytes+0x23>
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_n_bytes>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdx,%rbx
mov    %rsi,%r14
mov    %edi,%r15d
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
lea    (%r14,%rbp,1),%rsi
mov    %rbx,%rdx
sub    %rbp,%rdx
mov    %r15d,%edi
call   1030 <write@plt>
test   %rax,%rax
je     1151 <write_n_bytes+0x41>
add    %rax,%rbp
cmp    %rbx,%rbp
jne    1130 <write_n_bytes+0x20>
jmp    1153 <write_n_bytes+0x43>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

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