#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 *);
int /*<<< orphan*/ strncasecmp(char *, char *, int);
int numOccurances(char *string, char *pattern) {
  char *pin;
  int patternLen;
  int count = 0;
  patternLen = strlen(pattern);
  for (pin = string; *pin != '\0'; pin++) {
    if (*pin == pattern[0]) {
      if (!strncasecmp(pin, pattern, patternLen)) {
        pin += patternLen - 1;
        count++;
      }
    }
  }
  return count;
}


x86 assembly (clang, -O0):
<numOccurances>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     11c2 <numOccurances+0xa2>
mov    -0x18(%rbp),%rax
movsbl (%rax),%eax
mov    -0x10(%rbp),%rcx
movsbl (%rcx),%ecx
cmp    %ecx,%eax
jne    11ac <numOccurances+0x8c>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1040 <strncasecmp@plt>
cmp    $0x0,%eax
jne    11a7 <numOccurances+0x87>
mov    -0x1c(%rbp),%ecx
sub    $0x1,%ecx
mov    -0x18(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x18(%rbp)
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    11ac <numOccurances+0x8c>
jmp    11b1 <numOccurances+0x91>
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
jmp    114b <numOccurances+0x2b>
mov    -0x20(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<numOccurances>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rbx
mov    %rsi,%rdi
call   1030 <strlen@plt>
mov    %eax,%ebp
mov    (%rbx),%al
test   %al,%al
je     118b <numOccurances+0x6b>
mov    (%r14),%r12b
mov    %ebp,%ecx
add    $0xffffffff,%ecx
movslq %ecx,%r13
xor    %r15d,%r15d
jmp    116c <numOccurances+0x4c>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
movzbl 0x1(%rbx),%eax
add    $0x1,%rbx
test   %al,%al
je     118e <numOccurances+0x6e>
cmp    %r12b,%al
jne    1160 <numOccurances+0x40>
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %ebp,%edx
call   1040 <strncasecmp@plt>
test   %eax,%eax
jne    1160 <numOccurances+0x40>
add    %r13,%rbx
add    $0x1,%r15d
jmp    1160 <numOccurances+0x40>
xor    %r15d,%r15d
mov    %r15d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @numOccurances(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
store i8* %0, i8** %3
store i8* %1, i8** %4
store i32 0, i32* %7
%8 = load i8*, i8** %4
%9 = call i32 @strlen(i8* %8)
store i32 %9, i32* %6
%10 = load i8*, i8** %3
store i8* %10, i8** %5
br label %11
11: ; preds = %41, %2
%12 = load i8*, i8** %5
%13 = load i8, i8* %12
%14 = sext i8 %13 to i32
%15 = icmp ne i32 %14, 0
br i1 %15, label %16, label %44
16: ; preds = %11
%17 = load i8*, i8** %5
%18 = load i8, i8* %17
%19 = sext i8 %18 to i32
%20 = load i8*, i8** %4
%21 = getelementptr inbounds i8, i8* %20, i64 0
%22 = load i8, i8* %21
%23 = sext i8 %22 to i32
%24 = icmp eq i32 %19, %23
br i1 %24, label %25, label %40
25: ; preds = %16
%26 = load i8*, i8** %5
%27 = load i8*, i8** %4
%28 = load i32, i32* %6
%29 = call i32 @strncasecmp(i8* %26, i8* %27, i32 %28)
%30 = icmp ne i32 %29, 0
br i1 %30, label %39, label %31
31: ; preds = %25
%32 = load i32, i32* %6
%33 = sub nsw i32 %32, 1
%34 = load i8*, i8** %5
%35 = sext i32 %33 to i64
%36 = getelementptr inbounds i8, i8* %34, i64 %35
store i8* %36, i8** %5
%37 = load i32, i32* %7
%38 = add nsw i32 %37, 1
store i32 %38, i32* %7
br label %39
39: ; preds = %31, %25
br label %40
40: ; preds = %39, %16
br label %41
41: ; preds = %40
%42 = load i8*, i8** %5
%43 = getelementptr inbounds i8, i8* %42, i32 1
store i8* %43, i8** %5
br label %11
44: ; preds = %11
%45 = load i32, i32* %7
ret i32 %45
}

LLVM IR (-O2):
define i32 @numOccurances(i8* nocapture %0, i8* %1) {
%3 = tail call i32 @strlen(i8* %1)
%4 = load i8, i8* %0
%5 = icmp eq i8 %4, 0
br i1 %5, label %27, label %6
6: ; preds = %2
%7 = load i8, i8* %1
%8 = add nsw i32 %3, -1
%9 = sext i32 %8 to i64
br label %10
10: ; preds = %6, %21
%11 = phi i8 [ %4, %6 ], [ %25, %21 ]
%12 = phi i32 [ 0, %6 ], [ %23, %21 ]
%13 = phi i8* [ %0, %6 ], [ %24, %21 ]
%14 = icmp eq i8 %11, %7
br i1 %14, label %15, label %21
15: ; preds = %10
%16 = tail call i32 @strncasecmp(i8* %13, i8* %1, i32 %3)
%17 = icmp eq i32 %16, 0
br i1 %17, label %18, label %21
18: ; preds = %15
%19 = getelementptr inbounds i8, i8* %13, i64 %9
%20 = add nsw i32 %12, 1
br label %21
21: ; preds = %10, %18, %15
%22 = phi i8* [ %13, %15 ], [ %19, %18 ], [ %13, %10 ]
%23 = phi i32 [ %12, %15 ], [ %20, %18 ], [ %12, %10 ]
%24 = getelementptr inbounds i8, i8* %22, i64 1
%25 = load i8, i8* %24
%26 = icmp eq i8 %25, 0
br i1 %26, label %27, label %10
27: ; preds = %21, %2
%28 = phi i32 [ 0, %2 ], [ %23, %21 ]
ret i32 %28
}