#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*/ __insn_prefetch(char const *);
size_t __strnlen(char const *, size_t);
char *strchr(char const *, char const);
size_t strlen(char const *);
char *strstr2(char const *, char const *);
char *strstr_scan(char const *, char const *, size_t);
char *two_way_long_needle(unsigned char const *, size_t, unsigned char const *,
                          size_t);
char *strstr(const char *haystack_start, const char *needle_start) {
  const char *haystack = haystack_start;
  const char *needle = needle_start;
  __insn_prefetch(haystack);
  size_t needle_len = strlen(needle_start);
  size_t haystack_len;
  if (needle_len <= 2) {
    if (needle_len == 1)
      return strchr(haystack_start, *needle_start);
    if (needle_len == 0)
      return (char *)haystack_start;
    else
      return strstr2(haystack_start, needle_start);
  }
  if (__strnlen(haystack, needle_len) < needle_len)
    return NULL;
  if (needle_len < 40)
    return strstr_scan(haystack_start, needle_start, needle_len);
  else {
    haystack = strstr2(haystack_start, needle_start);
    if (haystack == NULL)
      return NULL;
    needle = needle_start;
    haystack_len = (haystack > haystack_start + needle_len
                        ? 1
                        : needle_len + haystack_start - haystack);
    return two_way_long_needle((const unsigned char *)haystack, haystack_len,
                               (const unsigned char *)needle, needle_len);
  }
}


x86 assembly (clang, -O0):
<strstr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rdi
call   1070 <__insn_prefetch@plt>
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x2,-0x30(%rbp)
ja     1203 <strstr+0x93>
cmpq   $0x1,-0x30(%rbp)
jne    11d5 <strstr+0x65>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rax
movsbl (%rax),%esi
call   1050 <strchr@plt>
mov    %rax,-0x8(%rbp)
jmp    12d4 <strstr+0x164>
cmpq   $0x0,-0x30(%rbp)
jne    11ed <strstr+0x7d>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    12d4 <strstr+0x164>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1030 <strstr2@plt>
mov    %rax,-0x8(%rbp)
jmp    12d4 <strstr+0x164>
mov    -0x20(%rbp),%rdi
mov    -0x30(%rbp),%rsi
call   1080 <__strnlen@plt>
cmp    -0x30(%rbp),%rax
jae    1227 <strstr+0xb7>
movq   $0x0,-0x8(%rbp)
jmp    12d4 <strstr+0x164>
cmpq   $0x28,-0x30(%rbp)
jae    124c <strstr+0xdc>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x30(%rbp),%rdx
call   1060 <strstr_scan@plt>
mov    %rax,-0x8(%rbp)
jmp    12d4 <strstr+0x164>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1030 <strstr2@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1275 <strstr+0x105>
movq   $0x0,-0x8(%rbp)
jmp    12d4 <strstr+0x164>
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rax
mov    -0x10(%rbp),%rcx
add    -0x30(%rbp),%rcx
cmp    %rcx,%rax
jbe    12a0 <strstr+0x130>
mov    $0x1,%eax
mov    %rax,-0x40(%rbp)
jmp    12b3 <strstr+0x143>
mov    -0x30(%rbp),%rax
add    -0x10(%rbp),%rax
mov    -0x20(%rbp),%rcx
sub    %rcx,%rax
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x38(%rbp),%rsi
mov    -0x28(%rbp),%rdx
mov    -0x30(%rbp),%rcx
call   1090 <two_way_long_needle@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<strstr>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%r14
mov    %rdi,%r15
call   1070 <__insn_prefetch@plt>
mov    %r14,%rdi
call   1040 <strlen@plt>
mov    %rax,%rbx
cmp    $0x2,%rax
ja     11ad <strstr+0x3d>
test   %rbx,%rbx
je     11c0 <strstr+0x50>
cmp    $0x1,%rbx
jne    11e2 <strstr+0x72>
movsbl (%r14),%esi
mov    %r15,%rdi
pop    %rbx
pop    %r14
pop    %r15
jmp    1050 <strchr@plt>
mov    %r15,%rdi
mov    %rbx,%rsi
call   1080 <__strnlen@plt>
cmp    %rbx,%rax
jae    11c9 <strstr+0x59>
xor    %r15d,%r15d
mov    %r15,%rax
pop    %rbx
pop    %r14
pop    %r15
ret
cmp    $0x27,%rbx
ja     11f2 <strstr+0x82>
mov    %r15,%rdi
mov    %r14,%rsi
mov    %rbx,%rdx
pop    %rbx
pop    %r14
pop    %r15
jmp    1060 <strstr_scan@plt>
mov    %r15,%rdi
mov    %r14,%rsi
pop    %rbx
pop    %r14
pop    %r15
jmp    1030 <strstr2@plt>
mov    %r15,%rdi
mov    %r14,%rsi
call   1030 <strstr2@plt>
test   %rax,%rax
je     11bd <strstr+0x4d>
add    %rbx,%r15
sub    %rax,%r15
mov    $0x1,%esi
cmovae %r15,%rsi
mov    %rax,%rdi
mov    %r14,%rdx
mov    %rbx,%rcx
pop    %rbx
pop    %r14
pop    %r15
jmp    1090 <two_way_long_needle@plt>

