#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 */
int /*<<< orphan*/ memcpy(char *, int *, int);
int send(int, char *, int, int /*<<< orphan*/);
int sendMessage(int socket, char *buf, int len) {
  int total = 0;
  int left = len;
  int n;
  memcpy(buf, &left, 1);
  while (total < len) {
    if (-1 == (n = send(socket, buf + total, left, 0)))
      return -1;
    total += n;
    left -= n;
  }
  return 0;
}


x86 assembly (clang, -O0):
<sendMessage>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x1c(%rbp),%rsi
mov    $0x1,%edx
call   1040 <memcpy@plt>
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    11ac <sendMessage+0x8c>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
movslq -0x18(%rbp),%rax
add    %rax,%rsi
mov    -0x1c(%rbp),%edx
xor    %ecx,%ecx
call   1030 <send@plt>
mov    %eax,%ecx
mov    %ecx,-0x20(%rbp)
mov    $0xffffffff,%eax
cmp    %ecx,%eax
jne    1193 <sendMessage+0x73>
movl   $0xffffffff,-0x4(%rbp)
jmp    11b3 <sendMessage+0x93>
mov    -0x20(%rbp),%eax
add    -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x20(%rbp),%ecx
mov    -0x1c(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x1c(%rbp)
jmp    1151 <sendMessage+0x31>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sendMessage>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%ebx
mov    %rsi,%r14
mov    %edi,%r15d
mov    %edx,0x4(%rsp)
lea    0x4(%rsp),%rsi
mov    %r14,%rdi
mov    $0x1,%edx
call   1040 <memcpy@plt>
test   %ebx,%ebx
jle    1175 <sendMessage+0x55>
xor    %ebp,%ebp
mov    0x4(%rsp),%edx
nop
movslq %ebp,%rsi
add    %r14,%rsi
mov    %r15d,%edi
xor    %ecx,%ecx
call   1030 <send@plt>
cmp    $0xffffffff,%eax
je     1179 <sendMessage+0x59>
add    %eax,%ebp
mov    0x4(%rsp),%edx
sub    %eax,%edx
mov    %edx,0x4(%rsp)
cmp    %ebx,%ebp
jl     1150 <sendMessage+0x30>
xor    %eax,%eax
jmp    117e <sendMessage+0x5e>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @sendMessage(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i32 %2, i32* %7
store i32 0, i32* %8
%11 = load i32, i32* %7
store i32 %11, i32* %9
%12 = load i8*, i8** %6
%13 = call i32 @memcpy(i8* %12, i32* %9, i32 1)
br label %14
14: ; preds = %28, %3
%15 = load i32, i32* %8
%16 = load i32, i32* %7
%17 = icmp slt i32 %15, %16
br i1 %17, label %18, label %35
18: ; preds = %14
%19 = load i32, i32* %5
%20 = load i8*, i8** %6
%21 = load i32, i32* %8
%22 = sext i32 %21 to i64
%23 = getelementptr inbounds i8, i8* %20, i64 %22
%24 = load i32, i32* %9
%25 = call i32 @send(i32 %19, i8* %23, i32 %24, i32 0)
store i32 %25, i32* %10
%26 = icmp eq i32 -1, %25
br i1 %26, label %27, label %28
27: ; preds = %18
store i32 -1, i32* %4
br label %36
28: ; preds = %18
%29 = load i32, i32* %10
%30 = load i32, i32* %8
%31 = add nsw i32 %30, %29
store i32 %31, i32* %8
%32 = load i32, i32* %10
%33 = load i32, i32* %9
%34 = sub nsw i32 %33, %32
store i32 %34, i32* %9
br label %14
35: ; preds = %14
store i32 0, i32* %4
br label %36
36: ; preds = %35, %27
%37 = load i32, i32* %4
ret i32 %37
}

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