#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 */
char *strchr(char const *, char const);
size_t strlen(char const *);
scalar_t__ strncmp(char const *, char const *, size_t);
char *strstr(const char *haystack, const char *needle) {
  size_t needlelen;
  if (*needle == '\0')
    return (char *)haystack;
  needlelen = strlen(needle);
  for (; (haystack = strchr(haystack, *needle)) != 0; haystack++)
    if (strncmp(haystack, needle, needlelen) == 0)
      return (char *)haystack;
  return 0;
}


x86 assembly (clang, -O0):
<strstr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x18(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
jne    115d <strstr+0x2d>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11ce <strstr+0x9e>
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rax
movsbl (%rax),%esi
call   1050 <strchr@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
je     11c6 <strstr+0x96>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1030 <strncmp@plt>
cmp    $0x0,%rax
jne    11b0 <strstr+0x80>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11ce <strstr+0x9e>
jmp    11b5 <strstr+0x85>
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
jmp    116a <strstr+0x3a>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<strstr>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%rbx
mov    (%rsi),%bpl
test   %bpl,%bpl
je     119d <strstr+0x6d>
mov    %rsi,%r14
mov    %rsi,%rdi
call   1040 <strlen@plt>
mov    %rax,%r15
movsbl %bpl,%esi
mov    %rbx,%rdi
call   1050 <strchr@plt>
test   %rax,%rax
je     119b <strstr+0x6b>
mov    %rax,%rbx
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %r15,%rdx
call   1030 <strncmp@plt>
test   %rax,%rax
je     119d <strstr+0x6d>
add    $0x1,%rbx
movsbl (%r14),%esi
mov    %rbx,%rdi
call   1050 <strchr@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    1170 <strstr+0x40>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
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
store i8* %0, i8** %4
store i8* %1, i8** %5
%7 = load i8*, i8** %5
%8 = load i8, i8* %7
%9 = sext i8 %8 to i32
%10 = icmp eq i32 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %2
%12 = load i8*, i8** %4
store i8* %12, i8** %3
br label %35
13: ; preds = %2
%14 = load i8*, i8** %5
%15 = call i64 @strlen(i8* %14)
store i64 %15, i64* %6
br label %16
16: ; preds = %31, %13
%17 = load i8*, i8** %4
%18 = load i8*, i8** %5
%19 = load i8, i8* %18
%20 = call i8* @strchr(i8* %17, i8 signext %19)
store i8* %20, i8** %4
%21 = icmp ne i8* %20, null
br i1 %21, label %22, label %34
22: ; preds = %16
%23 = load i8*, i8** %4
%24 = load i8*, i8** %5
%25 = load i64, i64* %6
%26 = call i64 @strncmp(i8* %23, i8* %24, i64 %25)
%27 = icmp eq i64 %26, 0
br i1 %27, label %28, label %30
28: ; preds = %22
%29 = load i8*, i8** %4
store i8* %29, i8** %3
br label %35
30: ; preds = %22
br label %31
31: ; preds = %30
%32 = load i8*, i8** %4
%33 = getelementptr inbounds i8, i8* %32, i32 1
store i8* %33, i8** %4
br label %16
34: ; preds = %16
store i8* null, i8** %3
br label %35
35: ; preds = %34, %28, %11
%36 = load i8*, i8** %3
ret i8* %36
}

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