#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 *, int);
int writenbytes(int fd, char *str, int size) {
  int rc = 0;
  int totalwritten = 0;
  while ((rc = write(fd, str + totalwritten, size - totalwritten)) > 0)
    totalwritten += rc;
  if (rc < 0)
    return -1;
  else
    return totalwritten;
}


x86 assembly (clang, -O0):
<writenbytes>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
movslq -0x1c(%rbp),%rax
add    %rax,%rsi
mov    -0x14(%rbp),%edx
sub    -0x1c(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jle    1163 <writenbytes+0x53>
mov    -0x18(%rbp),%eax
add    -0x1c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
jmp    1130 <writenbytes+0x20>
cmpl   $0x0,-0x18(%rbp)
jge    1179 <writenbytes+0x69>
movl   $0xffffffff,-0x4(%rbp)
jmp    117f <writenbytes+0x6f>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<writenbytes>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %edx,%r14d
mov    %rsi,%r15
mov    %edi,%r12d
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %ebp,%ebx
movslq %ebp,%rbp
lea    (%r15,%rbp,1),%rsi
mov    %r14d,%edx
sub    %ebp,%edx
mov    %r12d,%edi
call   1030 <write@plt>
add    %eax,%ebp
test   %eax,%eax
jg     1130 <writenbytes+0x20>
neg    %eax
sbb    %eax,%eax
or     %ebx,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

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