#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*/ dprintf(int, char *, int, int, ...);
scalar_t__ errno;
int write(int, char const *, int);
int WriteToDestination(int fd, const char *pchBuffer, int cch) {
  int cchWritten;
  if (fd < 0)
    return 0;
  dprintf(0x0002, "outputting content to fd %d: %d byte(s)", fd, cch);
  do {
    errno = 0;
    cchWritten = write(fd, pchBuffer, cch);
    dprintf(0x0002, "%d from %d byte(s) written (errno=%d)", cchWritten, cch,
            errno);
    if (cchWritten > 0) {
      cch -= cchWritten;
      pchBuffer += cchWritten;
    }
  } while (cchWritten >= 0 && cch > 0);
  return (cchWritten > 0) ? 0 : -1;
}


x86 assembly (clang, -O0):
<WriteToDestination>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    1148 <WriteToDestination+0x28>
movl   $0x0,-0x4(%rbp)
jmp    1206 <WriteToDestination+0xe6>
mov    -0x8(%rbp),%edx
mov    -0x14(%rbp),%ecx
mov    $0x2,%edi
lea    0xea6(%rip),%rsi
mov    $0x0,%al
call   1040 <dprintf@plt>
mov    0x2e78(%rip),%rax
movq   $0x0,(%rax)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%edx
mov    -0x14(%rbp),%ecx
mov    0x2e52(%rip),%rax
mov    (%rax),%r8
mov    $0x2,%edi
lea    0xe8b(%rip),%rsi
mov    $0x0,%al
call   1040 <dprintf@plt>
cmpl   $0x0,-0x18(%rbp)
jle    11ca <WriteToDestination+0xaa>
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)
jmp    11cf <WriteToDestination+0xaf>
xor    %eax,%eax
cmpl   $0x0,-0x18(%rbp)
mov    %al,-0x19(%rbp)
jl     11e8 <WriteToDestination+0xc8>
cmpl   $0x0,-0x14(%rbp)
setg   %al
mov    %al,-0x19(%rbp)
mov    -0x19(%rbp),%al
test   $0x1,%al
jne    1161 <WriteToDestination+0x41>
mov    -0x18(%rbp),%edx
mov    $0xffffffff,%eax
xor    %ecx,%ecx
cmp    $0x0,%edx
cmovg  %ecx,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<WriteToDestination>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
test   %edi,%edi
js     11ae <WriteToDestination+0x8e>
mov    %edx,%ebx
mov    %rsi,%r14
mov    %edi,%r12d
lea    0xec2(%rip),%rsi
mov    $0x2,%edi
mov    %r12d,%edx
mov    %ebx,%ecx
xor    %eax,%eax
call   1040 <dprintf@plt>
mov    0x2e8a(%rip),%r13
lea    0xecb(%rip),%r15
nopl   (%rax)
movq   $0x0,0x0(%r13)
mov    %r12d,%edi
mov    %r14,%rsi
mov    %ebx,%edx
call   1030 <write@plt>
mov    %eax,%ebp
mov    0x0(%r13),%r8
mov    $0x2,%edi
mov    %r15,%rsi
mov    %eax,%edx
mov    %ebx,%ecx
xor    %eax,%eax
call   1040 <dprintf@plt>
mov    $0x0,%eax
test   %ebp,%ebp
cmovns %ebp,%eax
js     11a3 <WriteToDestination+0x83>
add    %rax,%r14
sub    %eax,%ebx
test   %ebx,%ebx
jg     1160 <WriteToDestination+0x40>
xor    %eax,%eax
test   %ebp,%ebp
setle  %al
neg    %eax
jmp    11b0 <WriteToDestination+0x90>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [40 x i8] c"outputting content to fd %d: %d byte(s)\00"
@errno = global i64 0
@.str.1 = [38 x i8] c"%d from %d byte(s) written (errno=%d)\00"
define i32 @WriteToDestination(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
%9 = load i32, i32* %5
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %12
11: ; preds = %3
store i32 0, i32* %4
br label %49
12: ; preds = %3
%13 = load i32, i32* %5
%14 = load i32, i32* %7
%15 = call i32 (i32, i8*, i32, i32, ...) @dprintf(i32 2, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0), i32 %13, i32 %14)
br label %16
16: ; preds = %42, %12
store i64 0, i64* @errno
%17 = load i32, i32* %5
%18 = load i8*, i8** %6
%19 = load i32, i32* %7
%20 = call i32 @write(i32 %17, i8* %18, i32 %19)
store i32 %20, i32* %8
%21 = load i32, i32* %8
%22 = load i32, i32* %7
%23 = load i64, i64* @errno
%24 = call i32 (i32, i8*, i32, i32, ...) @dprintf(i32 2, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.1, i64 0, i64 0), i32 %21, i32 %22, i64 %23)
%25 = load i32, i32* %8
%26 = icmp sgt i32 %25, 0
br i1 %26, label %27, label %35
27: ; preds = %16
%28 = load i32, i32* %8
%29 = load i32, i32* %7
%30 = sub nsw i32 %29, %28
store i32 %30, i32* %7
%31 = load i32, i32* %8
%32 = load i8*, i8** %6
%33 = sext i32 %31 to i64
%34 = getelementptr inbounds i8, i8* %32, i64 %33
store i8* %34, i8** %6
br label %35
35: ; preds = %27, %16
br label %36
36: ; preds = %35
%37 = load i32, i32* %8
%38 = icmp sge i32 %37, 0
br i1 %38, label %39, label %42
39: ; preds = %36
%40 = load i32, i32* %7
%41 = icmp sgt i32 %40, 0
br label %42
42: ; preds = %39, %36
%43 = phi i1 [ false, %36 ], [ %41, %39 ]
br i1 %43, label %16, label %44
44: ; preds = %42
%45 = load i32, i32* %8
%46 = icmp sgt i32 %45, 0
%47 = zext i1 %46 to i64
%48 = select i1 %46, i32 0, i32 -1
store i32 %48, i32* %4
br label %49
49: ; preds = %44, %11
%50 = load i32, i32* %4
ret i32 %50
}

