#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__ EINTR;
scalar_t__ errno;
int write(int, char const *, int);
int write_uninterrupted(int sk, const char *buf, int len) {
  int c;
  while ((c = write(sk, buf, len)) < len) {
    if (c == -1) {
      if (errno == EINTR)
        continue;
      return -1;
    }
    buf += c;
    len -= c;
  }
  return 0;
}


x86 assembly (clang, -O0):
<write_uninterrupted>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x18(%rbp)
cmp    -0x14(%rbp),%eax
jge    1193 <write_uninterrupted+0x83>
cmpl   $0xffffffff,-0x18(%rbp)
jne    1172 <write_uninterrupted+0x62>
mov    0x2e92(%rip),%rax
mov    (%rax),%rax
mov    0x2e80(%rip),%rcx
cmp    (%rcx),%rax
jne    1166 <write_uninterrupted+0x56>
jmp    1122 <write_uninterrupted+0x12>
movl   $0xffffffff,-0x4(%rbp)
jmp    119a <write_uninterrupted+0x8a>
mov    -0x18(%rbp),%ecx
mov    -0x10(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x10(%rbp)
mov    -0x18(%rbp),%ecx
mov    -0x14(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x14(%rbp)
jmp    1122 <write_uninterrupted+0x12>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_uninterrupted>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %edx,%ebp
mov    %rsi,%rbx
mov    %edi,%r14d
mov    0x2eb9(%rip),%r15
mov    0x2eaa(%rip),%r12
xchg   %ax,%ax
mov    %r14d,%edi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1030 <write@plt>
mov    %ebp,%ecx
sub    %eax,%ecx
jle    115c <write_uninterrupted+0x4c>
cmp    $0xffffffff,%eax
jne    1153 <write_uninterrupted+0x43>
mov    (%r15),%rax
cmp    (%r12),%rax
je     1130 <write_uninterrupted+0x20>
jmp    1160 <write_uninterrupted+0x50>
cltq
add    %rax,%rbx
mov    %ecx,%ebp
jmp    1130 <write_uninterrupted+0x20>
xor    %eax,%eax
jmp    1165 <write_uninterrupted+0x55>
mov    $0xffffffff,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@errno = global i64 0
@EINTR = global i64 0
define i32 @write_uninterrupted(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i32 %2, i32* %7
br label %9
9: ; preds = %25, %23, %3
%10 = load i32, i32* %5
%11 = load i8*, i8** %6
%12 = load i32, i32* %7
%13 = call i32 @write(i32 %10, i8* %11, i32 %12)
store i32 %13, i32* %8
%14 = load i32, i32* %7
%15 = icmp slt i32 %13, %14
br i1 %15, label %16, label %33
16: ; preds = %9
%17 = load i32, i32* %8
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %25
19: ; preds = %16
%20 = load i64, i64* @errno
%21 = load i64, i64* @EINTR
%22 = icmp eq i64 %20, %21
br i1 %22, label %23, label %24
23: ; preds = %19
br label %9
24: ; preds = %19
store i32 -1, i32* %4
br label %34
25: ; preds = %16
%26 = load i32, i32* %8
%27 = load i8*, i8** %6
%28 = sext i32 %26 to i64
%29 = getelementptr inbounds i8, i8* %27, i64 %28
store i8* %29, i8** %6
%30 = load i32, i32* %8
%31 = load i32, i32* %7
%32 = sub nsw i32 %31, %30
store i32 %32, i32* %7
br label %9
33: ; preds = %9
store i32 0, i32* %4
br label %34
34: ; preds = %33, %24
%35 = load i32, i32* %4
ret i32 %35
}

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