#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 size_t ssize_t;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ assert(int /*<<< orphan*/);
scalar_t__ char_entity(char const **, size_t *, char const);
int /*<<< orphan*/ fputc(char const, int /*<<< orphan*/ *);
int /*<<< orphan*/ fputs(char const *, int /*<<< orphan*/ *);
ssize_t few_write(void *cookie, char const *buffer, size_t bytes) {
  FILE *realout = cookie;
  size_t size = bytes;
  char const *replace;
  size_t repsize;
  assert(buffer != NULL);
  while (bytes) {
    if (char_entity(&replace, &repsize, *buffer))
      fputs(replace, realout);
    else
      fputc(*buffer, realout);
    buffer++;
    bytes--;
  }
  return size;
}


x86 assembly (clang, -O0):
<few_write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x10(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
cmpq   $0x0,-0x18(%rbp)
je     11e0 <few_write+0xa0>
mov    -0x10(%rbp),%rax
lea    -0x30(%rbp),%rdi
lea    -0x38(%rbp),%rsi
movsbl (%rax),%edx
call   1060 <char_entity@plt>
cmp    $0x0,%rax
je     11b1 <few_write+0x71>
mov    -0x30(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1030 <fputs@plt>
jmp    11c3 <few_write+0x83>
mov    -0x10(%rbp),%rax
mov    (%rax),%al
mov    -0x20(%rbp),%rsi
movsbl %al,%edi
call   1040 <fputc@plt>
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
mov    -0x18(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x18(%rbp)
jmp    1176 <few_write+0x36>
mov    -0x28(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<few_write>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdx,%r14
mov    %rsi,%rbx
mov    %rdi,%r15
xor    %edi,%edi
test   %rsi,%rsi
setne  %dil
call   1050 <assert@plt>
test   %r14,%r14
je     11b8 <few_write+0x78>
xor    %ebp,%ebp
lea    0x8(%rsp),%r12
lea    0x10(%rsp),%r13
jmp    1195 <few_write+0x55>
nopl   0x0(%rax,%rax,1)
movsbl (%rbx,%rbp,1),%edi
mov    %r15,%rsi
call   1040 <fputc@plt>
add    $0x1,%rbp
cmp    %rbp,%r14
je     11b8 <few_write+0x78>
movsbl (%rbx,%rbp,1),%edx
mov    %r12,%rdi
mov    %r13,%rsi
call   1060 <char_entity@plt>
test   %rax,%rax
je     1180 <few_write+0x40>
mov    0x8(%rsp),%rdi
mov    %r15,%rsi
call   1030 <fputs@plt>
jmp    118c <few_write+0x4c>
mov    %r14,%rax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i64 @few_write(i8* %0, i8* %1, i64 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i32*
%8 = alloca i64
%9 = alloca i8*
%10 = alloca i64
store i8* %0, i8** %4
store i8* %1, i8** %5
store i64 %2, i64* %6
%11 = load i8*, i8** %4
%12 = bitcast i8* %11 to i32*
store i32* %12, i32** %7
%13 = load i64, i64* %6
store i64 %13, i64* %8
%14 = load i8*, i8** %5
%15 = icmp ne i8* %14, null
%16 = zext i1 %15 to i32
%17 = call i32 @assert(i32 %16)
br label %18
18: ; preds = %35, %3
%19 = load i64, i64* %6
%20 = icmp ne i64 %19, 0
br i1 %20, label %21, label %40
21: ; preds = %18
%22 = load i8*, i8** %5
%23 = load i8, i8* %22
%24 = call i64 @char_entity(i8** %9, i64* %10, i8 signext %23)
%25 = icmp ne i64 %24, 0
br i1 %25, label %26, label %30
26: ; preds = %21
%27 = load i8*, i8** %9
%28 = load i32*, i32** %7
%29 = call i32 @fputs(i8* %27, i32* %28)
br label %35
30: ; preds = %21
%31 = load i8*, i8** %5
%32 = load i8, i8* %31
%33 = load i32*, i32** %7
%34 = call i32 @fputc(i8 signext %32, i32* %33)
br label %35
35: ; preds = %30, %26
%36 = load i8*, i8** %5
%37 = getelementptr inbounds i8, i8* %36, i32 1
store i8* %37, i8** %5
%38 = load i64, i64* %6
%39 = add i64 %38, -1
store i64 %39, i64* %6
br label %18
40: ; preds = %18
%41 = load i64, i64* %8
ret i64 %41
}

LLVM IR (-O2):
define i64 @few_write(i8* nocapture %0, i8* %1, i64 returned %2) {
%4 = alloca i8*
%5 = alloca i64
%6 = bitcast i8* %0 to i32*
%7 = bitcast i8** %4 to i8*
%8 = bitcast i64* %5 to i8*
%9 = icmp ne i8* %1, null
%10 = zext i1 %9 to i32
%11 = tail call i32 @assert(i32 %10)
%12 = icmp eq i64 %2, 0
br i1 %12, label %29, label %13
13: ; preds = %3, %25
%14 = phi i8* [ %26, %25 ], [ %1, %3 ]
%15 = phi i64 [ %27, %25 ], [ %2, %3 ]
%16 = load i8, i8* %14
%17 = call i64 @char_entity(i8** %4, i64* %5, i8 signext %16)
%18 = icmp eq i64 %17, 0
br i1 %18, label %22, label %19
19: ; preds = %13
%20 = load i8*, i8** %4
%21 = call i32 @fputs(i8* %20, i32* %6)
br label %25
22: ; preds = %13
%23 = load i8, i8* %14
%24 = call i32 @fputc(i8 signext %23, i32* %6)
br label %25
25: ; preds = %22, %19
%26 = getelementptr inbounds i8, i8* %14, i64 1
%27 = add i64 %15, -1
%28 = icmp eq i64 %27, 0
br i1 %28, label %29, label %13
29: ; preds = %25, %3
ret i64 %2
}