#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_9__ TYPE_2__;
typedef struct TYPE_8__ TYPE_1__;

/* Type definitions */
struct TYPE_8__ {
  scalar_t__ rm_so;
  scalar_t__ rm_eo;
};
typedef TYPE_1__ regmatch_t;
struct TYPE_9__ {
  int re_nsub;
  int /*<<< orphan*/ member_0;
};
typedef TYPE_2__ regex_t;

/* Variables and functions */
int /*<<< orphan*/ REG_EXTENDED;
char *calloc(int, scalar_t__);
int /*<<< orphan*/ free(TYPE_1__ *);
TYPE_1__ *malloc(int);
int /*<<< orphan*/ memcpy(char *, char const *, scalar_t__);
scalar_t__ regcomp(TYPE_2__ *, char const *, int /*<<< orphan*/);
scalar_t__ regexec(TYPE_2__ *, char const *, int, TYPE_1__ *,
                   int /*<<< orphan*/);
int regexMatches(const char *regex, const char *str, char **matches) {
  regex_t r = {0};
  if (regcomp(&r, regex, REG_EXTENDED) != 0) {
    return 0;
  }
  int nmatch = r.re_nsub + 1;
  regmatch_t *rmatches = malloc(nmatch * sizeof(regmatch_t));
  if (regexec(&r, str, nmatch, rmatches, 0) != 0) {
    free(rmatches);
    return 0;
  }
  for (int i = 0; i < nmatch; i++) {
    regmatch_t m = rmatches[i];
    if (m.rm_so < 0) {
      break;
    }
    char *r = calloc(1, m.rm_eo - m.rm_so + 1);
    memcpy(r, str + m.rm_so, m.rm_eo - m.rm_so);
    matches[i] = r;
  }
  free(rmatches);
  return nmatch;
}


