#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 */
int strlen(char const *);
scalar_t__ strncmp(char *, char const *, int);
char *strstr(const char *s1, const char *s2) {
  char *sp = (char *)s1;
  int len1 = strlen(s1);
  int len2 = strlen(s2);
  while (len1 >= len2) {
    if (strncmp(sp, s2, len2) == 0) {
      return (sp);
    }
    ++sp;
    --len1;
  }
  return (0);
}


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    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x24(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x28(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x28(%rbp),%eax
jl     119d <strstr+0x7d>
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x28(%rbp),%edx
call   1030 <strncmp@plt>
cmp    $0x0,%rax
jne    1183 <strstr+0x63>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11a5 <strstr+0x85>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
mov    -0x24(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x24(%rbp)
jmp    1150 <strstr+0x30>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<strstr>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rbx
call   1040 <strlen@plt>
mov    %eax,%ebp
mov    %r14,%rdi
call   1040 <strlen@plt>
cmp    %eax,%ebp
jl     116f <strstr+0x4f>
mov    %eax,%r15d
add    $0x1,%ebp
cs nopw 0x0(%rax,%rax,1)
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %r15d,%edx
call   1030 <strncmp@plt>
test   %rax,%rax
je     1171 <strstr+0x51>
add    $0x1,%rbx
add    $0xffffffff,%ebp
cmp    %r15d,%ebp
jg     1150 <strstr+0x30>
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 i8*
%7 = alloca i32
%8 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
%9 = load i8*, i8** %4
store i8* %9, i8** %6
%10 = load i8*, i8** %4
%11 = call i32 @strlen(i8* %10)
store i32 %11, i32* %7
%12 = load i8*, i8** %5
%13 = call i32 @strlen(i8* %12)
store i32 %13, i32* %8
br label %14
14: ; preds = %26, %2
%15 = load i32, i32* %7
%16 = load i32, i32* %8
%17 = icmp sge i32 %15, %16
br i1 %17, label %18, label %31
18: ; preds = %14
%19 = load i8*, i8** %6
%20 = load i8*, i8** %5
%21 = load i32, i32* %8
%22 = call i64 @strncmp(i8* %19, i8* %20, i32 %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 %32
26: ; preds = %18
%27 = load i8*, i8** %6
%28 = getelementptr inbounds i8, i8* %27, i32 1
store i8* %28, i8** %6
%29 = load i32, i32* %7
%30 = add nsw i32 %29, -1
store i32 %30, i32* %7
br label %14
31: ; preds = %14
store i8* null, i8** %3
br label %32
32: ; preds = %31, %24
%33 = load i8*, i8** %3
ret i8* %33
}

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