#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__ EAGAIN;
scalar_t__ EINTR;
scalar_t__ errno;
long write(int, char *, long);
long int blocking_write(int outf, char *buffer, long num) {
  long int words = 0, temp;
  while (words < num) {
    temp = write(outf, buffer + words, num - words);
    if (temp == -1) {
      if (errno != EINTR && errno != EAGAIN)
        return (-1);
      temp = 0;
    }
    words += temp;
  }
  return (0);
}


x86 assembly (clang, -O0):
<blocking_write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0xc(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x20(%rbp),%rax
jge    11ba <blocking_write+0xaa>
mov    -0xc(%rbp),%edi
mov    -0x18(%rbp),%rsi
add    -0x28(%rbp),%rsi
mov    -0x20(%rbp),%rdx
sub    -0x28(%rbp),%rdx
call   1030 <write@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0xffffffffffffffff,-0x30(%rbp)
jne    11a9 <blocking_write+0x99>
mov    0x2e79(%rip),%rax
mov    (%rax),%rax
mov    0x2e67(%rip),%rcx
cmp    (%rcx),%rax
je     11a1 <blocking_write+0x91>
mov    0x2e5f(%rip),%rax
mov    (%rax),%rax
mov    0x2e3d(%rip),%rcx
cmp    (%rcx),%rax
je     11a1 <blocking_write+0x91>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    11c2 <blocking_write+0xb2>
movq   $0x0,-0x30(%rbp)
mov    -0x30(%rbp),%rax
add    -0x28(%rbp),%rax
mov    %rax,-0x28(%rbp)
jmp    112b <blocking_write+0x1b>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%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
test   %rdx,%rdx
jle    1183 <blocking_write+0x73>
mov    %rdx,%rbx
mov    %rsi,%r15
mov    %edi,%ebp
mov    0x2eb1(%rip),%r12
mov    0x2ea2(%rip),%r13
xor    %r14d,%r14d
jmp    1148 <blocking_write+0x38>
xor    %eax,%eax
nopl   (%rax)
add    %rax,%r14
cmp    %rbx,%r14
jge    1183 <blocking_write+0x73>
lea    (%r15,%r14,1),%rsi
mov    %rbx,%rdx
sub    %r14,%rdx
mov    %ebp,%edi
call   1030 <write@plt>
cmp    $0xffffffffffffffff,%rax
jne    1140 <blocking_write+0x30>
mov    (%r12),%rcx
cmp    0x0(%r13),%rcx
je     113b <blocking_write+0x2b>
mov    $0x0,%eax
mov    0x2e53(%rip),%rdx
cmp    (%rdx),%rcx
je     1140 <blocking_write+0x30>
mov    $0xffffffffffffffff,%rax
jmp    1185 <blocking_write+0x75>
xor    %eax,%eax
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
@EAGAIN = global i64 0
define i64 @blocking_write(i32 %0, i8* %1, i64 %2) {
%4 = alloca i64
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i64
%9 = alloca i64
store i32 %0, i32* %5
store i8* %1, i8** %6
store i64 %2, i64* %7
store i64 0, i64* %8
br label %10
10: ; preds = %35, %3
%11 = load i64, i64* %8
%12 = load i64, i64* %7
%13 = icmp slt i64 %11, %12
br i1 %13, label %14, label %39
14: ; preds = %10
%15 = load i32, i32* %5
%16 = load i8*, i8** %6
%17 = load i64, i64* %8
%18 = getelementptr inbounds i8, i8* %16, i64 %17
%19 = load i64, i64* %7
%20 = load i64, i64* %8
%21 = sub nsw i64 %19, %20
%22 = call i64 @write(i32 %15, i8* %18, i64 %21)
store i64 %22, i64* %9
%23 = load i64, i64* %9
%24 = icmp eq i64 %23, -1
br i1 %24, label %25, label %35
25: ; preds = %14
%26 = load i64, i64* @errno
%27 = load i64, i64* @EINTR
%28 = icmp ne i64 %26, %27
br i1 %28, label %29, label %34
29: ; preds = %25
%30 = load i64, i64* @errno
%31 = load i64, i64* @EAGAIN
%32 = icmp ne i64 %30, %31
br i1 %32, label %33, label %34
33: ; preds = %29
store i64 -1, i64* %4
br label %40
34: ; preds = %29, %25
store i64 0, i64* %9
br label %35
35: ; preds = %34, %14
%36 = load i64, i64* %9
%37 = load i64, i64* %8
%38 = add nsw i64 %37, %36
store i64 %38, i64* %8
br label %10
39: ; preds = %10
store i64 0, i64* %4
br label %40
40: ; preds = %39, %33
%41 = load i64, i64* %4
ret i64 %41
}

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