x86 assembly (clang, -O0):
<regexMatches>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
lea    -0x28(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1040 <memset@plt>
mov    -0x10(%rbp),%rsi
mov    0x2e41(%rip),%rax
mov    (%rax),%edx
lea    -0x28(%rbp),%rdi
call   1050 <regcomp@plt>
cmp    $0x0,%rax
je     11c0 <regexMatches+0x50>
movl   $0x0,-0x4(%rbp)
jmp    12b3 <regexMatches+0x143>
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
movslq -0x2c(%rbp),%rax
shl    $0x4,%rax
mov    %eax,%edi
call   1080 <malloc@plt>
mov    %rax,-0x38(%rbp)
mov    -0x18(%rbp),%rsi
mov    -0x2c(%rbp),%edx
mov    -0x38(%rbp),%rcx
lea    -0x28(%rbp),%rdi
xor    %r8d,%r8d
call   1090 <regexec@plt>
cmp    $0x0,%rax
je     1212 <regexMatches+0xa2>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
movl   $0x0,-0x4(%rbp)
jmp    12b3 <regexMatches+0x143>
movl   $0x0,-0x3c(%rbp)
mov    -0x3c(%rbp),%eax
cmp    -0x2c(%rbp),%eax
jge    12a4 <regexMatches+0x134>
mov    -0x38(%rbp),%rax
movslq -0x3c(%rbp),%rcx
shl    $0x4,%rcx
add    %rcx,%rax
mov    (%rax),%rcx
mov    %rcx,-0x50(%rbp)
mov    0x8(%rax),%rax
mov    %rax,-0x48(%rbp)
cmpq   $0x0,-0x50(%rbp)
jge    1253 <regexMatches+0xe3>
jmp    12a4 <regexMatches+0x134>
mov    -0x48(%rbp),%rsi
sub    -0x50(%rbp),%rsi
add    $0x1,%rsi
mov    $0x1,%edi
call   1060 <calloc@plt>
mov    %rax,-0x58(%rbp)
mov    -0x58(%rbp),%rdi
mov    -0x18(%rbp),%rsi
add    -0x50(%rbp),%rsi
mov    -0x48(%rbp),%rdx
sub    -0x50(%rbp),%rdx
call   1070 <memcpy@plt>
mov    -0x58(%rbp),%rdx
mov    -0x20(%rbp),%rax
movslq -0x3c(%rbp),%rcx
mov    %rdx,(%rax,%rcx,8)
mov    -0x3c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x3c(%rbp)
jmp    1219 <regexMatches+0xa9>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<regexMatches>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rdx,%r12
mov    %rsi,%r15
mov    %rdi,%rsi
movq   $0x0,0x10(%rsp)
mov    0x2e59(%rip),%rax
mov    (%rax),%edx
lea    0x10(%rsp),%rdi
call   1040 <regcomp@plt>
xor    %ebp,%ebp
test   %rax,%rax
jne    124b <regexMatches+0xeb>
mov    0x10(%rsp),%r14d
lea    0x1(%r14),%ebx
mov    %ebx,%edi
shl    $0x4,%edi
call   1070 <malloc@plt>
mov    %rax,%r13
lea    0x10(%rsp),%rdi
xor    %ebp,%ebp
mov    %r15,%rsi
mov    %ebx,%edx
mov    %rax,%rcx
xor    %r8d,%r8d
call   1080 <regexec@plt>
test   %rax,%rax
jne    1243 <regexMatches+0xe3>
test   %r14d,%r14d
js     1241 <regexMatches+0xe1>
mov    %r15,0x20(%rsp)
mov    %ebx,0xc(%rsp)
mov    %ebx,%eax
shl    $0x3,%rax
mov    %rax,0x18(%rsp)
xor    %r15d,%r15d
nopl   0x0(%rax)
mov    0x0(%r13,%r15,2),%rbp
test   %rbp,%rbp
js     123b <regexMatches+0xdb>
mov    0x8(%r13,%r15,2),%rbx
sub    %rbp,%rbx
lea    0x1(%rbx),%rsi
mov    $0x1,%edi
call   1050 <calloc@plt>
mov    %r12,%r14
mov    %rax,%r12
add    0x20(%rsp),%rbp
mov    %rax,%rdi
mov    %rbp,%rsi
mov    %rbx,%rdx
call   1060 <memcpy@plt>
mov    %r12,(%r14,%r15,1)
mov    %r14,%r12
add    $0x8,%r15
cmp    %r15,0x18(%rsp)
jne    11f0 <regexMatches+0x90>
mov    0xc(%rsp),%ebp
jmp    1243 <regexMatches+0xe3>
mov    %ebx,%ebp
mov    %r13,%rdi
call   1030 <free@plt>
mov    %ebp,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.TYPE_9__ = type { i32, i32 }
%struct.TYPE_8__ = type { i64, i64 }
@REG_EXTENDED = global i32 0
define i32 @regexMatches(i8* %0, i8* %1, i8** nocapture writeonly %2) {
%4 = alloca i64
%5 = bitcast i64* %4 to %struct.TYPE_9__*
%6 = bitcast i64* %4 to i8*
store i64 0, i64* %4
%7 = load i32, i32* @REG_EXTENDED
%8 = call i64 @regcomp(%struct.TYPE_9__* %5, i8* %0, i32 %7)
%9 = icmp eq i64 %8, 0
br i1 %9, label %10, label %41
10: ; preds = %3
%11 = bitcast i64* %4 to i32*
%12 = load i32, i32* %11
%13 = add i32 %12, 1
%14 = shl i32 %13, 4
%15 = call %struct.TYPE_8__* @malloc(i32 %14)
%16 = call i64 @regexec(%struct.TYPE_9__* %5, i8* %1, i32 %13, %struct.TYPE_8__* %15, i32 0)
%17 = icmp eq i64 %16, 0
br i1 %17, label %18, label %38
18: ; preds = %10
%19 = icmp slt i32 %12, 0
br i1 %19, label %38, label %20
20: ; preds = %18
%21 = zext i32 %13 to i64
br label %22
22: ; preds = %20, %27
%23 = phi i64 [ 0, %20 ], [ %36, %27 ]
%24 = getelementptr inbounds %struct.TYPE_8__, %struct.TYPE_8__* %15, i64 %23, i32 0
%25 = load i64, i64* %24
%26 = icmp slt i64 %25, 0
br i1 %26, label %38, label %27
27: ; preds = %22
%28 = getelementptr inbounds %struct.TYPE_8__, %struct.TYPE_8__* %15, i64 %23, i32 1
%29 = load i64, i64* %28
%30 = sub nsw i64 %29, %25
%31 = add nsw i64 %30, 1
%32 = call i8* @calloc(i32 1, i64 %31)
%33 = getelementptr inbounds i8, i8* %1, i64 %25
%34 = call i32 @memcpy(i8* %32, i8* %33, i64 %30)
%35 = getelementptr inbounds i8*, i8** %2, i64 %23
store i8* %32, i8** %35
%36 = add nuw nsw i64 %23, 1
%37 = icmp eq i64 %36, %21
br i1 %37, label %38, label %22
38: ; preds = %22, %27, %18, %10
%39 = phi i32 [ 0, %10 ], [ %13, %18 ], [ %13, %27 ], [ %13, %22 ]
%40 = call i32 @free(%struct.TYPE_8__* %15)
br label %41
41: ; preds = %38, %3
%42 = phi i32 [ 0, %3 ], [ %39, %38 ]
ret i32 %42
}