#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__ EWOULDBLOCK;
scalar_t__ errno;
int /*<<< orphan*/ perror(char *);
int write(int, unsigned char *, int);
int raw_writebuf(int fd, unsigned char *out, int size) {
  int rc;
  while (size > 0) {
    rc = write(fd, out, size);
    if (rc < 0) {
      if (errno == EAGAIN || errno == EWOULDBLOCK)
        continue;
      perror("writebuf: ");
      return -1;
    }
    out += rc;
    size -= rc;
  }
  return 0;
}


x86 assembly (clang, -O0):
<raw_writebuf>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jle    11ca <raw_writebuf+0xaa>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    11a9 <raw_writebuf+0x89>
mov    0x2e81(%rip),%rax
mov    (%rax),%rax
mov    0x2e5f(%rip),%rcx
cmp    (%rcx),%rax
je     118c <raw_writebuf+0x6c>
mov    0x2e67(%rip),%rax
mov    (%rax),%rax
mov    0x2e55(%rip),%rcx
cmp    (%rcx),%rax
jne    1191 <raw_writebuf+0x71>
jmp    1132 <raw_writebuf+0x12>
lea    0xe68(%rip),%rdi
call   1040 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11d1 <raw_writebuf+0xb1>
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    1132 <raw_writebuf+0x12>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<raw_writebuf>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
xor    %eax,%eax
test   %edx,%edx
jle    1195 <raw_writebuf+0x75>
mov    %edx,%ebp
mov    %rsi,%rbx
mov    %edi,%r15d
mov    0x2ea0(%rip),%r12
mov    0x2e81(%rip),%r13
mov    0x2e8a(%rip),%r14
jmp    1160 <raw_writebuf+0x40>
mov    %eax,%ecx
add    %rcx,%rbx
sub    %eax,%ebp
jle    1193 <raw_writebuf+0x73>
nopl   0x0(%rax)
mov    %r15d,%edi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1030 <write@plt>
test   %eax,%eax
jns    1150 <raw_writebuf+0x30>
mov    (%r12),%rax
cmp    0x0(%r13),%rax
je     1160 <raw_writebuf+0x40>
cmp    (%r14),%rax
je     1160 <raw_writebuf+0x40>
lea    0xe79(%rip),%rdi
call   1040 <perror@plt>
mov    $0xffffffff,%eax
jmp    1195 <raw_writebuf+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
@EAGAIN = global i64 0
@EWOULDBLOCK = global i64 0
@.str = [11 x i8] c"writebuf: \00"
define i32 @raw_writebuf(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 = %30, %27, %3
%10 = load i32, i32* %7
%11 = icmp sgt i32 %10, 0
br i1 %11, label %12, label %38
12: ; preds = %9
%13 = load i32, i32* %5
%14 = load i8*, i8** %6
%15 = load i32, i32* %7
%16 = call i32 @write(i32 %13, i8* %14, i32 %15)
store i32 %16, i32* %8
%17 = load i32, i32* %8
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %30
19: ; preds = %12
%20 = load i64, i64* @errno
%21 = load i64, i64* @EAGAIN
%22 = icmp eq i64 %20, %21
br i1 %22, label %27, label %23
23: ; preds = %19
%24 = load i64, i64* @errno
%25 = load i64, i64* @EWOULDBLOCK
%26 = icmp eq i64 %24, %25
br i1 %26, label %27, label %28
27: ; preds = %23, %19
br label %9
28: ; preds = %23
%29 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %4
br label %39
30: ; preds = %12
%31 = load i32, i32* %8
%32 = load i8*, i8** %6
%33 = sext i32 %31 to i64
%34 = getelementptr inbounds i8, i8* %32, i64 %33
store i8* %34, i8** %6
%35 = load i32, i32* %8
%36 = load i32, i32* %7
%37 = sub nsw i32 %36, %35
store i32 %37, i32* %7
br label %9
38: ; preds = %9
store i32 0, i32* %4
br label %39
39: ; preds = %38, %28
%40 = load i32, i32* %4
ret i32 %40
}

LLVM IR (-O2):
@errno = global i64 0
@EAGAIN = global i64 0
@EWOULDBLOCK = global i64 0
@.str = [11 x i8] c"writebuf: \00"
define i32 @raw_writebuf(i32 %0, i8* nocapture %1, i32 %2) {
%4 = icmp sgt i32 %2, 0
br i1 %4, label %5, label %25
5: ; preds = %3, %20
%6 = phi i32 [ %23, %20 ], [ %2, %3 ]
%7 = phi i8* [ %22, %20 ], [ %1, %3 ]
br label %8
8: ; preds = %5, %11
%9 = tail call i32 @write(i32 %0, i8* %7, i32 %6)
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %20
11: ; preds = %8
%12 = load i64, i64* @errno
%13 = load i64, i64* @EAGAIN
%14 = icmp eq i64 %12, %13
%15 = load i64, i64* @EWOULDBLOCK
%16 = icmp eq i64 %12, %15
%17 = select i1 %14, i1 true, i1 %16
br i1 %17, label %8, label %18
18: ; preds = %11
%19 = tail call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
br label %25
20: ; preds = %8
%21 = zext i32 %9 to i64
%22 = getelementptr inbounds i8, i8* %7, i64 %21
%23 = sub nsw i32 %6, %9
%24 = icmp sgt i32 %23, 0
br i1 %24, label %5, label %25
25: ; preds = %20, %3, %18
%26 = phi i32 [ -1, %18 ], [ 0, %3 ], [ 0, %20 ]
ret i32 %26
}