#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 EAGAIN;
int EINPROGRESS;
int errno;
int /*<<< orphan*/ perror(char *);
int send(int, char *, int, int /*<<< orphan*/);
void SendAll(int fd, char *buf, int total) {
  int toSend = total;
  int sent = 0;
  int ret = 0;
  int err = 0;
  while (toSend > 0) {
    ret = send(fd, buf + sent, toSend, 0);
    if (ret < 0) {
      err = errno;
      if (err == EAGAIN || err == EINPROGRESS) {
        err = 0;
        continue;
      }
      perror("send socket failed");
      return;
    }
    sent += ret;
    toSend -= ret;
  }
}


x86 assembly (clang, -O0):
<SendAll>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
cmpl   $0x0,-0x18(%rbp)
jle    11e2 <SendAll+0xc2>
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
movslq -0x1c(%rbp),%rax
add    %rax,%rsi
mov    -0x18(%rbp),%edx
xor    %ecx,%ecx
call   1030 <send@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    11c9 <SendAll+0xa9>
mov    0x2e55(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
mov    0x2e36(%rip),%rcx
cmp    (%rcx),%eax
je     11ac <SendAll+0x8c>
mov    -0x24(%rbp),%eax
mov    0x2e44(%rip),%rcx
cmp    (%rcx),%eax
jne    11b8 <SendAll+0x98>
movl   $0x0,-0x24(%rbp)
jmp    114d <SendAll+0x2d>
lea    0xe41(%rip),%rdi
call   1040 <perror@plt>
jmp    11e2 <SendAll+0xc2>
mov    -0x20(%rbp),%eax
add    -0x1c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x20(%rbp),%ecx
mov    -0x18(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x18(%rbp)
jmp    114d <SendAll+0x2d>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<SendAll>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,0x10(%rsp)
test   %edx,%edx
jle    11bc <SendAll+0x9c>
mov    %edx,%ebx
mov    %edi,%r15d
xor    %ecx,%ecx
mov    0x2e8f(%rip),%r13
mov    0x2e78(%rip),%r14
mov    0x2e91(%rip),%r12
mov    0x10(%rsp),%rbp
jmp    1172 <SendAll+0x52>
xchg   %ax,%ax
mov    0xc(%rsp),%ecx
add    %eax,%ecx
movslq %ecx,%rbp
add    0x10(%rsp),%rbp
sub    %eax,%ebx
jle    11bc <SendAll+0x9c>
mov    %ecx,0xc(%rsp)
cs nopw 0x0(%rax,%rax,1)
mov    %r15d,%edi
mov    %rbp,%rsi
mov    %ebx,%edx
xor    %ecx,%ecx
call   1030 <send@plt>
test   %eax,%eax
jns    1160 <SendAll+0x40>
mov    0x0(%r13),%eax
cmp    (%r14),%eax
je     1180 <SendAll+0x60>
cmp    (%r12),%eax
je     1180 <SendAll+0x60>
lea    0xe57(%rip),%rdi
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <perror@plt>
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@errno = global i32 0
@EAGAIN = global i32 0
@EINPROGRESS = global i32 0
@.str = [19 x i8] c"send socket failed\00"
define void @SendAll(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i32 %0, i32* %4
store i8* %1, i8** %5
store i32 %2, i32* %6
%11 = load i32, i32* %6
store i32 %11, i32* %7
store i32 0, i32* %8
store i32 0, i32* %9
store i32 0, i32* %10
br label %12
12: ; preds = %37, %34, %3
%13 = load i32, i32* %7
%14 = icmp sgt i32 %13, 0
br i1 %14, label %15, label %44
15: ; preds = %12
%16 = load i32, i32* %4
%17 = load i8*, i8** %5
%18 = load i32, i32* %8
%19 = sext i32 %18 to i64
%20 = getelementptr inbounds i8, i8* %17, i64 %19
%21 = load i32, i32* %7
%22 = call i32 @send(i32 %16, i8* %20, i32 %21, i32 0)
store i32 %22, i32* %9
%23 = load i32, i32* %9
%24 = icmp slt i32 %23, 0
br i1 %24, label %25, label %37
25: ; preds = %15
%26 = load i32, i32* @errno
store i32 %26, i32* %10
%27 = load i32, i32* %10
%28 = load i32, i32* @EAGAIN
%29 = icmp eq i32 %27, %28
br i1 %29, label %34, label %30
30: ; preds = %25
%31 = load i32, i32* %10
%32 = load i32, i32* @EINPROGRESS
%33 = icmp eq i32 %31, %32
br i1 %33, label %34, label %35
34: ; preds = %30, %25
store i32 0, i32* %10
br label %12
35: ; preds = %30
%36 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
br label %44
37: ; preds = %15
%38 = load i32, i32* %9
%39 = load i32, i32* %8
%40 = add nsw i32 %39, %38
store i32 %40, i32* %8
%41 = load i32, i32* %9
%42 = load i32, i32* %7
%43 = sub nsw i32 %42, %41
store i32 %43, i32* %7
br label %12
44: ; preds = %35, %12
ret void
}

LLVM IR (-O2):
@errno = global i32 0
@EAGAIN = global i32 0
@EINPROGRESS = global i32 0
@.str = [19 x i8] c"send socket failed\00"
define void @SendAll(i32 %0, i8* %1, i32 %2) {
%4 = icmp sgt i32 %2, 0
br i1 %4, label %5, label %27
5: ; preds = %3, %21
%6 = phi i8* [ %26, %21 ], [ %1, %3 ]
%7 = phi i32 [ %22, %21 ], [ 0, %3 ]
%8 = phi i32 [ %23, %21 ], [ %2, %3 ]
br label %9
9: ; preds = %5, %12
%10 = tail call i32 @send(i32 %0, i8* %6, i32 %8, i32 0)
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %21
12: ; preds = %9
%13 = load i32, i32* @errno
%14 = load i32, i32* @EAGAIN
%15 = icmp eq i32 %13, %14
%16 = load i32, i32* @EINPROGRESS
%17 = icmp eq i32 %13, %16
%18 = select i1 %15, i1 true, i1 %17
br i1 %18, label %9, label %19
19: ; preds = %12
%20 = tail call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
br label %27
21: ; preds = %9
%22 = add nsw i32 %10, %7
%23 = sub nsw i32 %8, %10
%24 = icmp sgt i32 %23, 0
%25 = sext i32 %22 to i64
%26 = getelementptr inbounds i8, i8* %1, i64 %25
br i1 %24, label %5, label %27
27: ; preds = %21, %3, %19
ret void
}