#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*/ computeTransitionFunction(char *, int, int **);
int /*<<< orphan*/ printf(char *, int);
int strlen(char *);
void search(char *pattern, char *string) {
  int patternLen = strlen(pattern);
  int stringLen = strlen(string);
  int TransitionFunction[patternLen + 1][256];
  computeTransitionFunction(pattern, patternLen, TransitionFunction);
  int i, state = 0;
  for (i = 0; i < stringLen; i++) {
    state = TransitionFunction[state][string[i]];
    if (state == patternLen) {
      printf("Pattern found at index %d \n", i - patternLen + 1);
    }
  }
}


x86 assembly (clang, -O0):
<search>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x18(%rbp)
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %rsp,%rcx
mov    %rcx,-0x20(%rbp)
mov    %rax,%rcx
shl    $0xa,%rcx
mov    %rsp,%rdx
sub    %rcx,%rdx
mov    %rdx,-0x38(%rbp)
mov    %rdx,%rsp
mov    %rax,-0x28(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1050 <computeTransitionFunction@plt>
movl   $0x0,-0x30(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0x18(%rbp),%eax
jge    11f9 <search+0xc9>
mov    -0x38(%rbp),%rax
movslq -0x30(%rbp),%rcx
shl    $0xa,%rcx
add    %rcx,%rax
mov    -0x10(%rbp),%rcx
movslq -0x2c(%rbp),%rdx
movsbq (%rcx,%rdx,1),%rcx
mov    (%rax,%rcx,4),%eax
mov    %eax,-0x30(%rbp)
mov    -0x30(%rbp),%eax
cmp    -0x14(%rbp),%eax
jne    11e6 <search+0xb6>
mov    -0x2c(%rbp),%esi
sub    -0x14(%rbp),%esi
add    $0x1,%esi
lea    0xe1f(%rip),%rdi
call   1040 <printf@plt>
jmp    11eb <search+0xbb>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    1197 <search+0x67>
mov    -0x20(%rbp),%rax
mov    %rax,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<search>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%rbx
mov    %rdi,%r13
call   1030 <strlen@plt>
mov    %eax,%r15d
mov    %rbx,-0x38(%rbp)
mov    %rbx,%rdi
call   1030 <strlen@plt>
mov    %eax,%r12d
lea    0x1(%r15),%eax
mov    %rsp,%rbx
shl    $0xa,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
mov    %r13,%rdi
mov    %r15d,%esi
mov    %rbx,%rdx
call   1050 <computeTransitionFunction@plt>
test   %r12d,%r12d
jle    11db <search+0xab>
mov    $0x1,%eax
sub    %r15d,%eax
mov    %rax,-0x30(%rbp)
mov    %r12d,%r12d
xor    %r13d,%r13d
xor    %r14d,%r14d
jmp    11a9 <search+0x79>
nopl   0x0(%rax)
add    $0x1,%r13
cmp    %r13,%r12
je     11db <search+0xab>
movslq %r14d,%rax
mov    -0x38(%rbp),%rcx
movsbq (%rcx,%r13,1),%rcx
shl    $0xa,%rax
add    %rbx,%rax
mov    (%rax,%rcx,4),%r14d
cmp    %r15d,%r14d
jne    11a0 <search+0x70>
mov    -0x30(%rbp),%rax
lea    (%rax,%r13,1),%esi
lea    0xe2c(%rip),%rdi
call   1040 <printf@plt>
jmp    11a0 <search+0x70>
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [28 x i8] c"Pattern found at index %d \0A\00"
define void @search(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i32
%10 = alloca i32
store i8* %0, i8** %3
store i8* %1, i8** %4
%11 = load i8*, i8** %3
%12 = call i32 @strlen(i8* %11)
store i32 %12, i32* %5
%13 = load i8*, i8** %4
%14 = call i32 @strlen(i8* %13)
store i32 %14, i32* %6
%15 = load i32, i32* %5
%16 = add nsw i32 %15, 1
%17 = zext i32 %16 to i64
%18 = call i8* @llvm.stacksave()
store i8* %18, i8** %7
%19 = alloca [256 x i32], i64 %17
store i64 %17, i64* %8
%20 = load i8*, i8** %3
%21 = load i32, i32* %5
%22 = bitcast [256 x i32]* %19 to i32**
%23 = call i32 @computeTransitionFunction(i8* %20, i32 %21, i32** %22)
store i32 0, i32* %10
store i32 0, i32* %9
br label %24
24: ; preds = %50, %2
%25 = load i32, i32* %9
%26 = load i32, i32* %6
%27 = icmp slt i32 %25, %26
br i1 %27, label %28, label %53
28: ; preds = %24
%29 = load i32, i32* %10
%30 = sext i32 %29 to i64
%31 = getelementptr inbounds [256 x i32], [256 x i32]* %19, i64 %30
%32 = load i8*, i8** %4
%33 = load i32, i32* %9
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds i8, i8* %32, i64 %34
%36 = load i8, i8* %35
%37 = sext i8 %36 to i64
%38 = getelementptr inbounds [256 x i32], [256 x i32]* %31, i64 0, i64 %37
%39 = load i32, i32* %38
store i32 %39, i32* %10
%40 = load i32, i32* %10
%41 = load i32, i32* %5
%42 = icmp eq i32 %40, %41
br i1 %42, label %43, label %49
43: ; preds = %28
%44 = load i32, i32* %9
%45 = load i32, i32* %5
%46 = sub nsw i32 %44, %45
%47 = add nsw i32 %46, 1
%48 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i32 %47)
br label %49
49: ; preds = %43, %28
br label %50
50: ; preds = %49
%51 = load i32, i32* %9
%52 = add nsw i32 %51, 1
store i32 %52, i32* %9
br label %24
53: ; preds = %24
%54 = load i8*, i8** %7
ret void
}

LLVM IR (-O2):
@.str = [28 x i8] c"Pattern found at index %d \0A\00"
define void @search(i8* %0, i8* %1) {
%3 = tail call i32 @strlen(i8* %0)
%4 = tail call i32 @strlen(i8* %1)
%5 = add nsw i32 %3, 1
%6 = zext i32 %5 to i64
%7 = alloca [256 x i32], i64 %6
%8 = bitcast [256 x i32]* %7 to i32**
%9 = call i32 @computeTransitionFunction(i8* %0, i32 %3, i32** %8)
%10 = icmp sgt i32 %4, 0
br i1 %10, label %11, label %31
11: ; preds = %2
%12 = sub i32 1, %3
%13 = zext i32 %4 to i64
br label %14
14: ; preds = %11, %28
%15 = phi i64 [ 0, %11 ], [ %29, %28 ]
%16 = phi i32 [ 0, %11 ], [ %22, %28 ]
%17 = sext i32 %16 to i64
%18 = getelementptr inbounds i8, i8* %1, i64 %15
%19 = load i8, i8* %18
%20 = sext i8 %19 to i64
%21 = getelementptr inbounds [256 x i32], [256 x i32]* %7, i64 %17, i64 %20
%22 = load i32, i32* %21
%23 = icmp eq i32 %22, %3
br i1 %23, label %24, label %28
24: ; preds = %14
%25 = trunc i64 %15 to i32
%26 = add i32 %12, %25
%27 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i32 %26)
br label %28
28: ; preds = %14, %24
%29 = add nuw nsw i64 %15, 1
%30 = icmp eq i64 %29, %13
br i1 %30, label %31, label %14
31: ; preds = %28, %2
ret void
}