#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 */
typedef int uint32_t;

/* Variables and functions */
scalar_t__ memcmp(char *, char *, int);
char *search_linear(char *needle, char *haystack, uint32_t needle_len,
                    uint32_t haystack_len) {
  char *k = haystack + (haystack_len - needle_len);
  char *t = haystack;
  if (needle_len == 0) {
    return NULL;
  }
  while (t <= k) {
    if (memcmp(t, needle, needle_len) == 0) {
      return t;
    }
    t++;
  }
  return NULL;
}


x86 assembly (clang, -O0):
<search_linear>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    %ecx,-0x20(%rbp)
mov    -0x18(%rbp),%rax
mov    -0x20(%rbp),%ecx
sub    -0x1c(%rbp),%ecx
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x30(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jne    1159 <search_linear+0x49>
movq   $0x0,-0x8(%rbp)
jmp    11ac <search_linear+0x9c>
jmp    115e <search_linear+0x4e>
mov    -0x30(%rbp),%rax
cmp    -0x28(%rbp),%rax
ja     11a4 <search_linear+0x94>
mov    -0x30(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1030 <memcmp@plt>
cmp    $0x0,%rax
jne    1193 <search_linear+0x83>
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11ac <search_linear+0x9c>
mov    -0x30(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x30(%rbp)
jmp    115e <search_linear+0x4e>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<search_linear>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
xor    %r14d,%r14d
test   %edx,%edx
je     1161 <search_linear+0x51>
mov    %edx,%r12d
sub    %edx,%ecx
js     1161 <search_linear+0x51>
mov    %rsi,%rbx
mov    %rdi,%r15
movslq %ecx,%rbp
add    %rsi,%rbp
xor    %r14d,%r14d
cs nopw 0x0(%rax,%rax,1)
nop
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %r12d,%edx
call   1030 <memcmp@plt>
test   %rax,%rax
je     115e <search_linear+0x4e>
add    $0x1,%rbx
cmp    %rbp,%rbx
jbe    1140 <search_linear+0x30>
jmp    1161 <search_linear+0x51>
mov    %rbx,%r14
mov    %r14,%rax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

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