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


x86 assembly (clang, -O0):
<serialwrite>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    114a <serialwrite+0x3a>
movl   $0x1,-0x4(%rbp)
jmp    1177 <serialwrite+0x67>
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)
cmpl   $0x0,-0x14(%rbp)
jg     1122 <serialwrite+0x12>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<serialwrite>:
push   %rbp
push   %r14
push   %rbx
mov    %edx,%ebp
mov    %rsi,%rbx
mov    %edi,%r14d
nopl   0x0(%rax)
mov    %r14d,%edi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1030 <write@plt>
test   %eax,%eax
js     113e <serialwrite+0x2e>
mov    %eax,%ecx
add    %rcx,%rbx
sub    %eax,%ebp
jg     1120 <serialwrite+0x10>
xor    %eax,%eax
jmp    1143 <serialwrite+0x33>
mov    $0x1,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
define i32 @serialwrite(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* %5
%11 = load i8*, i8** %6
%12 = load i32, i32* %7
%13 = call i32 @write(i32 %10, i8* %11, i32 %12)
store i32 %13, i32* %8
%14 = load i32, i32* %8
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %17
16: ; preds = %9
store i32 1, i32* %4
br label %29
17: ; preds = %9
%18 = load i32, i32* %8
%19 = load i32, i32* %7
%20 = sub nsw i32 %19, %18
store i32 %20, i32* %7
%21 = load i32, i32* %8
%22 = load i8*, i8** %6
%23 = sext i32 %21 to i64
%24 = getelementptr inbounds i8, i8* %22, i64 %23
store i8* %24, i8** %6
br label %25
25: ; preds = %17
%26 = load i32, i32* %7
%27 = icmp sgt i32 %26, 0
br i1 %27, label %9, label %28
28: ; preds = %25
store i32 0, i32* %4
br label %29
29: ; preds = %28, %16
%30 = load i32, i32* %4
ret i32 %30
}

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