#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 */
typedef struct TYPE_3__ TYPE_1__;

/* Type definitions */
struct TYPE_3__ {
  int rm_so;
  int /*<<< orphan*/ rm_eo;
};
typedef TYPE_1__ regmatch_t;
typedef int /*<<< orphan*/ regex_t;

/* Variables and functions */
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ free(char *);
scalar_t__ malloc(int);
int /*<<< orphan*/ memcpy(char *, char *, size_t);
scalar_t__ realloc(char **, int);
scalar_t__ regexec(int /*<<< orphan*/ *, char *, size_t, TYPE_1__ *,
                   int /*<<< orphan*/);
char *trim_whitespaces(char *);
char **find_all_occurances(char *page, regex_t *compiledReg) {
  size_t nsub = 1, returnPointer = 0, dynamicArray = 4;
  regmatch_t matchLocations[nsub];
  char **toReturn = (char **)malloc(sizeof(char *) * dynamicArray);
  if (toReturn == NULL) {
    exit(21);
  }
  bool keepRunning = true;
  do {
    if (regexec(compiledReg, page, nsub, matchLocations, 0) != 0) {
      break;
    }
    for (size_t i = 0; i < nsub; i++) {
      if (matchLocations[i].rm_so == -1) {
        keepRunning = false;
        break;
      }
      size_t lengthOfKey = matchLocations[i].rm_eo - matchLocations[i].rm_so;
      char *newKey = (char *)malloc(sizeof(char) * (lengthOfKey + 1));
      if (newKey == NULL) {
        exit(21);
      }
      memcpy(newKey, page + (matchLocations[i].rm_so), lengthOfKey);
      newKey[lengthOfKey] = '\0';
      if (++returnPointer == dynamicArray) {
        dynamicArray *= 2;
        toReturn = (char **)realloc(toReturn, sizeof(char *) * dynamicArray);
        if (toReturn == NULL) {
          exit(21);
        }
      }
      toReturn[returnPointer - 1] = trim_whitespaces(newKey);
      free(newKey);
    }
    page += matchLocations[0].rm_eo;
  } while (keepRunning);
  toReturn[returnPointer] = NULL;
  return toReturn;
}


