#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 *);
char *strstr(char *, char *);
int str_in_str_count(char *substr, char *str) {
  int substr_len = strlen(substr);
  int count = 0;
  char *tmp = str;
  if (substr_len)
    while ((tmp = strstr(tmp, substr))) {
      tmp += substr_len;
      count++;
    }
  return count;
}


x86 assembly (clang, -O0):
<str_in_str_count>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     1199 <str_in_str_count+0x79>
jmp    115a <str_in_str_count+0x3a>
mov    -0x20(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   1040 <strstr@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     1194 <str_in_str_count+0x74>
mov    -0x14(%rbp),%ecx
mov    -0x20(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    115a <str_in_str_count+0x3a>
jmp    1199 <str_in_str_count+0x79>
mov    -0x18(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<str_in_str_count>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rsi,%r15
mov    %rdi,%r12
call   1030 <strlen@plt>
xor    %ebp,%ebp
test   %eax,%eax
je     1176 <str_in_str_count+0x56>
mov    %eax,%r14d
mov    %r15,%rdi
mov    %r12,%rsi
call   1040 <strstr@plt>
test   %rax,%rax
je     1176 <str_in_str_count+0x56>
movslq %r14d,%rbx
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
add    %rbx,%rax
add    $0x1,%ebp
mov    %rax,%rdi
mov    %r12,%rsi
call   1040 <strstr@plt>
test   %rax,%rax
jne    1160 <str_in_str_count+0x40>
mov    %ebp,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

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