#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*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ stderr;
char *strdup(char const *);
int strlen(char *);
char *strstr(char *, char *);
char tolower(char);
char *my_strcasestr(const char *str, const char *str2) {
  char *tmp1 = NULL;
  char *tmp2 = NULL;
  char *tmp3 = NULL;
  char *retval = NULL;
  tmp1 = strdup(str);
  if (tmp1 == NULL) {
    fprintf(stderr, "Strdup failed\n");
    return NULL;
  }
  tmp2 = strdup(str2);
  if (tmp2 == NULL) {
    free(tmp1);
    fprintf(stderr, "Strdup failed\n");
    return NULL;
  }
  for (int i = 0; i < strlen(tmp1); i++)
    tmp1[i] = tolower(tmp1[i]);
  for (int i = 0; i < strlen(tmp2); i++)
    tmp2[i] = tolower(tmp2[i]);
  tmp3 = strstr(tmp1, tmp2);
  if (tmp3 != NULL) {
    retval = strdup(tmp3);
    if (retval == NULL)
      fprintf(stderr, "Search term found, but strdup failed.\n");
  }
  free(tmp1);
  free(tmp2);
  return retval;
}


x86 assembly (clang, -O0):
<my_strcasestr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
movq   $0x0,-0x38(%rbp)
mov    -0x10(%rbp),%rdi
call   1070 <strdup@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11ca <my_strcasestr+0x6a>
mov    0x2e31(%rip),%rax
mov    (%rax),%edi
lea    0xe48(%rip),%rsi
call   1050 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
jmp    1313 <my_strcasestr+0x1b3>
mov    -0x18(%rbp),%rdi
call   1070 <strdup@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    120d <my_strcasestr+0xad>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    0x2dee(%rip),%rax
mov    (%rax),%edi
lea    0xe05(%rip),%rsi
call   1050 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
jmp    1313 <my_strcasestr+0x1b3>
movl   $0x0,-0x3c(%rbp)
mov    -0x3c(%rbp),%eax
mov    %eax,-0x44(%rbp)
mov    -0x20(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x44(%rbp),%eax
cmp    %ecx,%eax
jge    125c <my_strcasestr+0xfc>
mov    -0x20(%rbp),%rax
movslq -0x3c(%rbp),%rcx
movsbl (%rax,%rcx,1),%edi
call   1060 <tolower@plt>
mov    %al,%dl
mov    -0x20(%rbp),%rax
movslq -0x3c(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x3c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x3c(%rbp)
jmp    1214 <my_strcasestr+0xb4>
movl   $0x0,-0x40(%rbp)
mov    -0x40(%rbp),%eax
mov    %eax,-0x48(%rbp)
mov    -0x28(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x48(%rbp),%eax
cmp    %ecx,%eax
jge    12ab <my_strcasestr+0x14b>
mov    -0x28(%rbp),%rax
movslq -0x40(%rbp),%rcx
movsbl (%rax,%rcx,1),%edi
call   1060 <tolower@plt>
mov    %al,%dl
mov    -0x28(%rbp),%rax
movslq -0x40(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x40(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x40(%rbp)
jmp    1263 <my_strcasestr+0x103>
mov    -0x20(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   1080 <strstr@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
je     12f9 <my_strcasestr+0x199>
mov    -0x30(%rbp),%rdi
call   1070 <strdup@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    12f4 <my_strcasestr+0x194>
mov    0x2cfa(%rip),%rax
mov    (%rax),%edi
lea    0xd20(%rip),%rsi
call   1050 <fprintf@plt>
jmp    12f9 <my_strcasestr+0x199>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x38(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<my_strcasestr>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%rbx
call   1070 <strdup@plt>
test   %rax,%rax
je     1243 <my_strcasestr+0xe3>
mov    %rax,%r14
mov    %rbx,%rdi
call   1070 <strdup@plt>
test   %rax,%rax
je     123b <my_strcasestr+0xdb>
mov    %rax,%r12
mov    %r14,%rdi
call   1040 <strlen@plt>
test   %eax,%eax
jle    11c1 <my_strcasestr+0x61>
xor    %ebx,%ebx
xchg   %ax,%ax
movsbl (%r14,%rbx,1),%edi
call   1060 <tolower@plt>
mov    %al,(%r14,%rbx,1)
add    $0x1,%rbx
mov    %r14,%rdi
call   1040 <strlen@plt>
cltq
cmp    %rax,%rbx
jl     11a0 <my_strcasestr+0x40>
mov    %r12,%rdi
call   1040 <strlen@plt>
test   %eax,%eax
jle    11f1 <my_strcasestr+0x91>
xor    %ebx,%ebx
nop
movsbl (%r12,%rbx,1),%edi
call   1060 <tolower@plt>
mov    %al,(%r12,%rbx,1)
add    $0x1,%rbx
mov    %r12,%rdi
call   1040 <strlen@plt>
cltq
cmp    %rax,%rbx
jl     11d0 <my_strcasestr+0x70>
mov    %r14,%rdi
mov    %r12,%rsi
call   1080 <strstr@plt>
test   %rax,%rax
je     1226 <my_strcasestr+0xc6>
mov    %rax,%rdi
call   1070 <strdup@plt>
mov    %rax,%r15
test   %rax,%rax
jne    1229 <my_strcasestr+0xc9>
mov    0x2dc8(%rip),%rax
mov    (%rax),%edi
lea    0xdee(%rip),%rsi
call   1050 <fprintf@plt>
xor    %r15d,%r15d
mov    %r14,%rdi
call   1030 <free@plt>
mov    %r12,%rdi
call   1030 <free@plt>
jmp    125b <my_strcasestr+0xfb>
mov    %r14,%rdi
call   1030 <free@plt>
mov    0x2d96(%rip),%rax
mov    (%rax),%edi
lea    0xdad(%rip),%rsi
call   1050 <fprintf@plt>
xor    %r15d,%r15d
mov    %r15,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [15 x i8] c"Strdup failed\0A\00"
@.str.1 = [39 x i8] c"Search term found, but strdup failed.\0A\00"
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*
%9 = alloca i8*
%10 = alloca i32
%11 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
store i8* null, i8** %6
store i8* null, i8** %7
store i8* null, i8** %8
store i8* null, i8** %9
%12 = load i8*, i8** %4
%13 = call i8* @strdup(i8* %12)
store i8* %13, i8** %6
%14 = load i8*, i8** %6
%15 = icmp eq i8* %14, null
br i1 %15, label %16, label %19
16: ; preds = %2
%17 = load i32, i32* @stderr
%18 = call i32 @fprintf(i32 %17, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
store i8* null, i8** %3
br label %90
19: ; preds = %2
%20 = load i8*, i8** %5
%21 = call i8* @strdup(i8* %20)
store i8* %21, i8** %7
%22 = load i8*, i8** %7
%23 = icmp eq i8* %22, null
br i1 %23, label %24, label %29
24: ; preds = %19
%25 = load i8*, i8** %6
%26 = call i32 @free(i8* %25)
%27 = load i32, i32* @stderr
%28 = call i32 @fprintf(i32 %27, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
store i8* null, i8** %3
br label %90
29: ; preds = %19
store i32 0, i32* %10
br label %30
30: ; preds = %46, %29
%31 = load i32, i32* %10
%32 = load i8*, i8** %6
%33 = call i32 @strlen(i8* %32)
%34 = icmp slt i32 %31, %33
br i1 %34, label %35, label %49
35: ; preds = %30
%36 = load i8*, i8** %6
%37 = load i32, i32* %10
%38 = sext i32 %37 to i64
%39 = getelementptr inbounds i8, i8* %36, i64 %38
%40 = load i8, i8* %39
%41 = call signext i8 @tolower(i8 signext %40)
%42 = load i8*, i8** %6
%43 = load i32, i32* %10
%44 = sext i32 %43 to i64
%45 = getelementptr inbounds i8, i8* %42, i64 %44
store i8 %41, i8* %45
br label %46
46: ; preds = %35
%47 = load i32, i32* %10
%48 = add nsw i32 %47, 1
store i32 %48, i32* %10
br label %30
49: ; preds = %30
store i32 0, i32* %11
br label %50
50: ; preds = %66, %49
%51 = load i32, i32* %11
%52 = load i8*, i8** %7
%53 = call i32 @strlen(i8* %52)
%54 = icmp slt i32 %51, %53
br i1 %54, label %55, label %69
55: ; preds = %50
%56 = load i8*, i8** %7
%57 = load i32, i32* %11
%58 = sext i32 %57 to i64
%59 = getelementptr inbounds i8, i8* %56, i64 %58
%60 = load i8, i8* %59
%61 = call signext i8 @tolower(i8 signext %60)
%62 = load i8*, i8** %7
%63 = load i32, i32* %11
%64 = sext i32 %63 to i64
%65 = getelementptr inbounds i8, i8* %62, i64 %64
store i8 %61, i8* %65
br label %66
66: ; preds = %55
%67 = load i32, i32* %11
%68 = add nsw i32 %67, 1
store i32 %68, i32* %11
br label %50
69: ; preds = %50
%70 = load i8*, i8** %6
%71 = load i8*, i8** %7
%72 = call i8* @strstr(i8* %70, i8* %71)
store i8* %72, i8** %8
%73 = load i8*, i8** %8
%74 = icmp ne i8* %73, null
br i1 %74, label %75, label %84
75: ; preds = %69
%76 = load i8*, i8** %8
%77 = call i8* @strdup(i8* %76)
store i8* %77, i8** %9
%78 = load i8*, i8** %9
%79 = icmp eq i8* %78, null
br i1 %79, label %80, label %83
80: ; preds = %75
%81 = load i32, i32* @stderr
%82 = call i32 @fprintf(i32 %81, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0))
br label %83
83: ; preds = %80, %75
br label %84
84: ; preds = %83, %69
%85 = load i8*, i8** %6
%86 = call i32 @free(i8* %85)
%87 = load i8*, i8** %7
%88 = call i32 @free(i8* %87)
%89 = load i8*, i8** %9
store i8* %89, i8** %3
br label %90
90: ; preds = %84, %24, %16
%91 = load i8*, i8** %3
ret i8* %91
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [15 x i8] c"Strdup failed\0A\00"
@.str.1 = [39 x i8] c"Search term found, but strdup failed.\0A\00"
define noalias i8* @my_strcasestr(i8* nocapture %0, i8* nocapture %1) {
%3 = tail call i8* @strdup(i8* %0)
%4 = icmp eq i8* %3, null
br i1 %4, label %5, label %8
5: ; preds = %2
%6 = load i32, i32* @stderr
%7 = tail call i32 @fprintf(i32 %6, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
br label %52
8: ; preds = %2
%9 = tail call i8* @strdup(i8* %1)
%10 = icmp eq i8* %9, null
br i1 %10, label %14, label %11
11: ; preds = %8
%12 = tail call i32 @strlen(i8* %3)
%13 = icmp sgt i32 %12, 0
br i1 %13, label %21, label %18
14: ; preds = %8
%15 = tail call i32 @free(i8* %3)
%16 = load i32, i32* @stderr
%17 = tail call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
br label %52
18: ; preds = %21, %11
%19 = tail call i32 @strlen(i8* %9)
%20 = icmp sgt i32 %19, 0
br i1 %20, label %33, label %30
21: ; preds = %11, %21
%22 = phi i64 [ %26, %21 ], [ 0, %11 ]
%23 = getelementptr inbounds i8, i8* %3, i64 %22
%24 = load i8, i8* %23
%25 = tail call signext i8 @tolower(i8 signext %24)
store i8 %25, i8* %23
%26 = add nuw nsw i64 %22, 1
%27 = tail call i32 @strlen(i8* %3)
%28 = sext i32 %27 to i64
%29 = icmp slt i64 %26, %28
br i1 %29, label %21, label %18
30: ; preds = %33, %18
%31 = tail call i8* @strstr(i8* %3, i8* %9)
%32 = icmp eq i8* %31, null
br i1 %32, label %48, label %42
33: ; preds = %18, %33
%34 = phi i64 [ %38, %33 ], [ 0, %18 ]
%35 = getelementptr inbounds i8, i8* %9, i64 %34
%36 = load i8, i8* %35
%37 = tail call signext i8 @tolower(i8 signext %36)
store i8 %37, i8* %35
%38 = add nuw nsw i64 %34, 1
%39 = tail call i32 @strlen(i8* %9)
%40 = sext i32 %39 to i64
%41 = icmp slt i64 %38, %40
br i1 %41, label %33, label %30
42: ; preds = %30
%43 = tail call i8* @strdup(i8* %31)
%44 = icmp eq i8* %43, null
br i1 %44, label %45, label %48
45: ; preds = %42
%46 = load i32, i32* @stderr
%47 = tail call i32 @fprintf(i32 %46, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0))
br label %48
48: ; preds = %42, %45, %30
%49 = phi i8* [ null, %45 ], [ %43, %42 ], [ null, %30 ]
%50 = tail call i32 @free(i8* %3)
%51 = tail call i32 @free(i8* %9)
br label %52
52: ; preds = %48, %14, %5
%53 = phi i8* [ null, %5 ], [ null, %14 ], [ %49, %48 ]
ret i8* %53
}