#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__ EAGAIN;
scalar_t__ EINTR;
scalar_t__ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ stderr;
int write(int, char *, size_t);
int cwrite(int fd, char *buf, size_t size) {
  int count = 0;
  do {
    int s = write(fd, buf + count, size - count);
    if (s < 0) {
      if (errno == EINTR)
        continue;
      if (errno == EAGAIN)
        break;
      fprintf(stderr, "write error occur\n");
      return -1;
    }
    count += s;
  } while (count < size);
  return count;
}


x86 assembly (clang, -O0):
<cwrite>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
movslq -0x1c(%rbp),%rax
add    %rax,%rsi
mov    -0x18(%rbp),%rdx
movslq -0x1c(%rbp),%rax
sub    %rax,%rdx
call   1030 <write@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    11c4 <cwrite+0xa4>
mov    0x2e6c(%rip),%rax
mov    (%rax),%rax
mov    0x2e5a(%rip),%rcx
cmp    (%rcx),%rax
jne    1184 <cwrite+0x64>
jmp    11cd <cwrite+0xad>
mov    0x2e4d(%rip),%rax
mov    (%rax),%rax
mov    0x2e2b(%rip),%rcx
cmp    (%rcx),%rax
jne    11a3 <cwrite+0x83>
jmp    11db <cwrite+0xbb>
mov    0x2e36(%rip),%rax
mov    (%rax),%edi
lea    0xe4d(%rip),%rsi
call   1040 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11e1 <cwrite+0xc1>
mov    -0x20(%rbp),%eax
add    -0x1c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
movslq -0x1c(%rbp),%rax
cmp    -0x18(%rbp),%rax
jb     113a <cwrite+0x1a>
mov    -0x1c(%rbp),%eax
mov    %eax,-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
push   %rax
mov    %rdx,%r14
mov    %rsi,%r15
mov    %edi,%ebp
xor    %ebx,%ebx
mov    0x2e94(%rip),%r13
jmp    114a <cwrite+0x2a>
xchg   %ax,%ax
add    %eax,%ebx
movslq %ebx,%r12
cmp    %r14,%r12
jae    1198 <cwrite+0x78>
movslq %ebx,%r12
lea    (%r15,%r12,1),%rsi
mov    %r14,%rdx
sub    %r12,%rdx
mov    %ebp,%edi
call   1030 <write@plt>
test   %eax,%eax
jns    1140 <cwrite+0x20>
mov    0x2e6f(%rip),%rax
mov    (%rax),%rax
cmp    0x0(%r13),%rax
je     1145 <cwrite+0x25>
mov    0x2e47(%rip),%rcx
cmp    (%rcx),%rax
je     1198 <cwrite+0x78>
mov    0x2e5b(%rip),%rax
mov    (%rax),%edi
lea    0xe72(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x8,%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
@EAGAIN = global i64 0
@stderr = global i32 0
@.str = [19 x i8] c"write error occur\0A\00"
define i32 @cwrite(i32 %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32
%9 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i64 %2, i64* %7
store i32 0, i32* %8
br label %10
10: ; preds = %40, %3
%11 = load i32, i32* %5
%12 = load i8*, i8** %6
%13 = load i32, i32* %8
%14 = sext i32 %13 to i64
%15 = getelementptr inbounds i8, i8* %12, i64 %14
%16 = load i64, i64* %7
%17 = load i32, i32* %8
%18 = sext i32 %17 to i64
%19 = sub i64 %16, %18
%20 = call i32 @write(i32 %11, i8* %15, i64 %19)
store i32 %20, i32* %9
%21 = load i32, i32* %9
%22 = icmp slt i32 %21, 0
br i1 %22, label %23, label %36
23: ; preds = %10
%24 = load i64, i64* @errno
%25 = load i64, i64* @EINTR
%26 = icmp eq i64 %24, %25
br i1 %26, label %27, label %28
27: ; preds = %23
br label %40
28: ; preds = %23
%29 = load i64, i64* @errno
%30 = load i64, i64* @EAGAIN
%31 = icmp eq i64 %29, %30
br i1 %31, label %32, label %33
32: ; preds = %28
br label %45
33: ; preds = %28
%34 = load i32, i32* @stderr
%35 = call i32 @fprintf(i32 %34, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %4
br label %47
36: ; preds = %10
%37 = load i32, i32* %9
%38 = load i32, i32* %8
%39 = add nsw i32 %38, %37
store i32 %39, i32* %8
br label %40
40: ; preds = %36, %27
%41 = load i32, i32* %8
%42 = sext i32 %41 to i64
%43 = load i64, i64* %7
%44 = icmp ult i64 %42, %43
br i1 %44, label %10, label %45
45: ; preds = %40, %32
%46 = load i32, i32* %8
store i32 %46, i32* %4
br label %47
47: ; preds = %45, %33
%48 = load i32, i32* %4
ret i32 %48
}

LLVM IR (-O2):
@errno = global i64 0
@EINTR = global i64 0
@EAGAIN = global i64 0
@stderr = global i32 0
@.str = [19 x i8] c"write error occur\0A\00"
define i32 @cwrite(i32 %0, i8* nocapture %1, i64 %2) {
br label %4
4: ; preds = %24, %3
%5 = phi i32 [ 0, %3 ], [ %26, %24 ]
%6 = sext i32 %5 to i64
%7 = getelementptr inbounds i8, i8* %1, i64 %6
%8 = sub i64 %2, %6
%9 = tail call i32 @write(i32 %0, i8* %7, i64 %8)
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %21
11: ; preds = %4
%12 = load i64, i64* @errno
%13 = load i64, i64* @EINTR
%14 = icmp eq i64 %12, %13
br i1 %14, label %24, label %15
15: ; preds = %11
%16 = load i64, i64* @EAGAIN
%17 = icmp eq i64 %12, %16
br i1 %17, label %28, label %18
18: ; preds = %15
%19 = load i32, i32* @stderr
%20 = tail call i32 @fprintf(i32 %19, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
br label %28
21: ; preds = %4
%22 = add nsw i32 %9, %5
%23 = sext i32 %22 to i64
br label %24
24: ; preds = %21, %11
%25 = phi i64 [ %23, %21 ], [ %6, %11 ]
%26 = phi i32 [ %22, %21 ], [ %5, %11 ]
%27 = icmp ult i64 %25, %2
br i1 %27, label %4, label %28
28: ; preds = %24, %15, %18
%29 = phi i32 [ -1, %18 ], [ %5, %15 ], [ %26, %24 ]
ret i32 %29
}