#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;
int /*<<< orphan*/ MSG_NOSIGNAL;
scalar_t__ errno;
int send(int, char *, unsigned int, int /*<<< orphan*/);
int socket_write(int fd, const void *data, unsigned datalen) {
  int retval;
  unsigned count;
  count = 0;
  do {
    retval = send(fd, ((char *)data) + count, datalen - count, MSG_NOSIGNAL);
    if (retval < 0) {
      if (errno == EINTR)
        continue;
      else
        return -1;
    } else if (retval == 0)
      break;
    count += retval;
  } while (count < datalen);
  return count;
}


x86 assembly (clang, -O0):
<socket_write>:
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    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x1c(%rbp),%eax
add    %rax,%rsi
mov    -0x14(%rbp),%edx
sub    -0x1c(%rbp),%edx
mov    0x2e8d(%rip),%rax
mov    (%rax),%ecx
call   1030 <send@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    1182 <socket_write+0x72>
mov    0x2e82(%rip),%rax
mov    (%rax),%rax
mov    0x2e70(%rip),%rcx
cmp    (%rcx),%rax
jne    1176 <socket_write+0x66>
jmp    119f <socket_write+0x8f>
movl   $0xffffffff,-0x4(%rbp)
jmp    11b1 <socket_write+0xa1>
cmpl   $0x0,-0x18(%rbp)
jne    1191 <socket_write+0x81>
jmp    11ab <socket_write+0x9b>
jmp    1196 <socket_write+0x86>
mov    -0x18(%rbp),%eax
add    -0x1c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x14(%rbp),%eax
jb     1129 <socket_write+0x19>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<socket_write>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edx,%ebp
mov    %rsi,%r14
mov    %edi,%r15d
xor    %ebx,%ebx
mov    0x2ea4(%rip),%r12
mov    0x2ea5(%rip),%r13
jmp    1154 <socket_write+0x44>
cs nopw 0x0(%rax,%rax,1)
nop
mov    0x2e99(%rip),%rax
mov    (%rax),%rax
cmp    0x0(%r13),%rax
jne    1177 <socket_write+0x67>
cmp    %ebp,%ebx
jae    117c <socket_write+0x6c>
mov    %ebx,%esi
add    %r14,%rsi
mov    %ebp,%edx
sub    %ebx,%edx
mov    (%r12),%ecx
mov    %r15d,%edi
call   1030 <send@plt>
test   %eax,%eax
js     1140 <socket_write+0x30>
je     117c <socket_write+0x6c>
add    %eax,%ebx
cmp    %ebp,%ebx
jb     1154 <socket_write+0x44>
jmp    117c <socket_write+0x6c>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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