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

/* Variables and functions */
int /*<<< orphan*/ die(char *);
scalar_t__ write(int, void *, scalar_t__);
ssize_t write_n(int fd, ssize_t n, void *buf) {
  ssize_t wr = 0;
  while (wr < n) {
    ssize_t w = write(fd, buf + wr, n - wr);
    if (w == -1)
      die("write");
    wr += w;
  }
  return wr;
}


x86 assembly (clang, -O0):
<write_n>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%rax
cmp    -0x10(%rbp),%rax
jge    118d <write_n+0x6d>
mov    -0x4(%rbp),%edi
mov    -0x18(%rbp),%rsi
add    -0x20(%rbp),%rsi
mov    -0x10(%rbp),%rdx
sub    -0x20(%rbp),%rdx
call   1030 <write@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0xffffffffffffffff,-0x28(%rbp)
jne    117c <write_n+0x5c>
lea    0xe89(%rip),%rdi
call   1040 <die@plt>
mov    -0x28(%rbp),%rax
add    -0x20(%rbp),%rax
mov    %rax,-0x20(%rbp)
jmp    113b <write_n+0x1b>
mov    -0x20(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_n>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
test   %rsi,%rsi
jle    117d <write_n+0x5d>
mov    %rdx,%r14
mov    %rsi,%r15
mov    %edi,%r13d
xor    %ebx,%ebx
lea    0xebe(%rip),%r12
jmp    1158 <write_n+0x38>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
add    %rbp,%rbx
cmp    %r15,%rbx
jge    117f <write_n+0x5f>
lea    (%r14,%rbx,1),%rsi
mov    %r15,%rdx
sub    %rbx,%rdx
mov    %r13d,%edi
call   1030 <write@plt>
mov    %rax,%rbp
cmp    $0xffffffffffffffff,%rax
jne    1150 <write_n+0x30>
mov    %r12,%rdi
call   1040 <die@plt>
jmp    1150 <write_n+0x30>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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