#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 const *);
char *strstr(char *, char const *);
int CountStringSimple(char *pBuffer, const char *S) {
  char *p = pBuffer;
  int lS = strlen(S);
  int n = 0;
  while ((p = strstr(p, S))) {
    n++;
    p += lS;
  }
  return n;
}


x86 assembly (clang, -O0):
<CountStringSimple>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1040 <strstr@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
je     1185 <CountStringSimple+0x65>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
mov    -0x1c(%rbp),%ecx
mov    -0x18(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x18(%rbp)
jmp    114b <CountStringSimple+0x2b>
mov    -0x20(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

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

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

LLVM IR (-O2):
define i32 @CountStringSimple(i8* %0, i8* %1) {
%3 = tail call i32 @strlen(i8* %1)
%4 = tail call i8* @strstr(i8* dereferenceable(1) %0, i8* dereferenceable(1) %1)
%5 = icmp eq i8* %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* [ %4, %6 ], [ %13, %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 i8* @strstr(i8* dereferenceable(1) %12, i8* dereferenceable(1) %1)
%14 = icmp eq i8* %13, null
br i1 %14, label %15, label %8
15: ; preds = %8, %2
%16 = phi i32 [ 0, %2 ], [ %11, %8 ]
ret i32 %16
}