#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*/ memset(char *, int, int);
int /*<<< orphan*/ perror(char *);
int send(int, char *, int, int /*<<< orphan*/);
int send_all(int fd, char *buf, int len) {
  memset(buf, 8, len);
  int pos = 0;
  while (pos < len) {
    int ret = send(fd, buf + pos, len - pos, 0);
    if (ret < 0) {
      perror("cannot send");
      exit(1);
    }
    pos += ret;
  }
  return pos;
}


x86 assembly (clang, -O0):
<send_all>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%edx
mov    $0x8,%esi
call   1040 <memset@plt>
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    11c2 <send_all+0x82>
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
movslq -0x18(%rbp),%rax
add    %rax,%rsi
mov    -0x14(%rbp),%edx
sub    -0x18(%rbp),%edx
xor    %ecx,%ecx
call   1030 <send@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    11b4 <send_all+0x74>
lea    0xe5b(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
mov    -0x1c(%rbp),%eax
add    -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
jmp    116a <send_all+0x2a>
mov    -0x18(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<send_all>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%ebx
mov    %rsi,%r14
mov    %edi,%r15d
mov    %rsi,%rdi
mov    $0x8,%esi
call   1040 <memset@plt>
xor    %ebp,%ebp
test   %ebx,%ebx
jle    118f <send_all+0x4f>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
movslq %ebp,%rax
lea    (%r14,%rax,1),%rsi
mov    %ebx,%edx
sub    %eax,%edx
mov    %r15d,%edi
xor    %ecx,%ecx
call   1030 <send@plt>
test   %eax,%eax
js     119c <send_all+0x5c>
add    %eax,%ebp
cmp    %ebx,%ebp
jl     1170 <send_all+0x30>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe5d(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@.str = [12 x i8] c"cannot send\00"
define i32 @send_all(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i32 %0, i32* %4
store i8* %1, i8** %5
store i32 %2, i32* %6
%9 = load i8*, i8** %5
%10 = load i32, i32* %6
%11 = call i32 @memset(i8* %9, i32 8, i32 %10)
store i32 0, i32* %7
br label %12
12: ; preds = %31, %3
%13 = load i32, i32* %7
%14 = load i32, i32* %6
%15 = icmp slt i32 %13, %14
br i1 %15, label %16, label %35
16: ; preds = %12
%17 = load i32, i32* %4
%18 = load i8*, i8** %5
%19 = load i32, i32* %7
%20 = sext i32 %19 to i64
%21 = getelementptr inbounds i8, i8* %18, i64 %20
%22 = load i32, i32* %6
%23 = load i32, i32* %7
%24 = sub nsw i32 %22, %23
%25 = call i32 @send(i32 %17, i8* %21, i32 %24, i32 0)
store i32 %25, i32* %8
%26 = load i32, i32* %8
%27 = icmp slt i32 %26, 0
br i1 %27, label %28, label %31
28: ; preds = %16
%29 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%30 = call i32 @exit(i32 1)
unreachable
31: ; preds = %16
%32 = load i32, i32* %8
%33 = load i32, i32* %7
%34 = add nsw i32 %33, %32
store i32 %34, i32* %7
br label %12
35: ; preds = %12
%36 = load i32, i32* %7
ret i32 %36
}

LLVM IR (-O2):
@.str = [12 x i8] c"cannot send\00"
define i32 @send_all(i32 %0, i8* %1, i32 %2) {
%4 = tail call i32 @memset(i8* %1, i32 8, i32 %2)
%5 = icmp sgt i32 %2, 0
br i1 %5, label %8, label %19
6: ; preds = %8
%7 = icmp slt i32 %15, %2
br i1 %7, label %8, label %19
8: ; preds = %3, %6
%9 = phi i32 [ %15, %6 ], [ 0, %3 ]
%10 = sext i32 %9 to i64
%11 = getelementptr inbounds i8, i8* %1, i64 %10
%12 = sub nsw i32 %2, %9
%13 = tail call i32 @send(i32 %0, i8* %11, i32 %12, i32 0)
%14 = icmp slt i32 %13, 0
%15 = add nsw i32 %13, %9
br i1 %14, label %16, label %6
16: ; preds = %8
%17 = tail call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%18 = tail call i32 @exit(i32 1)
unreachable
19: ; preds = %6, %3
%20 = phi i32 [ 0, %3 ], [ %15, %6 ]
ret i32 %20
}