#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*/ errquit(char *);
int write(int, char *, int);
int writen(int fd, char *buf, int numwrite) {
  int nwrite;
  int nleft = numwrite;
  char *ptr = buf;
  while (nleft) {
    nwrite = write(fd, ptr, nleft);
    if (nwrite == -1)
      errquit("write");
    else if (nwrite == 0)
      break;
    nleft -= nwrite;
    ptr += nwrite;
  }
  return numwrite - nleft;
}


x86 assembly (clang, -O0):
<writen>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
cmpl   $0x0,-0x1c(%rbp)
je     11ac <writen+0x8c>
mov    -0x4(%rbp),%edi
mov    -0x28(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    1177 <writen+0x57>
lea    0xe93(%rip),%rdi
call   1040 <errquit@plt>
jmp    118b <writen+0x6b>
cmpl   $0x0,-0x18(%rbp)
jne    1186 <writen+0x66>
jmp    11ac <writen+0x8c>
jmp    118b <writen+0x6b>
mov    -0x18(%rbp),%ecx
mov    -0x1c(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%ecx
mov    -0x28(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x28(%rbp)
jmp    1140 <writen+0x20>
mov    -0x14(%rbp),%eax
sub    -0x1c(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<writen>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edx,%r14d
xor    %ebp,%ebp
test   %edx,%edx
je     1181 <writen+0x61>
mov    %rsi,%rbx
mov    %edi,%r12d
lea    0xebf(%rip),%r15
mov    %r14d,%ebp
jmp    1163 <writen+0x43>
cs nopw 0x0(%rax,%rax,1)
mov    %r15,%rdi
call   1040 <errquit@plt>
movslq %r13d,%rax
add    %rax,%rbx
sub    %r13d,%ebp
je     117f <writen+0x5f>
mov    %r12d,%edi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1030 <write@plt>
mov    %eax,%r13d
cmp    $0xffffffff,%eax
je     1150 <writen+0x30>
test   %r13d,%r13d
jne    1158 <writen+0x38>
jmp    1181 <writen+0x61>
xor    %ebp,%ebp
sub    %ebp,%r14d
mov    %r14d,%eax
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 i32 @writen(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8*
store i32 %0, i32* %4
store i8* %1, i8** %5
store i32 %2, i32* %6
%10 = load i32, i32* %6
store i32 %10, i32* %8
%11 = load i8*, i8** %5
store i8* %11, i8** %9
br label %12
12: ; preds = %29, %3
%13 = load i32, i32* %8
%14 = icmp ne i32 %13, 0
br i1 %14, label %15, label %37
15: ; preds = %12
%16 = load i32, i32* %4
%17 = load i8*, i8** %9
%18 = load i32, i32* %8
%19 = call i32 @write(i32 %16, i8* %17, i32 %18)
store i32 %19, i32* %7
%20 = load i32, i32* %7
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %24
22: ; preds = %15
%23 = call i32 @errquit(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
br label %29
24: ; preds = %15
%25 = load i32, i32* %7
%26 = icmp eq i32 %25, 0
br i1 %26, label %27, label %28
27: ; preds = %24
br label %37
28: ; preds = %24
br label %29
29: ; preds = %28, %22
%30 = load i32, i32* %7
%31 = load i32, i32* %8
%32 = sub nsw i32 %31, %30
store i32 %32, i32* %8
%33 = load i32, i32* %7
%34 = load i8*, i8** %9
%35 = sext i32 %33 to i64
%36 = getelementptr inbounds i8, i8* %34, i64 %35
store i8* %36, i8** %9
br label %12
37: ; preds = %27, %12
%38 = load i32, i32* %6
%39 = load i32, i32* %8
%40 = sub nsw i32 %38, %39
ret i32 %40
}

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