#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*/ *strstr(char *, char *);
int csc_strcount_str(char *s, char *needle) {
  int n = 0, span = strlen(needle);
  while (strstr(s, needle) != NULL) {
    n++;
    s += span;
  }
  return n;
}


x86 assembly (clang, -O0):
<csc_strcount_str>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1040 <strstr@plt>
cmp    $0x0,%rax
je     1179 <csc_strcount_str+0x59>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
mov    -0x18(%rbp),%ecx
mov    -0x8(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x8(%rbp)
jmp    1143 <csc_strcount_str+0x23>
mov    -0x14(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<csc_strcount_str>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rbx
mov    %rsi,%rdi
call   1030 <strlen@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
mov    %r14,%rsi
call   1040 <strstr@plt>
test   %rax,%rax
je     1168 <csc_strcount_str+0x48>
movslq %ebp,%r15
add    %r15,%rbx
xor    %ebp,%ebp
nop
add    $0x1,%ebp
mov    %rbx,%rdi
mov    %r14,%rsi
call   1040 <strstr@plt>
add    %r15,%rbx
test   %rax,%rax
jne    1150 <csc_strcount_str+0x30>
jmp    116a <csc_strcount_str+0x4a>
xor    %ebp,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

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