#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 */
typedef size_t ssize_t;

/* Variables and functions */
size_t stats_send(int, char const *, size_t, int /*<<< orphan*/);
int peer_send_buff(int sockfd, const char *buff, size_t len) {
  ssize_t tot_sent = 0;
  while (tot_sent < len) {
    ssize_t sent = stats_send(sockfd, buff, len - tot_sent, 0);
    if (sent < 0)
      return -1;
    tot_sent += sent;
    buff += sent;
  }
  if (tot_sent == len)
    return 0;
  else
    return -1;
}


x86 assembly (clang, -O0):
<peer_send_buff>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%rax
cmp    -0x18(%rbp),%rax
jae    1187 <peer_send_buff+0x77>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
sub    -0x20(%rbp),%rdx
xor    %ecx,%ecx
call   1030 <stats_send@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jae    116a <peer_send_buff+0x5a>
movl   $0xffffffff,-0x4(%rbp)
jmp    11a8 <peer_send_buff+0x98>
mov    -0x28(%rbp),%rax
add    -0x20(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x28(%rbp),%rax
add    -0x10(%rbp),%rax
mov    %rax,-0x10(%rbp)
jmp    112b <peer_send_buff+0x1b>
mov    -0x20(%rbp),%rax
cmp    -0x18(%rbp),%rax
jne    11a1 <peer_send_buff+0x91>
movl   $0x0,-0x4(%rbp)
jmp    11a8 <peer_send_buff+0x98>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<peer_send_buff>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdx,%r14
test   %rdx,%rdx
je     1150 <peer_send_buff+0x40>
mov    %rsi,%rbx
mov    %edi,%r15d
xor    %ebp,%ebp
nopw   0x0(%rax,%rax,1)
mov    %r14,%rdx
sub    %rbp,%rdx
mov    %r15d,%edi
mov    %rbx,%rsi
xor    %ecx,%ecx
call   1030 <stats_send@plt>
add    %rax,%rbx
add    %rax,%rbp
cmp    %r14,%rbp
jb     1130 <peer_send_buff+0x20>
jmp    1152 <peer_send_buff+0x42>
xor    %ebp,%ebp
xor    %eax,%eax
cmp    %r14,%rbp
setne  %al
neg    %eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
define i32 @peer_send_buff(i32 %0, i8* %1, i64 %2) {
%4 = icmp eq i64 %2, 0
br i1 %4, label %13, label %5
5: ; preds = %3, %5
%6 = phi i64 [ %11, %5 ], [ 0, %3 ]
%7 = phi i8* [ %10, %5 ], [ %1, %3 ]
%8 = sub i64 %2, %6
%9 = tail call i64 @stats_send(i32 %0, i8* %7, i64 %8, i32 0)
%10 = getelementptr inbounds i8, i8* %7, i64 %9
%11 = add i64 %9, %6
%12 = icmp ult i64 %11, %2
br i1 %12, label %5, label %13
13: ; preds = %5, %3
%14 = phi i64 [ 0, %3 ], [ %11, %5 ]
%15 = icmp ne i64 %14, %2
%16 = sext i1 %15 to i32
ret i32 %16
}