#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 write(int, char *, int);
int SendBuffer(int sd, char *buf, int s) {
  int count = 0;
  int rc;
  do {
    if ((rc = write(sd, buf + count, s - count)) < 0)
      return rc;
    count += rc;
  } while (count < s);
  return 0;
}


x86 assembly (clang, -O0):
<SendBuffer>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
movslq -0x18(%rbp),%rax
add    %rax,%rsi
mov    -0x14(%rbp),%edx
sub    -0x18(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jge    1159 <SendBuffer+0x49>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1175 <SendBuffer+0x65>
mov    -0x1c(%rbp),%eax
add    -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jl     1129 <SendBuffer+0x19>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<SendBuffer>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%ebx
mov    %rsi,%r14
mov    %edi,%r15d
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
movslq %ebp,%rax
lea    (%r14,%rax,1),%rsi
mov    %ebx,%edx
sub    %eax,%edx
mov    %r15d,%edi
call   1030 <write@plt>
test   %eax,%eax
js     114f <SendBuffer+0x3f>
add    %eax,%ebp
cmp    %ebx,%ebp
jl     1130 <SendBuffer+0x20>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

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