#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*/ perror(char *);
size_t write(int, void *, size_t);
int send_all(int socket_fd, void *bytes, size_t len) {
  size_t bytes_written, total_bytes_written = 0;
  while (bytes_written != len) {
    bytes_written = write(socket_fd, bytes + total_bytes_written,
                          len - total_bytes_written);
    if (bytes_written == -1) {
      perror("failed to write to client");
      return -1;
    }
    total_bytes_written += bytes_written;
  }
}


x86 assembly (clang, -O0):
<send_all>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x20(%rbp),%rax
cmp    -0x18(%rbp),%rax
je     1199 <send_all+0x79>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
add    -0x28(%rbp),%rsi
mov    -0x18(%rbp),%rdx
sub    -0x28(%rbp),%rdx
call   1030 <write@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0xffffffffffffffff,-0x20(%rbp)
jne    1188 <send_all+0x68>
lea    0xe89(%rip),%rdi
call   1040 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1199 <send_all+0x79>
mov    -0x20(%rbp),%rax
add    -0x28(%rbp),%rax
mov    %rax,-0x28(%rbp)
jmp    113b <send_all+0x1b>
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<send_all>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdx,%rbx
mov    %rsi,%r14
mov    %edi,%r15d
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
lea    (%r14,%rbp,1),%rsi
mov    %rbx,%rdx
sub    %rbp,%rdx
mov    %r15d,%edi
call   1030 <write@plt>
cmp    $0xffffffffffffffff,%rax
je     1170 <send_all+0x50>
add    %rax,%rbp
cmp    %rbx,%rax
jne    1140 <send_all+0x20>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe89(%rip),%rdi
call   1040 <perror@plt>
jmp    1160 <send_all+0x40>

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

LLVM IR (-O2):
@.str = [26 x i8] c"failed to write to client\00"
define i32 @send_all(i32 %0, i8* nocapture %1, i64 %2) {
br label %7
4: ; preds = %7
%5 = add i64 %11, %8
%6 = icmp eq i64 %11, %2
br i1 %6, label %15, label %7
7: ; preds = %3, %4
%8 = phi i64 [ %5, %4 ], [ 0, %3 ]
%9 = getelementptr i8, i8* %1, i64 %8
%10 = sub i64 %2, %8
%11 = tail call i64 @write(i32 %0, i8* %9, i64 %10)
%12 = icmp eq i64 %11, -1
br i1 %12, label %13, label %4
13: ; preds = %7
%14 = tail call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
br label %15
15: ; preds = %4, %13
ret i32 -1
}