#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 */
scalar_t__ EINTR;
scalar_t__ errno;
int write(int, char const *, unsigned int);
int cwrite(int fd, const char *buffer, unsigned int r) {
  unsigned int i;
  int p;
  for (i = 0; i < r;) {
    do {
      p = write(fd, buffer + i, r - i);
    } while (p == -1 && (errno == EINTR));
    if (p < 1)
      return 0;
    i += p;
  }
  return 1;
}


x86 assembly (clang, -O0):
<cwrite>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jae    11ad <cwrite+0x9d>
jmp    113a <cwrite+0x2a>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%eax
add    %rax,%rsi
mov    -0x14(%rbp),%edx
sub    -0x18(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x1c(%rbp)
xor    %eax,%eax
cmpl   $0xffffffff,-0x1c(%rbp)
mov    %al,-0x1d(%rbp)
jne    117e <cwrite+0x6e>
mov    0x2e75(%rip),%rax
mov    (%rax),%rax
mov    0x2e63(%rip),%rcx
cmp    (%rcx),%rax
sete   %al
mov    %al,-0x1d(%rbp)
mov    -0x1d(%rbp),%al
test   $0x1,%al
jne    113a <cwrite+0x2a>
cmpl   $0x1,-0x1c(%rbp)
jge    119f <cwrite+0x8f>
movl   $0x0,-0x4(%rbp)
jmp    11b4 <cwrite+0xa4>
mov    -0x1c(%rbp),%eax
add    -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
jmp    1129 <cwrite+0x19>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cwrite>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,0x10(%rsp)
mov    $0x1,%eax
test   %edx,%edx
je     1194 <cwrite+0x84>
mov    %edx,%ebx
mov    %edi,%r13d
xor    %r14d,%r14d
mov    0x2ea5(%rip),%r15
mov    0x2e96(%rip),%r12
mov    %edx,0xc(%rsp)
cs nopw 0x0(%rax,%rax,1)
mov    %r14d,%ebp
add    0x10(%rsp),%rbp
sub    %r14d,%ebx
nopl   0x0(%rax,%rax,1)
mov    %r13d,%edi
mov    %rbp,%rsi
mov    %ebx,%edx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
jne    117b <cwrite+0x6b>
mov    (%r12),%rcx
cmp    %rcx,(%r15)
je     1160 <cwrite+0x50>
test   %eax,%eax
jle    1192 <cwrite+0x82>
add    %eax,%r14d
mov    0xc(%rsp),%ebx
cmp    %ebx,%r14d
jb     1150 <cwrite+0x40>
mov    $0x1,%eax
jmp    1194 <cwrite+0x84>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@errno = global i64 0
@EINTR = global i64 0
define i32 @cwrite(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* %8
br label %10
10: ; preds = %38, %3
%11 = load i32, i32* %8
%12 = load i32, i32* %7
%13 = icmp ult i32 %11, %12
br i1 %13, label %14, label %42
14: ; preds = %10
br label %15
15: ; preds = %32, %14
%16 = load i32, i32* %5
%17 = load i8*, i8** %6
%18 = load i32, i32* %8
%19 = zext i32 %18 to i64
%20 = getelementptr inbounds i8, i8* %17, i64 %19
%21 = load i32, i32* %7
%22 = load i32, i32* %8
%23 = sub i32 %21, %22
%24 = call i32 @write(i32 %16, i8* %20, i32 %23)
store i32 %24, i32* %9
br label %25
25: ; preds = %15
%26 = load i32, i32* %9
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %32
28: ; preds = %25
%29 = load i64, i64* @errno
%30 = load i64, i64* @EINTR
%31 = icmp eq i64 %29, %30
br label %32
32: ; preds = %28, %25
%33 = phi i1 [ false, %25 ], [ %31, %28 ]
br i1 %33, label %15, label %34
34: ; preds = %32
%35 = load i32, i32* %9
%36 = icmp slt i32 %35, 1
br i1 %36, label %37, label %38
37: ; preds = %34
store i32 0, i32* %4
br label %43
38: ; preds = %34
%39 = load i32, i32* %9
%40 = load i32, i32* %8
%41 = add i32 %40, %39
store i32 %41, i32* %8
br label %10
42: ; preds = %10
store i32 1, i32* %4
br label %43
43: ; preds = %42, %37
%44 = load i32, i32* %4
ret i32 %44
}

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