#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 const *, char const *, size_t const);
size_t strlen(char const *);
char *strstr(const char *haystack, const char *needle) {
  const size_t nlen = strlen(needle);
  const size_t hlen = strlen(haystack);
  for (size_t i = 0; i < hlen; i++) {
    if (memcmp(haystack + i, needle, nlen) == 0) {
      return (char *)haystack + i;
    }
  }
  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   1030 <strlen@plt>
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
mov    -0x30(%rbp),%rax
cmp    -0x28(%rbp),%rax
jae    11a6 <strstr+0x86>
mov    -0x10(%rbp),%rdi
add    -0x30(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1040 <memcmp@plt>
cmp    $0x0,%rax
jne    1190 <strstr+0x70>
mov    -0x10(%rbp),%rax
add    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11ae <strstr+0x8e>
jmp    1195 <strstr+0x75>
mov    -0x30(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x30(%rbp)
jmp    1152 <strstr+0x32>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<strstr>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%r12
mov    %rsi,%rdi
call   1030 <strlen@plt>
mov    %rax,%r15
mov    %r12,%rdi
call   1030 <strlen@plt>
test   %rax,%rax
je     116d <strstr+0x4d>
mov    %rax,%rbx
nopl   0x0(%rax)
mov    %r12,%rdi
mov    %r14,%rsi
mov    %r15,%rdx
call   1040 <memcmp@plt>
test   %rax,%rax
je     1170 <strstr+0x50>
add    $0x1,%r12
add    $0xffffffffffffffff,%rbx
jne    1150 <strstr+0x30>
xor    %r12d,%r12d
mov    %r12,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
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)
store i64 %12, i64* %7
store i64 0, i64* %8
br label %13
13: ; preds = %30, %2
%14 = load i64, i64* %8
%15 = load i64, i64* %7
%16 = icmp ult i64 %14, %15
br i1 %16, label %17, label %33
17: ; preds = %13
%18 = load i8*, i8** %4
%19 = load i64, i64* %8
%20 = getelementptr inbounds i8, i8* %18, i64 %19
%21 = load i8*, i8** %5
%22 = load i64, i64* %6
%23 = call i64 @memcmp(i8* %20, i8* %21, i64 %22)
%24 = icmp eq i64 %23, 0
br i1 %24, label %25, label %29
25: ; preds = %17
%26 = load i8*, i8** %4
%27 = load i64, i64* %8
%28 = getelementptr inbounds i8, i8* %26, i64 %27
store i8* %28, i8** %3
br label %34
29: ; preds = %17
br label %30
30: ; preds = %29
%31 = load i64, i64* %8
%32 = add i64 %31, 1
store i64 %32, i64* %8
br label %13
33: ; preds = %13
store i8* null, i8** %3
br label %34
34: ; preds = %33, %25
%35 = load i8*, i8** %3
ret i8* %35
}

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 = icmp eq i64 %4, 0
br i1 %5, label %14, label %8
6: ; preds = %8
%7 = icmp eq i64 %13, %4
br i1 %7, label %14, label %8
8: ; preds = %2, %6
%9 = phi i64 [ %13, %6 ], [ 0, %2 ]
%10 = getelementptr inbounds i8, i8* %0, i64 %9
%11 = tail call i64 @memcmp(i8* %10, i8* %1, i64 %3)
%12 = icmp eq i64 %11, 0
%13 = add nuw i64 %9, 1
br i1 %12, label %14, label %6
14: ; preds = %6, %8, %2
%15 = phi i8* [ null, %2 ], [ %10, %8 ], [ null, %6 ]
ret i8* %15
}