#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 const, int);
int /*<<< orphan*/ compute_transition_function(char *, int *, int const);
int /*<<< orphan*/ free(int *);
scalar_t__ malloc(int);
int /*<<< orphan*/ printf(char *, ...);
int strlen(char *);
int main() {
  char *text = "aaababaabaababaab";
  char *pattern = "aabab";
  const int num_chars_alphabet = 2;
  int length = strlen(pattern);
  int *array = (int *)malloc(sizeof(int) * num_chars_alphabet * (length + 1));
  compute_transition_function(pattern, array, num_chars_alphabet);
  printf("This is our chart\nstate\t");
  for (char j = 'a'; j < 'a' + num_chars_alphabet; j++)
    printf("%c\t", j);
  printf("\n");
  int i;
  for (i = 0; i <= length; i++) {
    printf("%d\t", i);
    int j;
    for (j = 0; j < num_chars_alphabet; j++)
      printf("%d\t", array[i * num_chars_alphabet + j]);
    printf("\n");
  }
  int offset = FA_match(text, array, num_chars_alphabet, 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    0xe91(%rip),%rax
mov    %rax,-0x18(%rbp)
movl   $0x2,-0x1c(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%eax
add    $0x1,%eax
cltq
shl    $0x3,%rax
mov    %eax,%edi
call   1060 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    $0x2,%edx
call   1080 <compute_transition_function@plt>
lea    0xe50(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movb   $0x61,-0x29(%rbp)
movsbl -0x29(%rbp),%eax
cmp    $0x63,%eax
jge    11ff <main+0x9f>
movsbl -0x29(%rbp),%esi
lea    0xe46(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x29(%rbp),%al
add    $0x1,%al
mov    %al,-0x29(%rbp)
jmp    11d3 <main+0x73>
lea    0xe3f(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x0,-0x30(%rbp)
mov    -0x30(%rbp),%eax
cmp    -0x20(%rbp),%eax
jg     128d <main+0x12d>
mov    -0x30(%rbp),%esi
lea    0xe0b(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x0,-0x34(%rbp)
cmpl   $0x2,-0x34(%rbp)
jge    1271 <main+0x111>
mov    -0x28(%rbp),%rax
mov    -0x30(%rbp),%ecx
shl    $0x1,%ecx
add    -0x34(%rbp),%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,4),%esi
lea    0xdd9(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    1238 <main+0xd8>
lea    0xdcd(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x30(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x30(%rbp)
jmp    1214 <main+0xb4>
mov    -0x10(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x20(%rbp),%ecx
mov    $0x2,%edx
call   1070 <FA_match@plt>
mov    %eax,-0x38(%rbp)
mov    -0x38(%rbp),%esi
lea    0xd8a(%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    0xe92(%rip),%rbx
mov    %rbx,%rdi
call   1050 <strlen@plt>
mov    %eax,%r14d
lea    0x8(,%r14,8),%edi
call   1070 <malloc@plt>
mov    %rax,%r15
mov    %rbx,%rdi
mov    %rax,%rsi
mov    $0x2,%edx
call   1090 <compute_transition_function@plt>
lea    0xe66(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
lea    0xe71(%rip),%rbx
mov    %rbx,%rdi
mov    $0x61,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %rbx,%rdi
mov    $0x62,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0xa,%edi
call   1040 <putchar@plt>
test   %r14d,%r14d
js     123c <main+0xcc>
mov    %r14d,%r13d
add    $0x1,%r13d
lea    0xe3a(%rip),%r12
xor    %ebx,%ebx
nopl   (%rax)
mov    %r12,%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    (%r15,%rbx,8),%esi
mov    %r12,%rdi
xor    %eax,%eax
call   1060 <printf@plt>
mov    0x4(%r15,%rbx,8),%esi
mov    %r12,%rdi
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0xa,%edi
call   1040 <putchar@plt>
add    $0x1,%rbx
cmp    %rbx,%r13
jne    1200 <main+0x90>
lea    0xdbd(%rip),%rdi
mov    %r15,%rsi
mov    $0x2,%edx
mov    %r14d,%ecx
call   1080 <FA_match@plt>
lea    0xddf(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %r15,%rdi
call   1030 <free@plt>
xor    %eax,%eax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [18 x i8] c"aaababaabaababaab\00"
@.str.1 = [6 x i8] c"aabab\00"
@.str.2 = [25 x i8] c"This is our chart\0Astate\09\00"
@.str.3 = [4 x i8] c"%c\09\00"
@.str.4 = [2 x i8] c"\0A\00"
@.str.5 = [4 x i8] c"%d\09\00"
@.str.6 = [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 i8
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i32 0, i32* %1
store i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8** %2
store i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8** %3
store i32 2, i32* %4
%11 = load i8*, i8** %3
%12 = call i32 @strlen(i8* %11)
store i32 %12, i32* %5
%13 = load i32, i32* %5
%14 = add nsw i32 %13, 1
%15 = sext i32 %14 to i64
%16 = mul i64 8, %15
%17 = trunc i64 %16 to i32
%18 = call i64 @malloc(i32 %17)
%19 = inttoptr i64 %18 to i32*
store i32* %19, i32** %6
%20 = load i8*, i8** %3
%21 = load i32*, i32** %6
%22 = call i32 @compute_transition_function(i8* %20, i32* %21, i32 2)
%23 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0))
store i8 97, i8* %7
br label %24
24: ; preds = %32, %0
%25 = load i8, i8* %7
%26 = sext i8 %25 to i32
%27 = icmp slt i32 %26, 99
br i1 %27, label %28, label %35
28: ; preds = %24
%29 = load i8, i8* %7
%30 = sext i8 %29 to i32
%31 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i32 %30)
br label %32
32: ; preds = %28
%33 = load i8, i8* %7
%34 = add i8 %33, 1
store i8 %34, i8* %7
br label %24
35: ; preds = %24
%36 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
store i32 0, i32* %8
br label %37
37: ; preds = %62, %35
%38 = load i32, i32* %8
%39 = load i32, i32* %5
%40 = icmp sle i32 %38, %39
br i1 %40, label %41, label %65
41: ; preds = %37
%42 = load i32, i32* %8
%43 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0), i32 %42)
store i32 0, i32* %9
br label %44
44: ; preds = %57, %41
%45 = load i32, i32* %9
%46 = icmp slt i32 %45, 2
br i1 %46, label %47, label %60
47: ; preds = %44
%48 = load i32*, i32** %6
%49 = load i32, i32* %8
%50 = mul nsw i32 %49, 2
%51 = load i32, i32* %9
%52 = add nsw i32 %50, %51
%53 = sext i32 %52 to i64
%54 = getelementptr inbounds i32, i32* %48, i64 %53
%55 = load i32, i32* %54
%56 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0), i32 %55)
br label %57
57: ; preds = %47
%58 = load i32, i32* %9
%59 = add nsw i32 %58, 1
store i32 %59, i32* %9
br label %44
60: ; preds = %44
%61 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
br label %62
62: ; preds = %60
%63 = load i32, i32* %8
%64 = add nsw i32 %63, 1
store i32 %64, i32* %8
br label %37
65: ; preds = %37
%66 = load i8*, i8** %2
%67 = load i32*, i32** %6
%68 = load i32, i32* %5
%69 = call i32 @FA_match(i8* %66, i32* %67, i32 2, i32 %68)
store i32 %69, i32* %10
%70 = load i32, i32* %10
%71 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.6, i64 0, i64 0), i32 %70)
%72 = load i32*, i32** %6
%73 = call i32 @free(i32* %72)
ret i32 0
}

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