#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 const *, int);
int __write(int fd, const void *buf, int buflen) {
  const unsigned char *p = buf;
  int n, i = 0;
  while (i < buflen) {
    n = write(fd, p + i, buflen - i);
    if (n < 0) {
      if (errno == EINTR)
        continue;
      else
        return n;
    }
    i += n;
  }
  return i;
}


x86 assembly (clang, -O0):
<__write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
movl   $0x0,-0x28(%rbp)
mov    -0x28(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    119b <__write+0x8b>
mov    -0x8(%rbp),%edi
mov    -0x20(%rbp),%rsi
movslq -0x28(%rbp),%rax
add    %rax,%rsi
mov    -0x14(%rbp),%edx
sub    -0x28(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    118d <__write+0x7d>
mov    0x2e76(%rip),%rax
mov    (%rax),%rax
mov    0x2e64(%rip),%rcx
cmp    (%rcx),%rax
jne    1182 <__write+0x72>
jmp    1131 <__write+0x21>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11a1 <__write+0x91>
mov    -0x24(%rbp),%eax
add    -0x28(%rbp),%eax
mov    %eax,-0x28(%rbp)
jmp    1131 <__write+0x21>
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<__write>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
test   %edx,%edx
jle    1189 <__write+0x79>
mov    %edi,%r13d
xor    %r12d,%r12d
mov    0x2eb1(%rip),%r14
mov    0x2ea2(%rip),%r15
mov    %edx,0xc(%rsp)
mov    %edx,%ebp
mov    %rsi,0x10(%rsp)
mov    %rsi,%rbx
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %r13d,%edi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1030 <write@plt>
test   %eax,%eax
jns    116b <__write+0x5b>
mov    (%r14),%rcx
cmp    (%r15),%rcx
je     1150 <__write+0x40>
jmp    1184 <__write+0x74>
add    %eax,%r12d
movslq %r12d,%rax
mov    0x10(%rsp),%rcx
lea    (%rcx,%rax,1),%rbx
mov    0xc(%rsp),%ebp
sub    %eax,%ebp
jg     1150 <__write+0x40>
jmp    118c <__write+0x7c>
mov    %eax,%r12d
jmp    118c <__write+0x7c>
xor    %r12d,%r12d
mov    %r12d,%eax
add    $0x18,%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 @__write(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%11 = load i8*, i8** %6
store i8* %11, i8** %8
store i32 0, i32* %10
br label %12
12: ; preds = %35, %32, %3
%13 = load i32, i32* %10
%14 = load i32, i32* %7
%15 = icmp slt i32 %13, %14
br i1 %15, label %16, label %39
16: ; preds = %12
%17 = load i32, i32* %5
%18 = load i8*, i8** %8
%19 = load i32, i32* %10
%20 = sext i32 %19 to i64
%21 = getelementptr inbounds i8, i8* %18, i64 %20
%22 = load i32, i32* %7
%23 = load i32, i32* %10
%24 = sub nsw i32 %22, %23
%25 = call i32 @write(i32 %17, i8* %21, i32 %24)
store i32 %25, i32* %9
%26 = load i32, i32* %9
%27 = icmp slt i32 %26, 0
br i1 %27, label %28, label %35
28: ; preds = %16
%29 = load i64, i64* @errno
%30 = load i64, i64* @EINTR
%31 = icmp eq i64 %29, %30
br i1 %31, label %32, label %33
32: ; preds = %28
br label %12
33: ; preds = %28
%34 = load i32, i32* %9
store i32 %34, i32* %4
br label %41
35: ; preds = %16
%36 = load i32, i32* %9
%37 = load i32, i32* %10
%38 = add nsw i32 %37, %36
store i32 %38, i32* %10
br label %12
39: ; preds = %12
%40 = load i32, i32* %10
store i32 %40, i32* %4
br label %41
41: ; preds = %39, %33
%42 = load i32, i32* %4
ret i32 %42
}

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