#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*/ PLD_strlower(char *);
int /*<<< orphan*/ free(char *);
char *strdup(char *);
char *strstr(char *, char *);
char *PLD_strstr(char *haystack, char *needle, int insensitive) {
  char *hs, *ne;
  char *result;
  if (insensitive > 0) {
    hs = strdup(haystack);
    PLD_strlower(hs);
    ne = strdup(needle);
    PLD_strlower(ne);
  } else {
    hs = haystack;
    ne = needle;
  }
  result = strstr(hs, ne);
  if ((result != NULL) && (insensitive > 0)) {
    result = result - hs + haystack;
  }
  if (insensitive) {
    free(hs);
    free(ne);
  }
  return result;
}


x86 assembly (clang, -O0):
<PLD_strstr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jle    118e <PLD_strstr+0x4e>
mov    -0x8(%rbp),%rdi
call   1050 <strdup@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
call   1040 <PLD_strlower@plt>
mov    -0x10(%rbp),%rdi
call   1050 <strdup@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
call   1040 <PLD_strlower@plt>
jmp    119e <PLD_strstr+0x5e>
mov    -0x8(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   1060 <strstr@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
je     11d7 <PLD_strstr+0x97>
cmpl   $0x0,-0x14(%rbp)
jle    11d7 <PLD_strstr+0x97>
mov    -0x30(%rbp),%rax
mov    -0x20(%rbp),%rcx
sub    %rcx,%rax
add    -0x8(%rbp),%rax
mov    %rax,-0x30(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     11f3 <PLD_strstr+0xb3>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x30(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<PLD_strstr>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %edx,%r14d
mov    %rsi,%r12
mov    %rdi,%rbx
test   %edx,%edx
jle    1188 <PLD_strstr+0x48>
call   1050 <strdup@plt>
mov    %rax,%r15
mov    %rax,%rdi
call   1040 <PLD_strlower@plt>
mov    %r12,%rdi
call   1050 <strdup@plt>
mov    %rax,%r12
mov    %rax,%rdi
call   1040 <PLD_strlower@plt>
mov    %r15,%rdi
mov    %r12,%rsi
call   1060 <strstr@plt>
mov    %rax,%rbp
jmp    119b <PLD_strstr+0x5b>
mov    %r12,%rsi
call   1060 <strstr@plt>
mov    %rax,%rbp
mov    %rbx,%r15
test   %r14d,%r14d
je     11ab <PLD_strstr+0x6b>
mov    %r15,%rdi
call   1030 <free@plt>
mov    %r12,%rdi
call   1030 <free@plt>
mov    %rbp,%rax
sub    %r15,%rax
add    %rax,%rbx
test   %rbp,%rbp
cmove  %rbp,%rbx
test   %r14d,%r14d
cmovle %rbp,%rbx
mov    %rbx,%rax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i8* @PLD_strstr(i8* %0, i8* %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i8*
store i8* %0, i8** %4
store i8* %1, i8** %5
store i32 %2, i32* %6
%10 = load i32, i32* %6
%11 = icmp sgt i32 %10, 0
br i1 %11, label %12, label %21
12: ; preds = %3
%13 = load i8*, i8** %4
%14 = call i8* @strdup(i8* %13)
store i8* %14, i8** %7
%15 = load i8*, i8** %7
%16 = call i32 @PLD_strlower(i8* %15)
%17 = load i8*, i8** %5
%18 = call i8* @strdup(i8* %17)
store i8* %18, i8** %8
%19 = load i8*, i8** %8
%20 = call i32 @PLD_strlower(i8* %19)
br label %24
21: ; preds = %3
%22 = load i8*, i8** %4
store i8* %22, i8** %7
%23 = load i8*, i8** %5
store i8* %23, i8** %8
br label %24
24: ; preds = %21, %12
%25 = load i8*, i8** %7
%26 = load i8*, i8** %8
%27 = call i8* @strstr(i8* %25, i8* %26)
store i8* %27, i8** %9
%28 = load i8*, i8** %9
%29 = icmp ne i8* %28, null
br i1 %29, label %30, label %41
30: ; preds = %24
%31 = load i32, i32* %6
%32 = icmp sgt i32 %31, 0
br i1 %32, label %33, label %41
33: ; preds = %30
%34 = load i8*, i8** %9
%35 = load i8*, i8** %7
%36 = ptrtoint i8* %34 to i64
%37 = ptrtoint i8* %35 to i64
%38 = sub i64 %36, %37
%39 = load i8*, i8** %4
%40 = getelementptr inbounds i8, i8* %39, i64 %38
store i8* %40, i8** %9
br label %41
41: ; preds = %33, %30, %24
%42 = load i32, i32* %6
%43 = icmp ne i32 %42, 0
br i1 %43, label %44, label %49
44: ; preds = %41
%45 = load i8*, i8** %7
%46 = call i32 @free(i8* %45)
%47 = load i8*, i8** %8
%48 = call i32 @free(i8* %47)
br label %49
49: ; preds = %44, %41
%50 = load i8*, i8** %9
ret i8* %50
}

LLVM IR (-O2):
define i8* @PLD_strstr(i8* %0, i8* nocapture %1, i32 %2) {
%4 = icmp sgt i32 %2, 0
br i1 %4, label %5, label %11
5: ; preds = %3
%6 = tail call i8* @strdup(i8* %0)
%7 = tail call i32 @PLD_strlower(i8* %6)
%8 = tail call i8* @strdup(i8* %1)
%9 = tail call i32 @PLD_strlower(i8* %8)
%10 = tail call i8* @strstr(i8* dereferenceable(1) %6, i8* dereferenceable(1) %8)
br label %14
11: ; preds = %3
%12 = tail call i8* @strstr(i8* dereferenceable(1) %0, i8* dereferenceable(1) %1)
%13 = icmp eq i32 %2, 0
br i1 %13, label %20, label %14
14: ; preds = %5, %11
%15 = phi i8* [ %10, %5 ], [ %12, %11 ]
%16 = phi i8* [ %8, %5 ], [ %1, %11 ]
%17 = phi i8* [ %6, %5 ], [ %0, %11 ]
%18 = tail call i32 @free(i8* %17)
%19 = tail call i32 @free(i8* %16)
br label %20
20: ; preds = %14, %11
%21 = phi i8* [ %15, %14 ], [ %12, %11 ]
%22 = phi i8* [ %17, %14 ], [ %0, %11 ]
%23 = icmp ne i8* %21, null
%24 = and i1 %4, %23
%25 = ptrtoint i8* %21 to i64
%26 = ptrtoint i8* %22 to i64
%27 = sub i64 %25, %26
%28 = getelementptr inbounds i8, i8* %0, i64 %27
%29 = select i1 %24, i8* %28, i8* %21
ret i8* %29
}