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

/* Variables and functions */
scalar_t__ EAGAIN;
scalar_t__ EINTR;
int /*<<< orphan*/ STDOUT_FILENO;
scalar_t__ errno;
size_t write(int /*<<< orphan*/, unsigned char const *, size_t);
int restartable_write(const unsigned char *buf, size_t buflen) {
  ssize_t rc, wroteb;
  rc = 0;
WRITELOOP:
  while (rc < buflen) {
    wroteb = write(STDOUT_FILENO, buf + rc, buflen - rc);
    if (wroteb == -1) {
      if (errno == EAGAIN || errno == EINTR)
        goto WRITELOOP;
      return -1;
    }
    rc += wroteb;
  }
  return rc;
}


x86 assembly (clang, -O0):
<restartable_write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
jmp    112d <restartable_write+0x1d>
mov    -0x20(%rbp),%rax
cmp    -0x18(%rbp),%rax
jae    11be <restartable_write+0xae>
mov    0x2eae(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rsi
add    -0x20(%rbp),%rsi
mov    -0x18(%rbp),%rdx
sub    -0x20(%rbp),%rdx
call   1030 <write@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0xffffffffffffffff,-0x28(%rbp)
jne    11ad <restartable_write+0x9d>
mov    0x2e69(%rip),%rax
mov    (%rax),%rax
mov    0x2e47(%rip),%rcx
cmp    (%rcx),%rax
je     119c <restartable_write+0x8c>
mov    0x2e4f(%rip),%rax
mov    (%rax),%rax
mov    0x2e3d(%rip),%rcx
cmp    (%rcx),%rax
jne    11a1 <restartable_write+0x91>
jmp    1128 <restartable_write+0x18>
movl   $0xffffffff,-0x4(%rbp)
jmp    11c5 <restartable_write+0xb5>
mov    -0x28(%rbp),%rax
add    -0x20(%rbp),%rax
mov    %rax,-0x20(%rbp)
jmp    112d <restartable_write+0x1d>
mov    -0x20(%rbp),%rax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<restartable_write>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%r15
xor    %ebx,%ebx
mov    0x2ec6(%rip),%r12
mov    0x2ea7(%rip),%r13
mov    0x2e88(%rip),%rbp
nopl   0x0(%rax,%rax,1)
cmp    %r14,%rbx
jae    117e <restartable_write+0x6e>
mov    (%r12),%edi
lea    (%r15,%rbx,1),%rsi
mov    %r14,%rdx
sub    %rbx,%rdx
call   1030 <write@plt>
cmp    $0xffffffffffffffff,%rax
je     1163 <restartable_write+0x53>
add    %rax,%rbx
jmp    1140 <restartable_write+0x30>
mov    0x0(%r13),%rax
cmp    0x0(%rbp),%rax
je     1140 <restartable_write+0x30>
mov    0x2e5c(%rip),%rcx
cmp    (%rcx),%rax
je     1140 <restartable_write+0x30>
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):
@STDOUT_FILENO = global i32 0
@errno = global i64 0
@EAGAIN = global i64 0
@EINTR = global i64 0
define i32 @restartable_write(i8* %0, i64 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i64
%7 = alloca i64
store i8* %0, i8** %4
store i64 %1, i64* %5
store i64 0, i64* %6
br label %8
8: ; preds = %32, %2
br label %9
9: ; preds = %34, %8
%10 = load i64, i64* %6
%11 = load i64, i64* %5
%12 = icmp ult i64 %10, %11
br i1 %12, label %13, label %38
13: ; preds = %9
%14 = load i32, i32* @STDOUT_FILENO
%15 = load i8*, i8** %4
%16 = load i64, i64* %6
%17 = getelementptr inbounds i8, i8* %15, i64 %16
%18 = load i64, i64* %5
%19 = load i64, i64* %6
%20 = sub i64 %18, %19
%21 = call i64 @write(i32 %14, i8* %17, i64 %20)
store i64 %21, i64* %7
%22 = load i64, i64* %7
%23 = icmp eq i64 %22, -1
br i1 %23, label %24, label %34
24: ; preds = %13
%25 = load i64, i64* @errno
%26 = load i64, i64* @EAGAIN
%27 = icmp eq i64 %25, %26
br i1 %27, label %32, label %28
28: ; preds = %24
%29 = load i64, i64* @errno
%30 = load i64, i64* @EINTR
%31 = icmp eq i64 %29, %30
br i1 %31, label %32, label %33
32: ; preds = %28, %24
br label %8
33: ; preds = %28
store i32 -1, i32* %3
br label %41
34: ; preds = %13
%35 = load i64, i64* %7
%36 = load i64, i64* %6
%37 = add i64 %36, %35
store i64 %37, i64* %6
br label %9
38: ; preds = %9
%39 = load i64, i64* %6
%40 = trunc i64 %39 to i32
store i32 %40, i32* %3
br label %41
41: ; preds = %38, %33
%42 = load i32, i32* %3
ret i32 %42
}

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