#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*/ exit(int);
int /*<<< orphan*/ perror(char *);
int write(int, void *, int);
void out_w(int fd, void *buf, int len) {
  int n;
  while (len > 0) {
    n = write(fd, buf, len);
    if (n <= 0) {
      perror("write");
      exit(1);
    }
    buf += n;
    len -= n;
  }
  return;
}


x86 assembly (clang, -O0):
<out_w>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jle    119f <out_w+0x6f>
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jg     117e <out_w+0x4e>
lea    0xe91(%rip),%rdi
call   1040 <perror@plt>
mov    $0x1,%edi
call   1050 <exit@plt>
mov    -0x18(%rbp),%ecx
mov    -0x10(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x10(%rbp)
mov    -0x18(%rbp),%ecx
mov    -0x14(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x14(%rbp)
jmp    1142 <out_w+0x12>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<out_w>:
push   %rbp
push   %r14
push   %rbx
test   %edx,%edx
jle    115a <out_w+0x2a>
mov    %edx,%ebp
mov    %rsi,%rbx
mov    %edi,%r14d
mov    %r14d,%edi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1030 <write@plt>
test   %eax,%eax
jle    115f <out_w+0x2f>
mov    %eax,%ecx
add    %rcx,%rbx
sub    %eax,%ebp
jg     1140 <out_w+0x10>
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xe9a(%rip),%rdi
call   1040 <perror@plt>
mov    $0x1,%edi
call   1050 <exit@plt>

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

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