#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*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, size_t, int, int,
                           int /*<<< orphan*/);
int send(int, void *, size_t, int);
int /*<<< orphan*/ stderr;
void sendall(int tcp_fd, void *buf, size_t length, int flags) {
  int incremental_sent;
  int total_sent = 0;
  while (total_sent < length) {
    incremental_sent =
        send(tcp_fd, buf + total_sent, length - total_sent, flags);
    if (incremental_sent <= 0) {
      fprintf(stderr,
              "Error sending buffer of len=%zu to fd=%d; sent=%d, errno=%d\n",
              length, tcp_fd, total_sent, errno);
      return;
    }
    total_sent += incremental_sent;
  }
}


x86 assembly (clang, -O0):
<sendall>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
movl   $0x0,-0x24(%rbp)
movslq -0x24(%rbp),%rax
cmp    -0x18(%rbp),%rax
jae    11b6 <sendall+0x96>
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
movslq -0x24(%rbp),%rax
add    %rax,%rsi
mov    -0x18(%rbp),%rdx
movslq -0x24(%rbp),%rax
sub    %rax,%rdx
mov    -0x1c(%rbp),%ecx
call   1030 <send@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jg     11a8 <sendall+0x88>
mov    0x2e60(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rdx
mov    -0x4(%rbp),%ecx
mov    -0x24(%rbp),%r8d
mov    0x2e44(%rip),%rax
mov    (%rax),%r9d
lea    0xe62(%rip),%rsi
call   1040 <fprintf@plt>
jmp    11b6 <sendall+0x96>
mov    -0x20(%rbp),%eax
add    -0x24(%rbp),%eax
mov    %eax,-0x24(%rbp)
jmp    113d <sendall+0x1d>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sendall>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
test   %rdx,%rdx
je     1163 <sendall+0x43>
mov    %ecx,%r15d
mov    %rdx,%rbx
mov    %rsi,%r12
mov    %edi,%r14d
xor    %eax,%eax
xor    %ebp,%ebp
nopl   (%rax)
lea    (%r12,%rax,1),%rsi
mov    %rbx,%rdx
sub    %rax,%rdx
mov    %r14d,%edi
mov    %r15d,%ecx
call   1030 <send@plt>
test   %eax,%eax
jle    116c <sendall+0x4c>
add    %eax,%ebp
movslq %ebp,%rax
cmp    %rbx,%rax
jb     1140 <sendall+0x20>
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2e6d(%rip),%rax
mov    (%rax),%edi
mov    0x2e5c(%rip),%rax
mov    (%rax),%r9d
lea    0xe7a(%rip),%rsi
mov    %rbx,%rdx
mov    %r14d,%ecx
mov    %ebp,%r8d
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <fprintf@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [61 x i8] c"Error sending buffer of len=%zu to fd=%d; sent=%d, errno=%d\0A\00"
@errno = global i32 0
define void @sendall(i32 %0, i8* %1, i64 %2, i32 %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i64 %2, i64* %7
store i32 %3, i32* %8
store i32 0, i32* %10
br label %11
11: ; preds = %37, %4
%12 = load i32, i32* %10
%13 = sext i32 %12 to i64
%14 = load i64, i64* %7
%15 = icmp ult i64 %13, %14
br i1 %15, label %16, label %41
16: ; preds = %11
%17 = load i32, i32* %5
%18 = load i8*, i8** %6
%19 = load i32, i32* %10
%20 = sext i32 %19 to i64
%21 = getelementptr i8, i8* %18, i64 %20
%22 = load i64, i64* %7
%23 = load i32, i32* %10
%24 = sext i32 %23 to i64
%25 = sub i64 %22, %24
%26 = load i32, i32* %8
%27 = call i32 @send(i32 %17, i8* %21, i64 %25, i32 %26)
store i32 %27, i32* %9
%28 = load i32, i32* %9
%29 = icmp sle i32 %28, 0
br i1 %29, label %30, label %37
30: ; preds = %16
%31 = load i32, i32* @stderr
%32 = load i64, i64* %7
%33 = load i32, i32* %5
%34 = load i32, i32* %10
%35 = load i32, i32* @errno
%36 = call i32 @fprintf(i32 %31, i8* getelementptr inbounds ([61 x i8], [61 x i8]* @.str, i64 0, i64 0), i64 %32, i32 %33, i32 %34, i32 %35)
br label %41
37: ; preds = %16
%38 = load i32, i32* %9
%39 = load i32, i32* %10
%40 = add nsw i32 %39, %38
store i32 %40, i32* %10
br label %11
41: ; preds = %30, %11
ret void
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [61 x i8] c"Error sending buffer of len=%zu to fd=%d; sent=%d, errno=%d\0A\00"
@errno = global i32 0
define void @sendall(i32 %0, i8* %1, i64 %2, i32 %3) {
%5 = icmp eq i64 %2, 0
br i1 %5, label %21, label %9
6: ; preds = %9
%7 = sext i32 %16 to i64
%8 = icmp ult i64 %7, %2
br i1 %8, label %9, label %21
9: ; preds = %4, %6
%10 = phi i64 [ %7, %6 ], [ 0, %4 ]
%11 = phi i32 [ %16, %6 ], [ 0, %4 ]
%12 = getelementptr i8, i8* %1, i64 %10
%13 = sub i64 %2, %10
%14 = tail call i32 @send(i32 %0, i8* %12, i64 %13, i32 %3)
%15 = icmp slt i32 %14, 1
%16 = add nsw i32 %14, %11
br i1 %15, label %17, label %6
17: ; preds = %9
%18 = load i32, i32* @stderr
%19 = load i32, i32* @errno
%20 = tail call i32 @fprintf(i32 %18, i8* getelementptr inbounds ([61 x i8], [61 x i8]* @.str, i64 0, i64 0), i64 %2, i32 %0, i32 %11, i32 %19)
br label %21
21: ; preds = %6, %4, %17
ret void
}