#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 send_buf(int fd, void *data, size_t len) {
  int ret, remain = len;
  char *buf = data;
  while (remain > 0) {
    if ((ret = write(fd, buf, remain)) <= 0)
      return -1;
    buf += ret;
    remain -= ret;
  }
  return 0;
}


x86 assembly (clang, -O0):
<send_buf>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    %eax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
cmpl   $0x0,-0x20(%rbp)
jle    1184 <send_buf+0x74>
mov    -0x8(%rbp),%edi
mov    -0x28(%rbp),%rsi
mov    -0x20(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jg     1163 <send_buf+0x53>
movl   $0xffffffff,-0x4(%rbp)
jmp    118b <send_buf+0x7b>
mov    -0x1c(%rbp),%ecx
mov    -0x28(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x28(%rbp)
mov    -0x1c(%rbp),%ecx
mov    -0x20(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x20(%rbp)
jmp    1132 <send_buf+0x22>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<send_buf>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdx,%rbx
xor    %r14d,%r14d
test   %ebx,%ebx
jle    1152 <send_buf+0x42>
mov    %rsi,%rbp
mov    %edi,%r15d
nopw   0x0(%rax,%rax,1)
mov    %r15d,%edi
mov    %rbp,%rsi
mov    %ebx,%edx
call   1030 <write@plt>
test   %eax,%eax
jle    114c <send_buf+0x3c>
mov    %eax,%ecx
add    %rcx,%rbp
sub    %eax,%ebx
jg     1130 <send_buf+0x20>
jmp    1152 <send_buf+0x42>
mov    $0xffffffff,%r14d
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

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