#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 /*<<< orphan*/ enough_mem(char *);
int /*<<< orphan*/ free(char *);
char *strdup(char const *);
char *strstr(char *, char *);
scalar_t__ toupper(int);
char *my_strcasestr(const char *haystack, const char *needle) {
  char *nhay = strdup(haystack);
  char *nneed = strdup(needle);
  const char *match;
  char *p;
  enough_mem(nhay);
  enough_mem(nneed);
  p = nhay;
  while (*p) {
    *p = (char)toupper((int)*p);
    p++;
  }
  p = nneed;
  while (*p) {
    *p = (char)toupper((int)*p);
    p++;
  }
  match = strstr(nhay, nneed);
  if (match) {
    match = haystack + (match - nhay);
  }
  free(nhay);
  free(nneed);
  return (char *)match;
}


x86 assembly (clang, -O0):
<my_strcasestr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
call   1060 <strdup@plt>
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <strdup@plt>
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
call   1050 <enough_mem@plt>
mov    -0x20(%rbp),%rdi
call   1050 <enough_mem@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
cmpb   $0x0,(%rax)
je     11c6 <my_strcasestr+0x76>
mov    -0x30(%rbp),%rax
movsbl (%rax),%edi
call   1040 <toupper@plt>
mov    %al,%cl
mov    -0x30(%rbp),%rax
mov    %cl,(%rax)
mov    -0x30(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x30(%rbp)
jmp    1194 <my_strcasestr+0x44>
mov    -0x20(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
cmpb   $0x0,(%rax)
je     1200 <my_strcasestr+0xb0>
mov    -0x30(%rbp),%rax
movsbl (%rax),%edi
call   1040 <toupper@plt>
mov    %al,%cl
mov    -0x30(%rbp),%rax
mov    %cl,(%rax)
mov    -0x30(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x30(%rbp)
jmp    11ce <my_strcasestr+0x7e>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1070 <strstr@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     1232 <my_strcasestr+0xe2>
mov    -0x8(%rbp),%rax
mov    -0x28(%rbp),%rcx
mov    -0x18(%rbp),%rdx
sub    %rdx,%rcx
add    %rcx,%rax
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x28(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<my_strcasestr>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %rdi,%r14
call   1060 <strdup@plt>
mov    %rax,%r12
mov    %rbx,%rdi
call   1060 <strdup@plt>
mov    %rax,%r15
mov    %r12,%rdi
call   1050 <enough_mem@plt>
mov    %r15,%rdi
call   1050 <enough_mem@plt>
mov    (%r12),%al
test   %al,%al
je     11a6 <my_strcasestr+0x56>
lea    0x1(%r12),%rbx
xchg   %ax,%ax
movsbl %al,%edi
call   1040 <toupper@plt>
mov    %al,-0x1(%rbx)
movzbl (%rbx),%eax
add    $0x1,%rbx
test   %al,%al
jne    1190 <my_strcasestr+0x40>
mov    (%r15),%al
test   %al,%al
je     11d6 <my_strcasestr+0x86>
mov    %r15,%rbx
add    $0x1,%rbx
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
movsbl %al,%edi
call   1040 <toupper@plt>
mov    %al,-0x1(%rbx)
movzbl (%rbx),%eax
add    $0x1,%rbx
test   %al,%al
jne    11c0 <my_strcasestr+0x70>
mov    %r12,%rdi
mov    %r15,%rsi
call   1070 <strstr@plt>
mov    %rax,%rbx
sub    %r12,%rax
add    %rax,%r14
test   %rbx,%rbx
cmovne %r14,%rbx
mov    %r12,%rdi
call   1030 <free@plt>
mov    %r15,%rdi
call   1030 <free@plt>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i8* @my_strcasestr(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
store i8* %0, i8** %3
store i8* %1, i8** %4
%9 = load i8*, i8** %3
%10 = call i8* @strdup(i8* %9)
store i8* %10, i8** %5
%11 = load i8*, i8** %4
%12 = call i8* @strdup(i8* %11)
store i8* %12, i8** %6
%13 = load i8*, i8** %5
%14 = call i32 @enough_mem(i8* %13)
%15 = load i8*, i8** %6
%16 = call i32 @enough_mem(i8* %15)
%17 = load i8*, i8** %5
store i8* %17, i8** %8
br label %18
18: ; preds = %22, %2
%19 = load i8*, i8** %8
%20 = load i8, i8* %19
%21 = icmp ne i8 %20, 0
br i1 %21, label %22, label %31
22: ; preds = %18
%23 = load i8*, i8** %8
%24 = load i8, i8* %23
%25 = sext i8 %24 to i32
%26 = call i64 @toupper(i32 %25)
%27 = trunc i64 %26 to i8
%28 = load i8*, i8** %8
store i8 %27, i8* %28
%29 = load i8*, i8** %8
%30 = getelementptr inbounds i8, i8* %29, i32 1
store i8* %30, i8** %8
br label %18
31: ; preds = %18
%32 = load i8*, i8** %6
store i8* %32, i8** %8
br label %33
33: ; preds = %37, %31
%34 = load i8*, i8** %8
%35 = load i8, i8* %34
%36 = icmp ne i8 %35, 0
br i1 %36, label %37, label %46
37: ; preds = %33
%38 = load i8*, i8** %8
%39 = load i8, i8* %38
%40 = sext i8 %39 to i32
%41 = call i64 @toupper(i32 %40)
%42 = trunc i64 %41 to i8
%43 = load i8*, i8** %8
store i8 %42, i8* %43
%44 = load i8*, i8** %8
%45 = getelementptr inbounds i8, i8* %44, i32 1
store i8* %45, i8** %8
br label %33
46: ; preds = %33
%47 = load i8*, i8** %5
%48 = load i8*, i8** %6
%49 = call i8* @strstr(i8* %47, i8* %48)
store i8* %49, i8** %7
%50 = load i8*, i8** %7
%51 = icmp ne i8* %50, null
br i1 %51, label %52, label %60
52: ; preds = %46
%53 = load i8*, i8** %3
%54 = load i8*, i8** %7
%55 = load i8*, i8** %5
%56 = ptrtoint i8* %54 to i64
%57 = ptrtoint i8* %55 to i64
%58 = sub i64 %56, %57
%59 = getelementptr inbounds i8, i8* %53, i64 %58
store i8* %59, i8** %7
br label %60
60: ; preds = %52, %46
%61 = load i8*, i8** %5
%62 = call i32 @free(i8* %61)
%63 = load i8*, i8** %6
%64 = call i32 @free(i8* %63)
%65 = load i8*, i8** %7
ret i8* %65
}

LLVM IR (-O2):
define i8* @my_strcasestr(i8* %0, i8* nocapture %1) {
%3 = tail call i8* @strdup(i8* %0)
%4 = tail call i8* @strdup(i8* %1)
%5 = tail call i32 @enough_mem(i8* %3)
%6 = tail call i32 @enough_mem(i8* %4)
%7 = load i8, i8* %3
%8 = icmp eq i8 %7, 0
br i1 %8, label %9, label %12
9: ; preds = %12, %2
%10 = load i8, i8* %4
%11 = icmp eq i8 %10, 0
br i1 %11, label %30, label %21
12: ; preds = %2, %12
%13 = phi i8 [ %19, %12 ], [ %7, %2 ]
%14 = phi i8* [ %18, %12 ], [ %3, %2 ]
%15 = sext i8 %13 to i32
%16 = tail call i64 @toupper(i32 %15)
%17 = trunc i64 %16 to i8
store i8 %17, i8* %14
%18 = getelementptr inbounds i8, i8* %14, i64 1
%19 = load i8, i8* %18
%20 = icmp eq i8 %19, 0
br i1 %20, label %9, label %12
21: ; preds = %9, %21
%22 = phi i8 [ %28, %21 ], [ %10, %9 ]
%23 = phi i8* [ %27, %21 ], [ %4, %9 ]
%24 = sext i8 %22 to i32
%25 = tail call i64 @toupper(i32 %24)
%26 = trunc i64 %25 to i8
store i8 %26, i8* %23
%27 = getelementptr inbounds i8, i8* %23, i64 1
%28 = load i8, i8* %27
%29 = icmp eq i8 %28, 0
br i1 %29, label %30, label %21
30: ; preds = %21, %9
%31 = tail call i8* @strstr(i8* dereferenceable(1) %3, i8* dereferenceable(1) %4)
%32 = icmp eq i8* %31, null
%33 = ptrtoint i8* %31 to i64
%34 = ptrtoint i8* %3 to i64
%35 = sub i64 %33, %34
%36 = getelementptr inbounds i8, i8* %0, i64 %35
%37 = select i1 %32, i8* null, i8* %36
%38 = tail call i32 @free(i8* %3)
%39 = tail call i32 @free(i8* %4)
ret i8* %37
}