#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);
unsigned long strlen(char const *);
scalar_t__ strncmp(char const *, char const *, unsigned long);
char *my_strstr(const char *s1, const char *s2) {
  long unsigned int len = strlen(s2);
  if (len == 0)
    return (char *)s1;
  for (s1 = strchr(s1, *s2); s1; s1 = strchr(s1 + 1, *s2))
    if (strncmp(s1, s2, len) == 0)
      return (char *)s1;
  return (char *)0;
}


x86 assembly (clang, -O0):
<my_strstr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1165 <my_strstr+0x35>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11d6 <my_strstr+0xa6>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rax
movsbl (%rax),%esi
call   1050 <strchr@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     11ce <my_strstr+0x9e>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1030 <strncmp@plt>
cmp    $0x0,%rax
jne    11ac <my_strstr+0x7c>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11d6 <my_strstr+0xa6>
jmp    11b1 <my_strstr+0x81>
mov    -0x10(%rbp),%rdi
add    $0x1,%rdi
mov    -0x18(%rbp),%rax
movsbl (%rax),%esi
call   1050 <strchr@plt>
mov    %rax,-0x10(%rbp)
jmp    1179 <my_strstr+0x49>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<my_strstr>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%r14
mov    %rdi,%rbx
mov    %rsi,%rdi
call   1040 <strlen@plt>
test   %rax,%rax
je     118d <my_strstr+0x5d>
mov    %rax,%r15
movsbl (%r14),%esi
mov    %rbx,%rdi
call   1050 <strchr@plt>
test   %rax,%rax
je     118b <my_strstr+0x5b>
mov    %rax,%rbx
nop
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %r15,%rdx
call   1030 <strncmp@plt>
test   %rax,%rax
je     118d <my_strstr+0x5d>
add    $0x1,%rbx
movsbl (%r14),%esi
mov    %rbx,%rdi
call   1050 <strchr@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    1160 <my_strstr+0x30>
xor    %ebx,%ebx
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i8* @my_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 = call i64 @strlen(i8* %7)
store i64 %8, i64* %6
%9 = load i64, i64* %6
%10 = icmp eq i64 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %2
%12 = load i8*, i8** %4
store i8* %12, i8** %3
br label %37
13: ; preds = %2
%14 = load i8*, i8** %4
%15 = load i8*, i8** %5
%16 = load i8, i8* %15
%17 = call i8* @strchr(i8* %14, i8 signext %16)
store i8* %17, i8** %4
br label %18
18: ; preds = %30, %13
%19 = load i8*, i8** %4
%20 = icmp ne i8* %19, null
br i1 %20, label %21, label %36
21: ; preds = %18
%22 = load i8*, i8** %4
%23 = load i8*, i8** %5
%24 = load i64, i64* %6
%25 = call i64 @strncmp(i8* %22, i8* %23, i64 %24)
%26 = icmp eq i64 %25, 0
br i1 %26, label %27, label %29
27: ; preds = %21
%28 = load i8*, i8** %4
store i8* %28, i8** %3
br label %37
29: ; preds = %21
br label %30
30: ; preds = %29
%31 = load i8*, i8** %4
%32 = getelementptr inbounds i8, i8* %31, i64 1
%33 = load i8*, i8** %5
%34 = load i8, i8* %33
%35 = call i8* @strchr(i8* %32, i8 signext %34)
store i8* %35, i8** %4
br label %18
36: ; preds = %18
store i8* null, i8** %3
br label %37
37: ; preds = %36, %27, %11
%38 = load i8*, i8** %3
ret i8* %38
}

LLVM IR (-O2):
define i8* @my_strstr(i8* %0, i8* %1) {
%3 = tail call i64 @strlen(i8* dereferenceable(1) %1)
%4 = icmp eq i64 %3, 0
br i1 %4, label %18, label %5
5: ; preds = %2
%6 = load i8, i8* %1
%7 = tail call i8* @strchr(i8* dereferenceable(1) %0, i8 signext %6)
%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 %3)
%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* %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
}