#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 */
char *create_substring(char *, char *, char *);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ strcmp(char *, char *);
char *strstr(char *, char const *);
char *strtok(char *, char *);
unsigned count_occurances_of(char *haystack, char *keyword) {
  unsigned count = 0;
  const char *TRACK_INFO = "trackinfo : [{";
  char *start_of_data = strstr(haystack, TRACK_INFO);
  char *tmp = create_substring(start_of_data, "trackinfo", "var CurrencyData");
  char *tok = strtok(tmp, ":\",");
  while (tok != NULL) {
    if (!strcmp(tok, keyword))
      count++;
    tok = strtok(NULL, ":\",");
  }
  free(tmp);
  return count;
}


x86 assembly (clang, -O0):
<count_occurances_of>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
lea    0xe92(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1070 <strstr@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
lea    0xe81(%rip),%rsi
lea    0xe84(%rip),%rdx
call   1050 <create_substring@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
lea    0xe81(%rip),%rsi
call   1060 <strtok@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
je     11f5 <count_occurances_of+0xa5>
mov    -0x38(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1040 <strcmp@plt>
cmp    $0x0,%eax
jne    11dc <count_occurances_of+0x8c>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
xor    %eax,%eax
mov    %eax,%edi
lea    0xe43(%rip),%rsi
call   1060 <strtok@plt>
mov    %rax,-0x38(%rbp)
jmp    11b2 <count_occurances_of+0x62>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
mov    -0x14(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<count_occurances_of>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r15
lea    0xe9e(%rip),%rsi
call   1070 <strstr@plt>
lea    0xea1(%rip),%rsi
lea    0xea4(%rip),%rdx
mov    %rax,%rdi
call   1050 <create_substring@plt>
mov    %rax,%r14
lea    0xea3(%rip),%rsi
mov    %rax,%rdi
call   1060 <strtok@plt>
xor    %ebx,%ebx
test   %rax,%rax
je     11c0 <count_occurances_of+0x70>
lea    0xe8d(%rip),%r12
nopl   (%rax)
mov    %rax,%rdi
mov    %r15,%rsi
call   1040 <strcmp@plt>
cmp    $0x1,%eax
adc    $0x0,%ebx
xor    %edi,%edi
mov    %r12,%rsi
call   1060 <strtok@plt>
test   %rax,%rax
jne    11a0 <count_occurances_of+0x50>
mov    %r14,%rdi
call   1030 <free@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [15 x i8] c"trackinfo : [{\00"
@.str.1 = [10 x i8] c"trackinfo\00"
@.str.2 = [17 x i8] c"var CurrencyData\00"
@.str.3 = [4 x i8] c":\22,\00"
define i32 @count_occurances_of(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i8*
store i8* %0, i8** %3
store i8* %1, i8** %4
store i32 0, i32* %5
store i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8** %6
%10 = load i8*, i8** %3
%11 = load i8*, i8** %6
%12 = call i8* @strstr(i8* %10, i8* %11)
store i8* %12, i8** %7
%13 = load i8*, i8** %7
%14 = call i8* @create_substring(i8* %13, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0))
store i8* %14, i8** %8
%15 = load i8*, i8** %8
%16 = call i8* @strtok(i8* %15, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0))
store i8* %16, i8** %9
br label %17
17: ; preds = %28, %2
%18 = load i8*, i8** %9
%19 = icmp ne i8* %18, null
br i1 %19, label %20, label %30
20: ; preds = %17
%21 = load i8*, i8** %9
%22 = load i8*, i8** %4
%23 = call i32 @strcmp(i8* %21, i8* %22)
%24 = icmp ne i32 %23, 0
br i1 %24, label %28, label %25
25: ; preds = %20
%26 = load i32, i32* %5
%27 = add i32 %26, 1
store i32 %27, i32* %5
br label %28
28: ; preds = %25, %20
%29 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0))
store i8* %29, i8** %9
br label %17
30: ; preds = %17
%31 = load i8*, i8** %8
%32 = call i32 @free(i8* %31)
%33 = load i32, i32* %5
ret i32 %33
}

LLVM IR (-O2):
@.str = [15 x i8] c"trackinfo : [{\00"
@.str.1 = [10 x i8] c"trackinfo\00"
@.str.2 = [17 x i8] c"var CurrencyData\00"
@.str.3 = [4 x i8] c":\22,\00"
define i32 @count_occurances_of(i8* %0, i8* nocapture %1) {
%3 = tail call i8* @strstr(i8* dereferenceable(1) %0, i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%4 = tail call i8* @create_substring(i8* %3, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0))
%5 = tail call i8* @strtok(i8* %4, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0))
%6 = icmp eq i8* %5, null
br i1 %6, label %16, label %7
7: ; preds = %2, %7
%8 = phi i8* [ %14, %7 ], [ %5, %2 ]
%9 = phi i32 [ %13, %7 ], [ 0, %2 ]
%10 = tail call i32 @strcmp(i8* %8, i8* dereferenceable(1) %1)
%11 = icmp eq i32 %10, 0
%12 = zext i1 %11 to i32
%13 = add i32 %9, %12
%14 = tail call i8* @strtok(i8* null, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0))
%15 = icmp eq i8* %14, null
br i1 %15, label %16, label %7
16: ; preds = %7, %2
%17 = phi i32 [ 0, %2 ], [ %13, %7 ]
%18 = tail call i32 @free(i8* %4)
ret i32 %17
}