#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 /*<<< orphan*/ puts(char *);
int strlen(char *);
int string_match(char *text, char *pattern) {
  if (!text || !pattern) {
    puts("NULL has been passed as one of the parameters");
    return -2;
  }
  int len_of_text = strlen(text);
  int len_of_pattern = strlen(pattern);
  if (len_of_text < len_of_pattern) {
    puts("Length of text is less than length of the pattern that has to be "
         "matched");
    return -3;
  }
  if (len_of_text == 0 || len_of_pattern == 0) {
    puts("Length of text or pattern is equal to 0");
    return -4;
  }
  int i = 0, j = 0;
  for (i = (len_of_text - len_of_pattern); i >= 0; i--) {
    j = 0;
    while ((j < len_of_pattern) && (pattern[j] == text[i + j])) {
      j++;
    }
    if (j == len_of_pattern) {
      return i + 1;
    }
  }
  return -1;
}


x86 assembly (clang, -O0):
<string_match>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1146 <string_match+0x26>
cmpq   $0x0,-0x18(%rbp)
jne    115e <string_match+0x3e>
lea    0xeb3(%rip),%rdi
call   1030 <puts@plt>
movl   $0xfffffffe,-0x4(%rbp)
jmp    1276 <string_match+0x156>
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x20(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x20(%rbp),%eax
jge    119a <string_match+0x7a>
lea    0xea5(%rip),%rdi
call   1030 <puts@plt>
movl   $0xfffffffd,-0x4(%rbp)
jmp    1276 <string_match+0x156>
cmpl   $0x0,-0x1c(%rbp)
je     11ae <string_match+0x8e>
cmpl   $0x0,-0x20(%rbp)
jne    11c6 <string_match+0xa6>
lea    0xec2(%rip),%rdi
call   1030 <puts@plt>
movl   $0xfffffffc,-0x4(%rbp)
jmp    1276 <string_match+0x156>
movl   $0x0,-0x24(%rbp)
movl   $0x0,-0x28(%rbp)
mov    -0x1c(%rbp),%eax
sub    -0x20(%rbp),%eax
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jl     126f <string_match+0x14f>
movl   $0x0,-0x28(%rbp)
mov    -0x28(%rbp),%ecx
xor    %eax,%eax
cmp    -0x20(%rbp),%ecx
mov    %al,-0x29(%rbp)
jge    1224 <string_match+0x104>
mov    -0x18(%rbp),%rax
movslq -0x28(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
mov    -0x10(%rbp),%rcx
mov    -0x24(%rbp),%edx
add    -0x28(%rbp),%edx
movslq %edx,%rdx
movsbl (%rcx,%rdx,1),%ecx
cmp    %ecx,%eax
sete   %al
mov    %al,-0x29(%rbp)
mov    -0x29(%rbp),%al
test   $0x1,%al
jne    1234 <string_match+0x114>
jmp    1242 <string_match+0x122>
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
jmp    11ee <string_match+0xce>
mov    -0x28(%rbp),%eax
cmp    -0x20(%rbp),%eax
jne    125c <string_match+0x13c>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x4(%rbp)
jmp    1276 <string_match+0x156>
jmp    1261 <string_match+0x141>
mov    -0x24(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x24(%rbp)
jmp    11dd <string_match+0xbd>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<string_match>:
push   %rbp
push   %r14
push   %rbx
test   %rdi,%rdi
je     115d <string_match+0x3d>
mov    %rsi,%r14
test   %rsi,%rsi
je     115d <string_match+0x3d>
mov    %rdi,%rbx
call   1040 <strlen@plt>
mov    %eax,%ebp
mov    %r14,%rdi
call   1040 <strlen@plt>
cmp    %eax,%ebp
jge    1170 <string_match+0x50>
lea    0xee0(%rip),%rdi
call   1030 <puts@plt>
mov    $0xfffffffd,%eax
jmp    11e3 <string_match+0xc3>
lea    0xe9c(%rip),%rdi
call   1030 <puts@plt>
mov    $0xfffffffe,%eax
jmp    11e3 <string_match+0xc3>
test   %ebp,%ebp
je     11cb <string_match+0xab>
mov    %eax,%ecx
test   %eax,%eax
je     11cb <string_match+0xab>
sub    %ecx,%ebp
mov    $0xffffffff,%eax
js     11e3 <string_match+0xc3>
mov    %ebp,%ebp
mov    %ecx,%esi
add    %rbp,%rbx
mov    $0x0,%edi
test   %ecx,%ecx
jle    11b5 <string_match+0x95>
xor    %edi,%edi
cs nopw 0x0(%rax,%rax,1)
nop
movzbl (%r14,%rdi,1),%edx
cmp    (%rbx,%rdi,1),%dl
jne    11b5 <string_match+0x95>
add    $0x1,%rdi
cmp    %rdi,%rsi
jne    11a0 <string_match+0x80>
jmp    11de <string_match+0xbe>
cmp    %ecx,%edi
je     11de <string_match+0xbe>
lea    -0x1(%rbp),%rdx
add    $0xffffffffffffffff,%rbx
test   %rbp,%rbp
mov    %rdx,%rbp
jg     118a <string_match+0x6a>
jmp    11e3 <string_match+0xc3>
lea    0xea5(%rip),%rdi
call   1030 <puts@plt>
mov    $0xfffffffc,%eax
jmp    11e3 <string_match+0xc3>
add    $0x1,%ebp
mov    %ebp,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [46 x i8] c"NULL has been passed as one of the parameters\00"
@.str.1 = [73 x i8] c"Length of text is less than length of the pattern that has to be matched\00"
@.str.2 = [40 x i8] c"Length of text or pattern is equal to 0\00"
define i32 @string_match(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
%10 = load i8*, i8** %4
%11 = icmp ne i8* %10, null
br i1 %11, label %12, label %15
12: ; preds = %2
%13 = load i8*, i8** %5
%14 = icmp ne i8* %13, null
br i1 %14, label %17, label %15
15: ; preds = %12, %2
%16 = call i32 @puts(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str, i64 0, i64 0))
store i32 -2, i32* %3
br label %80
17: ; preds = %12
%18 = load i8*, i8** %4
%19 = call i32 @strlen(i8* %18)
store i32 %19, i32* %6
%20 = load i8*, i8** %5
%21 = call i32 @strlen(i8* %20)
store i32 %21, i32* %7
%22 = load i32, i32* %6
%23 = load i32, i32* %7
%24 = icmp slt i32 %22, %23
br i1 %24, label %25, label %27
25: ; preds = %17
%26 = call i32 @puts(i8* getelementptr inbounds ([73 x i8], [73 x i8]* @.str.1, i64 0, i64 0))
store i32 -3, i32* %3
br label %80
27: ; preds = %17
%28 = load i32, i32* %6
%29 = icmp eq i32 %28, 0
br i1 %29, label %33, label %30
30: ; preds = %27
%31 = load i32, i32* %7
%32 = icmp eq i32 %31, 0
br i1 %32, label %33, label %35
33: ; preds = %30, %27
%34 = call i32 @puts(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.2, i64 0, i64 0))
store i32 -4, i32* %3
br label %80
35: ; preds = %30
store i32 0, i32* %8
store i32 0, i32* %9
%36 = load i32, i32* %6
%37 = load i32, i32* %7
%38 = sub nsw i32 %36, %37
store i32 %38, i32* %8
br label %39
39: ; preds = %76, %35
%40 = load i32, i32* %8
%41 = icmp sge i32 %40, 0
br i1 %41, label %42, label %79
42: ; preds = %39
store i32 0, i32* %9
br label %43
43: ; preds = %65, %42
%44 = load i32, i32* %9
%45 = load i32, i32* %7
%46 = icmp slt i32 %44, %45
br i1 %46, label %47, label %63
47: ; preds = %43
%48 = load i8*, i8** %5
%49 = load i32, i32* %9
%50 = sext i32 %49 to i64
%51 = getelementptr inbounds i8, i8* %48, i64 %50
%52 = load i8, i8* %51
%53 = sext i8 %52 to i32
%54 = load i8*, i8** %4
%55 = load i32, i32* %8
%56 = load i32, i32* %9
%57 = add nsw i32 %55, %56
%58 = sext i32 %57 to i64
%59 = getelementptr inbounds i8, i8* %54, i64 %58
%60 = load i8, i8* %59
%61 = sext i8 %60 to i32
%62 = icmp eq i32 %53, %61
br label %63
63: ; preds = %47, %43
%64 = phi i1 [ false, %43 ], [ %62, %47 ]
br i1 %64, label %65, label %68
65: ; preds = %63
%66 = load i32, i32* %9
%67 = add nsw i32 %66, 1
store i32 %67, i32* %9
br label %43
68: ; preds = %63
%69 = load i32, i32* %9
%70 = load i32, i32* %7
%71 = icmp eq i32 %69, %70
br i1 %71, label %72, label %75
72: ; preds = %68
%73 = load i32, i32* %8
%74 = add nsw i32 %73, 1
store i32 %74, i32* %3
br label %80
75: ; preds = %68
br label %76
76: ; preds = %75
%77 = load i32, i32* %8
%78 = add nsw i32 %77, -1
store i32 %78, i32* %8
br label %39
79: ; preds = %39
store i32 -1, i32* %3
br label %80
80: ; preds = %79, %72, %33, %25, %15
%81 = load i32, i32* %3
ret i32 %81
}

LLVM IR (-O2):
@.str = [46 x i8] c"NULL has been passed as one of the parameters\00"
@.str.1 = [73 x i8] c"Length of text is less than length of the pattern that has to be matched\00"
@.str.2 = [40 x i8] c"Length of text or pattern is equal to 0\00"
define i32 @string_match(i8* %0, i8* %1) {
%3 = icmp ne i8* %0, null
%4 = icmp ne i8* %1, null
%5 = and i1 %3, %4
br i1 %5, label %8, label %6
6: ; preds = %2
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([46 x i8], [46 x i8]* @.str, i64 0, i64 0))
br label %51
8: ; preds = %2
%9 = tail call i32 @strlen(i8* %0)
%10 = tail call i32 @strlen(i8* %1)
%11 = icmp slt i32 %9, %10
br i1 %11, label %12, label %14
12: ; preds = %8
%13 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([73 x i8], [73 x i8]* @.str.1, i64 0, i64 0))
br label %51
14: ; preds = %8
%15 = icmp eq i32 %9, 0
%16 = icmp eq i32 %10, 0
%17 = select i1 %15, i1 true, i1 %16
br i1 %17, label %18, label %20
18: ; preds = %14
%19 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([40 x i8], [40 x i8]* @.str.2, i64 0, i64 0))
br label %51
20: ; preds = %14
%21 = sub i32 %9, %10
%22 = icmp sgt i32 %21, -1
br i1 %22, label %23, label %51
23: ; preds = %20
%24 = icmp sgt i32 %10, 0
%25 = zext i32 %21 to i64
%26 = zext i32 %10 to i64
br label %27
27: ; preds = %23, %48
%28 = phi i64 [ %25, %23 ], [ %49, %48 ]
br i1 %24, label %29, label %42
29: ; preds = %27, %37
%30 = phi i64 [ %38, %37 ], [ 0, %27 ]
%31 = getelementptr inbounds i8, i8* %1, i64 %30
%32 = load i8, i8* %31
%33 = add nsw i64 %30, %28
%34 = getelementptr inbounds i8, i8* %0, i64 %33
%35 = load i8, i8* %34
%36 = icmp eq i8 %32, %35
br i1 %36, label %37, label %40
37: ; preds = %29
%38 = add nuw nsw i64 %30, 1
%39 = icmp eq i64 %38, %26
br i1 %39, label %45, label %29
40: ; preds = %29
%41 = trunc i64 %30 to i32
br label %42
42: ; preds = %40, %27
%43 = phi i32 [ 0, %27 ], [ %41, %40 ]
%44 = icmp eq i32 %43, %10
br i1 %44, label %45, label %48
45: ; preds = %42, %37
%46 = trunc i64 %28 to i32
%47 = add nsw i32 %46, 1
br label %51
48: ; preds = %42
%49 = add nsw i64 %28, -1
%50 = icmp sgt i64 %28, 0
br i1 %50, label %27, label %51
51: ; preds = %48, %20, %12, %18, %45, %6
%52 = phi i32 [ -2, %6 ], [ -3, %12 ], [ -4, %18 ], [ %47, %45 ], [ -1, %20 ], [ -1, %48 ]
ret i32 %52
}