#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 *, int);
int write_buffer(int sock, char *buffer, int len) {
  int bytes_written, total_written = 0;
  while (total_written < len) {
    if ((bytes_written =
             write(sock, buffer + total_written, len - total_written)) == -1) {
      if (errno != EINTR) {
        return -1;
      }
    } else {
      total_written += bytes_written;
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<write_buffer>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    1193 <write_buffer+0x83>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
movslq -0x1c(%rbp),%rax
add    %rax,%rsi
mov    -0x14(%rbp),%edx
sub    -0x1c(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x18(%rbp)
cmp    $0xffffffff,%eax
jne    1185 <write_buffer+0x75>
mov    0x2e7f(%rip),%rax
mov    (%rax),%rax
mov    0x2e6d(%rip),%rcx
cmp    (%rcx),%rax
je     1180 <write_buffer+0x70>
movl   $0xffffffff,-0x4(%rbp)
jmp    119a <write_buffer+0x8a>
jmp    118e <write_buffer+0x7e>
mov    -0x18(%rbp),%eax
add    -0x1c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
jmp    1129 <write_buffer+0x19>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_buffer>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
test   %edx,%edx
jle    116f <write_buffer+0x5f>
mov    %edx,%ebx
mov    %rsi,%r14
mov    %edi,%r15d
xor    %ebp,%ebp
mov    0x2eb0(%rip),%r12
mov    0x2ea1(%rip),%r13
jmp    1146 <write_buffer+0x36>
nopl   0x0(%rax)
add    %eax,%ebp
cmp    %ebx,%ebp
jge    116f <write_buffer+0x5f>
movslq %ebp,%rax
lea    (%r14,%rax,1),%rsi
mov    %ebx,%edx
sub    %eax,%edx
mov    %r15d,%edi
call   1030 <write@plt>
cmp    $0xffffffff,%eax
jne    1140 <write_buffer+0x30>
mov    (%r12),%rax
cmp    0x0(%r13),%rax
je     1142 <write_buffer+0x32>
mov    $0xffffffff,%eax
jmp    1171 <write_buffer+0x61>
xor    %eax,%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_buffer(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
store i32 0, i32* %9
br label %10
10: ; preds = %35, %3
%11 = load i32, i32* %9
%12 = load i32, i32* %7
%13 = icmp slt i32 %11, %12
br i1 %13, label %14, label %36
14: ; preds = %10
%15 = load i32, i32* %5
%16 = load i8*, i8** %6
%17 = load i32, i32* %9
%18 = sext i32 %17 to i64
%19 = getelementptr inbounds i8, i8* %16, i64 %18
%20 = load i32, i32* %7
%21 = load i32, i32* %9
%22 = sub nsw i32 %20, %21
%23 = call i32 @write(i32 %15, i8* %19, i32 %22)
store i32 %23, i32* %8
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %31
25: ; preds = %14
%26 = load i64, i64* @errno
%27 = load i64, i64* @EINTR
%28 = icmp ne i64 %26, %27
br i1 %28, label %29, label %30
29: ; preds = %25
store i32 -1, i32* %4
br label %37
30: ; preds = %25
br label %35
31: ; preds = %14
%32 = load i32, i32* %8
%33 = load i32, i32* %9
%34 = add nsw i32 %33, %32
store i32 %34, i32* %9
br label %35
35: ; preds = %31, %30
br label %10
36: ; preds = %10
store i32 0, i32* %4
br label %37
37: ; preds = %36, %29
%38 = load i32, i32* %4
ret i32 %38
}

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