#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 */
size_t strlen(char *);
char *strstr(char *, char *);
unsigned int str_count(char *str, char *substr) {
  size_t str_len = strlen(str);
  size_t substr_len = strlen(substr);
  unsigned int count = 0;
  if (substr_len > str_len)
    return -1;
  char *found_substr;
  found_substr = strstr(str, substr);
  while (found_substr != NULL) {
    found_substr = strstr(found_substr + substr_len, substr);
    ++count;
  }
  return count;
}


x86 assembly (clang, -O0):
<str_count>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x20(%rbp),%rax
jbe    116b <str_count+0x4b>
movl   $0xffffffff,-0x4(%rbp)
jmp    11b0 <str_count+0x90>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1040 <strstr@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
je     11aa <str_count+0x8a>
mov    -0x38(%rbp),%rdi
add    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1040 <strstr@plt>
mov    %rax,-0x38(%rbp)
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    117c <str_count+0x5c>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<str_count>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rsi,%r14
mov    %rdi,%r15
call   1030 <strlen@plt>
mov    %rax,%r12
mov    %r14,%rdi
call   1030 <strlen@plt>
mov    $0xffffffff,%ebp
cmp    %r12,%rax
jbe    1153 <str_count+0x33>
mov    %ebp,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %rax,%rbx
mov    %r15,%rdi
mov    %r14,%rsi
call   1040 <strstr@plt>
xor    %ebp,%ebp
test   %rax,%rax
je     1148 <str_count+0x28>
nopl   0x0(%rax,%rax,1)
add    %rbx,%rax
mov    %rax,%rdi
mov    %r14,%rsi
call   1040 <strstr@plt>
add    $0x1,%ebp
test   %rax,%rax
jne    1170 <str_count+0x50>
jmp    1148 <str_count+0x28>

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

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