#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 int ssize_t;

/* Variables and functions */
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int, int);
int /*<<< orphan*/ perror(char *);
int send(int, char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
void send_buffer(int fd, char *buffer, int bytes) {
  ssize_t retval;
  while (bytes > 0) {
    retval = send(fd, buffer, bytes, 0);
    if (retval < 0) {
      perror("error on send call");
      exit(1);
    }
    if (retval > bytes) {
      fprintf(stderr,
              "unexpected large return code from send %d with only %d bytes in "
              "send buffer\n",
              (int)retval, bytes);
    }
    bytes -= retval;
    buffer += retval;
  }
}


x86 assembly (clang, -O0):
<send_buffer>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jle    11d8 <send_buffer+0x98>
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
xor    %ecx,%ecx
call   1030 <send@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    1190 <send_buffer+0x50>
lea    0xe7f(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jle    11b7 <send_buffer+0x77>
mov    0x2e3d(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%edx
mov    -0x14(%rbp),%ecx
lea    0xe61(%rip),%rsi
call   1040 <fprintf@plt>
mov    -0x18(%rbp),%ecx
mov    -0x14(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x14(%rbp)
mov    -0x18(%rbp),%ecx
mov    -0x10(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x10(%rbp)
jmp    1152 <send_buffer+0x12>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<send_buffer>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
test   %edx,%edx
jle    11a4 <send_buffer+0x64>
mov    %edx,%ebx
mov    %rsi,%r14
mov    %edi,%r12d
mov    0x2e82(%rip),%r13
lea    0xeae(%rip),%r15
jmp    1179 <send_buffer+0x39>
nopw   0x0(%rax,%rax,1)
mov    %ebp,%eax
add    %rax,%r14
sub    %ebp,%ebx
jle    11a4 <send_buffer+0x64>
mov    %r12d,%edi
mov    %r14,%rsi
mov    %ebx,%edx
xor    %ecx,%ecx
call   1030 <send@plt>
test   %eax,%eax
js     11b3 <send_buffer+0x73>
mov    %eax,%ebp
cmp    %eax,%ebx
jae    1170 <send_buffer+0x30>
mov    0x0(%r13),%edi
mov    %r15,%rsi
mov    %ebp,%edx
mov    %ebx,%ecx
call   1040 <fprintf@plt>
jmp    1170 <send_buffer+0x30>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe46(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@.str = [19 x i8] c"error on send call\00"
@stderr = global i32 0
@.str.1 = [77 x i8] c"unexpected large return code from send %d with only %d bytes in send buffer\0A\00"
define void @send_buffer(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
store i32 %0, i32* %4
store i8* %1, i8** %5
store i32 %2, i32* %6
br label %8
8: ; preds = %30, %3
%9 = load i32, i32* %6
%10 = icmp sgt i32 %9, 0
br i1 %10, label %11, label %38
11: ; preds = %8
%12 = load i32, i32* %4
%13 = load i8*, i8** %5
%14 = load i32, i32* %6
%15 = call i32 @send(i32 %12, i8* %13, i32 %14, i32 0)
store i32 %15, i32* %7
%16 = load i32, i32* %7
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %21
18: ; preds = %11
%19 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
%20 = call i32 @exit(i32 1)
unreachable
21: ; preds = %11
%22 = load i32, i32* %7
%23 = load i32, i32* %6
%24 = icmp sgt i32 %22, %23
br i1 %24, label %25, label %30
25: ; preds = %21
%26 = load i32, i32* @stderr
%27 = load i32, i32* %7
%28 = load i32, i32* %6
%29 = call i32 @fprintf(i32 %26, i8* getelementptr inbounds ([77 x i8], [77 x i8]* @.str.1, i64 0, i64 0), i32 %27, i32 %28)
br label %30
30: ; preds = %25, %21
%31 = load i32, i32* %7
%32 = load i32, i32* %6
%33 = sub nsw i32 %32, %31
store i32 %33, i32* %6
%34 = load i32, i32* %7
%35 = load i8*, i8** %5
%36 = sext i32 %34 to i64
%37 = getelementptr inbounds i8, i8* %35, i64 %36
store i8* %37, i8** %5
br label %8
38: ; preds = %8
ret void
}

LLVM IR (-O2):
@.str = [19 x i8] c"error on send call\00"
@stderr = global i32 0
@.str.1 = [77 x i8] c"unexpected large return code from send %d with only %d bytes in send buffer\0A\00"
define void @send_buffer(i32 %0, i8* %1, i32 %2) {
%4 = icmp sgt i32 %2, 0
br i1 %4, label %5, label %23
5: ; preds = %3, %18
%6 = phi i8* [ %21, %18 ], [ %1, %3 ]
%7 = phi i32 [ %19, %18 ], [ %2, %3 ]
%8 = tail call i32 @send(i32 %0, i8* %6, i32 %7, i32 0)
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %13
10: ; preds = %5
%11 = tail call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
%12 = tail call i32 @exit(i32 1)
unreachable
13: ; preds = %5
%14 = icmp ult i32 %7, %8
br i1 %14, label %15, label %18
15: ; preds = %13
%16 = load i32, i32* @stderr
%17 = tail call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([77 x i8], [77 x i8]* @.str.1, i64 0, i64 0), i32 %8, i32 %7)
br label %18
18: ; preds = %15, %13
%19 = sub nsw i32 %7, %8
%20 = zext i32 %8 to i64
%21 = getelementptr inbounds i8, i8* %6, i64 %20
%22 = icmp sgt i32 %19, 0
br i1 %22, label %5, label %23
23: ; preds = %18, %3
ret void
}