#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 FA_match(char *, int *, int, int);
int /*<<< orphan*/ compute_transition_function(char *, int *, int);
int /*<<< orphan*/ free(int *);
scalar_t__ malloc(int);
int /*<<< orphan*/ printf(char *, ...);
int strlen(char *);
int main() {
  char *text = "abababacaba";
  char *pattern = "ababaca";
  int length = strlen(pattern);
  int *array = (int *)malloc(3 * (length + 1));
  compute_transition_function(pattern, array, 3);
  int i;
  printf("This is our chart\n");
  for (i = 0; i <= length; i++) {
    int j;
    for (j = 0; j < 3; j++)
      printf("%d  ", array[i * 3 + j]);
    printf("\n");
  }
  int offset = FA_match(text, array, 3, length);
  printf("offset is %d\n", offset);
  free(array);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe8a(%rip),%rax
mov    %rax,-0x10(%rbp)
lea    0xe8b(%rip),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
imul   $0x3,%eax,%edi
call   1060 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    $0x3,%edx
call   1080 <compute_transition_function@plt>
lea    0xe58(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x0,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jg     1230 <main+0xd0>
movl   $0x0,-0x30(%rbp)
cmpl   $0x3,-0x30(%rbp)
jge    1214 <main+0xb4>
mov    -0x28(%rbp),%rax
imul   $0x3,-0x2c(%rbp),%ecx
add    -0x30(%rbp),%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,4),%esi
lea    0xe28(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x30(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x30(%rbp)
jmp    11dd <main+0x7d>
lea    0xe1d(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    11ca <main+0x6a>
mov    -0x10(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x1c(%rbp),%ecx
mov    $0x3,%edx
call   1070 <FA_match@plt>
mov    %eax,-0x34(%rbp)
mov    -0x34(%rbp),%esi
lea    0xdda(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
lea    0xe7c(%rip),%r15
mov    %r15,%rdi
call   1060 <strlen@plt>
mov    %eax,%r14d
lea    (%r14,%r14,2),%edi
add    $0x3,%edi
call   1080 <malloc@plt>
mov    %rax,%r13
mov    %r15,%rdi
mov    %rax,%rsi
mov    $0x3,%edx
call   10a0 <compute_transition_function@plt>
lea    0xe66(%rip),%rdi
call   1050 <puts@plt>
test   %r14d,%r14d
js     1230 <main+0xb0>
mov    %r14d,%eax
shl    $0x2,%rax
lea    (%rax,%rax,2),%r12
add    $0xc,%r12
lea    0xe33(%rip),%r15
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    0x0(%r13,%rbx,1),%esi
mov    %r15,%rdi
xor    %eax,%eax
call   1070 <printf@plt>
mov    0x4(%r13,%rbx,1),%esi
mov    %r15,%rdi
xor    %eax,%eax
call   1070 <printf@plt>
mov    0x8(%r13,%rbx,1),%esi
mov    %r15,%rdi
xor    %eax,%eax
call   1070 <printf@plt>
mov    $0xa,%edi
call   1040 <putchar@plt>
add    $0xc,%rbx
cmp    %rbx,%r12
jne    11f0 <main+0x70>
lea    0xdc9(%rip),%rdi
mov    %r13,%rsi
mov    $0x3,%edx
mov    %r14d,%ecx
call   1090 <FA_match@plt>
lea    0xdcb(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1070 <printf@plt>
mov    %r13,%rdi
call   1030 <free@plt>
xor    %eax,%eax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [12 x i8] c"abababacaba\00"
@.str.1 = [8 x i8] c"ababaca\00"
@.str.2 = [19 x i8] c"This is our chart\0A\00"
@.str.3 = [5 x i8] c"%d \00"
@.str.4 = [2 x i8] c"\0A\00"
@.str.5 = [14 x i8] c"offset is %d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i32 0, i32* %1
store i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i8** %2
store i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i8** %3
%9 = load i8*, i8** %3
%10 = call i32 @strlen(i8* %9)
store i32 %10, i32* %4
%11 = load i32, i32* %4
%12 = add nsw i32 %11, 1
%13 = mul nsw i32 3, %12
%14 = call i64 @malloc(i32 %13)
%15 = inttoptr i64 %14 to i32*
store i32* %15, i32** %5
%16 = load i8*, i8** %3
%17 = load i32*, i32** %5
%18 = call i32 @compute_transition_function(i8* %16, i32* %17, i32 3)
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0))
store i32 0, i32* %6
br label %20
20: ; preds = %43, %0
%21 = load i32, i32* %6
%22 = load i32, i32* %4
%23 = icmp sle i32 %21, %22
br i1 %23, label %24, label %46
24: ; preds = %20
store i32 0, i32* %7
br label %25
25: ; preds = %38, %24
%26 = load i32, i32* %7
%27 = icmp slt i32 %26, 3
br i1 %27, label %28, label %41
28: ; preds = %25
%29 = load i32*, i32** %5
%30 = load i32, i32* %6
%31 = mul nsw i32 %30, 3
%32 = load i32, i32* %7
%33 = add nsw i32 %31, %32
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds i32, i32* %29, i64 %34
%36 = load i32, i32* %35
%37 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i32 %36)
br label %38
38: ; preds = %28
%39 = load i32, i32* %7
%40 = add nsw i32 %39, 1
store i32 %40, i32* %7
br label %25
41: ; preds = %25
%42 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
br label %43
43: ; preds = %41
%44 = load i32, i32* %6
%45 = add nsw i32 %44, 1
store i32 %45, i32* %6
br label %20
46: ; preds = %20
%47 = load i8*, i8** %2
%48 = load i32*, i32** %5
%49 = load i32, i32* %4
%50 = call i32 @FA_match(i8* %47, i32* %48, i32 3, i32 %49)
store i32 %50, i32* %8
%51 = load i32, i32* %8
%52 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.5, i64 0, i64 0), i32 %51)
%53 = load i32*, i32** %5
%54 = call i32 @free(i32* %53)
ret i32 0
}

LLVM IR (-O2):
@.str = [12 x i8] c"abababacaba\00"
@.str.1 = [8 x i8] c"ababaca\00"
@.str.3 = [5 x i8] c"%d \00"
@.str.5 = [14 x i8] c"offset is %d\0A\00"
@str = [18 x i8] c"This is our chart\00"
define i32 @main() {
%1 = tail call i32 @strlen(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
%2 = mul i32 %1, 3
%3 = add i32 %2, 3
%4 = tail call i64 @malloc(i32 %3)
%5 = inttoptr i64 %4 to i32*
%6 = tail call i32 @compute_transition_function(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i32* %5, i32 3)
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @str, i64 0, i64 0))
%8 = icmp slt i32 %1, 0
br i1 %8, label %29, label %9
9: ; preds = %0
%10 = add nuw i32 %1, 1
%11 = zext i32 %10 to i64
br label %12
12: ; preds = %9, %12
%13 = phi i64 [ 0, %9 ], [ %27, %12 ]
%14 = mul nuw nsw i64 %13, 3
%15 = getelementptr inbounds i32, i32* %5, i64 %14
%16 = load i32, i32* %15
%17 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i32 %16)
%18 = add nuw nsw i64 %14, 1
%19 = getelementptr inbounds i32, i32* %5, i64 %18
%20 = load i32, i32* %19
%21 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i32 %20)
%22 = add nuw nsw i64 %14, 2
%23 = getelementptr inbounds i32, i32* %5, i64 %22
%24 = load i32, i32* %23
%25 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i32 %24)
%26 = tail call i32 @putchar(i32 10)
%27 = add nuw nsw i64 %13, 1
%28 = icmp eq i64 %27, %11
br i1 %28, label %29, label %12
29: ; preds = %12, %0
%30 = tail call i32 @FA_match(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32* %5, i32 3, i32 %1)
%31 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str.5, i64 0, i64 0), i32 %30)
%32 = tail call i32 @free(i32* %5)
ret i32 0
}