#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 *strchr(char *, char);
scalar_t__ strlen(char *);
int count_chars(char *str, char token) {
  int count = 0;
  if (str != NULL) {
    char *temp = str;
    while (temp != NULL) {
      temp = strchr(temp, token);
      if (temp != NULL) {
        count++;
        if (strlen(temp) > 0)
          temp++;
      }
    }
  }
  return count;
}


x86 assembly (clang, -O0):
<count_chars>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %sil,%al
mov    %rdi,-0x8(%rbp)
mov    %al,-0x9(%rbp)
movl   $0x0,-0x10(%rbp)
cmpq   $0x0,-0x8(%rbp)
je     11aa <count_chars+0x8a>
mov    -0x8(%rbp),%rax
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     11a5 <count_chars+0x85>
mov    -0x18(%rbp),%rdi
movsbl -0x9(%rbp),%esi
call   1040 <strchr@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     11a0 <count_chars+0x80>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <strlen@plt>
cmp    $0x0,%rax
jle    119b <count_chars+0x7b>
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
jmp    11a0 <count_chars+0x80>
jmp    114c <count_chars+0x2c>
jmp    11aa <count_chars+0x8a>
mov    -0x10(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<count_chars>:
push   %rbp
push   %r14
push   %rbx
xor    %ebx,%ebx
test   %rdi,%rdi
je     1159 <count_chars+0x39>
movsbl %sil,%r14d
nop
mov    %r14d,%esi
call   1040 <strchr@plt>
test   %rax,%rax
je     1159 <count_chars+0x39>
mov    %rax,%rbp
add    $0x1,%ebx
mov    %rax,%rdi
call   1030 <strlen@plt>
xor    %edi,%edi
test   %rax,%rax
setg   %dil
add    %rbp,%rdi
jne    1130 <count_chars+0x10>
mov    %ebx,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

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

LLVM IR (-O2):
define i32 @count_chars(i8* %0, i8 signext %1) {
%3 = icmp eq i8* %0, null
br i1 %3, label %16, label %4
4: ; preds = %2, %9
%5 = phi i8* [ %14, %9 ], [ %0, %2 ]
%6 = phi i32 [ %10, %9 ], [ 0, %2 ]
%7 = tail call i8* @strchr(i8* %5, i8 signext %1)
%8 = icmp eq i8* %7, null
br i1 %8, label %16, label %9
9: ; preds = %4
%10 = add nuw nsw i32 %6, 1
%11 = tail call i64 @strlen(i8* %7)
%12 = icmp sgt i64 %11, 0
%13 = zext i1 %12 to i64
%14 = getelementptr i8, i8* %7, i64 %13
%15 = icmp eq i8* %14, null
br i1 %15, label %16, label %4
16: ; preds = %4, %9, %2
%17 = phi i32 [ 0, %2 ], [ %6, %4 ], [ %10, %9 ]
ret i32 %17
}