#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*/ dump_memory(void const *, int);
int errno;
int /*<<< orphan*/ printf(char *, ...);
scalar_t__ send(int, void const *, int, int /*<<< orphan*/);
char *strerror(int);
int send_packet(int sd, const void *buffer, int length) {
  int sentLength = 0;
  while (sentLength < length) {
    int len = (int)send(sd, buffer + sentLength, length - sentLength, 0);
    if (len < 0) {
      printf("ERROR: When sending data, %s (Errno:%d)\n", strerror(errno),
             errno);
      break;
    }
    sentLength += len;
  }
  if (0) {
    printf("Send ");
    dump_memory(buffer, length);
  }
  return sentLength;
}


x86 assembly (clang, -O0):
<send_packet>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    11b8 <send_packet+0x88>
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
movslq -0x18(%rbp),%rax
add    %rax,%rsi
mov    -0x14(%rbp),%edx
sub    -0x18(%rbp),%edx
xor    %ecx,%ecx
call   1030 <send@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    11aa <send_packet+0x7a>
mov    0x2e5c(%rip),%rax
mov    (%rax),%edi
call   1050 <strerror@plt>
mov    %rax,%rsi
mov    0x2e4b(%rip),%rax
mov    (%rax),%edx
lea    0xe62(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    11b8 <send_packet+0x88>
mov    -0x1c(%rbp),%eax
add    -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
jmp    1149 <send_packet+0x19>
mov    -0x18(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<send_packet>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
test   %edx,%edx
jle    1171 <send_packet+0x41>
mov    %edx,%ebp
mov    %rsi,%r14
mov    %edi,%r15d
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nop
movslq %ebx,%rax
lea    (%r14,%rax,1),%rsi
mov    %ebp,%edx
sub    %eax,%edx
mov    %r15d,%edi
xor    %ecx,%ecx
call   1030 <send@plt>
test   %eax,%eax
js     1175 <send_packet+0x45>
add    %eax,%ebx
cmp    %ebp,%ebx
jl     1150 <send_packet+0x20>
jmp    1198 <send_packet+0x68>
xor    %ebx,%ebx
jmp    1198 <send_packet+0x68>
mov    0x2e64(%rip),%rbp
mov    0x0(%rbp),%edi
call   1050 <strerror@plt>
mov    0x0(%rbp),%edx
lea    0xe72(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [41 x i8] c"ERROR: When sending data, %s (Errno:%d)\0A\00"
@errno = global i32 0
define i32 @send_packet(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i32 %0, i32* %4
store i8* %1, i8** %5
store i32 %2, i32* %6
store i32 0, i32* %7
br label %9
9: ; preds = %31, %3
%10 = load i32, i32* %7
%11 = load i32, i32* %6
%12 = icmp slt i32 %10, %11
br i1 %12, label %13, label %35
13: ; preds = %9
%14 = load i32, i32* %4
%15 = load i8*, i8** %5
%16 = load i32, i32* %7
%17 = sext i32 %16 to i64
%18 = getelementptr i8, i8* %15, i64 %17
%19 = load i32, i32* %6
%20 = load i32, i32* %7
%21 = sub nsw i32 %19, %20
%22 = call i64 @send(i32 %14, i8* %18, i32 %21, i32 0)
%23 = trunc i64 %22 to i32
store i32 %23, i32* %8
%24 = load i32, i32* %8
%25 = icmp slt i32 %24, 0
br i1 %25, label %26, label %31
26: ; preds = %13
%27 = load i32, i32* @errno
%28 = call i8* @strerror(i32 %27)
%29 = load i32, i32* @errno
%30 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0), i8* %28, i32 %29)
br label %35
31: ; preds = %13
%32 = load i32, i32* %8
%33 = load i32, i32* %7
%34 = add nsw i32 %33, %32
store i32 %34, i32* %7
br label %9
35: ; preds = %26, %9
%36 = load i32, i32* %7
ret i32 %36
}

LLVM IR (-O2):
@.str = [41 x i8] c"ERROR: When sending data, %s (Errno:%d)\0A\00"
@errno = global i32 0
define i32 @send_packet(i32 %0, i8* %1, i32 %2) {
%4 = icmp sgt i32 %2, 0
br i1 %4, label %7, label %21
5: ; preds = %7
%6 = icmp slt i32 %15, %2
br i1 %6, label %7, label %21
7: ; preds = %3, %5
%8 = phi i32 [ %15, %5 ], [ 0, %3 ]
%9 = sext i32 %8 to i64
%10 = getelementptr i8, i8* %1, i64 %9
%11 = sub nsw i32 %2, %8
%12 = tail call i64 @send(i32 %0, i8* %10, i32 %11, i32 0)
%13 = trunc i64 %12 to i32
%14 = icmp slt i32 %13, 0
%15 = add nsw i32 %8, %13
br i1 %14, label %16, label %5
16: ; preds = %7
%17 = load i32, i32* @errno
%18 = tail call i8* @strerror(i32 %17)
%19 = load i32, i32* @errno
%20 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0), i8* %18, i32 %19)
br label %21
21: ; preds = %5, %3, %16
%22 = phi i32 [ %8, %16 ], [ 0, %3 ], [ %15, %5 ]
ret i32 %22
}