#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;

/* Variables and functions */
int /*<<< orphan*/ perror(char *);
int write(int, int /*<<< orphan*/ *, int);
int elCommWrite(int fd, uint8_t *data, int len) {
  int rv;
  int length = len;
  int totalsent = 0;
  while (totalsent < length) {
    rv = write(fd, data + totalsent, length);
    if (rv < 0)
      perror("write(): error writing - trying again - ");
    else
      totalsent += rv;
  }
  return rv;
}


x86 assembly (clang, -O0):
<elCommWrite>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    1191 <elCommWrite+0x71>
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
movslq -0x20(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rsi
mov    -0x1c(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    1183 <elCommWrite+0x63>
lea    0xe87(%rip),%rdi
call   1040 <perror@plt>
jmp    118c <elCommWrite+0x6c>
mov    -0x18(%rbp),%eax
add    -0x20(%rbp),%eax
mov    %eax,-0x20(%rbp)
jmp    113f <elCommWrite+0x1f>
mov    -0x18(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<elCommWrite>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
test   %edx,%edx
jle    1180 <elCommWrite+0x60>
mov    %edx,%r13d
mov    %rsi,%r14
mov    %edi,%r12d
xor    %ebx,%ebx
lea    0xebf(%rip),%r15
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
movslq %ebx,%rax
lea    (%r14,%rax,4),%rsi
mov    %r12d,%edi
mov    %r13d,%edx
call   1030 <write@plt>
mov    %eax,%ebp
test   %eax,%eax
js     1171 <elCommWrite+0x51>
add    %ebp,%ebx
cmp    %r13d,%ebx
jl     1150 <elCommWrite+0x30>
jmp    1180 <elCommWrite+0x60>
mov    %r15,%rdi
call   1040 <perror@plt>
cmp    %r13d,%ebx
jl     1150 <elCommWrite+0x30>
jmp    1180 <elCommWrite+0x60>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [41 x i8] c"write(): error writing - trying again - \00"
define i32 @elCommWrite(i32 %0, i32* nocapture %1, i32 %2) {
%4 = icmp sgt i32 %2, 0
br i1 %4, label %5, label %18
5: ; preds = %3, %15
%6 = phi i32 [ %16, %15 ], [ 0, %3 ]
%7 = sext i32 %6 to i64
%8 = getelementptr inbounds i32, i32* %1, i64 %7
%9 = tail call i32 @write(i32 %0, i32* %8, i32 %2)
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %5
%12 = tail call i32 @perror(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0))
br label %15
13: ; preds = %5
%14 = add nsw i32 %9, %6
br label %15
15: ; preds = %13, %11
%16 = phi i32 [ %6, %11 ], [ %14, %13 ]
%17 = icmp slt i32 %16, %2
br i1 %17, label %5, label %18
18: ; preds = %15, %3
%19 = phi i32 [ undef, %3 ], [ %9, %15 ]
ret i32 %19
}