#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*/ perror(char *);
int write(int, void const *, size_t);
ssize_t fio_write(int fd, const void *buf, size_t count) {
  ssize_t ret = 0;
  ssize_t nr = 0;
  while (count != 0 && (nr = write(fd, buf, count)) != 0) {
    if (nr == -1) {
      if (errno == EINTR) {
        continue;
      }
      perror("write");
      break;
    }
    count -= nr;
    buf += nr;
    ret += nr;
  }
}


x86 assembly (clang, -O0):
<fio_write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
xor    %eax,%eax
cmpq   $0x0,-0x18(%rbp)
mov    %al,-0x21(%rbp)
je     116d <fio_write+0x4d>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1030 <write@plt>
mov    %eax,-0x20(%rbp)
cmp    $0x0,%eax
setne  %al
mov    %al,-0x21(%rbp)
mov    -0x21(%rbp),%al
test   $0x1,%al
jne    117d <fio_write+0x5d>
jmp    11e5 <fio_write+0xc5>
cmpl   $0xffffffff,-0x20(%rbp)
jne    11b7 <fio_write+0x97>
mov    0x2e52(%rip),%rax
mov    (%rax),%rax
mov    0x2e40(%rip),%rcx
cmp    (%rcx),%rax
jne    11a6 <fio_write+0x86>
jmp    1141 <fio_write+0x21>
lea    0xe53(%rip),%rdi
call   1040 <perror@plt>
jmp    11e5 <fio_write+0xc5>
movslq -0x20(%rbp),%rcx
mov    -0x18(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x18(%rbp)
mov    -0x20(%rbp),%ecx
mov    -0x10(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x10(%rbp)
mov    -0x20(%rbp),%eax
add    -0x1c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
jmp    1141 <fio_write+0x21>
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fio_write>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
test   %rdx,%rdx
je     1187 <fio_write+0x67>
mov    %rdx,%rbx
mov    %rsi,%r14
mov    %edi,%ebp
mov    0x2ea4(%rip),%r15
mov    0x2e95(%rip),%r12
jmp    1159 <fio_write+0x39>
cs nopw 0x0(%rax,%rax,1)
nop
mov    (%r15),%rax
cmp    (%r12),%rax
jne    117b <fio_write+0x5b>
mov    %ebp,%edi
mov    %r14,%rsi
mov    %rbx,%rdx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
je     1150 <fio_write+0x30>
test   %eax,%eax
je     1187 <fio_write+0x67>
cltq
add    %rax,%r14
sub    %rax,%rbx
jne    1159 <fio_write+0x39>
jmp    1187 <fio_write+0x67>
lea    0xe7e(%rip),%rdi
call   1040 <perror@plt>
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@errno = global i64 0
@EINTR = global i64 0
@.str = [6 x i8] c"write\00"
define i32 @fio_write(i32 %0, i8* nocapture %1, i64 %2) {
%4 = icmp eq i64 %2, 0
br i1 %4, label %21, label %5
5: ; preds = %3, %16
%6 = phi i64 [ %18, %16 ], [ %2, %3 ]
%7 = phi i8* [ %19, %16 ], [ %1, %3 ]
br label %8
8: ; preds = %5, %10
%9 = tail call i32 @write(i32 %0, i8* %7, i64 %6)
switch i32 %9, label %16 [
i32 0, label %21
i32 -1, label %10
]
10: ; preds = %8
%11 = load i64, i64* @errno
%12 = load i64, i64* @EINTR
%13 = icmp eq i64 %11, %12
br i1 %13, label %8, label %14
14: ; preds = %10
%15 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
br label %21
16: ; preds = %8
%17 = sext i32 %9 to i64
%18 = sub i64 %6, %17
%19 = getelementptr i8, i8* %7, i64 %17
%20 = icmp eq i64 %18, 0
br i1 %20, label %21, label %5
21: ; preds = %16, %8, %3, %14
ret i32 undef
}