#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 */
size_t strlen(char const *);
scalar_t__ strncmp(char const *, char const *, size_t);
char *strstr(char const *haystack, char const *needle) {
  size_t len;
  char sc;
  char c;
  c = *needle++;
  len = strlen(needle);
  if (c == '\0') {
    return ((char *)haystack);
  }
  do {
    do {
      sc = *haystack++;
      if (sc == '\0') {
        return (NULL);
      }
    } while (sc != c);
  } while (strncmp(haystack, needle, len) != 0);
  return ((char *)haystack - 1);
}


x86 assembly (clang, -O0):
<strstr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x18(%rbp)
mov    (%rax),%al
mov    %al,-0x22(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x20(%rbp)
movsbl -0x22(%rbp),%eax
cmp    $0x0,%eax
jne    116b <strstr+0x4b>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11e4 <strstr+0xc4>
jmp    1170 <strstr+0x50>
jmp    1175 <strstr+0x55>
mov    -0x10(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x10(%rbp)
mov    (%rax),%al
mov    %al,-0x21(%rbp)
movsbl -0x21(%rbp),%eax
cmp    $0x0,%eax
jne    11a3 <strstr+0x83>
movq   $0x0,-0x8(%rbp)
jmp    11e4 <strstr+0xc4>
jmp    11a8 <strstr+0x88>
movsbl -0x21(%rbp),%eax
movsbl -0x22(%rbp),%ecx
cmp    %ecx,%eax
jne    1175 <strstr+0x55>
jmp    11bd <strstr+0x9d>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1030 <strncmp@plt>
cmp    $0x0,%rax
jne    1170 <strstr+0x50>
mov    -0x10(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<strstr>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rsi,%r14
mov    %rdi,%r12
mov    (%rsi),%bpl
add    $0x1,%r14
mov    %r14,%rdi
call   1040 <strlen@plt>
test   %bpl,%bpl
je     117a <strstr+0x5a>
mov    %rax,%r15
cs nopw 0x0(%rax,%rax,1)
nop
movzbl (%r12),%eax
test   %al,%al
je     117f <strstr+0x5f>
mov    %r12,%rbx
add    $0x1,%r12
cmp    %bpl,%al
jne    1150 <strstr+0x30>
mov    %r12,%rdi
mov    %r14,%rsi
mov    %r15,%rdx
call   1030 <strncmp@plt>
test   %rax,%rax
jne    1150 <strstr+0x30>
jmp    1181 <strstr+0x61>
mov    %r12,%rbx
jmp    1181 <strstr+0x61>
xor    %ebx,%ebx
mov    %rbx,%rax
pop    %rbx
pop    %r12
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 i64
%7 = alloca i8
%8 = alloca i8
store i8* %0, i8** %4
store i8* %1, i8** %5
%9 = load i8*, i8** %5
%10 = getelementptr inbounds i8, i8* %9, i32 1
store i8* %10, i8** %5
%11 = load i8, i8* %9
store i8 %11, i8* %8
%12 = load i8*, i8** %5
%13 = call i64 @strlen(i8* %12)
store i64 %13, i64* %6
%14 = load i8, i8* %8
%15 = sext i8 %14 to i32
%16 = icmp eq i32 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %2
%18 = load i8*, i8** %4
store i8* %18, i8** %3
br label %46
19: ; preds = %2
br label %20
20: ; preds = %37, %19
br label %21
21: ; preds = %30, %20
%22 = load i8*, i8** %4
%23 = getelementptr inbounds i8, i8* %22, i32 1
store i8* %23, i8** %4
%24 = load i8, i8* %22
store i8 %24, i8* %7
%25 = load i8, i8* %7
%26 = sext i8 %25 to i32
%27 = icmp eq i32 %26, 0
br i1 %27, label %28, label %29
28: ; preds = %21
store i8* null, i8** %3
br label %46
29: ; preds = %21
br label %30
30: ; preds = %29
%31 = load i8, i8* %7
%32 = sext i8 %31 to i32
%33 = load i8, i8* %8
%34 = sext i8 %33 to i32
%35 = icmp ne i32 %32, %34
br i1 %35, label %21, label %36
36: ; preds = %30
br label %37
37: ; preds = %36
%38 = load i8*, i8** %4
%39 = load i8*, i8** %5
%40 = load i64, i64* %6
%41 = call i64 @strncmp(i8* %38, i8* %39, i64 %40)
%42 = icmp ne i64 %41, 0
br i1 %42, label %20, label %43
43: ; preds = %37
%44 = load i8*, i8** %4
%45 = getelementptr inbounds i8, i8* %44, i64 -1
store i8* %45, i8** %3
br label %46
46: ; preds = %43, %28, %17
%47 = load i8*, i8** %3
ret i8* %47
}

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