#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 */
scalar_t__ is_equal_upto(char *, char *, unsigned int);
int /*<<< orphan*/ printf(char *, unsigned int);
unsigned int strlen(char *);
void naive_matcher(char *text, char *pat) {
  unsigned t = strlen(text), p = strlen(pat);
  if (p > t)
    return;
  for (unsigned i = 0; i < t - p + 1; i++) {
    if (is_equal_upto(pat, text + i, p - 1))
      printf("strings match at shift %d\n", i);
  }
}


x86 assembly (clang, -O0):
<naive_matcher>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jbe    1169 <naive_matcher+0x39>
jmp    11c9 <naive_matcher+0x99>
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    -0x14(%rbp),%ecx
sub    -0x18(%rbp),%ecx
add    $0x1,%ecx
cmp    %ecx,%eax
jae    11c9 <naive_matcher+0x99>
mov    -0x10(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x1c(%rbp),%eax
add    %rax,%rsi
mov    -0x18(%rbp),%edx
sub    $0x1,%edx
call   1050 <is_equal_upto@plt>
cmp    $0x0,%rax
je     11b6 <naive_matcher+0x86>
mov    -0x1c(%rbp),%esi
lea    0xe4f(%rip),%rdi
call   1040 <printf@plt>
jmp    11bb <naive_matcher+0x8b>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1170 <naive_matcher+0x40>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<naive_matcher>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%r15
call   1030 <strlen@plt>
mov    %eax,%ebx
mov    %r14,%rdi
call   1030 <strlen@plt>
sub    %eax,%ebx
jb     1198 <naive_matcher+0x68>
inc    %ebx
je     1198 <naive_matcher+0x68>
mov    %eax,%ebp
add    $0xffffffff,%ebp
mov    %ebx,%r13d
xor    %ebx,%ebx
lea    0xe97(%rip),%r12
jmp    1179 <naive_matcher+0x49>
nopl   0x0(%rax,%rax,1)
add    $0x1,%rbx
cmp    %rbx,%r13
je     1198 <naive_matcher+0x68>
lea    (%r15,%rbx,1),%rsi
mov    %r14,%rdi
mov    %ebp,%edx
call   1050 <is_equal_upto@plt>
test   %rax,%rax
je     1170 <naive_matcher+0x40>
mov    %r12,%rdi
mov    %ebx,%esi
call   1040 <printf@plt>
jmp    1170 <naive_matcher+0x40>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [27 x i8] c"strings match at shift %d\0A\00"
define void @naive_matcher(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store i8* %0, i8** %3
store i8* %1, i8** %4
%8 = load i8*, i8** %3
%9 = call i32 @strlen(i8* %8)
store i32 %9, i32* %5
%10 = load i8*, i8** %4
%11 = call i32 @strlen(i8* %10)
store i32 %11, i32* %6
%12 = load i32, i32* %6
%13 = load i32, i32* %5
%14 = icmp ugt i32 %12, %13
br i1 %14, label %15, label %16
15: ; preds = %2
br label %41
16: ; preds = %2
store i32 0, i32* %7
br label %17
17: ; preds = %38, %16
%18 = load i32, i32* %7
%19 = load i32, i32* %5
%20 = load i32, i32* %6
%21 = sub i32 %19, %20
%22 = add i32 %21, 1
%23 = icmp ult i32 %18, %22
br i1 %23, label %24, label %41
24: ; preds = %17
%25 = load i8*, i8** %4
%26 = load i8*, i8** %3
%27 = load i32, i32* %7
%28 = zext i32 %27 to i64
%29 = getelementptr inbounds i8, i8* %26, i64 %28
%30 = load i32, i32* %6
%31 = sub i32 %30, 1
%32 = call i64 @is_equal_upto(i8* %25, i8* %29, i32 %31)
%33 = icmp ne i64 %32, 0
br i1 %33, label %34, label %37
34: ; preds = %24
%35 = load i32, i32* %7
%36 = call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i32 %35)
br label %37
37: ; preds = %34, %24
br label %38
38: ; preds = %37
%39 = load i32, i32* %7
%40 = add i32 %39, 1
store i32 %40, i32* %7
br label %17
41: ; preds = %15, %17
ret void
}

LLVM IR (-O2):
@.str = [27 x i8] c"strings match at shift %d\0A\00"
define void @naive_matcher(i8* %0, i8* %1) {
%3 = tail call i32 @strlen(i8* %0)
%4 = tail call i32 @strlen(i8* %1)
%5 = icmp ult i32 %3, %4
br i1 %5, label %24, label %6
6: ; preds = %2
%7 = add i32 %3, 1
%8 = sub i32 %7, %4
%9 = icmp eq i32 %8, 0
br i1 %9, label %24, label %10
10: ; preds = %6
%11 = add i32 %4, -1
%12 = zext i32 %8 to i64
br label %13
13: ; preds = %10, %21
%14 = phi i64 [ 0, %10 ], [ %22, %21 ]
%15 = getelementptr inbounds i8, i8* %0, i64 %14
%16 = tail call i64 @is_equal_upto(i8* %1, i8* %15, i32 %11)
%17 = icmp eq i64 %16, 0
br i1 %17, label %21, label %18
18: ; preds = %13
%19 = trunc i64 %14 to i32
%20 = tail call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i32 %19)
br label %21
21: ; preds = %13, %18
%22 = add nuw nsw i64 %14, 1
%23 = icmp eq i64 %22, %12
br i1 %23, label %24, label %13
24: ; preds = %21, %6, %2
ret void
}