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

/* Variables and functions */
scalar_t__ EINTR;
scalar_t__ errno;
scalar_t__ write(int, void const *, size_t);
size_t blocking_write(int fd, const void *buf, size_t count) {
  size_t left = count;
  ssize_t r;
  while (left > 0 && ((r = write(fd, buf, left)) >= 0 || errno == EINTR)) {
    buf += r;
    left -= r;
  }
  return count - left;
}


x86 assembly (clang, -O0):
<blocking_write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
xor    %eax,%eax
cmpq   $0x0,-0x20(%rbp)
mov    %al,-0x29(%rbp)
jbe    1181 <blocking_write+0x71>
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1030 <write@plt>
mov    %rax,%rcx
mov    %rcx,-0x28(%rbp)
mov    $0x1,%al
cmp    $0x0,%rcx
mov    %al,-0x2a(%rbp)
jge    117b <blocking_write+0x6b>
mov    0x2e78(%rip),%rax
mov    (%rax),%rax
mov    0x2e66(%rip),%rcx
cmp    (%rcx),%rax
sete   %al
mov    %al,-0x2a(%rbp)
mov    -0x2a(%rbp),%al
mov    %al,-0x29(%rbp)
mov    -0x29(%rbp),%al
test   $0x1,%al
jne    1191 <blocking_write+0x81>
jmp    11b1 <blocking_write+0xa1>
mov    -0x28(%rbp),%rax
add    -0x10(%rbp),%rax
mov    %rax,-0x10(%rbp)
mov    -0x28(%rbp),%rcx
mov    -0x20(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x20(%rbp)
jmp    112b <blocking_write+0x1b>
mov    -0x18(%rbp),%rax
sub    -0x20(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<blocking_write>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r14
test   %rdx,%rdx
je     1166 <blocking_write+0x56>
mov    %rsi,%rbx
mov    %edi,%ebp
mov    0x2eb1(%rip),%r12
mov    0x2ea2(%rip),%r13
mov    %r14,%r15
jmp    1148 <blocking_write+0x38>
nopl   0x0(%rax,%rax,1)
add    %rax,%rbx
sub    %rax,%r15
je     1166 <blocking_write+0x56>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %r15,%rdx
call   1030 <write@plt>
test   %rax,%rax
jns    1140 <blocking_write+0x30>
mov    0x0(%r13),%rcx
cmp    %rcx,(%r12)
je     1140 <blocking_write+0x30>
jmp    1169 <blocking_write+0x59>
xor    %r15d,%r15d
sub    %r15,%r14
mov    %r14,%rax
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
define i64 @blocking_write(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
%9 = load i64, i64* %6
store i64 %9, i64* %7
br label %10
10: ; preds = %27, %3
%11 = load i64, i64* %7
%12 = icmp ugt i64 %11, 0
br i1 %12, label %13, label %25
13: ; preds = %10
%14 = load i32, i32* %4
%15 = load i8*, i8** %5
%16 = load i64, i64* %7
%17 = call i64 @write(i32 %14, i8* %15, i64 %16)
store i64 %17, i64* %8
%18 = icmp sge i64 %17, 0
br i1 %18, label %23, label %19
19: ; preds = %13
%20 = load i64, i64* @errno
%21 = load i64, i64* @EINTR
%22 = icmp eq i64 %20, %21
br label %23
23: ; preds = %19, %13
%24 = phi i1 [ true, %13 ], [ %22, %19 ]
br label %25
25: ; preds = %23, %10
%26 = phi i1 [ false, %10 ], [ %24, %23 ]
br i1 %26, label %27, label %34
27: ; preds = %25
%28 = load i64, i64* %8
%29 = load i8*, i8** %5
%30 = getelementptr i8, i8* %29, i64 %28
store i8* %30, i8** %5
%31 = load i64, i64* %8
%32 = load i64, i64* %7
%33 = sub i64 %32, %31
store i64 %33, i64* %7
br label %10
34: ; preds = %25
%35 = load i64, i64* %6
%36 = load i64, i64* %7
%37 = sub i64 %35, %36
ret i64 %37
}

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