#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 */
char *strchr(char const *, char const);
size_t strlen(char const *);
int /*<<< orphan*/ strncmp(char const *, char const *, size_t);
char *strstr(const char *haystack, const char *needle) {
  if (!*needle)
    return (char *)haystack;
  if (!*haystack)
    return NULL;
  size_t n = strlen(needle);
  for (; (haystack = strchr(haystack, *needle)); haystack++) {
    if (!strncmp(haystack, needle, n)) {
      return (char *)haystack;
    }
  }
  return NULL;
}


x86 assembly (clang, -O0):
<strstr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x18(%rbp),%rax
cmpb   $0x0,(%rax)
jne    115a <strstr+0x2a>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11e4 <strstr+0xb4>
mov    -0x10(%rbp),%rax
cmpb   $0x0,(%rax)
jne    1174 <strstr+0x44>
movq   $0x0,-0x8(%rbp)
jmp    11e4 <strstr+0xb4>
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rax
movsbl (%rax),%esi
call   1050 <strchr@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
je     11dc <strstr+0xac>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1030 <strncmp@plt>
cmp    $0x0,%eax
jne    11c6 <strstr+0x96>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11e4 <strstr+0xb4>
jmp    11cb <strstr+0x9b>
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
jmp    1181 <strstr+0x51>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<strstr>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%rbx
mov    (%rsi),%bpl
test   %bpl,%bpl
je     119a <strstr+0x6a>
cmpb   $0x0,(%rbx)
je     1198 <strstr+0x68>
mov    %rsi,%r14
mov    %rsi,%rdi
call   1040 <strlen@plt>
mov    %rax,%r15
movsbl %bpl,%ebp
mov    %rbx,%rdi
mov    %ebp,%esi
call   1050 <strchr@plt>
test   %rax,%rax
je     1198 <strstr+0x68>
mov    %rax,%rbx
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %r15,%rdx
call   1030 <strncmp@plt>
test   %eax,%eax
je     119a <strstr+0x6a>
add    $0x1,%rbx
mov    %rbx,%rdi
mov    %ebp,%esi
call   1050 <strchr@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    1170 <strstr+0x40>
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 i64
store i8* %0, i8** %4
store i8* %1, i8** %5
%7 = load i8*, i8** %5
%8 = load i8, i8* %7
%9 = icmp ne i8 %8, 0
br i1 %9, label %12, label %10
10: ; preds = %2
%11 = load i8*, i8** %4
store i8* %11, i8** %3
br label %39
12: ; preds = %2
%13 = load i8*, i8** %4
%14 = load i8, i8* %13
%15 = icmp ne i8 %14, 0
br i1 %15, label %17, label %16
16: ; preds = %12
store i8* null, i8** %3
br label %39
17: ; preds = %12
%18 = load i8*, i8** %5
%19 = call i64 @strlen(i8* %18)
store i64 %19, i64* %6
br label %20
20: ; preds = %35, %17
%21 = load i8*, i8** %4
%22 = load i8*, i8** %5
%23 = load i8, i8* %22
%24 = call i8* @strchr(i8* %21, i8 signext %23)
store i8* %24, i8** %4
%25 = icmp ne i8* %24, null
br i1 %25, label %26, label %38
26: ; preds = %20
%27 = load i8*, i8** %4
%28 = load i8*, i8** %5
%29 = load i64, i64* %6
%30 = call i32 @strncmp(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 %39
34: ; preds = %26
br label %35
35: ; preds = %34
%36 = load i8*, i8** %4
%37 = getelementptr inbounds i8, i8* %36, i32 1
store i8* %37, i8** %4
br label %20
38: ; preds = %20
store i8* null, i8** %3
br label %39
39: ; preds = %38, %32, %16, %10
%40 = load i8*, i8** %3
ret i8* %40
}

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