#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 = 0; i <= (len_of_text - len_of_pattern); 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    127b <string_match+0x15b>
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    127b <string_match+0x15b>
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    127b <string_match+0x15b>
movl   $0x0,-0x24(%rbp)
movl   $0x0,-0x28(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
mov    -0x1c(%rbp),%ecx
sub    -0x20(%rbp),%ecx
cmp    %ecx,%eax
jg     1274 <string_match+0x154>
movl   $0x0,-0x28(%rbp)
mov    -0x28(%rbp),%ecx
xor    %eax,%eax
cmp    -0x20(%rbp),%ecx
mov    %al,-0x29(%rbp)
jge    1229 <string_match+0x109>
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    1239 <string_match+0x119>
jmp    1247 <string_match+0x127>
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
jmp    11f3 <string_match+0xd3>
mov    -0x28(%rbp),%eax
cmp    -0x20(%rbp),%eax
jne    1261 <string_match+0x141>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x4(%rbp)
jmp    127b <string_match+0x15b>
jmp    1266 <string_match+0x146>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    11db <string_match+0xbb>
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     1161 <string_match+0x41>
mov    %rsi,%r14
test   %rsi,%rsi
je     1161 <string_match+0x41>
mov    %rdi,%rbx
call   1040 <strlen@plt>
mov    %eax,%ebp
mov    %r14,%rdi
call   1040 <strlen@plt>
mov    %ebp,%r8d
sub    %eax,%r8d
jge    1174 <string_match+0x54>
lea    0xedc(%rip),%rdi
call   1030 <puts@plt>
mov    $0xfffffffd,%ecx
jmp    11e0 <string_match+0xc0>
lea    0xe98(%rip),%rdi
call   1030 <puts@plt>
mov    $0xfffffffe,%ecx
jmp    11e0 <string_match+0xc0>
test   %ebp,%ebp
je     11c8 <string_match+0xa8>
test   %eax,%eax
je     11c8 <string_match+0xa8>
add    $0x1,%r8d
mov    %eax,%edi
xor    %esi,%esi
mov    $0x0,%ecx
test   %eax,%eax
jle    11b0 <string_match+0x90>
xor    %ecx,%ecx
nop
movzbl (%r14,%rcx,1),%edx
cmp    (%rbx,%rcx,1),%dl
jne    11b0 <string_match+0x90>
add    $0x1,%rcx
cmp    %rcx,%rdi
jne    1190 <string_match+0x70>
jmp    11db <string_match+0xbb>
cs nopw 0x0(%rax,%rax,1)
nop
cmp    %eax,%ecx
je     11db <string_match+0xbb>
add    $0x1,%rsi
add    $0x1,%rbx
mov    $0xffffffff,%ecx
cmp    %r8,%rsi
jne    1184 <string_match+0x64>
jmp    11e0 <string_match+0xc0>
lea    0xea8(%rip),%rdi
call   1030 <puts@plt>
mov    $0xfffffffc,%ecx
jmp    11e0 <string_match+0xc0>
add    $0x1,%esi
mov    %esi,%ecx
mov    %ecx,%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
store i32 0, i32* %8
br label %36
36: ; preds = %76, %35
%37 = load i32, i32* %8
%38 = load i32, i32* %6
%39 = load i32, i32* %7
%40 = sub nsw i32 %38, %39
%41 = icmp sle i32 %37, %40
br i1 %41, label %42, label %79
42: ; preds = %36
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 %36
79: ; preds = %36
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 %50
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 %50
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 %24, label %18
18: ; preds = %14
%19 = icmp sgt i32 %10, 0
%20 = add i32 %9, 1
%21 = sub i32 %20, %10
%22 = zext i32 %21 to i64
%23 = zext i32 %10 to i64
br label %26
24: ; preds = %14
%25 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([40 x i8], [40 x i8]* @.str.2, i64 0, i64 0))
br label %50
26: ; preds = %18, %47
%27 = phi i64 [ 0, %18 ], [ %48, %47 ]
br i1 %19, label %28, label %41
28: ; preds = %26, %36
%29 = phi i64 [ %37, %36 ], [ 0, %26 ]
%30 = getelementptr inbounds i8, i8* %1, i64 %29
%31 = load i8, i8* %30
%32 = add nuw nsw i64 %29, %27
%33 = getelementptr inbounds i8, i8* %0, i64 %32
%34 = load i8, i8* %33
%35 = icmp eq i8 %31, %34
br i1 %35, label %36, label %39
36: ; preds = %28
%37 = add nuw nsw i64 %29, 1
%38 = icmp eq i64 %37, %23
br i1 %38, label %44, label %28
39: ; preds = %28
%40 = trunc i64 %29 to i32
br label %41
41: ; preds = %39, %26
%42 = phi i32 [ 0, %26 ], [ %40, %39 ]
%43 = icmp eq i32 %42, %10
br i1 %43, label %44, label %47
44: ; preds = %41, %36
%45 = trunc i64 %27 to i32
%46 = add nuw nsw i32 %45, 1
br label %50
47: ; preds = %41
%48 = add nuw nsw i64 %27, 1
%49 = icmp eq i64 %48, %22
br i1 %49, label %50, label %26
50: ; preds = %47, %12, %24, %44, %6
%51 = phi i32 [ -2, %6 ], [ -3, %12 ], [ -4, %24 ], [ %46, %44 ], [ -1, %47 ]
ret i32 %51
}