#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 *str, const char *substr) {
  const char *p = str;
  size_t len = strlen(substr);
  while ((p = strchr(p, substr[0])) != NULL) {
    if (strncmp(p, substr, len) == 0)
      return (char *)p;
    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     11ac <strstr+0x7c>
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    11b4 <strstr+0x84>
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 = getelementptr inbounds i8, i8* %13, i64 0
%15 = load i8, i8* %14
%16 = call i8* @strchr(i8* %12, i8 signext %15)
store i8* %16, i8** %6
%17 = icmp ne i8* %16, null
br i1 %17, label %18, label %29
18: ; preds = %11
%19 = load i8*, i8** %6
%20 = load i8*, i8** %5
%21 = load i64, i64* %7
%22 = call i64 @strncmp(i8* %19, i8* %20, i64 %21)
%23 = icmp eq i64 %22, 0
br i1 %23, label %24, label %26
24: ; preds = %18
%25 = load i8*, i8** %6
store i8* %25, i8** %3
br label %30
26: ; preds = %18
%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, %24
%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
}