#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 */
typedef int /*<<< orphan*/ uint8_t;
typedef scalar_t__ ssize_t;

/* Variables and functions */
scalar_t__ write(int, int /*<<< orphan*/ *, size_t);
int loopWrite(int sock, uint8_t *data, size_t len) {
  for (size_t i = 0; i < len; i++) {
    ssize_t x = write(sock, data + i, len - i);
    if (x < 0) {
      return -5;
    }
    i += (size_t)x;
  }
  return 0;
}


x86 assembly (clang, -O0):
<loopWrite>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%rax
cmp    -0x18(%rbp),%rax
jae    1190 <loopWrite+0x80>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x20(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rsi
mov    -0x18(%rbp),%rdx
sub    -0x20(%rbp),%rdx
call   1030 <write@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jge    1173 <loopWrite+0x63>
movl   $0xfffffffb,-0x4(%rbp)
jmp    1197 <loopWrite+0x87>
mov    -0x28(%rbp),%rax
add    -0x20(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    112b <loopWrite+0x1b>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<loopWrite>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
test   %rdx,%rdx
je     1153 <loopWrite+0x43>
mov    %rdx,%rbx
mov    %rsi,%r14
mov    %edi,%r15d
xor    %ebp,%ebp
nopw   0x0(%rax,%rax,1)
lea    (%r14,%rbp,4),%rsi
mov    %rbx,%rdx
sub    %rbp,%rdx
mov    %r15d,%edi
call   1030 <write@plt>
test   %rax,%rax
js     1157 <loopWrite+0x47>
add    %rax,%rbp
add    $0x1,%rbp
cmp    %rbx,%rbp
jb     1130 <loopWrite+0x20>
xor    %eax,%eax
jmp    115c <loopWrite+0x4c>
mov    $0xfffffffb,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @loopWrite(i32 %0, i32* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32*
%7 = alloca i64
%8 = alloca i64
%9 = alloca i64
store i32 %0, i32* %5
store i32* %1, i32** %6
store i64 %2, i64* %7
store i64 0, i64* %8
br label %10
10: ; preds = %30, %3
%11 = load i64, i64* %8
%12 = load i64, i64* %7
%13 = icmp ult i64 %11, %12
br i1 %13, label %14, label %33
14: ; preds = %10
%15 = load i32, i32* %5
%16 = load i32*, i32** %6
%17 = load i64, i64* %8
%18 = getelementptr inbounds i32, i32* %16, i64 %17
%19 = load i64, i64* %7
%20 = load i64, i64* %8
%21 = sub i64 %19, %20
%22 = call i64 @write(i32 %15, i32* %18, i64 %21)
store i64 %22, i64* %9
%23 = load i64, i64* %9
%24 = icmp slt i64 %23, 0
br i1 %24, label %25, label %26
25: ; preds = %14
store i32 -5, i32* %4
br label %34
26: ; preds = %14
%27 = load i64, i64* %9
%28 = load i64, i64* %8
%29 = add i64 %28, %27
store i64 %29, i64* %8
br label %30
30: ; preds = %26
%31 = load i64, i64* %8
%32 = add i64 %31, 1
store i64 %32, i64* %8
br label %10
33: ; preds = %10
store i32 0, i32* %4
br label %34
34: ; preds = %33, %25
%35 = load i32, i32* %4
ret i32 %35
}

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