#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);
size_t strlen(char const *);
scalar_t__ strncmp(char const *, char const *, size_t);
const char *enca_strstr(const char *haystack, const char *needle) {
  char c;
  size_t n;
  if (needle == NULL)
    return haystack;
  if ((n = strlen(needle)) == 0)
    return haystack;
  c = needle[0];
  while ((haystack = strchr(haystack, c)) != NULL) {
    if (strncmp(haystack, needle, n) == 0)
      return haystack;
  }
  return NULL;
}


x86 assembly (clang, -O0):
<enca_strstr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1158 <enca_strstr+0x28>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11d5 <enca_strstr+0xa5>
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    117c <enca_strstr+0x4c>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11d5 <enca_strstr+0xa5>
mov    -0x18(%rbp),%rax
mov    (%rax),%al
mov    %al,-0x19(%rbp)
mov    -0x10(%rbp),%rdi
movsbl -0x19(%rbp),%esi
call   1050 <strchr@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
je     11cd <enca_strstr+0x9d>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   1030 <strncmp@plt>
cmp    $0x0,%rax
jne    11c8 <enca_strstr+0x98>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11d5 <enca_strstr+0xa5>
jmp    1185 <enca_strstr+0x55>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<enca_strstr>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%rbx
test   %rsi,%rsi
je     1189 <enca_strstr+0x59>
mov    %rsi,%r14
mov    %rsi,%rdi
call   1040 <strlen@plt>
test   %rax,%rax
je     1189 <enca_strstr+0x59>
mov    %rax,%r15
movsbl (%r14),%ebp
cs nopw 0x0(%rax,%rax,1)
mov    %rbx,%rdi
mov    %ebp,%esi
call   1050 <strchr@plt>
test   %rax,%rax
je     1187 <enca_strstr+0x57>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r14,%rsi
mov    %r15,%rdx
call   1030 <strncmp@plt>
test   %rax,%rax
jne    1160 <enca_strstr+0x30>
jmp    1189 <enca_strstr+0x59>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i8* @enca_strstr(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8
%7 = alloca i64
store i8* %0, i8** %4
store i8* %1, i8** %5
%8 = load i8*, i8** %5
%9 = icmp eq i8* %8, null
br i1 %9, label %10, label %12
10: ; preds = %2
%11 = load i8*, i8** %4
store i8* %11, i8** %3
br label %37
12: ; preds = %2
%13 = load i8*, i8** %5
%14 = call i64 @strlen(i8* %13)
store i64 %14, i64* %7
%15 = icmp eq i64 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %12
%17 = load i8*, i8** %4
store i8* %17, i8** %3
br label %37
18: ; preds = %12
%19 = load i8*, i8** %5
%20 = getelementptr inbounds i8, i8* %19, i64 0
%21 = load i8, i8* %20
store i8 %21, i8* %6
br label %22
22: ; preds = %35, %18
%23 = load i8*, i8** %4
%24 = load i8, i8* %6
%25 = call i8* @strchr(i8* %23, i8 signext %24)
store i8* %25, i8** %4
%26 = icmp ne i8* %25, null
br i1 %26, label %27, label %36
27: ; preds = %22
%28 = load i8*, i8** %4
%29 = load i8*, i8** %5
%30 = load i64, i64* %7
%31 = call i64 @strncmp(i8* %28, i8* %29, i64 %30)
%32 = icmp eq i64 %31, 0
br i1 %32, label %33, label %35
33: ; preds = %27
%34 = load i8*, i8** %4
store i8* %34, i8** %3
br label %37
35: ; preds = %27
br label %22
36: ; preds = %22
store i8* null, i8** %3
br label %37
37: ; preds = %36, %33, %16, %10
%38 = load i8*, i8** %3
ret i8* %38
}

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