#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*/ gets(char *);
int /*<<< orphan*/ printf(char *, ...);
char *strstr(char *, char *);
int main() {
  char acText[100], acPattern[10];
  int iCount;
  char *pos = NULL, *start = NULL;
  printf("\nEnter the text\n");
  gets(acText);
  printf("\nEnter the substring to search for\n");
  gets(acPattern);
  start = acText;
  while ((pos = strstr(start, acPattern))) {
    iCount++;
    start = pos + 1;
  }
  printf("\nNo of occurences is %d\n", iCount);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x90,%rsp
movl   $0x0,-0x4(%rbp)
movq   $0x0,-0x88(%rbp)
movq   $0x0,-0x90(%rbp)
lea    0xea1(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    -0x70(%rbp),%rdi
call   1040 <gets@plt>
lea    0xe9b(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    -0x7a(%rbp),%rdi
call   1040 <gets@plt>
lea    -0x70(%rbp),%rax
mov    %rax,-0x90(%rbp)
mov    -0x90(%rbp),%rdi
lea    -0x7a(%rbp),%rsi
call   1050 <strstr@plt>
mov    %rax,-0x88(%rbp)
cmp    $0x0,%rax
je     11d2 <main+0xa2>
mov    -0x80(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x80(%rbp)
mov    -0x88(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x90(%rbp)
jmp    1191 <main+0x61>
mov    -0x80(%rbp),%esi
lea    0xe59(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
xor    %eax,%eax
add    $0x90,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
sub    $0x78,%rsp
lea    0xecc(%rip),%rdi
call   1030 <puts@plt>
lea    0x10(%rsp),%rbp
mov    %rbp,%rdi
call   1050 <gets@plt>
lea    0xec3(%rip),%rdi
call   1030 <puts@plt>
lea    0x6(%rsp),%rbx
mov    %rbx,%rdi
call   1050 <gets@plt>
mov    %rbp,%rdi
mov    %rbx,%rsi
call   1060 <strstr@plt>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
add    $0x1,%ebp
add    $0x1,%rax
mov    %rax,%rdi
mov    %rbx,%rsi
call   1060 <strstr@plt>
test   %rax,%rax
jne    1190 <main+0x50>
lea    0xe52(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x78,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [17 x i8] c"\0AEnter the text\0A\00"
@.str.1 = [36 x i8] c"\0AEnter the substring to search for\0A\00"
@.str.2 = [25 x i8] c"\0ANo of occurences is %d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [100 x i8]
%3 = alloca [10 x i8]
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
store i32 0, i32* %1
store i8* null, i8** %5
store i8* null, i8** %6
%7 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
%8 = getelementptr inbounds [100 x i8], [100 x i8]* %2, i64 0, i64 0
%9 = call i32 @gets(i8* %8)
%10 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.1, i64 0, i64 0))
%11 = getelementptr inbounds [10 x i8], [10 x i8]* %3, i64 0, i64 0
%12 = call i32 @gets(i8* %11)
%13 = getelementptr inbounds [100 x i8], [100 x i8]* %2, i64 0, i64 0
store i8* %13, i8** %6
br label %14
14: ; preds = %19, %0
%15 = load i8*, i8** %6
%16 = getelementptr inbounds [10 x i8], [10 x i8]* %3, i64 0, i64 0
%17 = call i8* @strstr(i8* %15, i8* %16)
store i8* %17, i8** %5
%18 = icmp ne i8* %17, null
br i1 %18, label %19, label %24
19: ; preds = %14
%20 = load i32, i32* %4
%21 = add nsw i32 %20, 1
store i32 %21, i32* %4
%22 = load i8*, i8** %5
%23 = getelementptr inbounds i8, i8* %22, i64 1
store i8* %23, i8** %6
br label %14
24: ; preds = %14
%25 = load i32, i32* %4
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0), i32 %25)
ret i32 0
}

LLVM IR (-O2):
@.str.2 = [25 x i8] c"\0ANo of occurences is %d\0A\00"
@str = [16 x i8] c"\0AEnter the text\00"
@str.3 = [35 x i8] c"\0AEnter the substring to search for\00"
define i32 @main() {
%1 = alloca [100 x i8]
%2 = alloca [10 x i8]
%3 = getelementptr inbounds [100 x i8], [100 x i8]* %1, i64 0, i64 0
%4 = getelementptr inbounds [10 x i8], [10 x i8]* %2, i64 0, i64 0
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @str, i64 0, i64 0))
%6 = call i32 @gets(i8* %3)
%7 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @str.3, i64 0, i64 0))
%8 = call i32 @gets(i8* %4)
%9 = call i8* @strstr(i8* dereferenceable(1) %3, i8* %4)
%10 = icmp ne i8* %9, null
br label %11
11: ; preds = %0, %11
%12 = phi i8* [ %16, %11 ], [ %9, %0 ]
%13 = phi i32 [ %14, %11 ], [ undef, %0 ]
%14 = add nsw i32 %13, 1
%15 = getelementptr inbounds i8, i8* %12, i64 1
%16 = call i8* @strstr(i8* dereferenceable(1) %15, i8* %4)
%17 = icmp eq i8* %16, null
br i1 %17, label %18, label %11
18: ; preds = %11
%19 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0), i32 %14)
ret i32 0
}