#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 const *);
char *strstr(char const *, char const *);
size_t gs_nstr(const char *str, const char *rech) {
  const char *s = str;
  size_t n = 0;
  size_t len_rech = strlen(rech);
  while (s) {
    s = strstr(s, rech);
    if (s) {
      n++;
      s += len_rech;
    }
  }
  return n;
}


x86 assembly (clang, -O0):
<gs_nstr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     1191 <gs_nstr+0x71>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1040 <strstr@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     118c <gs_nstr+0x6c>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
mov    -0x28(%rbp),%rax
add    -0x18(%rbp),%rax
mov    %rax,-0x18(%rbp)
jmp    114d <gs_nstr+0x2d>
mov    -0x20(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gs_nstr>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rbx
mov    %rsi,%rdi
call   1030 <strlen@plt>
test   %rbx,%rbx
je     1175 <gs_nstr+0x55>
mov    %rax,%r15
xor    %r12d,%r12d
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rdi
mov    %r14,%rsi
call   1040 <strstr@plt>
lea    (%rax,%r15,1),%rbx
xor    %ecx,%ecx
test   %rax,%rax
setne  %cl
cmove  %rax,%rbx
add    %rcx,%r12
test   %rbx,%rbx
jne    1150 <gs_nstr+0x30>
jmp    1178 <gs_nstr+0x58>
xor    %r12d,%r12d
mov    %r12,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

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

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