#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 scalar_t__ ssize_t;

/* Variables and functions */
scalar_t__ EINTR;
size_t SSIZE_MAX;
scalar_t__ errno;
scalar_t__ write(int, char const *, size_t);
int write_data(int fd, const void *buf, size_t nbytes) {
  size_t towrite, written = 0;
  ssize_t result;
  do {
    if (nbytes - written > SSIZE_MAX)
      towrite = SSIZE_MAX;
    else
      towrite = nbytes - written;
    if ((result = write(fd, (const char *)buf + written, towrite)) >= 0)
      written += result;
    else if (errno != EINTR)
      return 0;
  } while (written < nbytes);
  return 1;
}


x86 assembly (clang, -O0):
<write_data>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x18(%rbp),%rax
sub    -0x28(%rbp),%rax
mov    0x2eae(%rip),%rcx
cmp    (%rcx),%rax
jbe    1156 <write_data+0x46>
mov    0x2e9e(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x20(%rbp)
jmp    1162 <write_data+0x52>
mov    -0x18(%rbp),%rax
sub    -0x28(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
add    -0x28(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1030 <write@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
jl     1195 <write_data+0x85>
mov    -0x30(%rbp),%rax
add    -0x28(%rbp),%rax
mov    %rax,-0x28(%rbp)
jmp    11c0 <write_data+0xb0>
mov    0x2e3c(%rip),%rax
mov    (%rax),%rax
mov    0x2e2a(%rip),%rcx
cmp    (%rcx),%rax
je     11bb <write_data+0xab>
movl   $0x0,-0x4(%rbp)
jmp    11da <write_data+0xca>
jmp    11c0 <write_data+0xb0>
jmp    11c5 <write_data+0xb5>
mov    -0x28(%rbp),%rax
cmp    -0x18(%rbp),%rax
jb     112b <write_data+0x1b>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_data>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%rbx
mov    %rsi,%r14
mov    %edi,%r15d
xor    %ebp,%ebp
mov    0x2ebb(%rip),%r12
mov    0x2e9c(%rip),%r13
jmp    1148 <write_data+0x38>
cs nopw 0x0(%rax,%rax,1)
add    %rax,%rbp
cmp    %rbx,%rbp
jae    117e <write_data+0x6e>
mov    %rbx,%rdx
sub    %rbp,%rdx
mov    (%r12),%rax
cmp    %rax,%rdx
cmova  %rax,%rdx
lea    (%r14,%rbp,1),%rsi
mov    %r15d,%edi
call   1030 <write@plt>
test   %rax,%rax
jns    1140 <write_data+0x30>
mov    0x2e67(%rip),%rax
mov    (%rax),%rax
cmp    0x0(%r13),%rax
je     1143 <write_data+0x33>
xor    %eax,%eax
jmp    1183 <write_data+0x73>
mov    $0x1,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@SSIZE_MAX = global i64 0
@errno = global i64 0
@EINTR = global i64 0
define i32 @write_data(i32 %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i64
%9 = alloca i64
%10 = alloca i64
store i32 %0, i32* %5
store i8* %1, i8** %6
store i64 %2, i64* %7
store i64 0, i64* %9
br label %11
11: ; preds = %42, %3
%12 = load i64, i64* %7
%13 = load i64, i64* %9
%14 = sub i64 %12, %13
%15 = load i64, i64* @SSIZE_MAX
%16 = icmp ugt i64 %14, %15
br i1 %16, label %17, label %19
17: ; preds = %11
%18 = load i64, i64* @SSIZE_MAX
store i64 %18, i64* %8
br label %23
19: ; preds = %11
%20 = load i64, i64* %7
%21 = load i64, i64* %9
%22 = sub i64 %20, %21
store i64 %22, i64* %8
br label %23
23: ; preds = %19, %17
%24 = load i32, i32* %5
%25 = load i8*, i8** %6
%26 = load i64, i64* %9
%27 = getelementptr inbounds i8, i8* %25, i64 %26
%28 = load i64, i64* %8
%29 = call i64 @write(i32 %24, i8* %27, i64 %28)
store i64 %29, i64* %10
%30 = icmp sge i64 %29, 0
br i1 %30, label %31, label %35
31: ; preds = %23
%32 = load i64, i64* %10
%33 = load i64, i64* %9
%34 = add i64 %33, %32
store i64 %34, i64* %9
br label %41
35: ; preds = %23
%36 = load i64, i64* @errno
%37 = load i64, i64* @EINTR
%38 = icmp ne i64 %36, %37
br i1 %38, label %39, label %40
39: ; preds = %35
store i32 0, i32* %4
br label %47
40: ; preds = %35
br label %41
41: ; preds = %40, %31
br label %42
42: ; preds = %41
%43 = load i64, i64* %9
%44 = load i64, i64* %7
%45 = icmp ult i64 %43, %44
br i1 %45, label %11, label %46
46: ; preds = %42
store i32 1, i32* %4
br label %47
47: ; preds = %46, %39
%48 = load i32, i32* %4
ret i32 %48
}

LLVM IR (-O2):
@SSIZE_MAX = global i64 0
@errno = global i64 0
@EINTR = global i64 0
define i32 @write_data(i32 %0, i8* nocapture %1, i64 %2) {
br label %4
4: ; preds = %19, %3
%5 = phi i64 [ 0, %3 ], [ %20, %19 ]
%6 = sub i64 %2, %5
%7 = load i64, i64* @SSIZE_MAX
%8 = icmp ugt i64 %6, %7
%9 = select i1 %8, i64 %7, i64 %6
%10 = getelementptr inbounds i8, i8* %1, i64 %5
%11 = tail call i64 @write(i32 %0, i8* %10, i64 %9)
%12 = icmp sgt i64 %11, -1
br i1 %12, label %13, label %15
13: ; preds = %4
%14 = add i64 %11, %5
br label %19
15: ; preds = %4
%16 = load i64, i64* @errno
%17 = load i64, i64* @EINTR
%18 = icmp eq i64 %16, %17
br i1 %18, label %19, label %22
19: ; preds = %13, %15
%20 = phi i64 [ %14, %13 ], [ %5, %15 ]
%21 = icmp ult i64 %20, %2
br i1 %21, label %4, label %22
22: ; preds = %19, %15
%23 = phi i32 [ 0, %15 ], [ 1, %19 ]
ret i32 %23
}