LLVM IR (-O0):
define i8* @strstr(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i64
store i8* %0, i8** %4
store i8* %1, i8** %5
%10 = load i8*, i8** %4
store i8* %10, i8** %6
%11 = load i8*, i8** %5
store i8* %11, i8** %7
%12 = load i8*, i8** %6
%13 = call i32 @__insn_prefetch(i8* %12)
%14 = load i8*, i8** %5
%15 = call i64 @strlen(i8* %14)
store i64 %15, i64* %8
%16 = load i64, i64* %8
%17 = icmp ule i64 %16, 2
br i1 %17, label %18, label %35
18: ; preds = %2
%19 = load i64, i64* %8
%20 = icmp eq i64 %19, 1
br i1 %20, label %21, label %26
21: ; preds = %18
%22 = load i8*, i8** %4
%23 = load i8*, i8** %5
%24 = load i8, i8* %23
%25 = call i8* @strchr(i8* %22, i8 signext %24)
store i8* %25, i8** %3
br label %80
26: ; preds = %18
%27 = load i64, i64* %8
%28 = icmp eq i64 %27, 0
br i1 %28, label %29, label %31
29: ; preds = %26
%30 = load i8*, i8** %4
store i8* %30, i8** %3
br label %80
31: ; preds = %26
%32 = load i8*, i8** %4
%33 = load i8*, i8** %5
%34 = call i8* @strstr2(i8* %32, i8* %33)
store i8* %34, i8** %3
br label %80
35: ; preds = %2
%36 = load i8*, i8** %6
%37 = load i64, i64* %8
%38 = call i64 @__strnlen(i8* %36, i64 %37)
%39 = load i64, i64* %8
%40 = icmp ult i64 %38, %39
br i1 %40, label %41, label %42
41: ; preds = %35
store i8* null, i8** %3
br label %80
42: ; preds = %35
%43 = load i64, i64* %8
%44 = icmp ult i64 %43, 40
br i1 %44, label %45, label %50
45: ; preds = %42
%46 = load i8*, i8** %4
%47 = load i8*, i8** %5
%48 = load i64, i64* %8
%49 = call i8* @strstr_scan(i8* %46, i8* %47, i64 %48)
store i8* %49, i8** %3
br label %80
50: ; preds = %42
%51 = load i8*, i8** %4
%52 = load i8*, i8** %5
%53 = call i8* @strstr2(i8* %51, i8* %52)
store i8* %53, i8** %6
%54 = load i8*, i8** %6
%55 = icmp eq i8* %54, null
br i1 %55, label %56, label %57
56: ; preds = %50
store i8* null, i8** %3
br label %80
57: ; preds = %50
%58 = load i8*, i8** %5
store i8* %58, i8** %7
%59 = load i8*, i8** %6
%60 = load i8*, i8** %4
%61 = load i64, i64* %8
%62 = getelementptr inbounds i8, i8* %60, i64 %61
%63 = icmp ugt i8* %59, %62
br i1 %63, label %64, label %65
64: ; preds = %57
br label %73
65: ; preds = %57
%66 = load i64, i64* %8
%67 = load i8*, i8** %4
%68 = getelementptr inbounds i8, i8* %67, i64 %66
%69 = load i8*, i8** %6
%70 = ptrtoint i8* %68 to i64
%71 = ptrtoint i8* %69 to i64
%72 = sub i64 %70, %71
br label %73
73: ; preds = %65, %64
%74 = phi i64 [ 1, %64 ], [ %72, %65 ]
store i64 %74, i64* %9
%75 = load i8*, i8** %6
%76 = load i64, i64* %9
%77 = load i8*, i8** %7
%78 = load i64, i64* %8
%79 = call i8* @two_way_long_needle(i8* %75, i64 %76, i8* %77, i64 %78)
store i8* %79, i8** %3
br label %80
80: ; preds = %73, %56, %45, %41, %31, %29, %21
%81 = load i8*, i8** %3
ret i8* %81
}

LLVM IR (-O2):
define i8* @strstr(i8* %0, i8* %1) {
%3 = tail call i32 @__insn_prefetch(i8* %0)
%4 = tail call i64 @strlen(i8* dereferenceable(1) %1)
%5 = icmp ult i64 %4, 3
br i1 %5, label %6, label %12
6: ; preds = %2
switch i64 %4, label %10 [
i64 1, label %7
i64 0, label %30
]
7: ; preds = %6
%8 = load i8, i8* %1
%9 = tail call i8* @strchr(i8* dereferenceable(1) %0, i8 signext %8)
br label %30
10: ; preds = %6
%11 = tail call i8* @strstr2(i8* %0, i8* %1)
br label %30
12: ; preds = %2
%13 = tail call i64 @__strnlen(i8* %0, i64 %4)
%14 = icmp ult i64 %13, %4
br i1 %14, label %30, label %15
15: ; preds = %12
%16 = icmp ult i64 %4, 40
br i1 %16, label %17, label %19
17: ; preds = %15
%18 = tail call i8* @strstr_scan(i8* %0, i8* %1, i64 %4)
br label %30
19: ; preds = %15
%20 = tail call i8* @strstr2(i8* %0, i8* %1)
%21 = icmp eq i8* %20, null
br i1 %21, label %30, label %22
22: ; preds = %19
%23 = getelementptr inbounds i8, i8* %0, i64 %4
%24 = icmp ugt i8* %20, %23
%25 = ptrtoint i8* %23 to i64
%26 = ptrtoint i8* %20 to i64
%27 = sub i64 %25, %26
%28 = select i1 %24, i64 1, i64 %27
%29 = tail call i8* @two_way_long_needle(i8* %20, i64 %28, i8* %1, i64 %4)
br label %30
30: ; preds = %19, %12, %6, %22, %17, %10, %7
%31 = phi i8* [ %9, %7 ], [ %11, %10 ], [ %18, %17 ], [ %29, %22 ], [ %0, %6 ], [ null, %12 ], [ null, %19 ]
ret i8* %31
}