#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 send(int const, char const *const, size_t const, int /*<<< orphan*/);
int WriteToSocket(const int sock, const char *const buffer,
                  const size_t buflen) {
  size_t bytesWritten = 0;
  ssize_t writeResult;
  int retval = 0, done = 0;
  do {
    writeResult = send(sock, buffer + bytesWritten, buflen - bytesWritten, 0);
    if (writeResult == -1) {
      if (errno == EINTR)
        writeResult = 0;
      else {
        retval = 1;
        done = 1;
      }
    } else {
      bytesWritten += writeResult;
      if (writeResult == 0)
        done = 1;
    }
  } while (done == 0);
  return retval;
}


x86 assembly (clang, -O0):
<WriteToSocket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
movl   $0x0,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
add    -0x20(%rbp),%rsi
mov    -0x18(%rbp),%rdx
sub    -0x20(%rbp),%rdx
xor    %ecx,%ecx
call   1030 <send@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    1199 <WriteToSocket+0x89>
mov    0x2e79(%rip),%rax
mov    (%rax),%rax
mov    0x2e67(%rip),%rcx
cmp    (%rcx),%rax
jne    1186 <WriteToSocket+0x76>
movl   $0x0,-0x24(%rbp)
jmp    1194 <WriteToSocket+0x84>
movl   $0x1,-0x28(%rbp)
movl   $0x1,-0x2c(%rbp)
jmp    11bb <WriteToSocket+0xab>
movslq -0x24(%rbp),%rax
add    -0x20(%rbp),%rax
mov    %rax,-0x20(%rbp)
cmpl   $0x0,-0x24(%rbp)
jne    11b6 <WriteToSocket+0xa6>
movl   $0x1,-0x2c(%rbp)
jmp    11bb <WriteToSocket+0xab>
jmp    11c0 <WriteToSocket+0xb0>
cmpl   $0x0,-0x2c(%rbp)
je     1139 <WriteToSocket+0x29>
mov    -0x28(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<WriteToSocket>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdx,%rbp
mov    %edi,%r12d
xor    %r13d,%r13d
mov    0x2eb2(%rip),%r14
mov    0x2ea3(%rip),%r15
mov    %rdx,0x10(%rsp)
mov    %rsi,0x8(%rsp)
lea    (%rsi,%r13,1),%rbx
sub    %r13,%rbp
cs nopw 0x0(%rax,%rax,1)
mov    %r12d,%edi
mov    %rbx,%rsi
mov    %rbp,%rdx
xor    %ecx,%ecx
call   1030 <send@plt>
cmp    $0xffffffff,%eax
jne    1170 <WriteToSocket+0x60>
mov    (%r14),%rax
cmp    (%r15),%rax
je     1150 <WriteToSocket+0x40>
jmp    1188 <WriteToSocket+0x78>
nop
movslq %eax,%rcx
add    %rcx,%r13
test   %eax,%eax
mov    0x10(%rsp),%rbp
mov    0x8(%rsp),%rsi
jne    113f <WriteToSocket+0x2f>
xor    %eax,%eax
jmp    118d <WriteToSocket+0x7d>
mov    $0x1,%eax
add    $0x18,%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 @WriteToSocket(i32 %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i64
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i32 %0, i32* %4
store i8* %1, i8** %5
store i64 %2, i64* %6
store i64 0, i64* %7
store i32 0, i32* %9
store i32 0, i32* %10
br label %11
11: ; preds = %39, %3
%12 = load i32, i32* %4
%13 = load i8*, i8** %5
%14 = load i64, i64* %7
%15 = getelementptr inbounds i8, i8* %13, i64 %14
%16 = load i64, i64* %6
%17 = load i64, i64* %7
%18 = sub i64 %16, %17
%19 = call i32 @send(i32 %12, i8* %15, i64 %18, i32 0)
store i32 %19, i32* %8
%20 = load i32, i32* %8
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %29
22: ; preds = %11
%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 i32 0, i32* %8
br label %28
27: ; preds = %22
store i32 1, i32* %9
store i32 1, i32* %10
br label %28
28: ; preds = %27, %26
br label %38
29: ; preds = %11
%30 = load i32, i32* %8
%31 = sext i32 %30 to i64
%32 = load i64, i64* %7
%33 = add i64 %32, %31
store i64 %33, i64* %7
%34 = load i32, i32* %8
%35 = icmp eq i32 %34, 0
br i1 %35, label %36, label %37
36: ; preds = %29
store i32 1, i32* %10
br label %37
37: ; preds = %36, %29
br label %38
38: ; preds = %37, %28
br label %39
39: ; preds = %38
%40 = load i32, i32* %10
%41 = icmp eq i32 %40, 0
br i1 %41, label %11, label %42
42: ; preds = %39
%43 = load i32, i32* %9
ret i32 %43
}

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