#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 */
scalar_t__ write(int, char const *, size_t);
int write_socket(int fd, const char *buf, int len) {
  int currentsize = 0;
  while (currentsize < len) {
    int count = (int)write(fd, buf + currentsize, (size_t)(len - currentsize));
    if (count < 0)
      return -1;
    currentsize += count;
  }
  return currentsize;
}


x86 assembly (clang, -O0):
<write_socket>:
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    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    1178 <write_socket+0x68>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
movslq -0x18(%rbp),%rax
add    %rax,%rsi
mov    -0x14(%rbp),%eax
sub    -0x18(%rbp),%eax
movslq %eax,%rdx
call   1030 <write@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    116a <write_socket+0x5a>
movl   $0xffffffff,-0x4(%rbp)
jmp    117e <write_socket+0x6e>
mov    -0x1c(%rbp),%eax
add    -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
jmp    1129 <write_socket+0x19>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_socket>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%ebp
mov    %rsi,%r14
mov    %edi,%r15d
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
cmp    %ebp,%ebx
jge    1154 <write_socket+0x44>
movslq %ebx,%rsi
add    %r14,%rsi
mov    %ebp,%eax
sub    %ebx,%eax
movslq %eax,%rdx
mov    %r15d,%edi
call   1030 <write@plt>
add    %eax,%ebx
test   %eax,%eax
jns    1130 <write_socket+0x20>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

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