#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;
typedef int /*<<< orphan*/ fd_set;

/* Variables and functions */
scalar_t__ EAGAIN;
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
scalar_t__ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
int write(int, void const *, size_t);
ssize_t write_blocking(int fd, const void *data, size_t size) {
  ssize_t n = 0;
  while ((n = write(fd, data, size)) < 0 && errno == EAGAIN) {
    fd_set fds;
    FD_ZERO(&fds);
    FD_SET(fd, &fds);
    fprintf(stderr, "Waiting after write failed with EAGAIN!"
                    "\n");
    int rc = select(fd + 1, NULL, &fds, NULL, NULL);
    if (rc < 0)
      return -1;
  }
  return n;
}


x86 assembly (clang, -O0):
<write_blocking>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1030 <write@plt>
mov    %eax,%ecx
mov    %ecx,-0x1c(%rbp)
xor    %eax,%eax
cmp    $0x0,%ecx
mov    %al,-0x25(%rbp)
jge    11a7 <write_blocking+0x57>
mov    0x2e44(%rip),%rax
mov    (%rax),%rax
mov    0x2e2a(%rip),%rcx
cmp    (%rcx),%rax
sete   %al
mov    %al,-0x25(%rbp)
mov    -0x25(%rbp),%al
test   $0x1,%al
jne    11b7 <write_blocking+0x67>
jmp    1219 <write_blocking+0xc9>
lea    -0x20(%rbp),%rdi
call   1050 <FD_ZERO@plt>
mov    -0x8(%rbp),%edi
lea    -0x20(%rbp),%rsi
call   1070 <FD_SET@plt>
mov    0x2e0d(%rip),%rax
mov    (%rax),%edi
lea    0xe24(%rip),%rsi
call   1040 <fprintf@plt>
mov    -0x8(%rbp),%edi
add    $0x1,%edi
xor    %eax,%eax
mov    %eax,%r8d
lea    -0x20(%rbp),%rdx
mov    %r8,%rsi
mov    %r8,%rcx
call   1060 <select@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    1214 <write_blocking+0xc4>
movl   $0xffffffff,-0x4(%rbp)
jmp    121f <write_blocking+0xcf>
jmp    116a <write_blocking+0x1a>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_blocking>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rsi,%r15
mov    %edi,%ebx
lea    0x1(%rbx),%r12d
lea    0x4(%rsp),%rbp
lea    0xe8d(%rip),%r13
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %ebx,%edi
mov    %r15,%rsi
mov    %r14,%rdx
call   1030 <write@plt>
test   %eax,%eax
jns    11e5 <write_blocking+0x95>
mov    0x2e30(%rip),%rcx
mov    (%rcx),%rcx
mov    0x2e36(%rip),%rdx
cmp    %rcx,(%rdx)
jne    11e5 <write_blocking+0x95>
mov    %rbp,%rdi
call   1050 <FD_ZERO@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
call   1070 <FD_SET@plt>
mov    0x2e20(%rip),%rax
mov    (%rax),%edi
mov    %r13,%rsi
call   1040 <fprintf@plt>
mov    %r12d,%edi
xor    %esi,%esi
mov    %rbp,%rdx
xor    %ecx,%ecx
xor    %r8d,%r8d
call   1060 <select@plt>
test   %eax,%eax
jns    1180 <write_blocking+0x30>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@errno = global i64 0
@EAGAIN = global i64 0
@stderr = global i32 0
@.str = [41 x i8] c"Waiting after write failed with EAGAIN!\0A\00"
define i32 @write_blocking(i32 %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i64 %2, i64* %7
store i32 0, i32* %8
br label %11
11: ; preds = %35, %3
%12 = load i32, i32* %5
%13 = load i8*, i8** %6
%14 = load i64, i64* %7
%15 = call i32 @write(i32 %12, i8* %13, i64 %14)
store i32 %15, i32* %8
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %21
17: ; preds = %11
%18 = load i64, i64* @errno
%19 = load i64, i64* @EAGAIN
%20 = icmp eq i64 %18, %19
br label %21
21: ; preds = %17, %11
%22 = phi i1 [ false, %11 ], [ %20, %17 ]
br i1 %22, label %23, label %36
23: ; preds = %21
%24 = call i32 @FD_ZERO(i32* %9)
%25 = load i32, i32* %5
%26 = call i32 @FD_SET(i32 %25, i32* %9)
%27 = load i32, i32* @stderr
%28 = call i32 @fprintf(i32 %27, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0))
%29 = load i32, i32* %5
%30 = add nsw i32 %29, 1
%31 = call i32 @select(i32 %30, i32* null, i32* %9, i32* null, i32* null)
store i32 %31, i32* %10
%32 = load i32, i32* %10
%33 = icmp slt i32 %32, 0
br i1 %33, label %34, label %35
34: ; preds = %23
store i32 -1, i32* %4
br label %38
35: ; preds = %23
br label %11
36: ; preds = %21
%37 = load i32, i32* %8
store i32 %37, i32* %4
br label %38
38: ; preds = %36, %34
%39 = load i32, i32* %4
ret i32 %39
}

LLVM IR (-O2):
@errno = global i64 0
@EAGAIN = global i64 0
@stderr = global i32 0
@.str = [41 x i8] c"Waiting after write failed with EAGAIN!\0A\00"
define i32 @write_blocking(i32 %0, i8* nocapture %1, i64 %2) {
%4 = alloca i32
%5 = bitcast i32* %4 to i8*
%6 = add nsw i32 %0, 1
br label %7
7: ; preds = %14, %3
%8 = call i32 @write(i32 %0, i8* %1, i64 %2)
%9 = icmp slt i32 %8, 0
%10 = load i64, i64* @errno
%11 = load i64, i64* @EAGAIN
%12 = icmp eq i64 %10, %11
%13 = select i1 %9, i1 %12, i1 false
br i1 %13, label %14, label %21
14: ; preds = %7
%15 = call i32 @FD_ZERO(i32* %4)
%16 = call i32 @FD_SET(i32 %0, i32* %4)
%17 = load i32, i32* @stderr
%18 = call i32 @fprintf(i32 %17, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0))
%19 = call i32 @select(i32 %6, i32* null, i32* %4, i32* null, i32* null)
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %7
21: ; preds = %7, %14
%22 = phi i32 [ -1, %14 ], [ %8, %7 ]
ret i32 %22
}