#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 */
typedef int /*<<< orphan*/ txt;
typedef int /*<<< orphan*/ pat;

/* Variables and functions */
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ rabin_karp_search(char *, char *, int);
int /*<<< orphan*/ stdin;
int strlen(char *);
int main(void) {
  char txt[1000], pat[100];
  int txt_len, pat_len;
  int q = 1001;
  printf("Enter test string: ");
  fgets(txt, sizeof(txt), stdin);
  printf("Enter pattern to find: ");
  fgets(pat, sizeof(pat), stdin);
  txt_len = strlen(txt);
  pat_len = strlen(pat);
  if (txt[txt_len - 1] == '\n')
    txt[txt_len - 1] = '\0';
  if (pat[pat_len - 1] == '\n')
    pat[pat_len - 1] = '\0';
  rabin_karp_search(pat, txt, q);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x470,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x3e9,-0x46c(%rbp)
lea    0xe9d(%rip),%rdi
call   1040 <printf@plt>
lea    -0x3f0(%rbp),%rdi
mov    0x2e72(%rip),%rax
mov    (%rax),%edx
mov    $0x3e8,%esi
call   1050 <fgets@plt>
lea    0xe8b(%rip),%rdi
call   1040 <printf@plt>
lea    -0x460(%rbp),%rdi
mov    0x2e4c(%rip),%rax
mov    (%rax),%edx
mov    $0x64,%esi
call   1050 <fgets@plt>
lea    -0x3f0(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x464(%rbp)
lea    -0x460(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x468(%rbp)
mov    -0x464(%rbp),%eax
sub    $0x1,%eax
cltq
movsbl -0x3f0(%rbp,%rax,1),%eax
ff
cmp    $0xa,%eax
jne    11fb <main+0xbb>
mov    -0x464(%rbp),%eax
sub    $0x1,%eax
cltq
movb   $0x0,-0x3f0(%rbp,%rax,1)
mov    -0x468(%rbp),%eax
sub    $0x1,%eax
cltq
movsbl -0x460(%rbp,%rax,1),%eax
ff
cmp    $0xa,%eax
jne    122a <main+0xea>
mov    -0x468(%rbp),%eax
sub    $0x1,%eax
cltq
movb   $0x0,-0x460(%rbp,%rax,1)
lea    -0x460(%rbp),%rdi
lea    -0x3f0(%rbp),%rsi
mov    -0x46c(%rbp),%edx
call   1060 <rabin_karp_search@plt>
xor    %eax,%eax
add    $0x470,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x460,%rsp
lea    0xeae(%rip),%rdi
call   1040 <printf@plt>
mov    0x2e8a(%rip),%rbx
mov    (%rbx),%edx
lea    0x70(%rsp),%r14
mov    %r14,%rdi
mov    $0x3e8,%esi
call   1050 <fgets@plt>
lea    0xe9b(%rip),%rdi
call   1040 <printf@plt>
mov    (%rbx),%edx
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0x64,%esi
call   1050 <fgets@plt>
mov    %r14,%rdi
call   1030 <strlen@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1030 <strlen@plt>
movslq %ebp,%rcx
cmpb   $0xa,-0x1(%rcx,%r14,1)
jne    11b2 <main+0x72>
movb   $0x0,0x6f(%rsp,%rcx,1)
cltq
cmpb   $0xa,-0x1(%rax,%rbx,1)
jne    11c0 <main+0x80>
movb   $0x0,-0x1(%rsp,%rax,1)
mov    %rsp,%rdi
lea    0x70(%rsp),%rsi
mov    $0x3e9,%edx
call   1060 <rabin_karp_search@plt>
xor    %eax,%eax
add    $0x460,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [20 x i8] c"Enter test string: \00"
@stdin = global i32 0
@.str.1 = [24 x i8] c"Enter pattern to find: \00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [1000 x i8]
%3 = alloca [100 x i8]
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store i32 0, i32* %1
store i32 1001, i32* %6
%7 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
%8 = getelementptr inbounds [1000 x i8], [1000 x i8]* %2, i64 0, i64 0
%9 = load i32, i32* @stdin
%10 = call i32 @fgets(i8* %8, i32 1000, i32 %9)
%11 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
%12 = getelementptr inbounds [100 x i8], [100 x i8]* %3, i64 0, i64 0
%13 = load i32, i32* @stdin
%14 = call i32 @fgets(i8* %12, i32 100, i32 %13)
%15 = getelementptr inbounds [1000 x i8], [1000 x i8]* %2, i64 0, i64 0
%16 = call i32 @strlen(i8* %15)
store i32 %16, i32* %4
%17 = getelementptr inbounds [100 x i8], [100 x i8]* %3, i64 0, i64 0
%18 = call i32 @strlen(i8* %17)
store i32 %18, i32* %5
%19 = load i32, i32* %4
%20 = sub nsw i32 %19, 1
%21 = sext i32 %20 to i64
%22 = getelementptr inbounds [1000 x i8], [1000 x i8]* %2, i64 0, i64 %21
%23 = load i8, i8* %22
%24 = sext i8 %23 to i32
%25 = icmp eq i32 %24, 10
br i1 %25, label %26, label %31
26: ; preds = %0
%27 = load i32, i32* %4
%28 = sub nsw i32 %27, 1
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds [1000 x i8], [1000 x i8]* %2, i64 0, i64 %29
store i8 0, i8* %30
br label %31
31: ; preds = %26, %0
%32 = load i32, i32* %5
%33 = sub nsw i32 %32, 1
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds [100 x i8], [100 x i8]* %3, i64 0, i64 %34
%36 = load i8, i8* %35
%37 = sext i8 %36 to i32
%38 = icmp eq i32 %37, 10
br i1 %38, label %39, label %44
39: ; preds = %31
%40 = load i32, i32* %5
%41 = sub nsw i32 %40, 1
%42 = sext i32 %41 to i64
%43 = getelementptr inbounds [100 x i8], [100 x i8]* %3, i64 0, i64 %42
store i8 0, i8* %43
br label %44
44: ; preds = %39, %31
%45 = getelementptr inbounds [100 x i8], [100 x i8]* %3, i64 0, i64 0
%46 = getelementptr inbounds [1000 x i8], [1000 x i8]* %2, i64 0, i64 0
%47 = load i32, i32* %6
%48 = call i32 @rabin_karp_search(i8* %45, i8* %46, i32 %47)
ret i32 0
}

LLVM IR (-O2):
@.str = [20 x i8] c"Enter test string: \00"
@stdin = global i32 0
@.str.1 = [24 x i8] c"Enter pattern to find: \00"
define i32 @main() {
%1 = alloca [1000 x i8]
%2 = alloca [100 x i8]
%3 = getelementptr inbounds [1000 x i8], [1000 x i8]* %1, i64 0, i64 0
%4 = getelementptr inbounds [100 x i8], [100 x i8]* %2, i64 0, i64 0
%5 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
%6 = load i32, i32* @stdin
%7 = call i32 @fgets(i8* %3, i32 1000, i32 %6)
%8 = call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
%9 = load i32, i32* @stdin
%10 = call i32 @fgets(i8* %4, i32 100, i32 %9)
%11 = call i32 @strlen(i8* %3)
%12 = call i32 @strlen(i8* %4)
%13 = add nsw i32 %11, -1
%14 = sext i32 %13 to i64
%15 = getelementptr inbounds [1000 x i8], [1000 x i8]* %1, i64 0, i64 %14
%16 = load i8, i8* %15
%17 = icmp eq i8 %16, 10
br i1 %17, label %18, label %19
18: ; preds = %0
store i8 0, i8* %15
br label %19
19: ; preds = %18, %0
%20 = add nsw i32 %12, -1
%21 = sext i32 %20 to i64
%22 = getelementptr inbounds [100 x i8], [100 x i8]* %2, i64 0, i64 %21
%23 = load i8, i8* %22
%24 = icmp eq i8 %23, 10
br i1 %24, label %25, label %26
25: ; preds = %19
store i8 0, i8* %22
br label %26
26: ; preds = %25, %19
%27 = call i32 @rabin_karp_search(i8* %4, i8* %3, i32 1001)
ret i32 0
}