#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 socket_write(int fd, char *buf, int length) {
  while (length) {
    int r = write(fd, buf, length);
    if (r == 0)
      return -1;
    if (r < 0)
      return r;
    length -= r;
    buf += r;
  }
  return 0;
}


x86 assembly (clang, -O0):
<socket_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)
je     118a <socket_write+0x7a>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jne    1154 <socket_write+0x44>
movl   $0xffffffff,-0x4(%rbp)
jmp    1191 <socket_write+0x81>
cmpl   $0x0,-0x18(%rbp)
jge    1169 <socket_write+0x59>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1191 <socket_write+0x81>
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 <socket_write+0x12>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<socket_write>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
xor    %r14d,%r14d
test   %edx,%edx
je     1159 <socket_write+0x49>
mov    %edx,%ebp
mov    %rsi,%rbx
mov    %edi,%r15d
cs nopw 0x0(%rax,%rax,1)
mov    %r15d,%edi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1030 <write@plt>
test   %eax,%eax
je     114e <socket_write+0x3e>
js     1156 <socket_write+0x46>
mov    %eax,%ecx
add    %rcx,%rbx
sub    %eax,%ebp
jne    1130 <socket_write+0x20>
jmp    1159 <socket_write+0x49>
mov    $0xffffffff,%r14d
jmp    1159 <socket_write+0x49>
mov    %eax,%r14d
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

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