#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*/ assert(int);
int /*<<< orphan*/ free(int *);
int kmp_search(char *, char *, int *);
int *kmp_table(char *);
int /*<<< orphan*/ printf(char *, ...);
int strlen(char *);
int main(void) {
  char text[] = "ABCdefdeGdefdefHIJ";
  char pattern[] = "defdef";
  int *T = kmp_table(pattern);
  if (kmp_search(text, pattern, T) == strlen(text)) {
    printf("The pattern was not found in the string.\n");
  } else {
    printf("The pattern is found at position %d in the text.\n",
           kmp_search(text, pattern, T));
  }
  assert(kmp_search(text, pattern, T) == 9);
  free(T);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    0xe8a(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    0xe87(%rip),%rax
mov    %rax,-0x18(%rbp)
mov    0xe84(%rip),%ax
mov    %ax,-0x10(%rbp)
mov    0xe7c(%rip),%al
mov    %al,-0xe(%rbp)
mov    0xe74(%rip),%eax
mov    %eax,-0x27(%rbp)
mov    0xe6e(%rip),%ax
mov    %ax,-0x23(%rbp)
mov    0xe66(%rip),%al
mov    %al,-0x21(%rbp)
lea    -0x27(%rbp),%rdi
call   1040 <kmp_table@plt>
mov    %rax,-0x30(%rbp)
lea    -0x20(%rbp),%rdi
lea    -0x27(%rbp),%rsi
mov    -0x30(%rbp),%rdx
call   1070 <kmp_search@plt>
mov    %eax,-0x34(%rbp)
lea    -0x20(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,%ecx
mov    -0x34(%rbp),%eax
cmp    %ecx,%eax
jne    1200 <main+0xa0>
lea    0xe26(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
jmp    1221 <main+0xc1>
lea    -0x20(%rbp),%rdi
lea    -0x27(%rbp),%rsi
mov    -0x30(%rbp),%rdx
call   1070 <kmp_search@plt>
mov    %eax,%esi
lea    0xe2a(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
lea    -0x20(%rbp),%rdi
lea    -0x27(%rbp),%rsi
mov    -0x30(%rbp),%rdx
call   1070 <kmp_search@plt>
cmp    $0x9,%eax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1080 <assert@plt>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x28,%rsp
movaps 0xe7f(%rip),%xmm0
movaps %xmm0,0x10(%rsp)
movl   $0x4a4948,0x1f(%rsp)
movl   $0x666564,0xb(%rsp)
movl   $0x64666564,0x8(%rsp)
64
lea    0x8(%rsp),%r14
mov    %r14,%rdi
call   1050 <kmp_table@plt>
mov    %rax,%rbx
lea    0x10(%rsp),%r15
mov    %r15,%rdi
mov    %r14,%rsi
mov    %rax,%rdx
call   1080 <kmp_search@plt>
mov    %eax,%ebp
mov    %r15,%rdi
call   1060 <strlen@plt>
cmp    %eax,%ebp
jne    11dd <main+0x6d>
lea    0xe76(%rip),%rdi
call   1040 <puts@plt>
jmp    11ff <main+0x8f>
lea    0x10(%rsp),%rdi
lea    0x8(%rsp),%rsi
mov    %rbx,%rdx
call   1080 <kmp_search@plt>
lea    0xe24(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1070 <printf@plt>
lea    0x10(%rsp),%rdi
lea    0x8(%rsp),%rsi
mov    %rbx,%rdx
call   1080 <kmp_search@plt>
xor    %edi,%edi
cmp    $0x9,%eax
sete   %dil
call   1090 <assert@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
xor    %eax,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@__const.main.text = [19 x i8] c"ABCdefdeGdefdefHIJ\00"
@__const.main.pattern = [7 x i8] c"defdef\00"
@.str = [42 x i8] c"The pattern was not found in the string.\0A\00"
@.str.1 = [50 x i8] c"The pattern is found at position %d in the text.\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [19 x i8]
%3 = alloca [7 x i8]
%4 = alloca i32*
store i32 0, i32* %1
%5 = bitcast [19 x i8]* %2 to i8*
%6 = bitcast [7 x i8]* %3 to i8*
%7 = getelementptr inbounds [7 x i8], [7 x i8]* %3, i64 0, i64 0
%8 = call i32* @kmp_table(i8* %7)
store i32* %8, i32** %4
%9 = getelementptr inbounds [19 x i8], [19 x i8]* %2, i64 0, i64 0
%10 = getelementptr inbounds [7 x i8], [7 x i8]* %3, i64 0, i64 0
%11 = load i32*, i32** %4
%12 = call i32 @kmp_search(i8* %9, i8* %10, i32* %11)
%13 = getelementptr inbounds [19 x i8], [19 x i8]* %2, i64 0, i64 0
%14 = call i32 @strlen(i8* %13)
%15 = icmp eq i32 %12, %14
br i1 %15, label %16, label %18
16: ; preds = %0
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str, i64 0, i64 0))
br label %24
18: ; preds = %0
%19 = getelementptr inbounds [19 x i8], [19 x i8]* %2, i64 0, i64 0
%20 = getelementptr inbounds [7 x i8], [7 x i8]* %3, i64 0, i64 0
%21 = load i32*, i32** %4
%22 = call i32 @kmp_search(i8* %19, i8* %20, i32* %21)
%23 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.1, i64 0, i64 0), i32 %22)
br label %24
24: ; preds = %18, %16
%25 = getelementptr inbounds [19 x i8], [19 x i8]* %2, i64 0, i64 0
%26 = getelementptr inbounds [7 x i8], [7 x i8]* %3, i64 0, i64 0
%27 = load i32*, i32** %4
%28 = call i32 @kmp_search(i8* %25, i8* %26, i32* %27)
%29 = icmp eq i32 %28, 9
%30 = zext i1 %29 to i32
%31 = call i32 @assert(i32 %30)
%32 = load i32*, i32** %4
%33 = call i32 @free(i32* %32)
ret i32 0
}

LLVM IR (-O2):
@__const.main.text = [19 x i8] c"ABCdefdeGdefdefHIJ\00"
@__const.main.pattern = [7 x i8] c"defdef\00"
@.str.1 = [50 x i8] c"The pattern is found at position %d in the text.\0A\00"
@str = [41 x i8] c"The pattern was not found in the string.\00"
define i32 @main() {
%1 = alloca [19 x i8]
%2 = alloca [7 x i8]
%3 = getelementptr inbounds [19 x i8], [19 x i8]* %1, i64 0, i64 0
%4 = getelementptr inbounds [7 x i8], [7 x i8]* %2, i64 0, i64 0
%5 = call i32* @kmp_table(i8* %4)
%6 = call i32 @kmp_search(i8* %3, i8* %4, i32* %5)
%7 = call i32 @strlen(i8* %3)
%8 = icmp eq i32 %6, %7
br i1 %8, label %9, label %11
9: ; preds = %0
%10 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([41 x i8], [41 x i8]* @str, i64 0, i64 0))
br label %14
11: ; preds = %0
%12 = call i32 @kmp_search(i8* %3, i8* %4, i32* %5)
%13 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([50 x i8], [50 x i8]* @.str.1, i64 0, i64 0), i32 %12)
br label %14
14: ; preds = %11, %9
%15 = call i32 @kmp_search(i8* %3, i8* %4, i32* %5)
%16 = icmp eq i32 %15, 9
%17 = zext i1 %16 to i32
%18 = call i32 @assert(i32 %17)
%19 = call i32 @free(i32* %5)
ret i32 0
}