#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 WRITE(int sock, char *s, int len) {
  while (len > 0) {
    int rc = write(sock, s, len);
    if (rc <= 0)
      return -1;
    len -= rc;
    s += rc;
  }
  return 0;
}


x86 assembly (clang, -O0):
<WRITE>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jle    1175 <WRITE+0x65>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jg     1154 <WRITE+0x44>
movl   $0xffffffff,-0x4(%rbp)
jmp    117c <WRITE+0x6c>
mov    -0x18(%rbp),%ecx
mov    -0x14(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x14(%rbp)
mov    -0x18(%rbp),%ecx
mov    -0x10(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x10(%rbp)
jmp    1122 <WRITE+0x12>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<WRITE>:
push   %rbp
push   %r14
push   %rbx
mov    %edx,%ebx
mov    %rsi,%r14
mov    %edi,%ebp
nopl   0x0(%rax,%rax,1)
test   %ebx,%ebx
jle    1142 <WRITE+0x32>
mov    %ebp,%edi
mov    %r14,%rsi
mov    %ebx,%edx
call   1030 <write@plt>
sub    %eax,%ebx
cltq
add    %rax,%r14
test   %eax,%eax
jg     1120 <WRITE+0x10>
mov    $0xffffffff,%eax
jmp    1144 <WRITE+0x34>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

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

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