#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 */
unsigned int strlen(char const *);
int str_count(const char *needle, const char *haystack) {
  unsigned int j;
  unsigned int i;
  int count;
  count = 0;
  if (strlen(needle) > strlen(haystack)) {
    return count;
  }
  j = 0;
  for (i = 0; i < strlen(haystack); i++) {
    if (needle[j] == haystack[i]) {
      j++;
    } else {
      j = 0;
    }
    if (j == strlen(needle)) {
      count++;
      j = 0;
    }
  }
  return count;
}


x86 assembly (clang, -O0):
<str_count>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x28(%rbp),%eax
cmp    %ecx,%eax
jbe    1154 <str_count+0x44>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11f6 <str_count+0xe6>
movl   $0x0,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x2c(%rbp),%eax
cmp    %ecx,%eax
jae    11f0 <str_count+0xe0>
mov    -0x10(%rbp),%rax
mov    -0x1c(%rbp),%ecx
movsbl (%rax,%rcx,1),%eax
mov    -0x18(%rbp),%rcx
mov    -0x20(%rbp),%edx
movsbl (%rcx,%rdx,1),%ecx
cmp    %ecx,%eax
jne    11aa <str_count+0x9a>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    11b1 <str_count+0xa1>
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x30(%rbp),%eax
cmp    %ecx,%eax
jne    11dd <str_count+0xcd>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
movl   $0x0,-0x1c(%rbp)
jmp    11e2 <str_count+0xd2>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    1162 <str_count+0x52>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<str_count>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r12
mov    %rdi,%r14
call   1030 <strlen@plt>
mov    %eax,%r15d
mov    %r12,%rdi
call   1030 <strlen@plt>
xor    %ebp,%ebp
cmp    %eax,%r15d
ja     11a4 <str_count+0x94>
mov    %r12,%rdi
call   1030 <strlen@plt>
test   %eax,%eax
je     11a4 <str_count+0x94>
xor    %ebx,%ebx
xor    %ebp,%ebp
xor    %r15d,%r15d
jmp    1167 <str_count+0x57>
nopl   (%rax)
mov    %al,%cl
add    %ecx,%ebp
add    $0x1,%rbx
mov    %r12,%rdi
call   1030 <strlen@plt>
mov    %eax,%eax
cmp    %rax,%rbx
jae    11a4 <str_count+0x94>
mov    %r15d,%eax
movzbl (%r14,%rax,1),%eax
cmp    (%r12,%rbx,1),%al
je     1180 <str_count+0x70>
xor    %r13d,%r13d
jmp    1187 <str_count+0x77>
nopw   0x0(%rax,%rax,1)
add    $0x1,%r15d
mov    %r15d,%r13d
mov    %r14,%rdi
call   1030 <strlen@plt>
xor    %ecx,%ecx
mov    $0x0,%r15d
cmp    %eax,%r13d
sete   %al
je     1150 <str_count+0x40>
mov    %r13d,%r15d
jmp    1150 <str_count+0x40>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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