#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*/ Log(char *);
int /*<<< orphan*/ LogErrno(char *);
scalar_t__ write(int, char const *, size_t);
size_t WriteChars(int fd, const char *buf, size_t n) {
  size_t total = 0;
  while (total < n) {
    ssize_t got = write(fd, buf + total, n - total);
    if (got < 0) {
      LogErrno("write");
      return 0;
    }
    if (got == 0) {
      Log("write: could not write anything, send buffer full");
      return 0;
    }
    if ((size_t)got > n - total) {
      Log("write: overlong write (should never happen)");
    }
    total += got;
  }
  return total;
}


x86 assembly (clang, -O0):
<WriteChars>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0xc(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x20(%rbp),%rax
jae    11ef <WriteChars+0xbf>
mov    -0xc(%rbp),%edi
mov    -0x18(%rbp),%rsi
add    -0x28(%rbp),%rsi
mov    -0x20(%rbp),%rdx
sub    -0x28(%rbp),%rdx
call   1030 <write@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jge    1199 <WriteChars+0x69>
lea    0xe79(%rip),%rdi
call   1040 <LogErrno@plt>
movq   $0x0,-0x8(%rbp)
jmp    11f7 <WriteChars+0xc7>
cmpq   $0x0,-0x30(%rbp)
jne    11bd <WriteChars+0x8d>
lea    0xe5b(%rip),%rdi
call   1050 <Log@plt>
movq   $0x0,-0x8(%rbp)
jmp    11f7 <WriteChars+0xc7>
mov    -0x30(%rbp),%rax
mov    -0x20(%rbp),%rcx
sub    -0x28(%rbp),%rcx
cmp    %rcx,%rax
jbe    11de <WriteChars+0xae>
lea    0xe5f(%rip),%rdi
call   1050 <Log@plt>
mov    -0x30(%rbp),%rax
add    -0x28(%rbp),%rax
mov    %rax,-0x28(%rbp)
jmp    114b <WriteChars+0x1b>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<WriteChars>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
test   %rdx,%rdx
je     11a4 <WriteChars+0x74>
mov    %rdx,%r12
mov    %rsi,%r15
mov    %edi,%r13d
xor    %ebx,%ebx
jmp    1158 <WriteChars+0x28>
nopl   (%rax)
add    %rbp,%rbx
cmp    %r12,%rbx
jae    11a6 <WriteChars+0x76>
lea    (%r15,%rbx,1),%rsi
mov    %r12,%r14
sub    %rbx,%r14
mov    %r13d,%edi
mov    %r14,%rdx
call   1030 <write@plt>
test   %rax,%rax
js     118a <WriteChars+0x5a>
je     1198 <WriteChars+0x68>
mov    %rax,%rbp
cmp    %r14,%rax
jbe    1150 <WriteChars+0x20>
lea    0xeb5(%rip),%rdi
call   1050 <Log@plt>
jmp    1150 <WriteChars+0x20>
lea    0xe6f(%rip),%rdi
call   1040 <LogErrno@plt>
jmp    11a4 <WriteChars+0x74>
lea    0xe67(%rip),%rdi
call   1050 <Log@plt>
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"
@.str.1 = [50 x i8] c"write: could not write anything, send buffer full\00"
@.str.2 = [44 x i8] c"write: overlong write (should never happen)\00"
define i64 @WriteChars(i32 %0, i8* %1, i64 %2) {
%4 = alloca i64
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i64
%9 = alloca i64
store i32 %0, i32* %5
store i8* %1, i8** %6
store i64 %2, i64* %7
store i64 0, i64* %8
br label %10
10: ; preds = %40, %3
%11 = load i64, i64* %8
%12 = load i64, i64* %7
%13 = icmp ult i64 %11, %12
br i1 %13, label %14, label %44
14: ; preds = %10
%15 = load i32, i32* %5
%16 = load i8*, i8** %6
%17 = load i64, i64* %8
%18 = getelementptr inbounds i8, i8* %16, i64 %17
%19 = load i64, i64* %7
%20 = load i64, i64* %8
%21 = sub i64 %19, %20
%22 = call i64 @write(i32 %15, i8* %18, i64 %21)
store i64 %22, i64* %9
%23 = load i64, i64* %9
%24 = icmp slt i64 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %14
%26 = call i32 @LogErrno(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
store i64 0, i64* %4
br label %46
27: ; preds = %14
%28 = load i64, i64* %9
%29 = icmp eq i64 %28, 0
br i1 %29, label %30, label %32
30: ; preds = %27
%31 = call i32 @Log(i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.1, i64 0, i64 0))
store i64 0, i64* %4
br label %46
32: ; preds = %27
%33 = load i64, i64* %9
%34 = load i64, i64* %7
%35 = load i64, i64* %8
%36 = sub i64 %34, %35
%37 = icmp ugt i64 %33, %36
br i1 %37, label %38, label %40
38: ; preds = %32
%39 = call i32 @Log(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.2, i64 0, i64 0))
br label %40
40: ; preds = %38, %32
%41 = load i64, i64* %9
%42 = load i64, i64* %8
%43 = add i64 %42, %41
store i64 %43, i64* %8
br label %10
44: ; preds = %10
%45 = load i64, i64* %8
store i64 %45, i64* %4
br label %46
46: ; preds = %44, %30, %25
%47 = load i64, i64* %4
ret i64 %47
}

LLVM IR (-O2):
@.str = [6 x i8] c"write\00"
@.str.1 = [50 x i8] c"write: could not write anything, send buffer full\00"
@.str.2 = [44 x i8] c"write: overlong write (should never happen)\00"
define i64 @WriteChars(i32 %0, i8* nocapture %1, i64 %2) {
%4 = icmp eq i64 %2, 0
br i1 %4, label %24, label %5
5: ; preds = %3, %21
%6 = phi i64 [ %22, %21 ], [ 0, %3 ]
%7 = getelementptr inbounds i8, i8* %1, i64 %6
%8 = sub i64 %2, %6
%9 = tail call i64 @write(i32 %0, i8* %7, i64 %8)
%10 = icmp slt i64 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %5
%12 = tail call i32 @LogErrno(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
br label %24
13: ; preds = %5
%14 = icmp eq i64 %9, 0
br i1 %14, label %15, label %17
15: ; preds = %13
%16 = tail call i32 @Log(i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.1, i64 0, i64 0))
br label %24
17: ; preds = %13
%18 = icmp ugt i64 %9, %8
br i1 %18, label %19, label %21
19: ; preds = %17
%20 = tail call i32 @Log(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.2, i64 0, i64 0))
br label %21
21: ; preds = %17, %19
%22 = add i64 %9, %6
%23 = icmp ult i64 %22, %2
br i1 %23, label %5, label %24
24: ; preds = %21, %3, %15, %11
%25 = phi i64 [ 0, %11 ], [ 0, %15 ], [ 0, %3 ], [ %22, %21 ]
ret i64 %25
}