#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 */
size_t write(int, char *, size_t);
size_t _write_bytes(int fd, char *buf, const size_t size) {
  size_t bytes_remaining, bytes_written;
  char *ptr;
  bytes_remaining = size;
  ptr = buf;
  while (bytes_remaining > 0) {
    bytes_written = write(fd, ptr, bytes_remaining);
    if (bytes_written < 0)
      return 0;
    bytes_remaining -= bytes_written;
    ptr += bytes_written;
  }
  return size;
}


x86 assembly (clang, -O0):
<_write_bytes>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0xc(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x28(%rbp)
jbe    118a <_write_bytes+0x7a>
mov    -0xc(%rbp),%edi
mov    -0x38(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   1030 <write@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jae    116a <_write_bytes+0x5a>
movq   $0x0,-0x8(%rbp)
jmp    1192 <_write_bytes+0x82>
mov    -0x30(%rbp),%rcx
mov    -0x28(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x28(%rbp)
mov    -0x30(%rbp),%rax
add    -0x38(%rbp),%rax
mov    %rax,-0x38(%rbp)
jmp    1133 <_write_bytes+0x23>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_write_bytes>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdx,%r14
test   %rdx,%rdx
je     1146 <_write_bytes+0x36>
mov    %rsi,%rbx
mov    %edi,%r15d
mov    %r14,%rbp
nopl   0x0(%rax,%rax,1)
mov    %r15d,%edi
mov    %rbx,%rsi
mov    %rbp,%rdx
call   1030 <write@plt>
add    %rax,%rbx
sub    %rax,%rbp
jne    1130 <_write_bytes+0x20>
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

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