#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 */
size_t strlen(char const *);
char *strstr(char *, char const *);
size_t occurrences(const char *needle, const char *haystack) {
  if (NULL == needle || NULL == haystack)
    return -1;
  char *pos = (char *)haystack;
  size_t i = 0;
  size_t l = strlen(needle);
  while ((pos = strstr(pos, needle))) {
    pos += l;
    i++;
  }
  return i;
}


x86 assembly (clang, -O0):
<occurrences>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
xor    %eax,%eax
cmp    -0x10(%rbp),%rax
je     1148 <occurrences+0x28>
xor    %eax,%eax
cmp    -0x18(%rbp),%rax
jne    1155 <occurrences+0x35>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    11b2 <occurrences+0x92>
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x30(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1040 <strstr@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     11aa <occurrences+0x8a>
mov    -0x30(%rbp),%rax
add    -0x20(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
jmp    1172 <occurrences+0x52>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<occurrences>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    $0xffffffffffffffff,%rbx
test   %rdi,%rdi
je     1177 <occurrences+0x57>
mov    %rsi,%r12
test   %rsi,%rsi
je     1177 <occurrences+0x57>
mov    %rdi,%r14
call   1030 <strlen@plt>
mov    %rax,%r15
mov    %r12,%rdi
mov    %r14,%rsi
call   1040 <strstr@plt>
xor    %ebx,%ebx
test   %rax,%rax
je     1177 <occurrences+0x57>
nopl   0x0(%rax)
add    %r15,%rax
add    $0x1,%rbx
mov    %rax,%rdi
mov    %r14,%rsi
call   1040 <strstr@plt>
test   %rax,%rax
jne    1160 <occurrences+0x40>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

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

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