x86 assembly (clang, -O0):
<find_all_occurances>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x1,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
movq   $0x4,-0x28(%rbp)
mov    -0x18(%rbp),%rax
mov    %rsp,%rcx
mov    %rcx,-0x30(%rbp)
lea    0xf(,%rax,8),%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x68(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x38(%rbp)
mov    -0x28(%rbp),%rax
shl    $0x3,%rax
mov    %eax,%edi
call   1050 <malloc@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
jne    11e8 <find_all_occurances+0x78>
mov    $0x15,%edi
call   1090 <exit@plt>
movl   $0x1,-0x44(%rbp)
mov    -0x68(%rbp),%rcx
mov    -0x10(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x18(%rbp),%rdx
xor    %r8d,%r8d
call   1060 <regexec@plt>
cmp    $0x0,%rax
je     1216 <find_all_occurances+0xa6>
jmp    1363 <find_all_occurances+0x1f3>
movq   $0x0,-0x50(%rbp)
mov    -0x50(%rbp),%rax
cmp    -0x18(%rbp),%rax
jae    1344 <find_all_occurances+0x1d4>
mov    -0x68(%rbp),%rax
mov    -0x50(%rbp),%rcx
cmpl   $0xffffffff,(%rax,%rcx,8)
jne    124a <find_all_occurances+0xda>
movl   $0x0,-0x44(%rbp)
jmp    1344 <find_all_occurances+0x1d4>
mov    -0x68(%rbp),%rcx
mov    -0x50(%rbp),%rax
mov    0x4(%rcx,%rax,8),%eax
mov    -0x50(%rbp),%rdx
sub    (%rcx,%rdx,8),%eax
cltq
mov    %rax,-0x58(%rbp)
mov    -0x58(%rbp),%rax
add    $0x1,%rax
shl    $0x0,%rax
mov    %eax,%edi
call   1050 <malloc@plt>
mov    %rax,-0x60(%rbp)
cmpq   $0x0,-0x60(%rbp)
jne    128f <find_all_occurances+0x11f>
mov    $0x15,%edi
call   1090 <exit@plt>
mov    -0x68(%rbp),%rax
mov    -0x60(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x50(%rbp),%rcx
movslq (%rax,%rcx,8),%rax
add    %rax,%rsi
mov    -0x58(%rbp),%rdx
call   1040 <memcpy@plt>
mov    -0x60(%rbp),%rax
mov    -0x58(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
cmp    -0x28(%rbp),%rax
jne    130e <find_all_occurances+0x19e>
mov    -0x28(%rbp),%rax
shl    $0x1,%rax
mov    %rax,-0x28(%rbp)
mov    -0x40(%rbp),%rdi
mov    -0x28(%rbp),%rax
shl    $0x3,%rax
mov    %eax,%esi
call   1070 <realloc@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
jne    1309 <find_all_occurances+0x199>
mov    $0x15,%edi
call   1090 <exit@plt>
jmp    130e <find_all_occurances+0x19e>
mov    -0x60(%rbp),%rdi
call   1080 <trim_whitespaces@plt>
mov    %rax,%rdx
mov    -0x40(%rbp),%rax
mov    -0x20(%rbp),%rcx
sub    $0x1,%rcx
mov    %rdx,(%rax,%rcx,8)
mov    -0x60(%rbp),%rdi
call   1030 <free@plt>
mov    -0x50(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x50(%rbp)
jmp    121e <find_all_occurances+0xae>
mov    -0x68(%rbp),%rax
mov    0x4(%rax),%ecx
mov    -0x8(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x8(%rbp)
cmpl   $0x0,-0x44(%rbp)
jne    11ef <find_all_occurances+0x7f>
mov    -0x40(%rbp),%rax
mov    -0x20(%rbp),%rcx
movq   $0x0,(%rax,%rcx,8)
mov    -0x40(%rbp),%rax
mov    -0x30(%rbp),%rcx
mov    %rcx,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<find_all_occurances>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%r12
mov    %rdi,%r13
mov    $0x20,%edi
call   1050 <malloc@plt>
test   %rax,%rax
je     1277 <find_all_occurances+0x107>
mov    %rax,%r15
mov    %rsp,%rcx
xor    %ebp,%ebp
mov    $0x1,%edx
mov    %r12,%rdi
mov    %r13,%rsi
xor    %r8d,%r8d
call   1060 <regexec@plt>
test   %rax,%rax
jne    1284 <find_all_occurances+0x114>
mov    $0x4,%eax
mov    %rax,0x10(%rsp)
xor    %r14d,%r14d
jmp    1218 <find_all_occurances+0xa8>
nopw   0x0(%rax,%rax,1)
mov    %rax,%rcx
add    %r15,%r15
mov    %r15,0x10(%rsp)
mov    %rax,%r15
mov    %rbx,%rdi
call   1080 <trim_whitespaces@plt>
mov    %rax,(%r15,%r14,8)
mov    %rbx,%rdi
call   1030 <free@plt>
movslq 0x4(%rsp),%rax
add    %rax,%r13
mov    $0x1,%edx
mov    %r12,%rdi
mov    %r13,%rsi
mov    %rsp,%rcx
xor    %r8d,%r8d
call   1060 <regexec@plt>
mov    %rbp,%r14
test   %rax,%rax
jne    1284 <find_all_occurances+0x114>
movslq (%rsp),%rax
cmp    $0xffffffffffffffff,%rax
je     1281 <find_all_occurances+0x111>
movslq 0x4(%rsp),%rbp
sub    %rax,%rbp
lea    0x1(%rbp),%edi
call   1050 <malloc@plt>
test   %rax,%rax
je     1277 <find_all_occurances+0x107>
mov    %rax,%rbx
movslq (%rsp),%rsi
add    %r13,%rsi
mov    %rax,%rdi
mov    %rbp,%rdx
call   1040 <memcpy@plt>
movb   $0x0,(%rbx,%rbp,1)
lea    0x1(%r14),%rbp
mov    0x10(%rsp),%rax
cmp    %rax,%rbp
jne    11de <find_all_occurances+0x6e>
mov    %eax,%esi
shl    $0x4,%esi
mov    %r15,%rdi
mov    %rax,%r15
call   1070 <realloc@plt>
test   %rax,%rax
jne    11d0 <find_all_occurances+0x60>
mov    $0x15,%edi
call   1090 <exit@plt>
mov    %r14,%rbp
movq   $0x0,(%r15,%rbp,8)
mov    %r15,%rax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.TYPE_3__ = type { i32, i32 }
define i8** @find_all_occurances(i8* %0, i32* %1) {
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca i64
%6 = alloca i64
%7 = alloca i64
%8 = alloca i8*
%9 = alloca i64
%10 = alloca i8**
%11 = alloca i32
%12 = alloca i64
%13 = alloca i64
%14 = alloca i8*
store i8* %0, i8** %3
store i32* %1, i32** %4
store i64 1, i64* %5
store i64 0, i64* %6
store i64 4, i64* %7
%15 = load i64, i64* %5
%16 = call i8* @llvm.stacksave()
store i8* %16, i8** %8
%17 = alloca %struct.TYPE_3__, i64 %15
store i64 %15, i64* %9
%18 = load i64, i64* %7
%19 = mul i64 8, %18
%20 = trunc i64 %19 to i32
%21 = call i64 @malloc(i32 %20)
%22 = inttoptr i64 %21 to i8**
store i8** %22, i8*** %10
%23 = load i8**, i8*** %10
%24 = icmp eq i8** %23, null
br i1 %24, label %25, label %27
25: ; preds = %2
%26 = call i32 @exit(i32 21)
unreachable
27: ; preds = %2
store i32 1, i32* %11
br label %28
28: ; preds = %119, %27
%29 = load i32*, i32** %4
%30 = load i8*, i8** %3
%31 = load i64, i64* %5
%32 = call i64 @regexec(i32* %29, i8* %30, i64 %31, %struct.TYPE_3__* %17, i32 0)
%33 = icmp ne i64 %32, 0
br i1 %33, label %34, label %35
34: ; preds = %28
br label %122
35: ; preds = %28
store i64 0, i64* %12
br label %36
36: ; preds = %109, %35
%37 = load i64, i64* %12
%38 = load i64, i64* %5
%39 = icmp ult i64 %37, %38
br i1 %39, label %40, label %112
40: ; preds = %36
%41 = load i64, i64* %12
%42 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %17, i64 %41
%43 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %42, i32 0, i32 0
%44 = load i32, i32* %43
%45 = icmp eq i32 %44, -1
br i1 %45, label %46, label %47
46: ; preds = %40
store i32 0, i32* %11
br label %112
47: ; preds = %40
%48 = load i64, i64* %12
%49 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %17, i64 %48
%50 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %49, i32 0, i32 1
%51 = load i32, i32* %50
%52 = load i64, i64* %12
%53 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %17, i64 %52
%54 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %53, i32 0, i32 0
%55 = load i32, i32* %54
%56 = sub nsw i32 %51, %55
%57 = sext i32 %56 to i64
store i64 %57, i64* %13
%58 = load i64, i64* %13
%59 = add i64 %58, 1
%60 = mul i64 1, %59
%61 = trunc i64 %60 to i32
%62 = call i64 @malloc(i32 %61)
%63 = inttoptr i64 %62 to i8*
store i8* %63, i8** %14
%64 = load i8*, i8** %14
%65 = icmp eq i8* %64, null
br i1 %65, label %66, label %68
66: ; preds = %47
%67 = call i32 @exit(i32 21)
unreachable
68: ; preds = %47
%69 = load i8*, i8** %14
%70 = load i8*, i8** %3
%71 = load i64, i64* %12
%72 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %17, i64 %71
%73 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %72, i32 0, i32 0
%74 = load i32, i32* %73
%75 = sext i32 %74 to i64
%76 = getelementptr inbounds i8, i8* %70, i64 %75
%77 = load i64, i64* %13
%78 = call i32 @memcpy(i8* %69, i8* %76, i64 %77)
%79 = load i8*, i8** %14
%80 = load i64, i64* %13
%81 = getelementptr inbounds i8, i8* %79, i64 %80
store i8 0, i8* %81
%82 = load i64, i64* %6
%83 = add i64 %82, 1
store i64 %83, i64* %6
%84 = load i64, i64* %7
%85 = icmp eq i64 %83, %84
br i1 %85, label %86, label %100
86: ; preds = %68
%87 = load i64, i64* %7
%88 = mul i64 %87, 2
store i64 %88, i64* %7
%89 = load i8**, i8*** %10
%90 = load i64, i64* %7
%91 = mul i64 8, %90
%92 = trunc i64 %91 to i32
%93 = call i64 @realloc(i8** %89, i32 %92)
%94 = inttoptr i64 %93 to i8**
store i8** %94, i8*** %10
%95 = load i8**, i8*** %10
%96 = icmp eq i8** %95, null
br i1 %96, label %97, label %99
97: ; preds = %86
%98 = call i32 @exit(i32 21)
unreachable
99: ; preds = %86
br label %100
100: ; preds = %99, %68
%101 = load i8*, i8** %14
%102 = call i8* @trim_whitespaces(i8* %101)
%103 = load i8**, i8*** %10
%104 = load i64, i64* %6
%105 = sub i64 %104, 1
%106 = getelementptr inbounds i8*, i8** %103, i64 %105
store i8* %102, i8** %106
%107 = load i8*, i8** %14
%108 = call i32 @free(i8* %107)
br label %109
109: ; preds = %100
%110 = load i64, i64* %12
%111 = add i64 %110, 1
store i64 %111, i64* %12
br label %36
112: ; preds = %46, %36
%113 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %17, i64 0
%114 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %113, i32 0, i32 1
%115 = load i32, i32* %114
%116 = load i8*, i8** %3
%117 = sext i32 %115 to i64
%118 = getelementptr inbounds i8, i8* %116, i64 %117
store i8* %118, i8** %3
br label %119
119: ; preds = %112
%120 = load i32, i32* %11
%121 = icmp ne i32 %120, 0
br i1 %121, label %28, label %122
122: ; preds = %119, %34
%123 = load i8**, i8*** %10
%124 = load i64, i64* %6
%125 = getelementptr inbounds i8*, i8** %123, i64 %124
store i8* null, i8** %125
%126 = load i8**, i8*** %10
%127 = load i8*, i8** %8
ret i8** %126
}

LLVM IR (-O2):
%struct.TYPE_3__ = type { i32, i32 }
define i8** @find_all_occurances(i8* %0, i32* %1) {
%3 = alloca %struct.TYPE_3__
%4 = tail call i64 @malloc(i32 32)
%5 = icmp eq i64 %4, 0
br i1 %5, label %6, label %8
6: ; preds = %2
%7 = tail call i32 @exit(i32 21)
unreachable
8: ; preds = %2
%9 = inttoptr i64 %4 to i8**
%10 = call i64 @regexec(i32* %1, i8* %0, i64 1, %struct.TYPE_3__* %3, i32 0)
%11 = icmp eq i64 %10, 0
br i1 %11, label %12, label %60
12: ; preds = %8
%13 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %3, i64 0, i32 0
%14 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %3, i64 0, i32 1
br label %15
15: ; preds = %12, %49
%16 = phi i8* [ %0, %12 ], [ %57, %49 ]
%17 = phi i64 [ 0, %12 ], [ %38, %49 ]
%18 = phi i8** [ %9, %12 ], [ %51, %49 ]
%19 = phi i64 [ 4, %12 ], [ %50, %49 ]
%20 = load i32, i32* %13
%21 = icmp eq i32 %20, -1
br i1 %21, label %60, label %22
22: ; preds = %15
%23 = load i32, i32* %14
%24 = sub nsw i32 %23, %20
%25 = sext i32 %24 to i64
%26 = add i32 %24, 1
%27 = call i64 @malloc(i32 %26)
%28 = inttoptr i64 %27 to i8*
%29 = icmp eq i64 %27, 0
br i1 %29, label %30, label %32
30: ; preds = %22
%31 = call i32 @exit(i32 21)
unreachable
32: ; preds = %22
%33 = load i32, i32* %13
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds i8, i8* %16, i64 %34
%36 = call i32 @memcpy(i8* %28, i8* %35, i64 %25)
%37 = getelementptr inbounds i8, i8* %28, i64 %25
store i8 0, i8* %37
%38 = add i64 %17, 1
%39 = icmp eq i64 %38, %19
br i1 %39, label %40, label %49
40: ; preds = %32
%41 = shl i64 %19, 1
%42 = trunc i64 %19 to i32
%43 = shl i32 %42, 4
%44 = call i64 @realloc(i8** %18, i32 %43)
%45 = inttoptr i64 %44 to i8**
%46 = icmp eq i64 %44, 0
br i1 %46, label %47, label %49
47: ; preds = %40
%48 = call i32 @exit(i32 21)
unreachable
49: ; preds = %40, %32
%50 = phi i64 [ %41, %40 ], [ %19, %32 ]
%51 = phi i8** [ %45, %40 ], [ %18, %32 ]
%52 = call i8* @trim_whitespaces(i8* %28)
%53 = getelementptr inbounds i8*, i8** %51, i64 %17
store i8* %52, i8** %53
%54 = call i32 @free(i8* %28)
%55 = load i32, i32* %14
%56 = sext i32 %55 to i64
%57 = getelementptr inbounds i8, i8* %16, i64 %56
%58 = call i64 @regexec(i32* %1, i8* %57, i64 1, %struct.TYPE_3__* %3, i32 0)
%59 = icmp eq i64 %58, 0
br i1 %59, label %15, label %60
60: ; preds = %49, %15, %8
%61 = phi i8** [ %9, %8 ], [ %18, %15 ], [ %51, %49 ]
%62 = phi i64 [ 0, %8 ], [ %17, %15 ], [ %38, %49 ]
%63 = getelementptr inbounds i8*, i8** %61, i64 %62
store i8* null, i8** %63
ret i8** %61
}