#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__ strncasecmp(char const *, char const *, int);
int strnlen(char const *, int);
const char *strcasestr(const char *str, const char *pattern) {
  if (!str || !pattern) {
    return 0;
  }
  int len = strnlen(pattern, 0x1000);
  while (*str) {
    if (strncasecmp(str, pattern, len) == 0) {
      return str;
    }
    str++;
  }
  return 0;
}


x86 assembly (clang, -O0):
<strcasestr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1146 <strcasestr+0x26>
cmpq   $0x0,-0x18(%rbp)
jne    1153 <strcasestr+0x33>
movq   $0x0,-0x8(%rbp)
jmp    11b1 <strcasestr+0x91>
mov    -0x18(%rbp),%rdi
mov    $0x1000,%esi
call   1030 <strnlen@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%rax
cmpb   $0x0,(%rax)
je     11a9 <strcasestr+0x89>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1040 <strncasecmp@plt>
cmp    $0x0,%rax
jne    1198 <strcasestr+0x78>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11b1 <strcasestr+0x91>
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
jmp    1164 <strcasestr+0x44>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<strcasestr>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
xor    %r14d,%r14d
test   %rdi,%rdi
je     1186 <strcasestr+0x66>
mov    %rsi,%r15
test   %rsi,%rsi
je     1186 <strcasestr+0x66>
mov    %rdi,%rbx
mov    %r15,%rdi
mov    $0x1000,%esi
call   1030 <strnlen@plt>
cmpb   $0x0,(%rbx)
je     117e <strcasestr+0x5e>
mov    %eax,%ebp
xor    %r14d,%r14d
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %ebp,%edx
call   1040 <strncasecmp@plt>
test   %rax,%rax
je     1183 <strcasestr+0x63>
cmpb   $0x0,0x1(%rbx)
lea    0x1(%rbx),%rbx
jne    1160 <strcasestr+0x40>
jmp    1186 <strcasestr+0x66>
xor    %r14d,%r14d
jmp    1186 <strcasestr+0x66>
mov    %rbx,%r14
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
define i8* @strcasestr(i8* %0, i8* %1) {
%3 = icmp ne i8* %0, null
%4 = icmp ne i8* %1, null
%5 = and i1 %3, %4
br i1 %5, label %6, label %18
6: ; preds = %2
%7 = tail call i32 @strnlen(i8* %1, i32 4096)
%8 = load i8, i8* %0
%9 = icmp eq i8 %8, 0
br i1 %9, label %18, label %10
10: ; preds = %6, %14
%11 = phi i8* [ %15, %14 ], [ %0, %6 ]
%12 = tail call i64 @strncasecmp(i8* %11, i8* %1, i32 %7)
%13 = icmp eq i64 %12, 0
br i1 %13, label %18, label %14
14: ; preds = %10
%15 = getelementptr inbounds i8, i8* %11, i64 1
%16 = load i8, i8* %15
%17 = icmp eq i8 %16, 0
br i1 %17, label %18, label %10
18: ; preds = %14, %10, %6, %2
%19 = phi i8* [ null, %2 ], [ null, %6 ], [ null, %14 ], [ %11, %10 ]
ret i8* %19
}