#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, void *, int);
int write_exact(int fd, void *buf, int len) {
  int i, wrote = 0;
  do {
    if ((i = write(fd, buf + 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    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%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
jg     1159 <write_exact+0x49>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1174 <write_exact+0x64>
mov    -0x18(%rbp),%eax
add    -0x1c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x14(%rbp),%eax
jl     1129 <write_exact+0x19>
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   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%ebx
mov    %rsi,%r14
mov    %edi,%r15d
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
movslq %ebp,%rax
lea    (%r14,%rax,1),%rsi
mov    %ebx,%edx
sub    %eax,%edx
mov    %r15d,%edi
call   1030 <write@plt>
test   %eax,%eax
jle    114f <write_exact+0x3f>
add    %eax,%ebp
cmp    %ebx,%ebp
jl     1130 <write_exact+0x20>
jmp    1151 <write_exact+0x41>
mov    %eax,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @write_exact(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* %9
br label %10
10: ; preds = %27, %3
%11 = load i32, i32* %5
%12 = load i8*, i8** %6
%13 = load i32, i32* %9
%14 = sext i32 %13 to i64
%15 = getelementptr 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 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 %33
23: ; preds = %10
%24 = load i32, i32* %8
%25 = load i32, i32* %9
%26 = add nsw i32 %25, %24
store i32 %26, i32* %9
br label %27
27: ; preds = %23
%28 = load i32, i32* %9
%29 = load i32, i32* %7
%30 = icmp slt i32 %28, %29
br i1 %30, label %10, label %31
31: ; preds = %27
%32 = load i32, i32* %7
store i32 %32, i32* %4
br label %33
33: ; preds = %31, %21
%34 = load i32, i32* %4
ret i32 %34
}

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