#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 const *, char const *, int);
char *strstr(const char *needle, const char *haystack) {
  int i;
  int l1 = strlen(needle);
  int l2 = strlen(haystack);
  if (l2 > l1) {
    return NULL;
  }
  for (i = 0; i < l1 - l2; i++, needle++) {
    if (!strncmp(needle, haystack, l2)) {
      return (char *)(needle);
    }
  }
  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),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x20(%rbp),%eax
jle    1161 <strstr+0x41>
movq   $0x0,-0x8(%rbp)
jmp    11c6 <strstr+0xa6>
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    -0x20(%rbp),%ecx
sub    -0x24(%rbp),%ecx
cmp    %ecx,%eax
jge    11be <strstr+0x9e>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x24(%rbp),%edx
call   1030 <strncmp@plt>
cmp    $0x0,%eax
jne    119f <strstr+0x7f>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11c6 <strstr+0xa6>
jmp    11a4 <strstr+0x84>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
jmp    1168 <strstr+0x48>
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>
mov    %eax,%r15d
mov    %ebp,%eax
sub    %r15d,%eax
jl     116e <strstr+0x4e>
test   %eax,%eax
jle    116e <strstr+0x4e>
nopw   0x0(%rax,%rax,1)
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %r15d,%edx
call   1030 <strncmp@plt>
test   %eax,%eax
je     1170 <strstr+0x50>
add    $0x1,%rbx
add    $0xffffffff,%ebp
cmp    %ebp,%r15d
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 i32
%8 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
%9 = load i8*, i8** %4
%10 = call i32 @strlen(i8* %9)
store i32 %10, i32* %7
%11 = load i8*, i8** %5
%12 = call i32 @strlen(i8* %11)
store i32 %12, i32* %8
%13 = load i32, i32* %8
%14 = load i32, i32* %7
%15 = icmp sgt i32 %13, %14
br i1 %15, label %16, label %17
16: ; preds = %2
store i8* null, i8** %3
br label %39
17: ; preds = %2
store i32 0, i32* %6
br label %18
18: ; preds = %33, %17
%19 = load i32, i32* %6
%20 = load i32, i32* %7
%21 = load i32, i32* %8
%22 = sub nsw i32 %20, %21
%23 = icmp slt i32 %19, %22
br i1 %23, label %24, label %38
24: ; preds = %18
%25 = load i8*, i8** %4
%26 = load i8*, i8** %5
%27 = load i32, i32* %8
%28 = call i32 @strncmp(i8* %25, i8* %26, i32 %27)
%29 = icmp ne i32 %28, 0
br i1 %29, label %32, label %30
30: ; preds = %24
%31 = load i8*, i8** %4
store i8* %31, i8** %3
br label %39
32: ; preds = %24
br label %33
33: ; preds = %32
%34 = load i32, i32* %6
%35 = add nsw i32 %34, 1
store i32 %35, i32* %6
%36 = load i8*, i8** %4
%37 = getelementptr inbounds i8, i8* %36, i32 1
store i8* %37, i8** %4
br label %18
38: ; preds = %18
store i8* null, i8** %3
br label %39
39: ; preds = %38, %30, %16
%40 = load i8*, i8** %3
ret i8* %40
}

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