#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 */
int strlen(char *);
char *strstr(char *, char *);
int count_occurence(char *needle, char *haystack) {
  int nlen = strlen(needle);
  int i = 0;
  int offset = 0;
  if (nlen) {
    while (haystack = strstr(haystack + offset, needle)) {
      ++i;
      if (i)
        offset = nlen;
    }
  }
  return i;
}


x86 assembly (clang, -O0):
<count_occurence>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     119e <count_occurence+0x7e>
jmp    1159 <count_occurence+0x39>
mov    -0x10(%rbp),%rdi
movslq -0x1c(%rbp),%rax
add    %rax,%rdi
mov    -0x8(%rbp),%rsi
call   1040 <strstr@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
je     1199 <count_occurence+0x79>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
je     1194 <count_occurence+0x74>
mov    -0x14(%rbp),%eax
mov    %eax,-0x1c(%rbp)
jmp    1159 <count_occurence+0x39>
jmp    119e <count_occurence+0x7e>
mov    -0x18(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<count_occurence>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rsi,%r15
mov    %rdi,%r12
call   1030 <strlen@plt>
xor    %ebp,%ebp
test   %eax,%eax
je     1176 <count_occurence+0x56>
mov    %eax,%r14d
mov    %r15,%rdi
mov    %r12,%rsi
call   1040 <strstr@plt>
test   %rax,%rax
je     1176 <count_occurence+0x56>
movslq %r14d,%rbx
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
add    %rbx,%rax
mov    %rax,%rdi
mov    %r12,%rsi
call   1040 <strstr@plt>
add    $0x1,%ebp
test   %rax,%rax
jne    1160 <count_occurence+0x40>
mov    %ebp,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

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