#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*/ free(char *);
char *strdup(char const *);
size_t strlen(char *);
scalar_t__ strncasecmp(char *, char *, size_t);
int substr_appears(const char *str, char *sub_str) {
  int appearsCount = 0;
  size_t len = strlen(sub_str);
  char *copy = strdup(str);
  int i = 0;
  while (copy[i] != '\0') {
    if (strncasecmp((copy + i), sub_str, len) == 0) {
      appearsCount++;
    }
    copy[i++];
  }
  free(copy);
  return appearsCount;
}


x86 assembly (clang, -O0):
<substr_appears>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
call   1060 <strdup@plt>
mov    %rax,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    -0x28(%rbp),%rax
movslq -0x2c(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x0,%eax
je     11c6 <substr_appears+0x86>
mov    -0x28(%rbp),%rdi
movslq -0x2c(%rbp),%rax
add    %rax,%rdi
mov    -0x10(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1050 <strncasecmp@plt>
cmp    $0x0,%rax
jne    11b8 <substr_appears+0x78>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    1178 <substr_appears+0x38>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x14(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<substr_appears>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rsi,%r15
mov    %rdi,%rbx
mov    %rsi,%rdi
call   1040 <strlen@plt>
mov    %rax,%r12
mov    %rbx,%rdi
call   1060 <strdup@plt>
mov    %rax,%r14
cmpb   $0x0,(%rax)
je     1191 <substr_appears+0x51>
xor    %ebp,%ebp
mov    %r14,%rbx
xchg   %ax,%ax
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %r12,%rdx
call   1050 <strncasecmp@plt>
cmp    $0x1,%rax
adc    $0x0,%ebp
cmpb   $0x0,0x1(%rbx)
lea    0x1(%rbx),%rbx
jne    1170 <substr_appears+0x30>
jmp    1193 <substr_appears+0x53>
xor    %ebp,%ebp
mov    %r14,%rdi
call   1030 <free@plt>
mov    %ebp,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
define i32 @substr_appears(i8* nocapture %0, i8* nocapture %1) {
%3 = tail call i64 @strlen(i8* dereferenceable(1) %1)
%4 = tail call i8* @strdup(i8* %0)
%5 = load i8, i8* %4
%6 = icmp eq i8 %5, 0
br i1 %6, label %19, label %7
7: ; preds = %2, %7
%8 = phi i64 [ %15, %7 ], [ 0, %2 ]
%9 = phi i8* [ %16, %7 ], [ %4, %2 ]
%10 = phi i32 [ %14, %7 ], [ 0, %2 ]
%11 = tail call i64 @strncasecmp(i8* %9, i8* %1, i64 %3)
%12 = icmp eq i64 %11, 0
%13 = zext i1 %12 to i32
%14 = add nuw nsw i32 %10, %13
%15 = add nuw nsw i64 %8, 1
%16 = getelementptr inbounds i8, i8* %4, i64 %15
%17 = load i8, i8* %16
%18 = icmp eq i8 %17, 0
br i1 %18, label %19, label %7
19: ; preds = %7, %2
%20 = phi i32 [ 0, %2 ], [ %14, %7 ]
%21 = tail call i32 @free(i8* %4)
ret i32 %20
}