#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 */
typedef int /*<<< orphan*/ regmatch_t;
typedef int /*<<< orphan*/ regex_t;

/* Variables and functions */
int /*<<< orphan*/ ERROR_MALLOC();
void *calloc(int, int);
int compileRegex(int /*<<< orphan*/ *, char const *);
int /*<<< orphan*/ destroyRegex(int /*<<< orphan*/ *);
int executeRegex(int /*<<< orphan*/ *, char const *, char const *,
                 int /*<<< orphan*/ *, int);
int /*<<< orphan*/ free(int /*<<< orphan*/ *);
char *getGroup(char const *, int /*<<< orphan*/ *, int);
scalar_t__ hasNotMatched(int);
bool applyRegularExpression(const char *restrict string,
                            const char *restrict regex, int groupSize,
                            char ***groups) {
  int reti;
  regex_t regexSupport;
  regmatch_t *groupsSupport;
  char size;
  bool retVal;
  reti = compileRegex(&regexSupport, regex);
  if (reti != 0) {
    retVal = false;
    return false;
  }
  groupsSupport = calloc(sizeof(regmatch_t), (groupSize + 1));
  if (groupsSupport == NULL) {
    ERROR_MALLOC();
  }
  reti =
      executeRegex(&regexSupport, regex, string, groupsSupport, groupSize + 1);
  if (hasNotMatched(reti)) {
    retVal = false;
    goto exit;
  }
  retVal = true;
  if (groupSize == 0) {
    goto exit;
  }
  char **arrayOfString = NULL;
  arrayOfString = calloc(sizeof(char *), groupSize + 1);
  if (arrayOfString == NULL) {
    ERROR_MALLOC();
  }
  for (int i = 0; i < (groupSize + 1); i++) {
    arrayOfString[i] = getGroup(string, groupsSupport, i);
  }
  *groups = arrayOfString;
exit:
  free(groupsSupport);
  destroyRegex(&regexSupport);
  return retVal;
}


