#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);
int count_occurences(char *string, char substring) {
  if (string == NULL) {
    return 0;
  }
  int count = 1;
  char *substring_occurence = NULL;
  while (1) {
    substring_occurence = strchr(string, substring);
    if (substring_occurence != NULL) {
      count++;
      string = substring_occurence + 1;
    } else {
      break;
    }
  }
  return count;
}


x86 assembly (clang, -O0):
<count_occurences>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %sil,%al
mov    %rdi,-0x10(%rbp)
mov    %al,-0x11(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1139 <count_occurences+0x29>
movl   $0x0,-0x4(%rbp)
jmp    118e <count_occurences+0x7e>
movl   $0x1,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
movsbl -0x11(%rbp),%esi
call   1030 <strchr@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     117e <count_occurences+0x6e>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
jmp    1183 <count_occurences+0x73>
jmp    1188 <count_occurences+0x78>
jmp    1148 <count_occurences+0x38>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<count_occurences>:
push   %rbp
push   %rbx
push   %rax
test   %rdi,%rdi
je     1148 <count_occurences+0x38>
movsbl %sil,%ebx
mov    %ebx,%esi
call   1030 <strchr@plt>
mov    $0x1,%ebp
test   %rax,%rax
je     114a <count_occurences+0x3a>
nopl   (%rax)
add    $0x1,%ebp
add    $0x1,%rax
mov    %rax,%rdi
mov    %ebx,%esi
call   1030 <strchr@plt>
test   %rax,%rax
jne    1130 <count_occurences+0x20>
jmp    114a <count_occurences+0x3a>
xor    %ebp,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

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

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