#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 /*<<< orphan*/ memcmp(char const *, char const *, size_t);
size_t strlen(char const *);
char *strstr(const char *haystack, const char *needle) {
  size_t nl = strlen(needle);
  size_t hl = strlen(haystack);
  int i;
  if (nl > hl)
    return 0;
  for (i = hl - nl + 1; i; --i) {
    if (!memcmp(haystack, needle, nl))
      return (char *)haystack;
    ++haystack;
  }
  return 0;
}


x86 assembly (clang, -O0):
<strstr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rax
cmp    -0x28(%rbp),%rax
jbe    1165 <strstr+0x45>
movq   $0x0,-0x8(%rbp)
jmp    11c7 <strstr+0xa7>
mov    -0x28(%rbp),%rax
sub    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
je     11bf <strstr+0x9f>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1040 <memcmp@plt>
cmp    $0x0,%eax
jne    11a5 <strstr+0x85>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11c7 <strstr+0xa7>
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
mov    -0x2c(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x2c(%rbp)
jmp    1174 <strstr+0x54>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<strstr>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%r12
mov    %rsi,%rdi
call   1030 <strlen@plt>
mov    %rax,%r15
mov    %r12,%rdi
call   1030 <strlen@plt>
cmp    %r15,%rax
jb     117f <strstr+0x5f>
mov    %rax,%rbx
sub    %r15d,%eax
inc    %eax
je     117f <strstr+0x5f>
add    $0x1,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %r12,%rdi
mov    %r14,%rsi
mov    %r15,%rdx
call   1040 <bcmp@plt>
test   %eax,%eax
je     1182 <strstr+0x62>
add    $0x1,%r12
add    $0xffffffffffffffff,%rbx
cmp    %ebx,%r15d
jne    1160 <strstr+0x40>
xor    %r12d,%r12d
mov    %r12,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
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 i64
%7 = alloca i64
%8 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
%9 = load i8*, i8** %5
%10 = call i64 @strlen(i8* %9)
store i64 %10, i64* %6
%11 = load i8*, i8** %4
%12 = call i64 @strlen(i8* %11)
store i64 %12, i64* %7
%13 = load i64, i64* %6
%14 = load i64, i64* %7
%15 = icmp ugt i64 %13, %14
br i1 %15, label %16, label %17
16: ; preds = %2
store i8* null, i8** %3
br label %41
17: ; preds = %2
%18 = load i64, i64* %7
%19 = load i64, i64* %6
%20 = sub i64 %18, %19
%21 = add i64 %20, 1
%22 = trunc i64 %21 to i32
store i32 %22, i32* %8
br label %23
23: ; preds = %37, %17
%24 = load i32, i32* %8
%25 = icmp ne i32 %24, 0
br i1 %25, label %26, label %40
26: ; preds = %23
%27 = load i8*, i8** %4
%28 = load i8*, i8** %5
%29 = load i64, i64* %6
%30 = call i32 @memcmp(i8* %27, i8* %28, i64 %29)
%31 = icmp ne i32 %30, 0
br i1 %31, label %34, label %32
32: ; preds = %26
%33 = load i8*, i8** %4
store i8* %33, i8** %3
br label %41
34: ; preds = %26
%35 = load i8*, i8** %4
%36 = getelementptr inbounds i8, i8* %35, i32 1
store i8* %36, i8** %4
br label %37
37: ; preds = %34
%38 = load i32, i32* %8
%39 = add nsw i32 %38, -1
store i32 %39, i32* %8
br label %23
40: ; preds = %23
store i8* null, i8** %3
br label %41
41: ; preds = %40, %32, %16
%42 = load i8*, i8** %3
ret i8* %42
}

LLVM IR (-O2):
define i8* @strstr(i8* %0, i8* nocapture %1) {
%3 = tail call i64 @strlen(i8* dereferenceable(1) %1)
%4 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%5 = icmp ult i64 %4, %3
br i1 %5, label %20, label %6
6: ; preds = %2
%7 = sub i64 %4, %3
%8 = trunc i64 %7 to i32
%9 = add i32 %8, 1
%10 = icmp eq i32 %9, 0
br i1 %10, label %20, label %11
11: ; preds = %6, %16
%12 = phi i32 [ %18, %16 ], [ %9, %6 ]
%13 = phi i8* [ %17, %16 ], [ %0, %6 ]
%14 = tail call i32 @bcmp(i8* %13, i8* %1, i64 %3)
%15 = icmp eq i32 %14, 0
br i1 %15, label %20, label %16
16: ; preds = %11
%17 = getelementptr inbounds i8, i8* %13, i64 1
%18 = add nsw i32 %12, -1
%19 = icmp eq i32 %18, 0
br i1 %19, label %20, label %11
20: ; preds = %11, %16, %6, %2
%21 = phi i8* [ null, %2 ], [ null, %6 ], [ %13, %11 ], [ null, %16 ]
ret i8* %21
}