#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 */
#define EAGAIN 129
#define EINTR 128
int errno;
int write(int, char *, int);
void blockingWrite(int fd, char *string, int len) {
  int ret;
  while (len) {
    ret = write(fd, string, len);
    if (ret == 0)
      return;
    if (ret < 0) {
      switch (errno) {
      case EAGAIN:
      case EINTR:
        continue;
      default:
        return;
      }
    }
    len -= ret;
    string += ret;
  }
}


x86 assembly (clang, -O0):
<blockingWrite>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     119c <blockingWrite+0x8c>
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jne    114d <blockingWrite+0x3d>
jmp    119c <blockingWrite+0x8c>
cmpl   $0x0,-0x18(%rbp)
jge    117b <blockingWrite+0x6b>
mov    0x2e82(%rip),%rax
mov    (%rax),%eax
add    $0xffffff80,%eax
sub    $0x1,%eax
ja     1176 <blockingWrite+0x66>
jmp    1171 <blockingWrite+0x61>
jmp    1122 <blockingWrite+0x12>
jmp    119c <blockingWrite+0x8c>
mov    -0x18(%rbp),%ecx
mov    -0x14(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x14(%rbp)
mov    -0x18(%rbp),%ecx
mov    -0x10(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x10(%rbp)
jmp    1122 <blockingWrite+0x12>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<blockingWrite>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
test   %edx,%edx
je     115a <blockingWrite+0x4a>
mov    %edx,%ebx
mov    %rsi,%r14
mov    %edi,%ebp
mov    0x2eb7(%rip),%r15
nopl   0x0(%rax)
mov    %ebp,%edi
mov    %r14,%rsi
mov    %ebx,%edx
call   1030 <write@plt>
test   %eax,%eax
je     115a <blockingWrite+0x4a>
jns    1151 <blockingWrite+0x41>
mov    (%r15),%eax
and    $0xfffffffe,%eax
cmp    $0x80,%eax
je     1130 <blockingWrite+0x20>
jmp    115a <blockingWrite+0x4a>
mov    %eax,%ecx
add    %rcx,%r14
sub    %eax,%ebx
jne    1130 <blockingWrite+0x20>
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

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