#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 write(int, int /*<<< orphan*/ *, int);
int lowlevel_write(int fd, uint8_t *data, int len) {
  int n_written;
  int total_written = 0;
  while (len > 0) {
    n_written = write(fd, data, len);
    len -= n_written;
    data += n_written;
    total_written += n_written;
  }
  return total_written;
}


x86 assembly (clang, -O0):
<lowlevel_write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x0,-0x1c(%rbp)
cmpl   $0x0,-0x14(%rbp)
jle    1173 <lowlevel_write+0x63>
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x18(%rbp)
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
shl    $0x2,%rcx
add    %rcx,%rax
mov    %rax,-0x10(%rbp)
mov    -0x18(%rbp),%eax
add    -0x1c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
jmp    1129 <lowlevel_write+0x19>
mov    -0x1c(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<lowlevel_write>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
test   %edx,%edx
jle    114d <lowlevel_write+0x3d>
mov    %edx,%ebp
mov    %rsi,%rbx
mov    %edi,%r15d
xor    %r14d,%r14d
cs nopw 0x0(%rax,%rax,1)
mov    %r15d,%edi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1030 <write@plt>
add    %eax,%r14d
movslq %eax,%rcx
lea    (%rbx,%rcx,4),%rbx
sub    %eax,%ebp
jg     1130 <lowlevel_write+0x20>
jmp    1150 <lowlevel_write+0x40>
xor    %r14d,%r14d
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

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