#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__ _kstrncmp(char const *, char const *, size_t);
size_t strlen(char const *);
char *strstr(const char *haystack, const char *needle) {
  size_t length = strlen(needle);
  size_t last = strlen(haystack) - length;
  size_t x;
  for (x = 0; x <= last; x++)
    if (_kstrncmp(&haystack[x], needle, length) == 0)
      return (char *)&haystack[x];
  return NULL;
}


x86 assembly (clang, -O0):
<strstr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
sub    -0x20(%rbp),%rax
mov    %rax,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
mov    -0x30(%rbp),%rax
cmp    -0x28(%rbp),%rax
ja     11aa <strstr+0x8a>
mov    -0x10(%rbp),%rdi
add    -0x30(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1030 <_kstrncmp@plt>
cmp    $0x0,%rax
jne    1194 <strstr+0x74>
mov    -0x10(%rbp),%rax
add    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11b2 <strstr+0x92>
jmp    1199 <strstr+0x79>
mov    -0x30(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x30(%rbp)
jmp    1156 <strstr+0x36>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<strstr>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%r15
mov    %rsi,%rdi
call   1040 <strlen@plt>
mov    %rax,%r12
mov    %r15,%rdi
call   1040 <strlen@plt>
mov    %rax,%r13
sub    %r12,%r13
xor    %ebp,%ebp
nopl   0x0(%rax)
lea    (%r15,%rbp,1),%rbx
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %r12,%rdx
call   1030 <_kstrncmp@plt>
test   %rax,%rax
je     1172 <strstr+0x52>
add    $0x1,%rbp
cmp    %r13,%rbp
jbe    1150 <strstr+0x30>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
define i8* @strstr(i8* %0, i8* %1) {
%3 = tail call i64 @strlen(i8* dereferenceable(1) %1)
%4 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%5 = sub i64 %4, %3
br label %8
6: ; preds = %8
%7 = icmp ugt i64 %13, %5
br i1 %7, label %14, label %8
8: ; preds = %2, %6
%9 = phi i64 [ 0, %2 ], [ %13, %6 ]
%10 = getelementptr inbounds i8, i8* %0, i64 %9
%11 = tail call i64 @_kstrncmp(i8* %10, i8* %1, i64 %3)
%12 = icmp eq i64 %11, 0
%13 = add i64 %9, 1
br i1 %12, label %14, label %6
14: ; preds = %6, %8
%15 = phi i8* [ %10, %8 ], [ null, %6 ]
ret i8* %15
}