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


x86 assembly (clang, -O0):
<socket_writeline>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0xc(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jbe    11b5 <socket_writeline+0xa5>
mov    -0xc(%rbp),%edi
mov    -0x38(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   1030 <write@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
ja     1195 <socket_writeline+0x85>
mov    0x2e7d(%rip),%rax
mov    (%rax),%rax
mov    0x2e6b(%rip),%rcx
cmp    (%rcx),%rax
jne    1183 <socket_writeline+0x73>
movq   $0x0,-0x30(%rbp)
jmp    1190 <socket_writeline+0x80>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    11bd <socket_writeline+0xad>
jmp    1195 <socket_writeline+0x85>
mov    -0x30(%rbp),%rcx
mov    -0x28(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x28(%rbp)
mov    -0x30(%rbp),%rax
add    -0x38(%rbp),%rax
mov    %rax,-0x38(%rbp)
jmp    1133 <socket_writeline+0x23>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<socket_writeline>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
test   %rdx,%rdx
je     116e <socket_writeline+0x5e>
mov    %rdx,%r14
mov    %rsi,%rbx
mov    %edi,%r15d
mov    0x2eb0(%rip),%r12
mov    0x2ea1(%rip),%r13
mov    %rdx,%rbp
jmp    1148 <socket_writeline+0x38>
nopl   0x0(%rax)
add    %rax,%rbx
sub    %rax,%rbp
je     1171 <socket_writeline+0x61>
mov    %r15d,%edi
mov    %rbx,%rsi
mov    %rbp,%rdx
call   1030 <write@plt>
test   %rax,%rax
jne    1140 <socket_writeline+0x30>
mov    (%r12),%rcx
cmp    0x0(%r13),%rcx
je     1140 <socket_writeline+0x30>
mov    $0xffffffffffffffff,%r14
jmp    1171 <socket_writeline+0x61>
xor    %r14d,%r14d
mov    %r14,%rax
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 i64 @socket_writeline(i32 %0, i8* %1, i64 %2) {
%4 = alloca i64
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i64
%9 = alloca i64
%10 = alloca i8*
store i32 %0, i32* %5
store i8* %1, i8** %6
store i64 %2, i64* %7
%11 = load i8*, i8** %6
store i8* %11, i8** %10
%12 = load i64, i64* %7
store i64 %12, i64* %8
br label %13
13: ; preds = %29, %3
%14 = load i64, i64* %8
%15 = icmp ugt i64 %14, 0
br i1 %15, label %16, label %36
16: ; preds = %13
%17 = load i32, i32* %5
%18 = load i8*, i8** %10
%19 = load i64, i64* %8
%20 = call i64 @write(i32 %17, i8* %18, i64 %19)
store i64 %20, i64* %9
%21 = icmp ule i64 %20, 0
br i1 %21, label %22, label %29
22: ; preds = %16
%23 = load i64, i64* @errno
%24 = load i64, i64* @EINTR
%25 = icmp eq i64 %23, %24
br i1 %25, label %26, label %27
26: ; preds = %22
store i64 0, i64* %9
br label %28
27: ; preds = %22
store i64 -1, i64* %4
br label %38
28: ; preds = %26
br label %29
29: ; preds = %28, %16
%30 = load i64, i64* %9
%31 = load i64, i64* %8
%32 = sub i64 %31, %30
store i64 %32, i64* %8
%33 = load i64, i64* %9
%34 = load i8*, i8** %10
%35 = getelementptr inbounds i8, i8* %34, i64 %33
store i8* %35, i8** %10
br label %13
36: ; preds = %13
%37 = load i64, i64* %7
store i64 %37, i64* %4
br label %38
38: ; preds = %36, %27
%39 = load i64, i64* %4
ret i64 %39
}

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