#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 /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ stderr;
int strlen(char const *);
char *strstr(char const *, char const *);
int str_count(const char *text, const char *sub) {
  int ret = 0;
  int sub_len = strlen(sub);
  if (text == NULL || sub == NULL) {
    ret = -1;
    fprintf(stderr, "str_count get NULL pointer\n");
    return ret;
  }
  while (*text != '\0') {
    text = strstr(text, sub);
    if (text != NULL) {
      ret++;
      text = text + sub_len;
    } else
      break;
  }
  return ret;
}


x86 assembly (clang, -O0):
<str_count>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x20(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1169 <str_count+0x39>
cmpq   $0x0,-0x18(%rbp)
jne    1190 <str_count+0x60>
movl   $0xffffffff,-0x1c(%rbp)
mov    0x2e69(%rip),%rax
mov    (%rax),%edi
lea    0xe80(%rip),%rsi
call   1040 <fprintf@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11ee <str_count+0xbe>
jmp    1195 <str_count+0x65>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     11e8 <str_count+0xb8>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1050 <strstr@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     11de <str_count+0xae>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%rax
movslq -0x20(%rbp),%rcx
add    %rcx,%rax
mov    %rax,-0x10(%rbp)
jmp    11e3 <str_count+0xb3>
jmp    11e8 <str_count+0xb8>
jmp    1195 <str_count+0x65>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<str_count>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rbx
mov    %rsi,%rdi
call   1030 <strlen@plt>
test   %rbx,%rbx
je     1182 <str_count+0x52>
test   %r14,%r14
je     1182 <str_count+0x52>
cmpb   $0x0,(%rbx)
je     119e <str_count+0x6e>
movslq %eax,%r15
xor    %ebp,%ebp
nopl   0x0(%rax)
mov    %rbx,%rdi
mov    %r14,%rsi
call   1050 <strstr@plt>
test   %rax,%rax
je     11a0 <str_count+0x70>
add    $0x1,%ebp
mov    %rax,%rbx
add    %r15,%rbx
cmpb   $0x0,(%rax,%r15,1)
jne    1160 <str_count+0x30>
jmp    11a0 <str_count+0x70>
mov    0x2e57(%rip),%rax
mov    (%rax),%edi
lea    0xe6e(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0xffffffff,%ebp
jmp    11a0 <str_count+0x70>
xor    %ebp,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [28 x i8] c"str_count get NULL pointer\0A\00"
define i32 @str_count(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
store i32 0, i32* %6
%8 = load i8*, i8** %5
%9 = call i32 @strlen(i8* %8)
store i32 %9, i32* %7
%10 = load i8*, i8** %4
%11 = icmp eq i8* %10, null
br i1 %11, label %15, label %12
12: ; preds = %2
%13 = load i8*, i8** %5
%14 = icmp eq i8* %13, null
br i1 %14, label %15, label %19
15: ; preds = %12, %2
store i32 -1, i32* %6
%16 = load i32, i32* @stderr
%17 = call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
%18 = load i32, i32* %6
store i32 %18, i32* %3
br label %42
19: ; preds = %12
br label %20
20: ; preds = %39, %19
%21 = load i8*, i8** %4
%22 = load i8, i8* %21
%23 = sext i8 %22 to i32
%24 = icmp ne i32 %23, 0
br i1 %24, label %25, label %40
25: ; preds = %20
%26 = load i8*, i8** %4
%27 = load i8*, i8** %5
%28 = call i8* @strstr(i8* %26, i8* %27)
store i8* %28, i8** %4
%29 = load i8*, i8** %4
%30 = icmp ne i8* %29, null
br i1 %30, label %31, label %38
31: ; preds = %25
%32 = load i32, i32* %6
%33 = add nsw i32 %32, 1
store i32 %33, i32* %6
%34 = load i8*, i8** %4
%35 = load i32, i32* %7
%36 = sext i32 %35 to i64
%37 = getelementptr inbounds i8, i8* %34, i64 %36
store i8* %37, i8** %4
br label %39
38: ; preds = %25
br label %40
39: ; preds = %31
br label %20
40: ; preds = %38, %20
%41 = load i32, i32* %6
store i32 %41, i32* %3
br label %42
42: ; preds = %40, %15
%43 = load i32, i32* %3
ret i32 %43
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [28 x i8] c"str_count get NULL pointer\0A\00"
define i32 @str_count(i8* %0, i8* %1) {
%3 = tail call i32 @strlen(i8* %1)
%4 = icmp eq i8* %0, null
%5 = icmp eq i8* %1, null
%6 = or i1 %4, %5
br i1 %6, label %12, label %7
7: ; preds = %2
%8 = load i8, i8* %0
%9 = icmp eq i8 %8, 0
br i1 %9, label %25, label %10
10: ; preds = %7
%11 = sext i32 %3 to i64
br label %15
12: ; preds = %2
%13 = load i32, i32* @stderr
%14 = tail call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
br label %25
15: ; preds = %10, %20
%16 = phi i32 [ 0, %10 ], [ %21, %20 ]
%17 = phi i8* [ %0, %10 ], [ %22, %20 ]
%18 = tail call i8* @strstr(i8* %17, i8* dereferenceable(1) %1)
%19 = icmp eq i8* %18, null
br i1 %19, label %25, label %20
20: ; preds = %15
%21 = add nuw nsw i32 %16, 1
%22 = getelementptr inbounds i8, i8* %18, i64 %11
%23 = load i8, i8* %22
%24 = icmp eq i8 %23, 0
br i1 %24, label %25, label %15
25: ; preds = %15, %20, %7, %12
%26 = phi i32 [ -1, %12 ], [ 0, %7 ], [ %16, %15 ], [ %21, %20 ]
ret i32 %26
}