#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 */
char *memchr(char const *, int, size_t);
size_t count_chr(const void *buf, int c, size_t len) {
  size_t n = 0;
  const char *pbuf = buf;
  const char *orig_buf = buf;
  const char *next;
  for (;;) {
    next = memchr(pbuf, c, len - (pbuf - orig_buf));
    if (next == NULL) {
      break;
    }
    n++;
    pbuf = next;
    pbuf++;
  }
  return n;
}


x86 assembly (clang, -O0):
<count_chr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0xc(%rbp),%esi
mov    -0x18(%rbp),%rdx
mov    -0x28(%rbp),%rax
mov    -0x30(%rbp),%rcx
sub    %rcx,%rax
sub    %rax,%rdx
call   1030 <memchr@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    116d <count_chr+0x5d>
jmp    1192 <count_chr+0x82>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
mov    -0x38(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
jmp    113b <count_chr+0x2b>
mov    -0x20(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<count_chr>:
push   %rbp
push   %r14
push   %rbx
mov    %rdx,%rbp
mov    %esi,%r14d
mov    %rdi,%rbx
call   1030 <memchr@plt>
test   %rax,%rax
je     1150 <count_chr+0x40>
add    %rbp,%rbx
xor    %ebp,%ebp
nopl   0x0(%rax)
add    $0x1,%rbp
add    $0x1,%rax
mov    %rbx,%rdx
sub    %rax,%rdx
mov    %rax,%rdi
mov    %r14d,%esi
call   1030 <memchr@plt>
test   %rax,%rax
jne    1130 <count_chr+0x20>
jmp    1152 <count_chr+0x42>
xor    %ebp,%ebp
mov    %rbp,%rax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
define i64 @count_chr(i8* %0, i32 %1, i64 %2) {
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i64
%7 = alloca i64
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i8*
store i8* %0, i8** %4
store i32 %1, i32* %5
store i64 %2, i64* %6
store i64 0, i64* %7
%11 = load i8*, i8** %4
store i8* %11, i8** %8
%12 = load i8*, i8** %4
store i8* %12, i8** %9
br label %13
13: ; preds = %27, %3
%14 = load i8*, i8** %8
%15 = load i32, i32* %5
%16 = load i64, i64* %6
%17 = load i8*, i8** %8
%18 = load i8*, i8** %9
%19 = ptrtoint i8* %17 to i64
%20 = ptrtoint i8* %18 to i64
%21 = sub i64 %19, %20
%22 = sub i64 %16, %21
%23 = call i8* @memchr(i8* %14, i32 %15, i64 %22)
store i8* %23, i8** %10
%24 = load i8*, i8** %10
%25 = icmp eq i8* %24, null
br i1 %25, label %26, label %27
26: ; preds = %13
br label %33
27: ; preds = %13
%28 = load i64, i64* %7
%29 = add i64 %28, 1
store i64 %29, i64* %7
%30 = load i8*, i8** %10
store i8* %30, i8** %8
%31 = load i8*, i8** %8
%32 = getelementptr inbounds i8, i8* %31, i32 1
store i8* %32, i8** %8
br label %13
33: ; preds = %26
%34 = load i64, i64* %7
ret i64 %34
}

LLVM IR (-O2):
define i64 @count_chr(i8* %0, i32 %1, i64 %2) {
%4 = ptrtoint i8* %0 to i64
%5 = add i64 %4, %2
%6 = tail call i8* @memchr(i8* %0, i32 %1, i64 %2)
%7 = icmp eq i8* %6, null
br i1 %7, label %17, label %8
8: ; preds = %3, %8
%9 = phi i8* [ %15, %8 ], [ %6, %3 ]
%10 = phi i64 [ %11, %8 ], [ 0, %3 ]
%11 = add i64 %10, 1
%12 = getelementptr inbounds i8, i8* %9, i64 1
%13 = ptrtoint i8* %12 to i64
%14 = sub i64 %5, %13
%15 = tail call i8* @memchr(i8* %12, i32 %1, i64 %14)
%16 = icmp eq i8* %15, null
br i1 %16, label %17, label %8
17: ; preds = %8, %3
%18 = phi i64 [ 0, %3 ], [ %11, %8 ]
ret i64 %18
}