#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 */
int write(int, char *, size_t);
int write_n_bytes(int fd, char *buff, size_t size) {
  int i = 0;
  size_t wrote = 0;
  for (;;) {
    i = 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,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
add    -0x28(%rbp),%rsi
mov    -0x18(%rbp),%rdx
sub    -0x28(%rbp),%rdx
call   1030 <write@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jg     1162 <write_n_bytes+0x52>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    118d <write_n_bytes+0x7d>
movslq -0x1c(%rbp),%rax
add    -0x28(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x18(%rbp),%rax
jne    1188 <write_n_bytes+0x78>
mov    -0x28(%rbp),%rax
mov    %eax,-0x4(%rbp)
jmp    118d <write_n_bytes+0x7d>
jmp    1132 <write_n_bytes+0x22>
mov    -0x4(%rbp),%eax
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   %eax,%eax
jle    1152 <write_n_bytes+0x42>
mov    %eax,%eax
add    %rax,%rbp
cmp    %rbx,%rbp
jne    1130 <write_n_bytes+0x20>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @write_n_bytes(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
store i32 0, i32* %8
store i64 0, i64* %9
br label %10
10: ; preds = %34, %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 i32 @write(i32 %11, i8* %14, i64 %17)
store i32 %18, i32* %8
%19 = load i32, i32* %8
%20 = icmp sle i32 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %10
%22 = load i32, i32* %8
store i32 %22, i32* %4
br label %35
23: ; preds = %10
%24 = load i32, i32* %8
%25 = sext i32 %24 to i64
%26 = load i64, i64* %9
%27 = add i64 %26, %25
store i64 %27, i64* %9
%28 = load i64, i64* %9
%29 = load i64, i64* %7
%30 = icmp eq i64 %28, %29
br i1 %30, label %31, label %34
31: ; preds = %23
%32 = load i64, i64* %9
%33 = trunc i64 %32 to i32
store i32 %33, i32* %4
br label %35
34: ; preds = %23
br label %10
35: ; preds = %31, %21
%36 = load i32, i32* %4
ret i32 %36
}

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