#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 */
scalar_t__ curl_strnequal(char const *, char const *, size_t);
size_t strlen(char const *);
char *Curl_strcasestr(const char *haystack, const char *needle) {
  size_t nlen = strlen(needle);
  size_t hlen = strlen(haystack);
  while (hlen-- >= nlen) {
    if (curl_strnequal(haystack, needle, nlen))
      return (char *)haystack;
    haystack++;
  }
  return NULL;
}


x86 assembly (clang, -O0):
<Curl_strcasestr>:
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    -0x28(%rbp),%rax
mov    %rax,%rcx
add    $0xffffffffffffffff,%rcx
mov    %rcx,-0x28(%rbp)
cmp    -0x20(%rbp),%rax
jb     119c <Curl_strcasestr+0x7c>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1040 <curl_strnequal@plt>
cmp    $0x0,%rax
je     118b <Curl_strcasestr+0x6b>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11a4 <Curl_strcasestr+0x84>
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
jmp    114a <Curl_strcasestr+0x2a>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<Curl_strcasestr>:
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     1170 <Curl_strcasestr+0x50>
mov    %rax,%rbx
nopl   0x0(%rax)
mov    %r12,%rdi
mov    %r14,%rsi
mov    %r15,%rdx
call   1040 <curl_strnequal@plt>
test   %rax,%rax
jne    1173 <Curl_strcasestr+0x53>
add    $0xffffffffffffffff,%rbx
add    $0x1,%r12
cmp    %r15,%rbx
jae    1150 <Curl_strcasestr+0x30>
xor    %r12d,%r12d
mov    %r12,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i8* @Curl_strcasestr(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i64
store i8* %0, i8** %4
store i8* %1, i8** %5
%8 = load i8*, i8** %5
%9 = call i64 @strlen(i8* %8)
store i64 %9, i64* %6
%10 = load i8*, i8** %4
%11 = call i64 @strlen(i8* %10)
store i64 %11, i64* %7
br label %12
12: ; preds = %25, %2
%13 = load i64, i64* %7
%14 = add i64 %13, -1
store i64 %14, i64* %7
%15 = load i64, i64* %6
%16 = icmp uge i64 %13, %15
br i1 %16, label %17, label %28
17: ; preds = %12
%18 = load i8*, i8** %4
%19 = load i8*, i8** %5
%20 = load i64, i64* %6
%21 = call i64 @curl_strnequal(i8* %18, i8* %19, i64 %20)
%22 = icmp ne i64 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %17
%24 = load i8*, i8** %4
store i8* %24, i8** %3
br label %29
25: ; preds = %17
%26 = load i8*, i8** %4
%27 = getelementptr inbounds i8, i8* %26, i32 1
store i8* %27, i8** %4
br label %12
28: ; preds = %12
store i8* null, i8** %3
br label %29
29: ; preds = %28, %23
%30 = load i8*, i8** %3
ret i8* %30
}

LLVM IR (-O2):
define i8* @Curl_strcasestr(i8* %0, i8* %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 %15, label %6
6: ; preds = %2, %12
%7 = phi i64 [ %9, %12 ], [ %4, %2 ]
%8 = phi i8* [ %13, %12 ], [ %0, %2 ]
%9 = add i64 %7, -1
%10 = tail call i64 @curl_strnequal(i8* %8, i8* %1, i64 %3)
%11 = icmp eq i64 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %6
%13 = getelementptr inbounds i8, i8* %8, i64 1
%14 = icmp ult i64 %9, %3
br i1 %14, label %15, label %6
15: ; preds = %6, %12, %2
%16 = phi i8* [ null, %2 ], [ null, %12 ], [ %8, %6 ]
ret i8* %16
}