#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;

/* Variables and functions */
scalar_t__ EINTR;
scalar_t__ errno;
int write(int, void const *, int);
ssize_t full_write(int fd, const void *ptr, size_t nbytes) {
  int nleft, nwritten;
  nleft = nbytes;
  while (nleft > 0) {
  REISSUE_WRITE:
    nwritten = write(fd, ptr, nleft);
    if (nwritten < 0) {
      if (errno == EINTR) {
        goto REISSUE_WRITE;
      }
      return -1;
    }
    nleft -= nwritten;
    ptr = ((const char *)ptr) + nwritten;
  }
  return (nbytes - nleft);
}


x86 assembly (clang, -O0):
<full_write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jle    119f <full_write+0x8f>
jmp    1139 <full_write+0x29>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    1180 <full_write+0x70>
mov    0x2e84(%rip),%rax
mov    (%rax),%rax
mov    0x2e72(%rip),%rcx
cmp    (%rcx),%rax
jne    1174 <full_write+0x64>
jmp    1139 <full_write+0x29>
movl   $0xffffffff,-0x4(%rbp)
jmp    11ad <full_write+0x9d>
mov    -0x20(%rbp),%ecx
mov    -0x1c(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%rax
movslq -0x20(%rbp),%rcx
add    %rcx,%rax
mov    %rax,-0x10(%rbp)
jmp    112a <full_write+0x1a>
mov    -0x18(%rbp),%rax
movslq -0x1c(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<full_write>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %r14d,%ebp
test   %r14d,%r14d
jle    1166 <full_write+0x56>
mov    %rsi,%rbx
mov    %edi,%r15d
mov    0x2ead(%rip),%r12
mov    0x2e9e(%rip),%r13
mov    %r14d,%ebp
nopl   (%rax)
mov    %r15d,%edi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1030 <write@plt>
test   %eax,%eax
jns    115d <full_write+0x4d>
mov    (%r12),%rax
cmp    0x0(%r13),%rax
je     1140 <full_write+0x30>
jmp    116b <full_write+0x5b>
mov    %eax,%ecx
add    %rcx,%rbx
sub    %eax,%ebp
jg     1140 <full_write+0x30>
sub    %ebp,%r14d
jmp    1171 <full_write+0x61>
mov    $0xffffffff,%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 @full_write(i32 %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32
%9 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i64 %2, i64* %7
%10 = load i64, i64* %7
%11 = trunc i64 %10 to i32
store i32 %11, i32* %8
br label %12
12: ; preds = %29, %3
%13 = load i32, i32* %8
%14 = icmp sgt i32 %13, 0
br i1 %14, label %15, label %37
15: ; preds = %12
br label %16
16: ; preds = %27, %15
%17 = load i32, i32* %5
%18 = load i8*, i8** %6
%19 = load i32, i32* %8
%20 = call i32 @write(i32 %17, i8* %18, i32 %19)
store i32 %20, i32* %9
%21 = load i32, i32* %9
%22 = icmp slt i32 %21, 0
br i1 %22, label %23, label %29
23: ; preds = %16
%24 = load i64, i64* @errno
%25 = load i64, i64* @EINTR
%26 = icmp eq i64 %24, %25
br i1 %26, label %27, label %28
27: ; preds = %23
br label %16
28: ; preds = %23
store i32 -1, i32* %4
br label %43
29: ; preds = %16
%30 = load i32, i32* %9
%31 = load i32, i32* %8
%32 = sub nsw i32 %31, %30
store i32 %32, i32* %8
%33 = load i8*, i8** %6
%34 = load i32, i32* %9
%35 = sext i32 %34 to i64
%36 = getelementptr inbounds i8, i8* %33, i64 %35
store i8* %36, i8** %6
br label %12
37: ; preds = %12
%38 = load i64, i64* %7
%39 = load i32, i32* %8
%40 = sext i32 %39 to i64
%41 = sub i64 %38, %40
%42 = trunc i64 %41 to i32
store i32 %42, i32* %4
br label %43
43: ; preds = %37, %28
%44 = load i32, i32* %4
ret i32 %44
}

LLVM IR (-O2):
@errno = global i64 0
@EINTR = global i64 0
define i32 @full_write(i32 %0, i8* nocapture %1, i64 %2) {
%4 = trunc i64 %2 to i32
%5 = icmp sgt i32 %4, 0
br i1 %5, label %6, label %21
6: ; preds = %3, %16
%7 = phi i32 [ %17, %16 ], [ %4, %3 ]
%8 = phi i8* [ %19, %16 ], [ %1, %3 ]
br label %9
9: ; preds = %6, %12
%10 = tail call i32 @write(i32 %0, i8* %8, i32 %7)
%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 %24
16: ; preds = %9
%17 = sub nsw i32 %7, %10
%18 = zext i32 %10 to i64
%19 = getelementptr inbounds i8, i8* %8, i64 %18
%20 = icmp sgt i32 %17, 0
br i1 %20, label %6, label %21
21: ; preds = %16, %3
%22 = phi i32 [ %4, %3 ], [ %17, %16 ]
%23 = sub i32 %4, %22
br label %24
24: ; preds = %12, %21
%25 = phi i32 [ %23, %21 ], [ -1, %12 ]
ret i32 %25
}