#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 chisocket_send(int, char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int sockSend(int socket, char *buffer, int length) {
  int nbytes, nleft, nwritten = 0;
  nleft = length;
  while (nleft > 0) {
    if ((nbytes = chisocket_send(socket, buffer, nleft, 0)) == -1) {
      perror("Could not send message");
      return -1;
    }
    nleft -= nbytes;
    buffer += nbytes;
    nwritten += nbytes;
  }
  return nwritten;
}


x86 assembly (clang, -O0):
<sockSend>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x0,-0x20(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jle    11a8 <sockSend+0x88>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x1c(%rbp),%edx
xor    %ecx,%ecx
call   1030 <chisocket_send@plt>
mov    %eax,-0x18(%rbp)
cmp    $0xffffffff,%eax
jne    117e <sockSend+0x5e>
lea    0xe93(%rip),%rdi
call   1040 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11ae <sockSend+0x8e>
mov    -0x18(%rbp),%ecx
mov    -0x1c(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%ecx
mov    -0x10(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x10(%rbp)
mov    -0x18(%rbp),%eax
add    -0x20(%rbp),%eax
mov    %eax,-0x20(%rbp)
jmp    113f <sockSend+0x1f>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sockSend>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
test   %edx,%edx
jle    1163 <sockSend+0x43>
mov    %edx,%ebp
mov    %rsi,%rbx
mov    %edi,%r15d
xor    %r14d,%r14d
cs nopw 0x0(%rax,%rax,1)
mov    %r15d,%edi
mov    %rbx,%rsi
mov    %ebp,%edx
xor    %ecx,%ecx
call   1030 <chisocket_send@plt>
cmp    $0xffffffff,%eax
je     1174 <sockSend+0x54>
movslq %eax,%rcx
add    %rcx,%rbx
add    %eax,%r14d
sub    %eax,%ebp
jg     1140 <sockSend+0x20>
jmp    1166 <sockSend+0x46>
xor    %r14d,%r14d
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe85(%rip),%rdi
call   1040 <perror@plt>
mov    $0xffffffff,%r14d
jmp    1166 <sockSend+0x46>

LLVM IR (-O0):
@.str = [23 x i8] c"Could not send message\00"
define i32 @sockSend(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* %10
%11 = load i32, i32* %7
store i32 %11, i32* %9
br label %12
12: ; preds = %23, %3
%13 = load i32, i32* %9
%14 = icmp sgt i32 %13, 0
br i1 %14, label %15, label %34
15: ; preds = %12
%16 = load i32, i32* %5
%17 = load i8*, i8** %6
%18 = load i32, i32* %9
%19 = call i32 @chisocket_send(i32 %16, i8* %17, i32 %18, i32 0)
store i32 %19, i32* %8
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %23
21: ; preds = %15
%22 = call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %4
br label %36
23: ; preds = %15
%24 = load i32, i32* %8
%25 = load i32, i32* %9
%26 = sub nsw i32 %25, %24
store i32 %26, i32* %9
%27 = load i32, i32* %8
%28 = load i8*, i8** %6
%29 = sext i32 %27 to i64
%30 = getelementptr inbounds i8, i8* %28, i64 %29
store i8* %30, i8** %6
%31 = load i32, i32* %8
%32 = load i32, i32* %10
%33 = add nsw i32 %32, %31
store i32 %33, i32* %10
br label %12
34: ; preds = %12
%35 = load i32, i32* %10
store i32 %35, i32* %4
br label %36
36: ; preds = %34, %21
%37 = load i32, i32* %4
ret i32 %37
}

LLVM IR (-O2):
@.str = [23 x i8] c"Could not send message\00"
define i32 @sockSend(i32 %0, i8* %1, i32 %2) {
%4 = icmp sgt i32 %2, 0
br i1 %4, label %5, label %19
5: ; preds = %3, %13
%6 = phi i32 [ %17, %13 ], [ 0, %3 ]
%7 = phi i32 [ %14, %13 ], [ %2, %3 ]
%8 = phi i8* [ %16, %13 ], [ %1, %3 ]
%9 = tail call i32 @chisocket_send(i32 %0, i8* %8, i32 %7, i32 0)
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %13
11: ; preds = %5
%12 = tail call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
br label %19
13: ; preds = %5
%14 = sub nsw i32 %7, %9
%15 = sext i32 %9 to i64
%16 = getelementptr inbounds i8, i8* %8, i64 %15
%17 = add nsw i32 %9, %6
%18 = icmp sgt i32 %14, 0
br i1 %18, label %5, label %19
19: ; preds = %13, %3, %11
%20 = phi i32 [ -1, %11 ], [ 0, %3 ], [ %17, %13 ]
ret i32 %20
}