#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__ memcmp(char *, char const *, unsigned int);
char *strstr2(char const *, char const *);
char *strstr_scan(const char *haystack, const char *needle,
                  unsigned int needle_len) {
  char *match;
  while (1) {
    match = strstr2(haystack, needle);
    if (match == NULL)
      return NULL;
    if (memcmp(match + 2, needle + 2, needle_len - 2) == 0)
      return match;
    haystack = match + 1;
  }
}


x86 assembly (clang, -O0):
<strstr_scan>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1030 <strstr2@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    115c <strstr_scan+0x3c>
movq   $0x0,-0x8(%rbp)
jmp    119f <strstr_scan+0x7f>
mov    -0x28(%rbp),%rdi
add    $0x2,%rdi
mov    -0x18(%rbp),%rsi
add    $0x2,%rsi
mov    -0x1c(%rbp),%edx
sub    $0x2,%edx
call   1040 <memcmp@plt>
cmp    $0x0,%rax
jne    118e <strstr_scan+0x6e>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    119f <strstr_scan+0x7f>
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
jmp    1133 <strstr_scan+0x13>
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<strstr_scan>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%r14d
mov    %rsi,%r15
call   1030 <strstr2@plt>
test   %rax,%rax
je     117b <strstr_scan+0x5b>
mov    %rax,%rbp
lea    0x2(%r15),%rbx
add    $0xfffffffe,%r14d
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
lea    0x2(%rbp),%rdi
mov    %rbx,%rsi
mov    %r14d,%edx
call   1040 <memcmp@plt>
test   %rax,%rax
je     117d <strstr_scan+0x5d>
add    $0x1,%rbp
mov    %rbp,%rdi
mov    %r15,%rsi
call   1030 <strstr2@plt>
mov    %rax,%rbp
test   %rax,%rax
jne    1150 <strstr_scan+0x30>
xor    %ebp,%ebp
mov    %rbp,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i8* @strstr_scan(i8* %0, i8* %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i8*
store i8* %0, i8** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
br label %9
9: ; preds = %3, %27
%10 = load i8*, i8** %5
%11 = load i8*, i8** %6
%12 = call i8* @strstr2(i8* %10, i8* %11)
store i8* %12, i8** %8
%13 = load i8*, i8** %8
%14 = icmp eq i8* %13, null
br i1 %14, label %15, label %16
15: ; preds = %9
store i8* null, i8** %4
br label %30
16: ; preds = %9
%17 = load i8*, i8** %8
%18 = getelementptr inbounds i8, i8* %17, i64 2
%19 = load i8*, i8** %6
%20 = getelementptr inbounds i8, i8* %19, i64 2
%21 = load i32, i32* %7
%22 = sub i32 %21, 2
%23 = call i64 @memcmp(i8* %18, i8* %20, i32 %22)
%24 = icmp eq i64 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %16
%26 = load i8*, i8** %8
store i8* %26, i8** %4
br label %30
27: ; preds = %16
%28 = load i8*, i8** %8
%29 = getelementptr inbounds i8, i8* %28, i64 1
store i8* %29, i8** %5
br label %9
30: ; preds = %25, %15
%31 = load i8*, i8** %4
ret i8* %31
}

LLVM IR (-O2):
define i8* @strstr_scan(i8* %0, i8* %1, i32 %2) {
%4 = tail call i8* @strstr2(i8* %0, i8* %1)
%5 = icmp eq i8* %4, null
br i1 %5, label %18, label %6
6: ; preds = %3
%7 = getelementptr inbounds i8, i8* %1, i64 2
%8 = add i32 %2, -2
br label %9
9: ; preds = %6, %14
%10 = phi i8* [ %4, %6 ], [ %16, %14 ]
%11 = getelementptr inbounds i8, i8* %10, i64 2
%12 = tail call i64 @memcmp(i8* %11, i8* %7, i32 %8)
%13 = icmp eq i64 %12, 0
br i1 %13, label %18, label %14
14: ; preds = %9
%15 = getelementptr inbounds i8, i8* %10, i64 1
%16 = tail call i8* @strstr2(i8* %15, i8* %1)
%17 = icmp eq i8* %16, null
br i1 %17, label %18, label %9
18: ; preds = %14, %9, %3
%19 = phi i8* [ null, %3 ], [ %10, %9 ], [ null, %14 ]
ret i8* %19
}