#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__ EAGAIN;
scalar_t__ EINTR;
scalar_t__ errno;
int write(int, char const *, size_t);
int write_socket(int s, const void *buf, size_t size) {
  const char *data = (const char *)buf;
  size_t total_count = 0;
  while (total_count < size) {
    ssize_t count = write(s, data + total_count, size - total_count);
    if (count == -1) {
      if (errno != EAGAIN && errno != EINTR) {
        return 0;
      } else {
        continue;
      }
    }
    total_count += count;
  }
  return 1;
}


x86 assembly (clang, -O0):
<write_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x18(%rbp),%rax
jae    11bc <write_socket+0xac>
mov    -0x8(%rbp),%edi
mov    -0x20(%rbp),%rsi
add    -0x28(%rbp),%rsi
mov    -0x18(%rbp),%rdx
sub    -0x28(%rbp),%rdx
call   1030 <write@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0xffffffff,-0x2c(%rbp)
jne    11ab <write_socket+0x9b>
mov    0x2e73(%rip),%rax
mov    (%rax),%rax
mov    0x2e51(%rip),%rcx
cmp    (%rcx),%rax
je     11a6 <write_socket+0x96>
mov    0x2e59(%rip),%rax
mov    (%rax),%rax
mov    0x2e47(%rip),%rcx
cmp    (%rcx),%rax
je     11a6 <write_socket+0x96>
movl   $0x0,-0x4(%rbp)
jmp    11c3 <write_socket+0xb3>
jmp    1133 <write_socket+0x23>
movslq -0x2c(%rbp),%rax
add    -0x28(%rbp),%rax
mov    %rax,-0x28(%rbp)
jmp    1133 <write_socket+0x23>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_socket>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    $0x1,%eax
test   %rdx,%rdx
je     117f <write_socket+0x6f>
mov    %rdx,%rbx
mov    %rsi,%r15
mov    %edi,%r12d
xor    %ebp,%ebp
mov    0x2ea9(%rip),%r13
mov    0x2e8a(%rip),%r14
jmp    114a <write_socket+0x3a>
cltq
add    %rax,%rbp
cmp    %rbx,%rbp
jae    117a <write_socket+0x6a>
lea    (%r15,%rbp,1),%rsi
mov    %rbx,%rdx
sub    %rbp,%rdx
mov    %r12d,%edi
call   1030 <write@plt>
cmp    $0xffffffff,%eax
jne    1140 <write_socket+0x30>
mov    0x0(%r13),%rax
cmp    (%r14),%rax
je     1145 <write_socket+0x35>
mov    0x2e67(%rip),%rcx
cmp    (%rcx),%rax
je     1145 <write_socket+0x35>
xor    %eax,%eax
jmp    117f <write_socket+0x6f>
mov    $0x1,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@errno = global i64 0
@EAGAIN = global i64 0
@EINTR = global i64 0
define i32 @write_socket(i32 %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i8*
%9 = alloca i64
%10 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i64 %2, i64* %7
%11 = load i8*, i8** %6
store i8* %11, i8** %8
store i64 0, i64* %9
br label %12
12: ; preds = %37, %36, %3
%13 = load i64, i64* %9
%14 = load i64, i64* %7
%15 = icmp ult i64 %13, %14
br i1 %15, label %16, label %42
16: ; preds = %12
%17 = load i32, i32* %5
%18 = load i8*, i8** %8
%19 = load i64, i64* %9
%20 = getelementptr inbounds i8, i8* %18, i64 %19
%21 = load i64, i64* %7
%22 = load i64, i64* %9
%23 = sub i64 %21, %22
%24 = call i32 @write(i32 %17, i8* %20, i64 %23)
store i32 %24, i32* %10
%25 = load i32, i32* %10
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %37
27: ; preds = %16
%28 = load i64, i64* @errno
%29 = load i64, i64* @EAGAIN
%30 = icmp ne i64 %28, %29
br i1 %30, label %31, label %36
31: ; preds = %27
%32 = load i64, i64* @errno
%33 = load i64, i64* @EINTR
%34 = icmp ne i64 %32, %33
br i1 %34, label %35, label %36
35: ; preds = %31
store i32 0, i32* %4
br label %43
36: ; preds = %31, %27
br label %12
37: ; preds = %16
%38 = load i32, i32* %10
%39 = sext i32 %38 to i64
%40 = load i64, i64* %9
%41 = add i64 %40, %39
store i64 %41, i64* %9
br label %12
42: ; preds = %12
store i32 1, i32* %4
br label %43
43: ; preds = %42, %35
%44 = load i32, i32* %4
ret i32 %44
}

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