#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, char const *, int);
int sys_writen(int fd, const void *vptr, int n) {
  int nleft;
  int nwritten;
  const char *ptr;
  ptr = vptr;
  nleft = n;
  while (nleft > 0) {
    if ((nwritten = write(fd, ptr, nleft)) <= 0) {
      if (nwritten < 0 && errno == EINTR)
        nwritten = 0;
      else
        return (-1);
    }
    nleft -= nwritten;
    ptr += nwritten;
  }
  return n;
}


x86 assembly (clang, -O0):
<sys_writen>:
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,-0x28(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jle    11b7 <sys_writen+0xa7>
mov    -0x8(%rbp),%edi
mov    -0x28(%rbp),%rsi
mov    -0x18(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jg     1196 <sys_writen+0x86>
cmpl   $0x0,-0x1c(%rbp)
jge    1185 <sys_writen+0x75>
mov    0x2e7a(%rip),%rax
mov    (%rax),%rax
mov    0x2e68(%rip),%rcx
cmp    (%rcx),%rax
jne    1185 <sys_writen+0x75>
movl   $0x0,-0x1c(%rbp)
jmp    1191 <sys_writen+0x81>
movl   $0xffffffff,-0x4(%rbp)
jmp    11bd <sys_writen+0xad>
jmp    1196 <sys_writen+0x86>
mov    -0x1c(%rbp),%ecx
mov    -0x18(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x18(%rbp)
mov    -0x1c(%rbp),%ecx
mov    -0x28(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x28(%rbp)
jmp    1130 <sys_writen+0x20>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

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

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