LLVM IR (-O2):
@.str = [40 x i8] c"outputting content to fd %d: %d byte(s)\00"
@errno = global i64 0
@.str.1 = [38 x i8] c"%d from %d byte(s) written (errno=%d)\00"
define i32 @WriteToDestination(i32 %0, i8* nocapture %1, i32 %2) {
%4 = icmp slt i32 %0, 0
br i1 %4, label %24, label %5
5: ; preds = %3
%6 = tail call i32 (i32, i8*, i32, i32, ...) @dprintf(i32 2, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0), i32 %0, i32 %2)
br label %7
7: ; preds = %7, %5
%8 = phi i8* [ %1, %5 ], [ %16, %7 ]
%9 = phi i32 [ %2, %5 ], [ %17, %7 ]
store i64 0, i64* @errno
%10 = tail call i32 @write(i32 %0, i8* %8, i32 %9)
%11 = load i64, i64* @errno
%12 = tail call i32 (i32, i8*, i32, i32, ...) @dprintf(i32 2, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.1, i64 0, i64 0), i32 %10, i32 %9, i64 %11)
%13 = icmp sgt i32 %10, 0
%14 = select i1 %13, i32 %10, i32 0
%15 = zext i32 %14 to i64
%16 = getelementptr i8, i8* %8, i64 %15
%17 = sub nsw i32 %9, %14
%18 = icmp sgt i32 %10, -1
%19 = icmp sgt i32 %17, 0
%20 = select i1 %18, i1 %19, i1 false
br i1 %20, label %7, label %21
21: ; preds = %7
%22 = xor i1 %13, true
%23 = sext i1 %22 to i32
br label %24
24: ; preds = %3, %21
%25 = phi i32 [ %23, %21 ], [ 0, %3 ]
ret i32 %25
}