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

/* Variables and functions */
scalar_t__ EINTR;
scalar_t__ errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ perror(char *);
int write(int, char *, size_t);
ssize_t rio_write(int fd, const void *buf, size_t count) {
  char *p = (char *)buf;
  ssize_t n = 0;
  int nwrite;
  while (count > 0) {
    while ((nwrite = write(fd, p, count)) == -1 && errno == EINTR)
      ;
    if (nwrite == -1 && errno != EINTR) {
      perror("write faild");
      exit(1);
    }
    count -= nwrite;
    p += nwrite;
    n += nwrite;
  }
  return n;
}


x86 assembly (clang, -O0):
<rio_write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
cmpq   $0x0,-0x18(%rbp)
jbe    121c <rio_write+0xec>
jmp    1162 <rio_write+0x32>
mov    -0x4(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1030 <write@plt>
mov    %eax,%ecx
mov    %ecx,-0x28(%rbp)
xor    %eax,%eax
cmp    $0xffffffff,%ecx
mov    %al,-0x29(%rbp)
jne    119f <rio_write+0x6f>
mov    0x2e54(%rip),%rax
mov    (%rax),%rax
mov    0x2e42(%rip),%rcx
cmp    (%rcx),%rax
sete   %al
mov    %al,-0x29(%rbp)
mov    -0x29(%rbp),%al
test   $0x1,%al
jne    11af <rio_write+0x7f>
jmp    11b4 <rio_write+0x84>
jmp    1162 <rio_write+0x32>
cmpl   $0xffffffff,-0x28(%rbp)
jne    11ee <rio_write+0xbe>
mov    0x2e1b(%rip),%rax
mov    (%rax),%rax
mov    0x2e09(%rip),%rcx
cmp    (%rcx),%rax
je     11ee <rio_write+0xbe>
lea    0xe21(%rip),%rdi
call   1040 <perror@plt>
mov    $0x1,%edi
call   1050 <exit@plt>
movslq -0x28(%rbp),%rcx
mov    -0x18(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x18(%rbp)
mov    -0x28(%rbp),%ecx
mov    -0x20(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x20(%rbp)
mov    -0x28(%rbp),%eax
add    -0x24(%rbp),%eax
mov    %eax,-0x24(%rbp)
jmp    1152 <rio_write+0x22>
mov    -0x24(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rio_write>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
test   %rdx,%rdx
je     11b0 <rio_write+0x80>
mov    %rdx,%rbx
mov    %rsi,%rbp
mov    %edi,%r15d
xor    %r14d,%r14d
mov    0x2e8d(%rip),%r12
mov    0x2e7e(%rip),%r13
jmp    1170 <rio_write+0x40>
movslq %eax,%rcx
add    %rcx,%rbp
add    %eax,%r14d
sub    %rcx,%rbx
je     11b3 <rio_write+0x83>
nopw   0x0(%rax,%rax,1)
mov    %r15d,%edi
mov    %rbp,%rsi
mov    %rbx,%rdx
call   1030 <write@plt>
mov    (%r12),%rcx
mov    0x0(%r13),%rdx
cmp    $0xffffffff,%eax
jne    1190 <rio_write+0x60>
cmp    %rdx,%rcx
je     1170 <rio_write+0x40>
cmp    $0xffffffff,%eax
jne    115c <rio_write+0x2c>
cmp    %rdx,%rcx
je     115c <rio_write+0x2c>
lea    0xe5f(%rip),%rdi
call   1040 <perror@plt>
mov    $0x1,%edi
call   1050 <exit@plt>
xor    %r14d,%r14d
mov    %r14d,%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
@.str = [12 x i8] c"write faild\00"
define i32 @rio_write(i32 %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
store i32 %0, i32* %4
store i8* %1, i8** %5
store i64 %2, i64* %6
%10 = load i8*, i8** %5
store i8* %10, i8** %7
store i32 0, i32* %8
br label %11
11: ; preds = %38, %3
%12 = load i64, i64* %6
%13 = icmp ugt i64 %12, 0
br i1 %13, label %14, label %50
14: ; preds = %11
br label %15
15: ; preds = %27, %14
%16 = load i32, i32* %4
%17 = load i8*, i8** %7
%18 = load i64, i64* %6
%19 = call i32 @write(i32 %16, i8* %17, i64 %18)
store i32 %19, i32* %9
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %25
21: ; preds = %15
%22 = load i64, i64* @errno
%23 = load i64, i64* @EINTR
%24 = icmp eq i64 %22, %23
br label %25
25: ; preds = %21, %15
%26 = phi i1 [ false, %15 ], [ %24, %21 ]
br i1 %26, label %27, label %28
27: ; preds = %25
br label %15
28: ; preds = %25
%29 = load i32, i32* %9
%30 = icmp eq i32 %29, -1
br i1 %30, label %31, label %38
31: ; preds = %28
%32 = load i64, i64* @errno
%33 = load i64, i64* @EINTR
%34 = icmp ne i64 %32, %33
br i1 %34, label %35, label %38
35: ; preds = %31
%36 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%37 = call i32 @exit(i32 1)
unreachable
38: ; preds = %31, %28
%39 = load i32, i32* %9
%40 = sext i32 %39 to i64
%41 = load i64, i64* %6
%42 = sub i64 %41, %40
store i64 %42, i64* %6
%43 = load i32, i32* %9
%44 = load i8*, i8** %7
%45 = sext i32 %43 to i64
%46 = getelementptr inbounds i8, i8* %44, i64 %45
store i8* %46, i8** %7
%47 = load i32, i32* %9
%48 = load i32, i32* %8
%49 = add nsw i32 %48, %47
store i32 %49, i32* %8
br label %11
50: ; preds = %11
%51 = load i32, i32* %8
ret i32 %51
}

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