#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_count0(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;
  }
  do {
    text = strstr(text, sub);
    if (text != NULL) {
      ret++;
      text = text + sub_len;
    } else
      break;
  } while (*text != '\0');
  return ret;
}


x86 assembly (clang, -O0):
<str_count0>:
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_count0+0x39>
cmpq   $0x0,-0x18(%rbp)
jne    1190 <str_count0+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_count0+0xbe>
jmp    1195 <str_count0+0x65>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1050 <strstr@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     11ce <str_count0+0x9e>
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    11d3 <str_count0+0xa3>
jmp    11e8 <str_count0+0xb8>
jmp    11d8 <str_count0+0xa8>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
jne    1195 <str_count0+0x65>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<str_count0>:
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_count0+0x52>
test   %r14,%r14
je     1182 <str_count0+0x52>
movslq %eax,%r15
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %rbx,%rdi
mov    %r14,%rsi
call   1050 <strstr@plt>
test   %rax,%rax
je     119c <str_count0+0x6c>
add    $0x1,%ebp
mov    %rax,%rbx
add    %r15,%rbx
cmpb   $0x0,(%rax,%r15,1)
jne    1160 <str_count0+0x30>
jmp    119c <str_count0+0x6c>
mov    0x2e57(%rip),%rax
mov    (%rax),%edi
lea    0xe6e(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0xffffffff,%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_count0(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 = %35, %19
%21 = load i8*, i8** %4
%22 = load i8*, i8** %5
%23 = call i8* @strstr(i8* %21, i8* %22)
store i8* %23, i8** %4
%24 = load i8*, i8** %4
%25 = icmp ne i8* %24, null
br i1 %25, label %26, label %33
26: ; preds = %20
%27 = load i32, i32* %6
%28 = add nsw i32 %27, 1
store i32 %28, i32* %6
%29 = load i8*, i8** %4
%30 = load i32, i32* %7
%31 = sext i32 %30 to i64
%32 = getelementptr inbounds i8, i8* %29, i64 %31
store i8* %32, i8** %4
br label %34
33: ; preds = %20
br label %40
34: ; preds = %26
br label %35
35: ; preds = %34
%36 = load i8*, i8** %4
%37 = load i8, i8* %36
%38 = sext i8 %37 to i32
%39 = icmp ne i32 %38, 0
br i1 %39, label %20, label %40
40: ; preds = %35, %33
%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_count0(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 %9, label %7
7: ; preds = %2
%8 = sext i32 %3 to i64
br label %12
9: ; preds = %2
%10 = load i32, i32* @stderr
%11 = tail call i32 @fprintf(i32 %10, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
br label %22
12: ; preds = %7, %17
%13 = phi i8* [ %19, %17 ], [ %0, %7 ]
%14 = phi i32 [ %18, %17 ], [ 0, %7 ]
%15 = tail call i8* @strstr(i8* dereferenceable(1) %13, i8* dereferenceable(1) %1)
%16 = icmp eq i8* %15, null
br i1 %16, label %22, label %17
17: ; preds = %12
%18 = add nuw nsw i32 %14, 1
%19 = getelementptr inbounds i8, i8* %15, i64 %8
%20 = load i8, i8* %19
%21 = icmp eq i8 %20, 0
br i1 %21, label %22, label %12
22: ; preds = %17, %12, %9
%23 = phi i32 [ -1, %9 ], [ %14, %12 ], [ %18, %17 ]
ret i32 %23
}