#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 const);
char *strstr(const char *s1, const char *s2) {
  const char *p = s1;
  const size_t len = strlen(s2);
  for (; (p = strchr(p, *s2)) != 0; p++) {
    if (strncmp(p, s2, len) == 0)
      return (char *)p;
  }
  return (NULL);
}


x86 assembly (clang, -O0):
<strstr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rax
movsbl (%rax),%esi
call   1050 <strchr@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     11b1 <strstr+0x81>
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   1030 <strncmp@plt>
cmp    $0x0,%rax
jne    119b <strstr+0x6b>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11b9 <strstr+0x89>
jmp    11a0 <strstr+0x70>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    1155 <strstr+0x25>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

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

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