#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, char *, size_t);
int write_line(int sock, char buffer[], int len) {
  int left = len;
  while (left > 0) {
    ssize_t n = write(sock, buffer, (size_t)left);
    if (n == -1)
      if (errno == EINTR)
        continue;
      else
        return -1;
    left -= n;
    buffer += n;
  }
  return len;
}


x86 assembly (clang, -O0):
<write_line>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jle    119b <write_line+0x8b>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
movslq -0x18(%rbp),%rdx
call   1030 <write@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    117a <write_line+0x6a>
mov    0x2e8a(%rip),%rax
mov    (%rax),%rax
mov    0x2e78(%rip),%rcx
cmp    (%rcx),%rax
jne    116e <write_line+0x5e>
jmp    1128 <write_line+0x18>
movl   $0xffffffff,-0x4(%rbp)
jmp    11a1 <write_line+0x91>
mov    -0x1c(%rbp),%ecx
mov    -0x18(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x18(%rbp)
mov    -0x1c(%rbp),%ecx
mov    -0x10(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x10(%rbp)
jmp    1128 <write_line+0x18>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_line>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edx,%r14d
test   %edx,%edx
jle    116c <write_line+0x5c>
mov    %rsi,%rbx
mov    %edi,%r13d
mov    0x2eb1(%rip),%r15
mov    0x2ea2(%rip),%r12
mov    %r14d,%ebp
jmp    114b <write_line+0x3b>
nopl   0x0(%rax,%rax,1)
sub    %eax,%ebp
cltq
add    %rax,%rbx
test   %ebp,%ebp
jle    116c <write_line+0x5c>
mov    %ebp,%edx
mov    %r13d,%edi
mov    %rbx,%rsi
call   1030 <write@plt>
cmp    $0xffffffff,%eax
jne    1140 <write_line+0x30>
mov    (%r15),%rax
cmp    (%r12),%rax
je     1147 <write_line+0x37>
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 @write_line(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%10 = load i32, i32* %7
store i32 %10, i32* %8
br label %11
11: ; preds = %28, %26, %3
%12 = load i32, i32* %8
%13 = icmp sgt i32 %12, 0
br i1 %13, label %14, label %36
14: ; preds = %11
%15 = load i32, i32* %5
%16 = load i8*, i8** %6
%17 = load i32, i32* %8
%18 = sext i32 %17 to i64
%19 = call i32 @write(i32 %15, i8* %16, i64 %18)
store i32 %19, i32* %9
%20 = load i32, i32* %9
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %28
22: ; preds = %14
%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
br label %11
27: ; preds = %22
store i32 -1, i32* %4
br label %38
28: ; preds = %14
%29 = load i32, i32* %9
%30 = load i32, i32* %8
%31 = sub nsw i32 %30, %29
store i32 %31, i32* %8
%32 = load i32, i32* %9
%33 = load i8*, i8** %6
%34 = sext i32 %32 to i64
%35 = getelementptr inbounds i8, i8* %33, i64 %34
store i8* %35, i8** %6
br label %11
36: ; preds = %11
%37 = load i32, i32* %7
store i32 %37, i32* %4
br label %38
38: ; preds = %36, %27
%39 = load i32, i32* %4
ret i32 %39
}

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