#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 char uint8_t;
typedef int /*<<< orphan*/ data;

/* Variables and functions */
int /*<<< orphan*/ memset(char *, char, int);
int /*<<< orphan*/ printf(char *, char const *);
char *rsearchstr(int, char *, int, char *);
char *searchstr(int, char const *, int, char *);
int strlen(char const *);
int main() {
  char *s = "ababbbcabaabbbabababbbababbba";
  char *p = "ababbba";
  printf("rsrch %s\n", rsearchstr(strlen(s), s, strlen(p), p));
  printf("rsrch %s\n", rsearchstr(strlen(s) - 1, s, strlen(p), p));
  const uint8_t *pos1 = searchstr(strlen(s), s, strlen(p), p);
  printf("srch %s\n", pos1);
  printf("srch %s\n", searchstr(strlen(pos1 + 1), pos1 + 1, strlen(p), p));
  char data[10000];
  memset(data, 'a', sizeof(data));
  for (int i = 0; i < 10000; i += 10) {
    data[i] = 'b';
  }
  data[10000 - 10] = 'a';
  data[10000 - 11] = 'b';
  p = "aaaaaaaaaa";
  printf("srch %s\n", searchstr(sizeof(data), data, strlen(p), p));
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x2780,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe97(%rip),%rax
mov    %rax,-0x10(%rbp)
lea    0xea2(%rip),%rax
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x2774(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x2770(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x2774(%rbp),%edi
mov    -0x2770(%rbp),%rsi
mov    %eax,%edx
mov    -0x18(%rbp),%rcx
call   1070 <rsearchstr@plt>
mov    %rax,%rsi
lea    0xe61(%rip),%rdi
call   1040 <printf@plt>
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
sub    $0x1,%eax
mov    %eax,-0x2764(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x2760(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x2764(%rbp),%edi
mov    -0x2760(%rbp),%rsi
mov    %eax,%edx
mov    -0x18(%rbp),%rcx
call   1070 <rsearchstr@plt>
mov    %rax,%rsi
lea    0xe14(%rip),%rdi
call   1040 <printf@plt>
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x2754(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x2750(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x2754(%rbp),%edi
mov    -0x2750(%rbp),%rsi
mov    %eax,%edx
mov    -0x18(%rbp),%rcx
call   1060 <searchstr@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rsi
lea    0xdc6(%rip),%rdi
call   1040 <printf@plt>
mov    -0x20(%rbp),%rdi
add    $0x1,%rdi
call   1030 <strlen@plt>
mov    %eax,-0x2744(%rbp)
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x2740(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x2744(%rbp),%edi
mov    -0x2740(%rbp),%rsi
mov    %eax,%edx
mov    -0x18(%rbp),%rcx
call   1060 <searchstr@plt>
mov    %rax,%rsi
lea    0xd74(%rip),%rdi
call   1040 <printf@plt>
lea    -0x2730(%rbp),%rdi
mov    $0x61,%esi
mov    $0x2710,%edx
call   1050 <memset@plt>
movl   $0x0,-0x2734(%rbp)
cmpl   $0x2710,-0x2734(%rbp)
jge    1303 <main+0x1b3>
movslq -0x2734(%rbp),%rax
movb   $0x62,-0x2730(%rbp,%rax,1)
62
mov    -0x2734(%rbp),%eax
add    $0xa,%eax
mov    %eax,-0x2734(%rbp)
jmp    12d0 <main+0x180>
movb   $0x61,-0x2a(%rbp)
movb   $0x62,-0x2b(%rbp)
lea    0xd16(%rip),%rax
mov    %rax,-0x18(%rbp)
lea    -0x2730(%rbp),%rax
mov    %rax,-0x2780(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x2780(%rbp),%rsi
mov    %eax,%edx
mov    -0x18(%rbp),%rcx
mov    $0x2710,%edi
call   1060 <searchstr@plt>
mov    %rax,%rsi
lea    0xcd1(%rip),%rdi
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x2780,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x2710,%rsp
lea    0xe9a(%rip),%r14
mov    %r14,%rdi
call   1030 <strlen@plt>
mov    %eax,%ebp
lea    0xe9f(%rip),%r12
mov    %r12,%rdi
call   1030 <strlen@plt>
mov    %ebp,%edi
mov    %r14,%rsi
mov    %eax,%edx
mov    %r12,%rcx
call   1070 <rsearchstr@plt>
lea    0xe89(%rip),%r15
mov    %r15,%rdi
mov    %rax,%rsi
call   1040 <printf@plt>
mov    %r14,%rdi
call   1030 <strlen@plt>
lea    -0x1(%rax),%ebp
mov    %r12,%rdi
call   1030 <strlen@plt>
mov    %ebp,%edi
mov    %r14,%rsi
mov    %eax,%edx
mov    %r12,%rcx
call   1070 <rsearchstr@plt>
mov    %r15,%rdi
mov    %rax,%rsi
call   1040 <printf@plt>
mov    %r14,%rdi
call   1030 <strlen@plt>
mov    %eax,%ebp
mov    %r12,%rdi
call   1030 <strlen@plt>
mov    %ebp,%edi
mov    %r14,%rsi
mov    %eax,%edx
mov    %r12,%rcx
call   1060 <searchstr@plt>
mov    %rax,%rbp
lea    0xe27(%rip),%r14
mov    %r14,%rdi
mov    %rax,%rsi
call   1040 <printf@plt>
add    $0x1,%rbp
mov    %rbp,%rdi
call   1030 <strlen@plt>
mov    %eax,%ebx
mov    %r12,%rdi
call   1030 <strlen@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    %eax,%edx
mov    %r12,%rcx
call   1060 <searchstr@plt>
mov    %r14,%rdi
mov    %rax,%rsi
call   1040 <printf@plt>
mov    %rsp,%rdi
mov    $0x61,%esi
mov    $0x2710,%edx
call   1050 <memset@plt>
mov    $0xffffffffffffffb0,%rax
nopl   0x0(%rax)
movb   $0x62,0x50(%rsp,%rax,1)
movb   $0x62,0x5a(%rsp,%rax,1)
movb   $0x62,0x64(%rsp,%rax,1)
movb   $0x62,0x6e(%rsp,%rax,1)
movb   $0x62,0x78(%rsp,%rax,1)
movb   $0x62,0x82(%rsp,%rax,1)
62
movb   $0x62,0x8c(%rsp,%rax,1)
62
movb   $0x62,0x96(%rsp,%rax,1)
62
add    $0x50,%rax
cmp    $0x26c0,%rax
jb     1250 <main+0x100>
movw   $0x6162,0x2705(%rsp)
lea    0xd8a(%rip),%rbx
mov    %rbx,%rdi
call   1030 <strlen@plt>
mov    %rsp,%rsi
mov    $0x2710,%edi
mov    %eax,%edx
mov    %rbx,%rcx
call   1060 <searchstr@plt>
lea    0xd60(%rip),%rdi
mov    %rax,%rsi
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x2710,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [30 x i8] c"ababbbcabaabbbabababbbababbba\00"
@.str.1 = [8 x i8] c"ababbba\00"
@.str.2 = [10 x i8] c"rsrch %s\0A\00"
@.str.3 = [9 x i8] c"srch %s\0A\00"
@.str.4 = [11 x i8] c"aaaaaaaaaa\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca [10000 x i8]
%6 = alloca i32
store i32 0, i32* %1
store i8* getelementptr inbounds ([30 x i8], [30 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
%7 = load i8*, i8** %2
%8 = call i32 @strlen(i8* %7)
%9 = load i8*, i8** %2
%10 = load i8*, i8** %3
%11 = call i32 @strlen(i8* %10)
%12 = load i8*, i8** %3
%13 = call i8* @rsearchstr(i32 %8, i8* %9, i32 %11, i8* %12)
%14 = call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i8* %13)
%15 = load i8*, i8** %2
%16 = call i32 @strlen(i8* %15)
%17 = sub nsw i32 %16, 1
%18 = load i8*, i8** %2
%19 = load i8*, i8** %3
%20 = call i32 @strlen(i8* %19)
%21 = load i8*, i8** %3
%22 = call i8* @rsearchstr(i32 %17, i8* %18, i32 %20, i8* %21)
%23 = call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i8* %22)
%24 = load i8*, i8** %2
%25 = call i32 @strlen(i8* %24)
%26 = load i8*, i8** %2
%27 = load i8*, i8** %3
%28 = call i32 @strlen(i8* %27)
%29 = load i8*, i8** %3
%30 = call i8* @searchstr(i32 %25, i8* %26, i32 %28, i8* %29)
store i8* %30, i8** %4
%31 = load i8*, i8** %4
%32 = call i32 @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i8* %31)
%33 = load i8*, i8** %4
%34 = getelementptr inbounds i8, i8* %33, i64 1
%35 = call i32 @strlen(i8* %34)
%36 = load i8*, i8** %4
%37 = getelementptr inbounds i8, i8* %36, i64 1
%38 = load i8*, i8** %3
%39 = call i32 @strlen(i8* %38)
%40 = load i8*, i8** %3
%41 = call i8* @searchstr(i32 %35, i8* %37, i32 %39, i8* %40)
%42 = call i32 @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i8* %41)
%43 = getelementptr inbounds [10000 x i8], [10000 x i8]* %5, i64 0, i64 0
%44 = call i32 @memset(i8* %43, i8 signext 97, i32 10000)
store i32 0, i32* %6
br label %45
45: ; preds = %52, %0
%46 = load i32, i32* %6
%47 = icmp slt i32 %46, 10000
br i1 %47, label %48, label %55
48: ; preds = %45
%49 = load i32, i32* %6
%50 = sext i32 %49 to i64
%51 = getelementptr inbounds [10000 x i8], [10000 x i8]* %5, i64 0, i64 %50
store i8 98, i8* %51
br label %52
52: ; preds = %48
%53 = load i32, i32* %6
%54 = add nsw i32 %53, 10
store i32 %54, i32* %6
br label %45
55: ; preds = %45
%56 = getelementptr inbounds [10000 x i8], [10000 x i8]* %5, i64 0, i64 9990
store i8 97, i8* %56
%57 = getelementptr inbounds [10000 x i8], [10000 x i8]* %5, i64 0, i64 9989
store i8 98, i8* %57
store i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0), i8** %3
%58 = getelementptr inbounds [10000 x i8], [10000 x i8]* %5, i64 0, i64 0
%59 = load i8*, i8** %3
%60 = call i32 @strlen(i8* %59)
%61 = load i8*, i8** %3
%62 = call i8* @searchstr(i32 10000, i8* %58, i32 %60, i8* %61)
%63 = call i32 @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i8* %62)
ret i32 0
}

LLVM IR (-O2):
@.str = [30 x i8] c"ababbbcabaabbbabababbbababbba\00"
@.str.1 = [8 x i8] c"ababbba\00"
@.str.2 = [10 x i8] c"rsrch %s\0A\00"
@.str.3 = [9 x i8] c"srch %s\0A\00"
@.str.4 = [11 x i8] c"aaaaaaaaaa\00"
define i32 @main() {
%1 = alloca [10000 x i8]
%2 = tail call i32 @strlen(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0))
%3 = tail call i32 @strlen(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
%4 = tail call i8* @rsearchstr(i32 %2, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i32 %3, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
%5 = tail call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i8* %4)
%6 = tail call i32 @strlen(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0))
%7 = add nsw i32 %6, -1
%8 = tail call i32 @strlen(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
%9 = tail call i8* @rsearchstr(i32 %7, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i32 %8, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
%10 = tail call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i8* %9)
%11 = tail call i32 @strlen(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0))
%12 = tail call i32 @strlen(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
%13 = tail call i8* @searchstr(i32 %11, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i32 %12, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
%14 = tail call i32 @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i8* %13)
%15 = getelementptr inbounds i8, i8* %13, i64 1
%16 = tail call i32 @strlen(i8* %15)
%17 = tail call i32 @strlen(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
%18 = tail call i8* @searchstr(i32 %16, i8* %15, i32 %17, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
%19 = tail call i32 @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i8* %18)
%20 = getelementptr inbounds [10000 x i8], [10000 x i8]* %1, i64 0, i64 0
%21 = call i32 @memset(i8* %20, i8 signext 97, i32 10000)
br label %28
22: ; preds = %28
%23 = getelementptr inbounds [10000 x i8], [10000 x i8]* %1, i64 0, i64 9990
store i8 97, i8* %23
%24 = getelementptr inbounds [10000 x i8], [10000 x i8]* %1, i64 0, i64 9989
store i8 98, i8* %24
%25 = call i32 @strlen(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0))
%26 = call i8* @searchstr(i32 10000, i8* %20, i32 %25, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0))
%27 = call i32 @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i8* %26)
ret i32 0
28: ; preds = %28, %0
%29 = phi i64 [ 0, %0 ], [ %45, %28 ]
%30 = getelementptr inbounds [10000 x i8], [10000 x i8]* %1, i64 0, i64 %29
store i8 98, i8* %30
%31 = or i64 %29, 10
%32 = getelementptr inbounds [10000 x i8], [10000 x i8]* %1, i64 0, i64 %31
store i8 98, i8* %32
%33 = add nuw nsw i64 %29, 20
%34 = getelementptr inbounds [10000 x i8], [10000 x i8]* %1, i64 0, i64 %33
store i8 98, i8* %34
%35 = add nuw nsw i64 %29, 30
%36 = getelementptr inbounds [10000 x i8], [10000 x i8]* %1, i64 0, i64 %35
store i8 98, i8* %36
%37 = add nuw nsw i64 %29, 40
%38 = getelementptr inbounds [10000 x i8], [10000 x i8]* %1, i64 0, i64 %37
store i8 98, i8* %38
%39 = add nuw nsw i64 %29, 50
%40 = getelementptr inbounds [10000 x i8], [10000 x i8]* %1, i64 0, i64 %39
store i8 98, i8* %40
%41 = add nuw nsw i64 %29, 60
%42 = getelementptr inbounds [10000 x i8], [10000 x i8]* %1, i64 0, i64 %41
store i8 98, i8* %42
%43 = add nuw nsw i64 %29, 70
%44 = getelementptr inbounds [10000 x i8], [10000 x i8]* %1, i64 0, i64 %43
store i8 98, i8* %44
%45 = add nuw nsw i64 %29, 80
%46 = icmp ult i64 %29, 9920
br i1 %46, label %28, label %22
}