#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, unsigned char *, int);
int bwrite(int fd, unsigned char *buf, int len) {
  int r;
  while (len > 0) {
    do {
      r = write(fd, buf, len);
    } while (r == -1 && errno == EINTR);
    if (r < 1)
      return 0;
    buf += r;
    len -= r;
  }
  return 1;
}


x86 assembly (clang, -O0):
<bwrite>:
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    11ae <bwrite+0x9e>
jmp    1131 <bwrite+0x21>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x18(%rbp)
xor    %eax,%eax
cmpl   $0xffffffff,-0x18(%rbp)
mov    %al,-0x19(%rbp)
jne    116c <bwrite+0x5c>
mov    0x2e87(%rip),%rax
mov    (%rax),%rax
mov    0x2e75(%rip),%rcx
cmp    (%rcx),%rax
sete   %al
mov    %al,-0x19(%rbp)
mov    -0x19(%rbp),%al
test   $0x1,%al
jne    1131 <bwrite+0x21>
cmpl   $0x1,-0x18(%rbp)
jge    118d <bwrite+0x7d>
movl   $0x0,-0x4(%rbp)
jmp    11b5 <bwrite+0xa5>
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 <bwrite+0x12>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bwrite>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    $0x1,%r14d
test   %edx,%edx
jle    116e <bwrite+0x5e>
mov    %edx,%ebp
mov    %rsi,%rbx
mov    %edi,%r15d
mov    0x2eac(%rip),%r12
mov    0x2e9d(%rip),%r13
nopl   0x0(%rax,%rax,1)
mov    %r15d,%edi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
jne    115c <bwrite+0x4c>
mov    0x0(%r13),%rcx
cmp    %rcx,(%r12)
je     1140 <bwrite+0x30>
test   %eax,%eax
jle    116b <bwrite+0x5b>
mov    %eax,%ecx
add    %rcx,%rbx
sub    %eax,%ebp
jg     1140 <bwrite+0x30>
jmp    116e <bwrite+0x5e>
xor    %r14d,%r14d
mov    %r14d,%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
define i32 @bwrite(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 = %31, %3
%10 = load i32, i32* %7
%11 = icmp sgt i32 %10, 0
br i1 %11, label %12, label %39
12: ; preds = %9
br label %13
13: ; preds = %25, %12
%14 = load i32, i32* %5
%15 = load i8*, i8** %6
%16 = load i32, i32* %7
%17 = call i32 @write(i32 %14, i8* %15, i32 %16)
store i32 %17, i32* %8
br label %18
18: ; preds = %13
%19 = load i32, i32* %8
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %25
21: ; preds = %18
%22 = load i64, i64* @errno
%23 = load i64, i64* @EINTR
%24 = icmp eq i64 %22, %23
br label %25
25: ; preds = %21, %18
%26 = phi i1 [ false, %18 ], [ %24, %21 ]
br i1 %26, label %13, label %27
27: ; preds = %25
%28 = load i32, i32* %8
%29 = icmp slt i32 %28, 1
br i1 %29, label %30, label %31
30: ; preds = %27
store i32 0, i32* %4
br label %40
31: ; preds = %27
%32 = load i32, i32* %8
%33 = load i8*, i8** %6
%34 = sext i32 %32 to i64
%35 = getelementptr inbounds i8, i8* %33, i64 %34
store i8* %35, i8** %6
%36 = load i32, i32* %8
%37 = load i32, i32* %7
%38 = sub nsw i32 %37, %36
store i32 %38, i32* %7
br label %9
39: ; preds = %9
store i32 1, i32* %4
br label %40
40: ; preds = %39, %30
%41 = load i32, i32* %4
ret i32 %41
}

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