x86 assembly (clang, -O0):
<applyRegularExpression>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    %rcx,-0x28(%rbp)
mov    -0x18(%rbp),%rsi
lea    -0x30(%rbp),%rdi
call   1070 <compileRegex@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
je     11c4 <applyRegularExpression+0x44>
movl   $0x0,-0x40(%rbp)
movl   $0x0,-0x4(%rbp)
jmp    12d4 <applyRegularExpression+0x154>
mov    -0x1c(%rbp),%esi
add    $0x1,%esi
mov    $0x4,%edi
call   1050 <calloc@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    11ea <applyRegularExpression+0x6a>
mov    $0x0,%al
call   1080 <ERROR_MALLOC@plt>
mov    -0x18(%rbp),%rsi
mov    -0x10(%rbp),%rdx
mov    -0x38(%rbp),%rcx
mov    -0x1c(%rbp),%r8d
add    $0x1,%r8d
lea    -0x30(%rbp),%rdi
call   10a0 <executeRegex@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%edi
call   1060 <hasNotMatched@plt>
cmp    $0x0,%rax
je     1228 <applyRegularExpression+0xa8>
movl   $0x0,-0x40(%rbp)
jmp    12bc <applyRegularExpression+0x13c>
movl   $0x1,-0x40(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jne    123e <applyRegularExpression+0xbe>
jmp    12bc <applyRegularExpression+0x13c>
movq   $0x0,-0x48(%rbp)
mov    -0x1c(%rbp),%esi
add    $0x1,%esi
mov    $0x8,%edi
call   1050 <calloc@plt>
mov    %rax,-0x48(%rbp)
cmpq   $0x0,-0x48(%rbp)
jne    126c <applyRegularExpression+0xec>
mov    $0x0,%al
call   1080 <ERROR_MALLOC@plt>
movl   $0x0,-0x4c(%rbp)
mov    -0x4c(%rbp),%eax
mov    -0x1c(%rbp),%ecx
add    $0x1,%ecx
cmp    %ecx,%eax
jge    12b1 <applyRegularExpression+0x131>
mov    -0x10(%rbp),%rdi
mov    -0x38(%rbp),%rsi
mov    -0x4c(%rbp),%edx
call   1040 <getGroup@plt>
mov    %rax,%rdx
mov    -0x48(%rbp),%rax
movslq -0x4c(%rbp),%rcx
mov    %rdx,(%rax,%rcx,8)
mov    -0x4c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x4c(%rbp)
jmp    1273 <applyRegularExpression+0xf3>
mov    -0x48(%rbp),%rcx
mov    -0x28(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
lea    -0x30(%rbp),%rdi
call   1090 <destroyRegex@plt>
mov    -0x40(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<applyRegularExpression>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rcx,%r14
mov    %edx,%ebx
mov    %rsi,%rbp
mov    %rdi,%r15
lea    0x4(%rsp),%rdi
call   1070 <compileRegex@plt>
xor    %r13d,%r13d
test   %eax,%eax
jne    1264 <applyRegularExpression+0xe4>
mov    %rbx,0x8(%rsp)
add    $0x1,%ebx
mov    $0x4,%edi
mov    %ebx,%esi
call   1050 <calloc@plt>
mov    %rax,%r12
test   %rax,%rax
jne    11d1 <applyRegularExpression+0x51>
xor    %eax,%eax
call   1080 <ERROR_MALLOC@plt>
lea    0x4(%rsp),%rdi
mov    %rbp,%rsi
mov    %r15,%rdx
mov    %r12,%rcx
mov    %ebx,%r8d
call   10a0 <executeRegex@plt>
mov    %eax,%edi
call   1060 <hasNotMatched@plt>
xor    %r13d,%r13d
test   %rax,%rax
jne    1252 <applyRegularExpression+0xd2>
mov    $0x1,%r13d
cmpl   $0x0,0x8(%rsp)
je     1252 <applyRegularExpression+0xd2>
mov    %r14,0x10(%rsp)
mov    $0x8,%edi
mov    %ebx,%esi
call   1050 <calloc@plt>
mov    %rax,%r14
test   %rax,%rax
jne    1223 <applyRegularExpression+0xa3>
xor    %eax,%eax
call   1080 <ERROR_MALLOC@plt>
cmpl   $0x0,0x8(%rsp)
js     124a <applyRegularExpression+0xca>
mov    %ebx,%ebx
xor    %ebp,%ebp
xchg   %ax,%ax
mov    %r15,%rdi
mov    %r12,%rsi
mov    %ebp,%edx
call   1040 <getGroup@plt>
mov    %rax,(%r14,%rbp,8)
add    $0x1,%rbp
cmp    %rbp,%rbx
jne    1230 <applyRegularExpression+0xb0>
mov    0x10(%rsp),%rax
mov    %r14,(%rax)
mov    %r12,%rdi
call   1030 <free@plt>
lea    0x4(%rsp),%rdi
call   1090 <destroyRegex@plt>
mov    %r13d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @applyRegularExpression(i8* noalias %0, i8* noalias %1, i32 %2, i8*** %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i8***
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32*
%13 = alloca i8
%14 = alloca i32
%15 = alloca i8**
%16 = alloca i32
store i8* %0, i8** %6
store i8* %1, i8** %7
store i32 %2, i32* %8
store i8*** %3, i8**** %9
%17 = load i8*, i8** %7
%18 = call i32 @compileRegex(i32* %11, i8* %17)
store i32 %18, i32* %10
%19 = load i32, i32* %10
%20 = icmp ne i32 %19, 0
br i1 %20, label %21, label %22
21: ; preds = %4
store i32 0, i32* %14
store i32 0, i32* %5
br label %81
22: ; preds = %4
%23 = load i32, i32* %8
%24 = add nsw i32 %23, 1
%25 = call i8* @calloc(i32 4, i32 %24)
%26 = bitcast i8* %25 to i32*
store i32* %26, i32** %12
%27 = load i32*, i32** %12
%28 = icmp eq i32* %27, null
br i1 %28, label %29, label %31
29: ; preds = %22
%30 = call i32 (...) @ERROR_MALLOC()
br label %31
31: ; preds = %29, %22
%32 = load i8*, i8** %7
%33 = load i8*, i8** %6
%34 = load i32*, i32** %12
%35 = load i32, i32* %8
%36 = add nsw i32 %35, 1
%37 = call i32 @executeRegex(i32* %11, i8* %32, i8* %33, i32* %34, i32 %36)
store i32 %37, i32* %10
%38 = load i32, i32* %10
%39 = call i64 @hasNotMatched(i32 %38)
%40 = icmp ne i64 %39, 0
br i1 %40, label %41, label %42
41: ; preds = %31
store i32 0, i32* %14
br label %76
42: ; preds = %31
store i32 1, i32* %14
%43 = load i32, i32* %8
%44 = icmp eq i32 %43, 0
br i1 %44, label %45, label %46
45: ; preds = %42
br label %76
46: ; preds = %42
store i8** null, i8*** %15
%47 = load i32, i32* %8
%48 = add nsw i32 %47, 1
%49 = call i8* @calloc(i32 8, i32 %48)
%50 = bitcast i8* %49 to i8**
store i8** %50, i8*** %15
%51 = load i8**, i8*** %15
%52 = icmp eq i8** %51, null
br i1 %52, label %53, label %55
53: ; preds = %46
%54 = call i32 (...) @ERROR_MALLOC()
br label %55
55: ; preds = %53, %46
store i32 0, i32* %16
br label %56
56: ; preds = %70, %55
%57 = load i32, i32* %16
%58 = load i32, i32* %8
%59 = add nsw i32 %58, 1
%60 = icmp slt i32 %57, %59
br i1 %60, label %61, label %73
61: ; preds = %56
%62 = load i8*, i8** %6
%63 = load i32*, i32** %12
%64 = load i32, i32* %16
%65 = call i8* @getGroup(i8* %62, i32* %63, i32 %64)
%66 = load i8**, i8*** %15
%67 = load i32, i32* %16
%68 = sext i32 %67 to i64
%69 = getelementptr inbounds i8*, i8** %66, i64 %68
store i8* %65, i8** %69
br label %70
70: ; preds = %61
%71 = load i32, i32* %16
%72 = add nsw i32 %71, 1
store i32 %72, i32* %16
br label %56
73: ; preds = %56
%74 = load i8**, i8*** %15
%75 = load i8***, i8**** %9
store i8** %74, i8*** %75
br label %76
76: ; preds = %73, %45, %41
%77 = load i32*, i32** %12
%78 = call i32 @free(i32* %77)
%79 = call i32 @destroyRegex(i32* %11)
%80 = load i32, i32* %14
store i32 %80, i32* %5
br label %81
81: ; preds = %76, %21
%82 = load i32, i32* %5
ret i32 %82
}

LLVM IR (-O2):
define i32 @applyRegularExpression(i8* noalias %0, i8* noalias %1, i32 %2, i8*** nocapture writeonly %3) {
%5 = alloca i32
%6 = bitcast i32* %5 to i8*
%7 = call i32 @compileRegex(i32* %5, i8* %1)
%8 = icmp eq i32 %7, 0
br i1 %8, label %9, label %45
9: ; preds = %4
%10 = add i32 %2, 1
%11 = call i8* @calloc(i32 4, i32 %10)
%12 = bitcast i8* %11 to i32*
%13 = icmp eq i8* %11, null
br i1 %13, label %14, label %16
14: ; preds = %9
%15 = call i32 (...) @ERROR_MALLOC()
br label %16
16: ; preds = %14, %9
%17 = call i32 @executeRegex(i32* %5, i8* %1, i8* %0, i32* %12, i32 %10)
%18 = call i64 @hasNotMatched(i32 %17)
%19 = icmp eq i64 %18, 0
br i1 %19, label %20, label %41
20: ; preds = %16
%21 = icmp eq i32 %2, 0
br i1 %21, label %41, label %22
22: ; preds = %20
%23 = call i8* @calloc(i32 8, i32 %10)
%24 = bitcast i8* %23 to i8**
%25 = icmp eq i8* %23, null
br i1 %25, label %26, label %28
26: ; preds = %22
%27 = call i32 (...) @ERROR_MALLOC()
br label %28
28: ; preds = %26, %22
%29 = icmp slt i32 %2, 0
br i1 %29, label %32, label %30
30: ; preds = %28
%31 = zext i32 %10 to i64
br label %34
32: ; preds = %34, %28
%33 = bitcast i8*** %3 to i8**
store i8* %23, i8** %33
br label %41
34: ; preds = %30, %34
%35 = phi i64 [ 0, %30 ], [ %39, %34 ]
%36 = trunc i64 %35 to i32
%37 = call i8* @getGroup(i8* %0, i32* %12, i32 %36)
%38 = getelementptr inbounds i8*, i8** %24, i64 %35
store i8* %37, i8** %38
%39 = add nuw nsw i64 %35, 1
%40 = icmp eq i64 %39, %31
br i1 %40, label %32, label %34
41: ; preds = %16, %20, %32
%42 = phi i32 [ 1, %20 ], [ 1, %32 ], [ 0, %16 ]
%43 = call i32 @free(i32* %12)
%44 = call i32 @destroyRegex(i32* %5)
br label %45
45: ; preds = %4, %41
%46 = phi i32 [ %42, %41 ], [ 0, %4 ]
ret i32 %46
}