#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 */
int strlen(char const *);
scalar_t__ strncasecmp(char const *, char const *, int);
char *internal_stristr(const char *haystack, const char *needle) {
  int len = 0;
  const char *p = haystack;
  if (!(haystack && needle)) {
    return NULL;
  }
  len = strlen(needle);
  while (*p != '\0') {
    if (strncasecmp(p, needle, len) == 0)
      return (char *)p;
    p++;
  }
  return NULL;
}


x86 assembly (clang, -O0):
<internal_stristr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1155 <internal_stristr+0x35>
cmpq   $0x0,-0x18(%rbp)
jne    1162 <internal_stristr+0x42>
movq   $0x0,-0x8(%rbp)
jmp    11be <internal_stristr+0x9e>
mov    -0x18(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x28(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     11b6 <internal_stristr+0x96>
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1040 <strncasecmp@plt>
cmp    $0x0,%rax
jne    11a5 <internal_stristr+0x85>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11be <internal_stristr+0x9e>
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
jmp    116e <internal_stristr+0x4e>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<internal_stristr>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
xor    %r14d,%r14d
test   %rdi,%rdi
je     1176 <internal_stristr+0x56>
mov    %rsi,%r15
test   %rsi,%rsi
je     1176 <internal_stristr+0x56>
mov    %rdi,%rbx
mov    %r15,%rdi
call   1030 <strlen@plt>
cmpb   $0x0,(%rbx)
je     116e <internal_stristr+0x4e>
mov    %eax,%ebp
xor    %r14d,%r14d
nopl   0x0(%rax)
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %ebp,%edx
call   1040 <strncasecmp@plt>
test   %rax,%rax
je     1173 <internal_stristr+0x53>
cmpb   $0x0,0x1(%rbx)
lea    0x1(%rbx),%rbx
jne    1150 <internal_stristr+0x30>
jmp    1176 <internal_stristr+0x56>
xor    %r14d,%r14d
jmp    1176 <internal_stristr+0x56>
mov    %rbx,%r14
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
define i8* @internal_stristr(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 @strlen(i8* %1)
%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 = %10, %14, %6, %2
%19 = phi i8* [ null, %2 ], [ null, %6 ], [ %11, %10 ], [ null, %14 ]
ret i8* %19
}