#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 *strindex(char *, int);
int strcount(char *str, int ch) {
  int retval = 0;
  char *tmp = str;
  if (tmp == NULL)
    return retval;
  do {
    if ((tmp = strindex(tmp, ch)) != NULL) {
      tmp++;
      retval++;
    }
  } while (tmp != NULL);
  return retval;
}


x86 assembly (clang, -O0):
<strcount>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1144 <strcount+0x34>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    118e <strcount+0x7e>
jmp    1149 <strcount+0x39>
mov    -0x20(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1030 <strindex@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     1178 <strcount+0x68>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    117d <strcount+0x6d>
cmpq   $0x0,-0x20(%rbp)
jne    1149 <strcount+0x39>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<strcount>:
push   %rbp
push   %rbx
push   %rax
test   %rdi,%rdi
je     1135 <strcount+0x25>
mov    %esi,%ebx
mov    $0xffffffff,%ebp
nop
mov    %ebx,%esi
call   1030 <strindex@plt>
lea    0x1(%rax),%rdi
add    $0x1,%ebp
test   %rax,%rax
jne    1120 <strcount+0x10>
jmp    1137 <strcount+0x27>
xor    %ebp,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

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

LLVM IR (-O2):
define i32 @strcount(i8* %0, i32 %1) {
%3 = icmp eq i8* %0, null
br i1 %3, label %11, label %4
4: ; preds = %2, %4
%5 = phi i32 [ %10, %4 ], [ 0, %2 ]
%6 = phi i8* [ %9, %4 ], [ %0, %2 ]
%7 = tail call i8* @strindex(i8* %6, i32 %1)
%8 = icmp eq i8* %7, null
%9 = getelementptr inbounds i8, i8* %7, i64 1
%10 = add nuw nsw i32 %5, 1
br i1 %8, label %11, label %4
11: ; preds = %4, %2
%12 = phi i32 [ 0, %2 ], [ %5, %4 ]
ret i32 %12
}