#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 /*<<< orphan*/ perror(char *);
int write(int, unsigned char *, unsigned int);
int do_write(int fd, unsigned char *buffer, unsigned int size) {
  int n, from = 0;
  while (1) {
    n = write(fd, &buffer[from], size - from);
    if (n <= 0) {
      perror("write");
      return -1;
    }
    if (n + from == size)
      return 0;
    from += n;
  }
}


x86 assembly (clang, -O0):
<do_write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
movslq -0x1c(%rbp),%rax
add    %rax,%rsi
mov    -0x14(%rbp),%edx
sub    -0x1c(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jg     1177 <do_write+0x57>
lea    0xe9a(%rip),%rdi
call   1040 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11a0 <do_write+0x80>
mov    -0x18(%rbp),%eax
add    -0x1c(%rbp),%eax
cmp    -0x14(%rbp),%eax
jne    1192 <do_write+0x72>
movl   $0x0,-0x4(%rbp)
jmp    11a0 <do_write+0x80>
mov    -0x18(%rbp),%eax
add    -0x1c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
jmp    1139 <do_write+0x19>
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_write>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%ebx
mov    %rsi,%r14
mov    %edi,%r15d
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
movslq %ebp,%rax
lea    (%r14,%rax,1),%rsi
mov    %ebx,%edx
sub    %eax,%edx
mov    %r15d,%edi
call   1030 <write@plt>
test   %eax,%eax
jle    116a <do_write+0x4a>
add    %eax,%ebp
cmp    %ebx,%ebp
jne    1140 <do_write+0x20>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe8f(%rip),%rdi
call   1040 <perror@plt>
mov    $0xffffffff,%eax
jmp    115f <do_write+0x3f>

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

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