#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 */
typedef size_t ssize_t;

/* Variables and functions */
int errno;
size_t write(int, char *, size_t);
int write_to_fd(int fd, char *buf, size_t count) {
  ssize_t pos = 0;
  ssize_t rv = 0;
  do {
    rv = write(fd, buf + pos, count - pos);
    if (rv < 0)
      return -errno;
    pos += rv;
  } while (count > pos);
  return count;
}


x86 assembly (clang, -O0):
<write_to_fd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
add    -0x20(%rbp),%rsi
mov    -0x18(%rbp),%rdx
sub    -0x20(%rbp),%rdx
call   1030 <write@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jae    116d <write_to_fd+0x5d>
xor    %eax,%eax
mov    0x2e7d(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    118e <write_to_fd+0x7e>
mov    -0x28(%rbp),%rax
add    -0x20(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rax
cmp    -0x20(%rbp),%rax
ja     1133 <write_to_fd+0x23>
mov    -0x18(%rbp),%rax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_to_fd>:
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>
add    %rax,%rbp
cmp    %rbx,%rbp
jb     1130 <write_to_fd+0x20>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

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