#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*/ free(int *);
int *getNext(char *);
int /*<<< orphan*/ printf(char *, ...);
int strlen(char *);
int kmp(char *s, char *p, int pos) {
  int i = pos;
  int j = 0;
  int slen = strlen(s);
  int plen = strlen(p);
  int *next = getNext(p);
  while (i < slen) {
    if (j == -1 || s[i] == p[j]) {
      i++, j++;
      if (j == plen) {
        return i - plen;
      }
    } else {
      j = next[j];
      printf("%s\n", s);
      printf("%s\n", p);
      printf("%d %d\n", i, j);
      printf("\n");
    }
  }
  free(next);
  next = NULL;
  return -1;
}


x86 assembly (clang, -O0):
<kmp>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x18(%rbp),%rdi
call   1060 <getNext@plt>
mov    %rax,-0x38(%rbp)
mov    -0x20(%rbp),%eax
cmp    -0x28(%rbp),%eax
jge    1245 <kmp+0x105>
cmpl   $0xffffffff,-0x24(%rbp)
je     11bb <kmp+0x7b>
mov    -0x10(%rbp),%rax
movslq -0x20(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
mov    -0x18(%rbp),%rcx
movslq -0x24(%rbp),%rdx
movsbl (%rcx,%rdx,1),%ecx
cmp    %ecx,%eax
jne    11ec <kmp+0xac>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x2c(%rbp),%eax
jne    11e7 <kmp+0xa7>
mov    -0x20(%rbp),%eax
sub    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    125d <kmp+0x11d>
jmp    1240 <kmp+0x100>
mov    -0x38(%rbp),%rax
movslq -0x24(%rbp),%rcx
mov    (%rax,%rcx,4),%eax
mov    %eax,-0x24(%rbp)
mov    -0x10(%rbp),%rsi
lea    0xdfb(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x18(%rbp),%rsi
lea    0xde9(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x20(%rbp),%esi
mov    -0x24(%rbp),%edx
lea    0xdd9(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xdd0(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    1185 <kmp+0x45>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x38(%rbp)
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<kmp>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edx,%ebx
mov    %rsi,%r14
mov    %rdi,%r15
call   1060 <strlen@plt>
mov    %eax,%ebp
mov    %r14,%rdi
call   1060 <strlen@plt>
mov    %eax,%r12d
mov    %r14,%rdi
call   1080 <getNext@plt>
mov    %rax,(%rsp)
cmp    %ebx,%ebp
jle    1200 <kmp+0xa0>
xor    %r13d,%r13d
jmp    11b0 <kmp+0x50>
nopw   0x0(%rax,%rax,1)
add    $0x1,%ebx
add    $0x1,%r13d
cmp    %r12d,%r13d
je     1210 <kmp+0xb0>
cmp    %ebp,%ebx
jge    1200 <kmp+0xa0>
cmp    $0xffffffff,%r13d
je     11a0 <kmp+0x40>
movslq %ebx,%rax
movzbl (%r15,%rax,1),%ecx
movslq %r13d,%rax
cmp    (%r14,%rax,1),%cl
je     11a0 <kmp+0x40>
mov    (%rsp),%rcx
mov    (%rcx,%rax,4),%r13d
mov    %r15,%rdi
call   1050 <puts@plt>
mov    %r14,%rdi
call   1050 <puts@plt>
lea    0xe1a(%rip),%rdi
mov    %ebx,%esi
mov    %r13d,%edx
xor    %eax,%eax
call   1070 <printf@plt>
mov    $0xa,%edi
call   1040 <putchar@plt>
cmp    %ebp,%ebx
jl     11b0 <kmp+0x50>
mov    (%rsp),%rdi
call   1030 <free@plt>
mov    $0xffffffff,%ebx
jmp    1213 <kmp+0xb3>
sub    %r12d,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@.str.1 = [7 x i8] c"%d %d\0A\00"
define i32 @kmp(i8* %0, i8* %1, i32 %2) {
%4 = tail call i32 @strlen(i8* %0)
%5 = tail call i32 @strlen(i8* %1)
%6 = tail call i32* @getNext(i8* %1)
%7 = icmp sgt i32 %4, %2
br i1 %7, label %8, label %37
8: ; preds = %3, %33
%9 = phi i32 [ %35, %33 ], [ %2, %3 ]
%10 = phi i32 [ %34, %33 ], [ 0, %3 ]
%11 = icmp eq i32 %10, -1
br i1 %11, label %20, label %12
12: ; preds = %8
%13 = sext i32 %9 to i64
%14 = getelementptr inbounds i8, i8* %0, i64 %13
%15 = load i8, i8* %14
%16 = sext i32 %10 to i64
%17 = getelementptr inbounds i8, i8* %1, i64 %16
%18 = load i8, i8* %17
%19 = icmp eq i8 %15, %18
br i1 %19, label %20, label %26
20: ; preds = %12, %8
%21 = add nsw i32 %9, 1
%22 = add nsw i32 %10, 1
%23 = icmp eq i32 %22, %5
br i1 %23, label %24, label %33
24: ; preds = %20
%25 = sub nsw i32 %21, %5
br label %39
26: ; preds = %12
%27 = getelementptr inbounds i32, i32* %6, i64 %16
%28 = load i32, i32* %27
%29 = tail call i32 @puts(i8* dereferenceable(1) %0)
%30 = tail call i32 @puts(i8* dereferenceable(1) %1)
%31 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i32 %9, i32 %28)
%32 = tail call i32 @putchar(i32 10)
br label %33
33: ; preds = %20, %26
%34 = phi i32 [ %22, %20 ], [ %28, %26 ]
%35 = phi i32 [ %21, %20 ], [ %9, %26 ]
%36 = icmp slt i32 %35, %4
br i1 %36, label %8, label %37
37: ; preds = %33, %3
%38 = tail call i32 @free(i32* %6)
br label %39
39: ; preds = %37, %24
%40 = phi i32 [ %25, %24 ], [ -1, %37 ]
ret i32 %40
}