#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*/ printf(char *);
int strlen(char *);
char *strstr(char *, char *);
int string_count(char *str, char *s) {
  char *ptr;
  int num, len;
  if ((len = strlen(s)) == 0) {
    printf("string_count: length of s == 0\n");
    return 0;
  }
  ptr = str;
  for (num = 0; (ptr = strstr(ptr, s)) != NULL; num++)
    ptr += len;
  return num;
}


x86 assembly (clang, -O0):
<string_count>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x28(%rbp)
cmp    $0x0,%eax
jne    116d <string_count+0x3d>
lea    0xea4(%rip),%rdi
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    11bc <string_count+0x8c>
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1050 <strstr@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     11b6 <string_count+0x86>
mov    -0x28(%rbp),%ecx
mov    -0x20(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x20(%rbp)
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    117c <string_count+0x4c>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<string_count>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r15
mov    %rdi,%r14
mov    %rsi,%rdi
call   1040 <strlen@plt>
test   %eax,%eax
je     1178 <string_count+0x48>
mov    %eax,%ebp
mov    %r14,%rdi
mov    %r15,%rsi
call   1050 <strstr@plt>
test   %rax,%rax
je     1184 <string_count+0x54>
movslq %ebp,%rbx
xor    %ebp,%ebp
add    %rbx,%rax
add    $0x1,%ebp
mov    %rax,%rdi
mov    %r15,%rsi
call   1050 <strstr@plt>
test   %rax,%rax
jne    1160 <string_count+0x30>
jmp    1186 <string_count+0x56>
lea    0xe81(%rip),%rdi
call   1030 <puts@plt>
xor    %ebp,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [32 x i8] c"string_count: length of s == 0\0A\00"
define i32 @string_count(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
%9 = load i8*, i8** %5
%10 = call i32 @strlen(i8* %9)
store i32 %10, i32* %8
%11 = icmp eq i32 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %2
%13 = call i32 @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %3
br label %31
14: ; preds = %2
%15 = load i8*, i8** %4
store i8* %15, i8** %6
store i32 0, i32* %7
br label %16
16: ; preds = %26, %14
%17 = load i8*, i8** %6
%18 = load i8*, i8** %5
%19 = call i8* @strstr(i8* %17, i8* %18)
store i8* %19, i8** %6
%20 = icmp ne i8* %19, null
br i1 %20, label %21, label %29
21: ; preds = %16
%22 = load i32, i32* %8
%23 = load i8*, i8** %6
%24 = sext i32 %22 to i64
%25 = getelementptr inbounds i8, i8* %23, i64 %24
store i8* %25, i8** %6
br label %26
26: ; preds = %21
%27 = load i32, i32* %7
%28 = add nsw i32 %27, 1
store i32 %28, i32* %7
br label %16
29: ; preds = %16
%30 = load i32, i32* %7
store i32 %30, i32* %3
br label %31
31: ; preds = %29, %12
%32 = load i32, i32* %3
ret i32 %32
}

LLVM IR (-O2):
@str = [31 x i8] c"string_count: length of s == 0\00"
define i32 @string_count(i8* %0, i8* %1) {
%3 = tail call i32 @strlen(i8* %1)
%4 = icmp eq i32 %3, 0
br i1 %4, label %10, label %5
5: ; preds = %2
%6 = tail call i8* @strstr(i8* dereferenceable(1) %0, i8* dereferenceable(1) %1)
%7 = icmp eq i8* %6, null
br i1 %7, label %19, label %8
8: ; preds = %5
%9 = sext i32 %3 to i64
br label %12
10: ; preds = %2
%11 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @str, i64 0, i64 0))
br label %19
12: ; preds = %8, %12
%13 = phi i8* [ %6, %8 ], [ %17, %12 ]
%14 = phi i32 [ 0, %8 ], [ %16, %12 ]
%15 = getelementptr inbounds i8, i8* %13, i64 %9
%16 = add nuw nsw i32 %14, 1
%17 = tail call i8* @strstr(i8* dereferenceable(1) %15, i8* dereferenceable(1) %1)
%18 = icmp eq i8* %17, null
br i1 %18, label %19, label %12
19: ; preds = %12, %5, %10
%20 = phi i32 [ 0, %10 ], [ 0, %5 ], [ %16, %12 ]
ret i32 %20
}