#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++;
    printf("\nSubstring found at position %ld\n", pos - acText + 1);
    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     11f2 <main+0xc2>
mov    -0x80(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x80(%rbp)
mov    -0x88(%rbp),%rsi
lea    -0x70(%rbp),%rax
sub    %rax,%rsi
add    $0x1,%rsi
lea    0xe61(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x88(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x90(%rbp)
jmp    1191 <main+0x61>
mov    -0x80(%rbp),%esi
lea    0xe5b(%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   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x80,%rsp
lea    0xee5(%rip),%rdi
call   1030 <puts@plt>
lea    0x10(%rsp),%rbp
mov    %rbp,%rdi
call   1050 <gets@plt>
lea    0xedc(%rip),%rdi
call   1030 <puts@plt>
lea    0x6(%rsp),%r14
mov    %r14,%rdi
call   1050 <gets@plt>
mov    %rbp,%rdi
mov    %r14,%rsi
call   1060 <strstr@plt>
mov    %rax,%rbx
mov    $0x1,%r12d
sub    %rbp,%r12
lea    0xe61(%rip),%r15
nop
add    $0x1,%ebp
lea    (%r12,%rbx,1),%rsi
mov    %r15,%rdi
xor    %eax,%eax
call   1040 <printf@plt>
add    $0x1,%rbx
mov    %rbx,%rdi
mov    %r14,%rsi
call   1060 <strstr@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    11a0 <main+0x60>
lea    0xe53(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x80,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
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 = [34 x i8] c"\0ASubstring found at position %ld\0A\00"
@.str.3 = [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 %31
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 [100 x i8], [100 x i8]* %2, i64 0, i64 0
%24 = ptrtoint i8* %22 to i64
%25 = ptrtoint i8* %23 to i64
%26 = sub i64 %24, %25
%27 = add nsw i64 %26, 1
%28 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.2, i64 0, i64 0), i64 %27)
%29 = load i8*, i8** %5
%30 = getelementptr inbounds i8, i8* %29, i64 1
store i8* %30, i8** %6
br label %14
31: ; preds = %14
%32 = load i32, i32* %4
%33 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0), i32 %32)
ret i32 0
}

LLVM IR (-O2):
@.str.2 = [34 x i8] c"\0ASubstring found at position %ld\0A\00"
@.str.3 = [25 x i8] c"\0ANo of occurences is %d\0A\00"
@str = [16 x i8] c"\0AEnter the text\00"
@str.4 = [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.4, 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
%11 = ptrtoint [100 x i8]* %1 to i64
%12 = sub i64 1, %11
br label %13
13: ; preds = %0, %13
%14 = phi i8* [ %9, %0 ], [ %21, %13 ]
%15 = phi i32 [ undef, %0 ], [ %16, %13 ]
%16 = add nsw i32 %15, 1
%17 = ptrtoint i8* %14 to i64
%18 = add i64 %12, %17
%19 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @.str.2, i64 0, i64 0), i64 %18)
%20 = getelementptr inbounds i8, i8* %14, i64 1
%21 = call i8* @strstr(i8* dereferenceable(1) %20, i8* %4)
%22 = icmp eq i8* %21, null
br i1 %22, label %23, label %13
23: ; preds = %13
%24 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0), i32 %16)
ret i32 0
}