#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 */
scalar_t__ EINTR;
scalar_t__ errno;
size_t write(int, void const *, size_t);
ssize_t nwrite(int fd, const void *buf, size_t count) {
  ssize_t ret = 0;
  ssize_t written = 0;
  for (written = 0; written != count; written += ret) {
    ret = write(fd, buf + written, count - written);
    if (ret < 0) {
      if (errno == EINTR)
        ret = 0;
      else
        goto out;
    }
  }
  ret = written;
out:
  return ret;
}


x86 assembly (clang, -O0):
<nwrite>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x18(%rbp),%rax
je     11b7 <nwrite+0xa7>
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
add    -0x28(%rbp),%rsi
mov    -0x18(%rbp),%rdx
sub    -0x28(%rbp),%rdx
call   1030 <write@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jae    11a1 <nwrite+0x91>
mov    0x2e69(%rip),%rax
mov    (%rax),%rax
mov    0x2e57(%rip),%rcx
cmp    (%rcx),%rax
jne    1197 <nwrite+0x87>
movq   $0x0,-0x20(%rbp)
jmp    119c <nwrite+0x8c>
jmp    11bf <nwrite+0xaf>
jmp    11a1 <nwrite+0x91>
jmp    11a6 <nwrite+0x96>
mov    -0x20(%rbp),%rax
add    -0x28(%rbp),%rax
mov    %rax,-0x28(%rbp)
jmp    113b <nwrite+0x2b>
mov    -0x28(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<nwrite>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdx,%rbx
test   %rdx,%rdx
je     114a <nwrite+0x3a>
mov    %rsi,%r14
mov    %edi,%r15d
xor    %ebp,%ebp
nopw   0x0(%rax,%rax,1)
lea    (%r14,%rbp,1),%rsi
mov    %rbx,%rdx
sub    %rbp,%rdx
mov    %r15d,%edi
call   1030 <write@plt>
add    %rax,%rbp
cmp    %rbx,%rbp
jne    1130 <nwrite+0x20>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

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