#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 write_exact(char *buffer, int len) {
  int i, wrote = 0;
  do {
    if ((i = write(1, buffer + wrote, len - wrote)) <= 0)
      return i;
    wrote += i;
  } while (wrote < len);
  return len;
}


x86 assembly (clang, -O0):
<write_exact>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x10(%rbp),%rsi
movslq -0x1c(%rbp),%rax
add    %rax,%rsi
mov    -0x14(%rbp),%edx
sub    -0x1c(%rbp),%edx
mov    $0x1,%edi
call   1030 <write@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jg     1158 <write_exact+0x48>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1173 <write_exact+0x63>
mov    -0x18(%rbp),%eax
add    -0x1c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x14(%rbp),%eax
jl     1126 <write_exact+0x16>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_exact>:
push   %rbp
push   %r14
push   %rbx
mov    %esi,%ebx
mov    %rdi,%r14
xor    %ebp,%ebp
nopl   0x0(%rax,%rax,1)
movslq %ebp,%rax
lea    (%r14,%rax,1),%rsi
mov    %ebx,%edx
sub    %eax,%edx
mov    $0x1,%edi
call   1030 <write@plt>
test   %eax,%eax
jle    1141 <write_exact+0x31>
add    %eax,%ebp
cmp    %ebx,%ebp
jl     1120 <write_exact+0x10>
jmp    1143 <write_exact+0x33>
mov    %eax,%ebx
mov    %ebx,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

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

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