#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 *realloc(int *, int);
int strlen(char *);
int strncmp(char *, char *, int);
int *searchSubstring(char *input, char *substring, int *nFound,
                     int overlappedSearch) {
  int substringLength = strlen(substring);
  int inputLength = strlen(input);
  int resComparison;
  int *positionsFound = NULL;
  int positionsFoundLen = 0;
  int i = 0;
  while (i <= inputLength - substringLength) {
    resComparison = strncmp(input + i, substring, substringLength);
    if (resComparison == 0) {
      if (positionsFoundLen % 128 == 0) {
        positionsFound =
            realloc(positionsFound, (positionsFoundLen + 128) * sizeof(int));
      }
      positionsFound[positionsFoundLen] = i;
      positionsFoundLen++;
      if (overlappedSearch) {
        i = i + 1;
      } else {
        i = i + substringLength;
      }
    } else {
      i = i + 1;
    }
  }
  *nFound = positionsFoundLen;
  return positionsFound;
}


x86 assembly (clang, -O0):
<searchSubstring>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x24(%rbp)
movq   $0x0,-0x30(%rbp)
movl   $0x0,-0x34(%rbp)
movl   $0x0,-0x38(%rbp)
mov    -0x38(%rbp),%eax
mov    -0x24(%rbp),%ecx
sub    -0x20(%rbp),%ecx
cmp    %ecx,%eax
jg     1226 <searchSubstring+0xf6>
mov    -0x8(%rbp),%rdi
movslq -0x38(%rbp),%rax
add    %rax,%rdi
mov    -0x10(%rbp),%rsi
mov    -0x20(%rbp),%edx
call   1030 <strncmp@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jne    1218 <searchSubstring+0xe8>
mov    -0x34(%rbp),%eax
mov    $0x80,%ecx
cltd
idiv   %ecx
cmp    $0x0,%edx
jne    11db <searchSubstring+0xab>
mov    -0x30(%rbp),%rdi
mov    -0x34(%rbp),%eax
add    $0x80,%eax
cltq
shl    $0x2,%rax
mov    %eax,%esi
call   1050 <realloc@plt>
mov    %rax,-0x30(%rbp)
mov    -0x38(%rbp),%edx
mov    -0x30(%rbp),%rax
movslq -0x34(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x1c(%rbp)
je     120a <searchSubstring+0xda>
mov    -0x38(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x38(%rbp)
jmp    1213 <searchSubstring+0xe3>
mov    -0x38(%rbp),%eax
add    -0x20(%rbp),%eax
mov    %eax,-0x38(%rbp)
jmp    1221 <searchSubstring+0xf1>
mov    -0x38(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x38(%rbp)
jmp    1175 <searchSubstring+0x45>
mov    -0x34(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x30(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<searchSubstring>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %ecx,%r13d
mov    %rdx,0x8(%rsp)
mov    %rdi,%r12
mov    %rsi,0x10(%rsp)
mov    %rsi,%rdi
call   1040 <strlen@plt>
mov    %eax,%ebp
mov    %r12,%rdi
call   1040 <strlen@plt>
mov    %eax,%ebx
sub    %ebp,%ebx
js     11dd <searchSubstring+0xad>
test   %r13d,%r13d
mov    $0x1,%eax
mov    %ebp,%ecx
cmovne %eax,%ecx
mov    %ecx,0x4(%rsp)
xor    %r13d,%r13d
xor    %r14d,%r14d
xor    %r15d,%r15d
jmp    119d <searchSubstring+0x6d>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    $0x1,%eax
add    %eax,%r13d
cmp    %ebx,%r13d
jg     11e3 <searchSubstring+0xb3>
movslq %r13d,%rdi
add    %r12,%rdi
mov    0x10(%rsp),%rsi
mov    %ebp,%edx
call   1030 <strncmp@plt>
test   %eax,%eax
jne    1190 <searchSubstring+0x60>
test   $0x7f,%r14b
jne    11cc <searchSubstring+0x9c>
lea    0x200(,%r14,4),%esi
mov    %r15,%rdi
call   1050 <realloc@plt>
mov    %rax,%r15
movslq %r14d,%rax
mov    %r13d,(%r15,%rax,4)
add    $0x1,%r14d
mov    0x4(%rsp),%eax
jmp    1195 <searchSubstring+0x65>
xor    %r15d,%r15d
xor    %r14d,%r14d
mov    0x8(%rsp),%rax
mov    %r14d,(%rax)
mov    %r15,%rax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32* @searchSubstring(i8* %0, i8* %1, i32* %2, i32 %3) {
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32*
%13 = alloca i32
%14 = alloca i32
store i8* %0, i8** %5
store i8* %1, i8** %6
store i32* %2, i32** %7
store i32 %3, i32* %8
%15 = load i8*, i8** %6
%16 = call i32 @strlen(i8* %15)
store i32 %16, i32* %9
%17 = load i8*, i8** %5
%18 = call i32 @strlen(i8* %17)
store i32 %18, i32* %10
store i32* null, i32** %12
store i32 0, i32* %13
store i32 0, i32* %14
br label %19
19: ; preds = %68, %4
%20 = load i32, i32* %14
%21 = load i32, i32* %10
%22 = load i32, i32* %9
%23 = sub nsw i32 %21, %22
%24 = icmp sle i32 %20, %23
br i1 %24, label %25, label %69
25: ; preds = %19
%26 = load i8*, i8** %5
%27 = load i32, i32* %14
%28 = sext i32 %27 to i64
%29 = getelementptr inbounds i8, i8* %26, i64 %28
%30 = load i8*, i8** %6
%31 = load i32, i32* %9
%32 = call i32 @strncmp(i8* %29, i8* %30, i32 %31)
store i32 %32, i32* %11
%33 = load i32, i32* %11
%34 = icmp eq i32 %33, 0
br i1 %34, label %35, label %65
35: ; preds = %25
%36 = load i32, i32* %13
%37 = srem i32 %36, 128
%38 = icmp eq i32 %37, 0
br i1 %38, label %39, label %47
39: ; preds = %35
%40 = load i32*, i32** %12
%41 = load i32, i32* %13
%42 = add nsw i32 %41, 128
%43 = sext i32 %42 to i64
%44 = mul i64 %43, 4
%45 = trunc i64 %44 to i32
%46 = call i32* @realloc(i32* %40, i32 %45)
store i32* %46, i32** %12
br label %47
47: ; preds = %39, %35
%48 = load i32, i32* %14
%49 = load i32*, i32** %12
%50 = load i32, i32* %13
%51 = sext i32 %50 to i64
%52 = getelementptr inbounds i32, i32* %49, i64 %51
store i32 %48, i32* %52
%53 = load i32, i32* %13
%54 = add nsw i32 %53, 1
store i32 %54, i32* %13
%55 = load i32, i32* %8
%56 = icmp ne i32 %55, 0
br i1 %56, label %57, label %60
57: ; preds = %47
%58 = load i32, i32* %14
%59 = add nsw i32 %58, 1
store i32 %59, i32* %14
br label %64
60: ; preds = %47
%61 = load i32, i32* %14
%62 = load i32, i32* %9
%63 = add nsw i32 %61, %62
store i32 %63, i32* %14
br label %64
64: ; preds = %60, %57
br label %68
65: ; preds = %25
%66 = load i32, i32* %14
%67 = add nsw i32 %66, 1
store i32 %67, i32* %14
br label %68
68: ; preds = %65, %64
br label %19
69: ; preds = %19
%70 = load i32, i32* %13
%71 = load i32*, i32** %7
store i32 %70, i32* %71
%72 = load i32*, i32** %12
ret i32* %72
}

LLVM IR (-O2):
define i32* @searchSubstring(i8* %0, i8* %1, i32* nocapture writeonly %2, i32 %3) {
%5 = tail call i32 @strlen(i8* %1)
%6 = tail call i32 @strlen(i8* %0)
%7 = sub nsw i32 %6, %5
%8 = icmp slt i32 %7, 0
br i1 %8, label %38, label %9
9: ; preds = %4
%10 = icmp eq i32 %3, 0
%11 = select i1 %10, i32 %5, i32 1
br label %12
12: ; preds = %9, %32
%13 = phi i32 [ 0, %9 ], [ %36, %32 ]
%14 = phi i32 [ 0, %9 ], [ %35, %32 ]
%15 = phi i32* [ null, %9 ], [ %34, %32 ]
%16 = sext i32 %13 to i64
%17 = getelementptr inbounds i8, i8* %0, i64 %16
%18 = tail call i32 @strncmp(i8* %17, i8* %1, i32 %5)
%19 = icmp eq i32 %18, 0
br i1 %19, label %20, label %32
20: ; preds = %12
%21 = and i32 %14, 127
%22 = icmp eq i32 %21, 0
br i1 %22, label %23, label %27
23: ; preds = %20
%24 = shl i32 %14, 2
%25 = add i32 %24, 512
%26 = tail call i32* @realloc(i32* %15, i32 %25)
br label %27
27: ; preds = %23, %20
%28 = phi i32* [ %26, %23 ], [ %15, %20 ]
%29 = sext i32 %14 to i64
%30 = getelementptr inbounds i32, i32* %28, i64 %29
store i32 %13, i32* %30
%31 = add nsw i32 %14, 1
br label %32
32: ; preds = %12, %27
%33 = phi i32 [ %11, %27 ], [ 1, %12 ]
%34 = phi i32* [ %28, %27 ], [ %15, %12 ]
%35 = phi i32 [ %31, %27 ], [ %14, %12 ]
%36 = add nsw i32 %13, %33
%37 = icmp sgt i32 %36, %7
br i1 %37, label %38, label %12
38: ; preds = %32, %4
%39 = phi i32* [ null, %4 ], [ %34, %32 ]
%40 = phi i32 [ 0, %4 ], [ %35, %32 ]
store i32 %40, i32* %2
ret i32* %39
}