char *mystristr(const char *HAYSTACK, const char *NEEDLE) {
  const char *end;
  assert(HAYSTACK);
  assert(NEEDLE);
  assert(strlen(NEEDLE) > 0);
  end = strchr(HAYSTACK, 0) - strlen(NEEDLE);
  while (HAYSTACK <= end) {
    if (strincmp(HAYSTACK, NEEDLE) == 0)
      return (char *)HAYSTACK;
    HAYSTACK++;
  }
  return 0;
}


x86 assembly (clang, -O0):
<mystristr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    $0x0,%al
call   1060 <assert@plt>
mov    -0x18(%rbp),%rdi
mov    $0x0,%al
call   1060 <assert@plt>
mov    -0x18(%rbp),%rdi
call   1030 <strlen@plt>
cmp    $0x0,%rax
seta   %al
and    $0x1,%al
movzbl %al,%edi
mov    $0x0,%al
call   1060 <assert@plt>
mov    -0x10(%rbp),%rdi
xor    %esi,%esi
call   1040 <strchr@plt>
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,%rdx
mov    -0x28(%rbp),%rax
xor    %ecx,%ecx
sub    %rdx,%rcx
add    %rcx,%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
cmp    -0x20(%rbp),%rax
ja     11f1 <mystristr+0xb1>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    $0x0,%al
call   1050 <strincmp@plt>
cmp    $0x0,%eax
jne    11e0 <mystristr+0xa0>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11f9 <mystristr+0xb9>
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
jmp    11ad <mystristr+0x6d>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mystristr>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%r14
mov    %rdi,%rbx
xor    %eax,%eax
call   1050 <assert@plt>
mov    %r14,%rdi
xor    %eax,%eax
call   1050 <assert@plt>
xor    %edi,%edi
cmpb   $0x0,(%r14)
setne  %dil
xor    %eax,%eax
call   1050 <assert@plt>
mov    %rbx,%rdi
call   1030 <strlen@plt>
mov    %rax,%r15
add    %rbx,%r15
mov    %r14,%rdi
call   1030 <strlen@plt>
sub    %rax,%r15
cmp    %rbx,%r15
jb     119a <mystristr+0x6a>
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1040 <strincmp@plt>
test   %eax,%eax
je     119c <mystristr+0x6c>
add    $0x1,%rbx
cmp    %r15,%rbx
jbe    1180 <mystristr+0x50>
xor    %ebx,%ebx
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
define i8* @mystristr(i8* %0, i8* %1) {
%3 = tail call i32 (i8*, ...) bitcast (i32 (...)* @assert to i32 (i8*, ...)*)(i8* %0)
%4 = tail call i32 (i8*, ...) bitcast (i32 (...)* @assert to i32 (i8*, ...)*)(i8* %1)
%5 = load i8, i8* %1
%6 = icmp ne i8 %5, 0
%7 = zext i1 %6 to i32
%8 = tail call i32 (i32, ...) bitcast (i32 (...)* @assert to i32 (i32, ...)*)(i32 %7)
%9 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%10 = getelementptr i8, i8* %0, i64 %9
%11 = tail call i64 @strlen(i8* %1)
%12 = sub i64 0, %11
%13 = getelementptr inbounds i8, i8* %10, i64 %12
%14 = icmp ult i8* %13, %0
br i1 %14, label %22, label %15
15: ; preds = %2, %19
%16 = phi i8* [ %20, %19 ], [ %0, %2 ]
%17 = tail call i32 (i8*, i8*, ...) bitcast (i32 (...)* @strincmp to i32 (i8*, i8*, ...)*)(i8* %16, i8* %1)
%18 = icmp eq i32 %17, 0
br i1 %18, label %22, label %19
19: ; preds = %15
%20 = getelementptr inbounds i8, i8* %16, i64 1
%21 = icmp ugt i8* %20, %13
br i1 %21, label %22, label %15
22: ; preds = %15, %19, %2
%23 = phi i8* [ null, %2 ], [ null, %19 ], [ %16, %15 ]
ret i8* %23
}