#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 */
typedef int /*<<< orphan*/ f;

/* Variables and functions */
int /*<<< orphan*/ snprintf(char *, int, char *, int, int /*<<< orphan*/);
int strcspn(char *, char *);
size_t strlen(char *);
scalar_t__ strncasecmp(char *, char *, size_t);
int tolower(char);
int /*<<< orphan*/ toupper(char);
char *strcasestr(char *a, char *b) {
  size_t l;
  char f[3];
  snprintf(f, sizeof(f), "%c%c", tolower(*b), toupper(*b));
  for (l = strcspn(a, f); l != strlen(a); l += strcspn(a + l + 1, f) + 1)
    if (strncasecmp(a + l, b, strlen(b)) == 0)
      return a + l;
  return NULL;
}


x86 assembly (clang, -O0):
<strcasestr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
lea    -0x23(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%rax
movsbl (%rax),%edi
call   1070 <tolower@plt>
mov    %eax,-0x28(%rbp)
mov    -0x18(%rbp),%rax
movsbl (%rax),%edi
call   1030 <toupper@plt>
mov    -0x30(%rbp),%rdi
mov    -0x28(%rbp),%ecx
mov    %eax,%r8d
mov    $0x3,%esi
lea    0xe57(%rip),%rdx
call   1050 <snprintf@plt>
mov    -0x10(%rbp),%rdi
lea    -0x23(%rbp),%rsi
call   1060 <strcspn@plt>
cltq
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,%rcx
mov    -0x38(%rbp),%rax
cmp    %rcx,%rax
je     1256 <strcasestr+0xf6>
mov    -0x10(%rbp),%rax
add    -0x20(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x48(%rbp),%rdi
mov    -0x40(%rbp),%rsi
mov    %rax,%rdx
call   1080 <strncasecmp@plt>
cmp    $0x0,%rax
jne    122a <strcasestr+0xca>
mov    -0x10(%rbp),%rax
add    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    125e <strcasestr+0xfe>
jmp    122f <strcasestr+0xcf>
mov    -0x10(%rbp),%rdi
add    -0x20(%rbp),%rdi
add    $0x1,%rdi
lea    -0x23(%rbp),%rsi
call   1060 <strcspn@plt>
add    $0x1,%eax
cltq
add    -0x20(%rbp),%rax
mov    %rax,-0x20(%rbp)
jmp    11c1 <strcasestr+0x61>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<strcasestr>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r15
mov    %rdi,%r14
movsbl (%rsi),%edi
call   1070 <tolower@plt>
mov    %eax,%ebx
movsbl (%r15),%edi
call   1030 <toupper@plt>
lea    0xe75(%rip),%rdx
lea    0x5(%rsp),%rbp
mov    %rbp,%rdi
mov    $0x3,%esi
mov    %ebx,%ecx
mov    %eax,%r8d
call   1050 <snprintf@plt>
mov    %r14,%rdi
mov    %rbp,%rsi
call   1060 <strcspn@plt>
movslq %eax,%r13
mov    %r14,%rdi
call   1040 <strlen@plt>
cmp    %r13,%rax
je     1206 <strcasestr+0xa6>
mov    %rax,%r12
mov    %r15,%rdi
call   1040 <strlen@plt>
mov    %rax,%rbx
nopl   0x0(%rax,%rax,1)
lea    (%r14,%r13,1),%rbp
mov    %rbp,%rdi
mov    %r15,%rsi
mov    %rbx,%rdx
call   1080 <strncasecmp@plt>
test   %rax,%rax
je     1208 <strcasestr+0xa8>
add    $0x1,%rbp
mov    %rbp,%rdi
lea    0x5(%rsp),%rsi
call   1060 <strcspn@plt>
cltq
add    %rax,%r13
add    $0x1,%r13
cmp    %r12,%r13
jne    11d0 <strcasestr+0x70>
xor    %ebp,%ebp
mov    %rbp,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [5 x i8] c"%c%c\00"
define i8* @strcasestr(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i64
%7 = alloca [3 x i8]
store i8* %0, i8** %4
store i8* %1, i8** %5
%8 = getelementptr inbounds [3 x i8], [3 x i8]* %7, i64 0, i64 0
%9 = load i8*, i8** %5
%10 = load i8, i8* %9
%11 = call i32 @tolower(i8 signext %10)
%12 = load i8*, i8** %5
%13 = load i8, i8* %12
%14 = call i32 @toupper(i8 signext %13)
%15 = call i32 @snprintf(i8* %8, i32 3, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 %11, i32 %14)
%16 = load i8*, i8** %4
%17 = getelementptr inbounds [3 x i8], [3 x i8]* %7, i64 0, i64 0
%18 = call i32 @strcspn(i8* %16, i8* %17)
%19 = sext i32 %18 to i64
store i64 %19, i64* %6
br label %20
20: ; preds = %39, %2
%21 = load i64, i64* %6
%22 = load i8*, i8** %4
%23 = call i64 @strlen(i8* %22)
%24 = icmp ne i64 %21, %23
br i1 %24, label %25, label %50
25: ; preds = %20
%26 = load i8*, i8** %4
%27 = load i64, i64* %6
%28 = getelementptr inbounds i8, i8* %26, i64 %27
%29 = load i8*, i8** %5
%30 = load i8*, i8** %5
%31 = call i64 @strlen(i8* %30)
%32 = call i64 @strncasecmp(i8* %28, i8* %29, i64 %31)
%33 = icmp eq i64 %32, 0
br i1 %33, label %34, label %38
34: ; preds = %25
%35 = load i8*, i8** %4
%36 = load i64, i64* %6
%37 = getelementptr inbounds i8, i8* %35, i64 %36
store i8* %37, i8** %3
br label %51
38: ; preds = %25
br label %39
39: ; preds = %38
%40 = load i8*, i8** %4
%41 = load i64, i64* %6
%42 = getelementptr inbounds i8, i8* %40, i64 %41
%43 = getelementptr inbounds i8, i8* %42, i64 1
%44 = getelementptr inbounds [3 x i8], [3 x i8]* %7, i64 0, i64 0
%45 = call i32 @strcspn(i8* %43, i8* %44)
%46 = add nsw i32 %45, 1
%47 = sext i32 %46 to i64
%48 = load i64, i64* %6
%49 = add i64 %48, %47
store i64 %49, i64* %6
br label %20
50: ; preds = %20
store i8* null, i8** %3
br label %51
51: ; preds = %50, %34
%52 = load i8*, i8** %3
ret i8* %52
}

LLVM IR (-O2):
@.str = [5 x i8] c"%c%c\00"
define i8* @strcasestr(i8* %0, i8* nocapture %1) {
%3 = alloca [3 x i8]
%4 = getelementptr inbounds [3 x i8], [3 x i8]* %3, i64 0, i64 0
%5 = load i8, i8* %1
%6 = tail call i32 @tolower(i8 signext %5)
%7 = load i8, i8* %1
%8 = tail call i32 @toupper(i8 signext %7)
%9 = call i32 @snprintf(i8* %4, i32 3, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 %6, i32 %8)
%10 = call i32 @strcspn(i8* %0, i8* %4)
%11 = sext i32 %10 to i64
%12 = call i64 @strlen(i8* dereferenceable(1) %0)
%13 = icmp eq i64 %12, %11
br i1 %13, label %28, label %14
14: ; preds = %2
%15 = call i64 @strlen(i8* %1)
br label %16
16: ; preds = %14, %21
%17 = phi i64 [ %11, %14 ], [ %26, %21 ]
%18 = getelementptr inbounds i8, i8* %0, i64 %17
%19 = call i64 @strncasecmp(i8* %18, i8* %1, i64 %15)
%20 = icmp eq i64 %19, 0
br i1 %20, label %28, label %21
21: ; preds = %16
%22 = getelementptr inbounds i8, i8* %18, i64 1
%23 = call i32 @strcspn(i8* %22, i8* %4)
%24 = add nsw i32 %23, 1
%25 = sext i32 %24 to i64
%26 = add i64 %17, %25
%27 = icmp eq i64 %26, %12
br i1 %27, label %28, label %16
28: ; preds = %16, %21, %2
%29 = phi i8* [ null, %2 ], [ null, %21 ], [ %18, %16 ]
ret i8* %29
}