#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 w_write(int, char *, int);
int w_writen(int fd, char *buf, unsigned int count) {
  int left = count;
  int writen = 0;
  while (left > 0) {
    int n = w_write(fd, buf + writen, left);
    if (n <= 0) {
      break;
    }
    left -= n;
    writen += n;
  }
  return writen;
}


x86 assembly (clang, -O0):
<w_writen>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
cmpl   $0x0,-0x18(%rbp)
jle    117a <w_writen+0x6a>
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
movslq -0x1c(%rbp),%rax
add    %rax,%rsi
mov    -0x18(%rbp),%edx
call   1030 <w_write@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jg     1161 <w_writen+0x51>
jmp    117a <w_writen+0x6a>
mov    -0x20(%rbp),%ecx
mov    -0x18(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x18(%rbp)
mov    -0x20(%rbp),%eax
add    -0x1c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
jmp    112f <w_writen+0x1f>
mov    -0x1c(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<w_writen>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%ebx
mov    %rsi,%r14
mov    %edi,%r15d
xor    %ecx,%ecx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %ecx,%ebp
test   %ebx,%ebx
jle    114f <w_writen+0x3f>
movslq %ebp,%rsi
add    %r14,%rsi
mov    %r15d,%edi
mov    %ebx,%edx
call   1030 <w_write@plt>
sub    %eax,%ebx
lea    (%rax,%rbp,1),%ecx
test   %eax,%eax
jg     1130 <w_writen+0x20>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

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