#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 *);
int /*<<< orphan*/ strncmp(char *, char const *, int);
char *strstr(const char *s1, const char *s2) {
  int count = 0;
  char *ptr = NULL;
  int s1_length = strlen(s1);
  int s2_length = strlen(s2);
  ptr = (char *)s1;
  for (count = 0; count < s1_length; count++) {
    if (!strncmp(ptr, s2, s2_length))
      return (ptr);
    else
      ptr++;
  }
  return (ptr = NULL);
}


x86 assembly (clang, -O0):
<strstr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x30(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x2c(%rbp),%eax
jge    11b7 <strstr+0x97>
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x30(%rbp),%edx
call   1030 <strncmp@plt>
cmp    $0x0,%eax
jne    1198 <strstr+0x78>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11c7 <strstr+0xa7>
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
jmp    11a9 <strstr+0x89>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1166 <strstr+0x46>
movq   $0x0,-0x28(%rbp)
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>
test   %ebp,%ebp
jle    116b <strstr+0x4b>
mov    %eax,%r15d
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %r15d,%edx
call   1030 <strncmp@plt>
test   %eax,%eax
je     116d <strstr+0x4d>
add    $0x1,%rbx
add    $0xffffffff,%ebp
jne    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 i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
store i32 0, i32* %6
store i8* null, i8** %7
%10 = load i8*, i8** %4
%11 = call i32 @strlen(i8* %10)
store i32 %11, i32* %8
%12 = load i8*, i8** %5
%13 = call i32 @strlen(i8* %12)
store i32 %13, i32* %9
%14 = load i8*, i8** %4
store i8* %14, i8** %7
store i32 0, i32* %6
br label %15
15: ; preds = %31, %2
%16 = load i32, i32* %6
%17 = load i32, i32* %8
%18 = icmp slt i32 %16, %17
br i1 %18, label %19, label %34
19: ; preds = %15
%20 = load i8*, i8** %7
%21 = load i8*, i8** %5
%22 = load i32, i32* %9
%23 = call i32 @strncmp(i8* %20, i8* %21, i32 %22)
%24 = icmp ne i32 %23, 0
br i1 %24, label %27, label %25
25: ; preds = %19
%26 = load i8*, i8** %7
store i8* %26, i8** %3
br label %35
27: ; preds = %19
%28 = load i8*, i8** %7
%29 = getelementptr inbounds i8, i8* %28, i32 1
store i8* %29, i8** %7
br label %30
30: ; preds = %27
br label %31
31: ; preds = %30
%32 = load i32, i32* %6
%33 = add nsw i32 %32, 1
store i32 %33, i32* %6
br label %15
34: ; preds = %15
store i8* null, i8** %7
store i8* null, i8** %3
br label %35
35: ; preds = %34, %25
%36 = load i8*, i8** %3
ret i8* %36
}

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 sgt i32 %3, 0
br i1 %5, label %6, label %15
6: ; preds = %2, %11
%7 = phi i32 [ %13, %11 ], [ 0, %2 ]
%8 = phi i8* [ %12, %11 ], [ %0, %2 ]
%9 = tail call i32 @strncmp(i8* %8, i8* %1, i32 %4)
%10 = icmp eq i32 %9, 0
br i1 %10, label %15, label %11
11: ; preds = %6
%12 = getelementptr inbounds i8, i8* %8, i64 1
%13 = add nuw nsw i32 %7, 1
%14 = icmp eq i32 %13, %3
br i1 %14, label %15, label %6
15: ; preds = %6, %11, %2
%16 = phi i8* [ null, %2 ], [ null, %11 ], [ %8, %6 ]
ret i8* %16
}