#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*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int, int);
int /*<<< orphan*/ perror(char *);
size_t send(int, char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ stdout;
void send_buffer(int fd, char *buffer, int bytes) {
  size_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(stdout,
              "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    11e0 <send_buffer+0xa0>
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
xor    %ecx,%ecx
call   1030 <send@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jae    1192 <send_buffer+0x52>
lea    0xe7d(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
mov    -0x20(%rbp),%rax
movslq -0x14(%rbp),%rcx
cmp    %rcx,%rax
jbe    11c1 <send_buffer+0x81>
mov    0x2e36(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%rax
mov    %eax,%edx
mov    -0x14(%rbp),%ecx
lea    0xe57(%rip),%rsi
call   1040 <fprintf@plt>
mov    -0x20(%rbp),%rcx
movslq -0x14(%rbp),%rax
sub    %rcx,%rax
mov    %eax,-0x14(%rbp)
mov    -0x20(%rbp),%rax
add    -0x10(%rbp),%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    1184 <send_buffer+0x64>
mov    %edx,%ebx
mov    %rsi,%r14
mov    %edi,%r12d
mov    0x2ea2(%rip),%r13
lea    0xebb(%rip),%r15
jmp    1159 <send_buffer+0x39>
nopw   0x0(%rax,%rax,1)
sub    %ebp,%ebx
add    %rbp,%r14
test   %ebx,%ebx
jle    1184 <send_buffer+0x64>
mov    %r12d,%edi
mov    %r14,%rsi
mov    %ebx,%edx
xor    %ecx,%ecx
call   1030 <send@plt>
mov    %rax,%rbp
mov    %ebx,%eax
cmp    %rax,%rbp
jbe    1150 <send_buffer+0x30>
mov    0x0(%r13),%edi
mov    %r15,%rsi
mov    %ebp,%edx
mov    %ebx,%ecx
call   1040 <fprintf@plt>
jmp    1150 <send_buffer+0x30>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [19 x i8] c"error on send call\00"
@stdout = 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 i64
store i32 %0, i32* %4
store i8* %1, i8** %5
store i32 %2, i32* %6
br label %8
8: ; preds = %32, %3
%9 = load i32, i32* %6
%10 = icmp sgt i32 %9, 0
br i1 %10, label %11, label %41
11: ; preds = %8
%12 = load i32, i32* %4
%13 = load i8*, i8** %5
%14 = load i32, i32* %6
%15 = call i64 @send(i32 %12, i8* %13, i32 %14, i32 0)
store i64 %15, i64* %7
%16 = load i64, i64* %7
%17 = icmp ult i64 %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 i64, i64* %7
%23 = load i32, i32* %6
%24 = sext i32 %23 to i64
%25 = icmp ugt i64 %22, %24
br i1 %25, label %26, label %32
26: ; preds = %21
%27 = load i32, i32* @stdout
%28 = load i64, i64* %7
%29 = trunc i64 %28 to i32
%30 = load i32, i32* %6
%31 = call i32 @fprintf(i32 %27, i8* getelementptr inbounds ([77 x i8], [77 x i8]* @.str.1, i64 0, i64 0), i32 %29, i32 %30)
br label %32
32: ; preds = %26, %21
%33 = load i64, i64* %7
%34 = load i32, i32* %6
%35 = sext i32 %34 to i64
%36 = sub i64 %35, %33
%37 = trunc i64 %36 to i32
store i32 %37, i32* %6
%38 = load i64, i64* %7
%39 = load i8*, i8** %5
%40 = getelementptr inbounds i8, i8* %39, i64 %38
store i8* %40, i8** %5
br label %8
41: ; preds = %8
ret void
}

LLVM IR (-O2):
@stdout = 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 %22
5: ; preds = %3, %17
%6 = phi i8* [ %20, %17 ], [ %1, %3 ]
%7 = phi i32 [ %19, %17 ], [ %2, %3 ]
%8 = tail call i64 @send(i32 %0, i8* %6, i32 %7, i32 0)
%9 = zext i32 %7 to i64
%10 = icmp ugt i64 %8, %9
br i1 %10, label %13, label %11
11: ; preds = %5
%12 = trunc i64 %8 to i32
br label %17
13: ; preds = %5
%14 = load i32, i32* @stdout
%15 = trunc i64 %8 to i32
%16 = tail call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([77 x i8], [77 x i8]* @.str.1, i64 0, i64 0), i32 %15, i32 %7)
br label %17
17: ; preds = %11, %13
%18 = phi i32 [ %12, %11 ], [ %15, %13 ]
%19 = sub i32 %7, %18
%20 = getelementptr inbounds i8, i8* %6, i64 %8
%21 = icmp sgt i32 %19, 0
br i1 %21, label %5, label %22
22: ; preds = %17, %3
ret void
}