#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__ {
  size_t rm_eo;
  int rm_so;
};
typedef TYPE_1__ regmatch_t;
typedef int /*<<< orphan*/ regex_t;

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ REG_EXTENDED;
int /*<<< orphan*/ assert(int /*<<< orphan*/);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *);
char *malloc(int);
scalar_t__ regcomp(int /*<<< orphan*/ *, char const *, int /*<<< orphan*/);
scalar_t__ regexec(int /*<<< orphan*/ *, char const *, size_t, TYPE_1__ *,
                   int /*<<< orphan*/);
int /*<<< orphan*/ regfree(int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strcpy(char *, char const *);
int strlen(char const *);
char *capture(const char *source, const char *regex) {
  size_t maxGroups = 2;
  regex_t regexCompiled;
  regmatch_t groupArray[maxGroups];
  char *res;
  if (regcomp(&regexCompiled, regex, REG_EXTENDED)) {
    fprintf(stderr, "Could not compile regular expression: '%s'.\n", regex);
    exit(EXIT_FAILURE);
  }
  if (regexec(&regexCompiled, source, maxGroups, groupArray, 0) == 0) {
    regfree(&regexCompiled);
    char sourceCopy[strlen(source) + 1];
    strcpy(sourceCopy, source);
    sourceCopy[groupArray[1].rm_eo] = '\0';
    char *match = sourceCopy + groupArray[1].rm_so;
    res = malloc(strlen(match) + 1);
    assert(res != NULL);
    strcpy(res, match);
    return res;
  } else {
    res = malloc(1);
    res[0] = '\0';
    return res;
  }
}


x86 assembly (clang, -O0):
<capture>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movq   $0x2,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    %rsp,%rcx
mov    %rcx,-0x30(%rbp)
mov    %rax,%rdx
shl    $0x4,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x68(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x38(%rbp)
mov    -0x18(%rbp),%rsi
mov    0x2dfa(%rip),%rax
mov    (%rax),%edx
lea    -0x24(%rbp),%rdi
call   1060 <regcomp@plt>
cmp    $0x0,%rax
je     1212 <capture+0x82>
mov    0x2dee(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rdx
lea    0xe01(%rip),%rsi
call   1080 <fprintf@plt>
mov    0x2dcd(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
mov    -0x68(%rbp),%rcx
mov    -0x10(%rbp),%rsi
mov    -0x20(%rbp),%rdx
lea    -0x24(%rbp),%rdi
xor    %r8d,%r8d
call   10a0 <regexec@plt>
cmp    $0x0,%rax
jne    12e1 <capture+0x151>
lea    -0x24(%rbp),%rdi
call   1040 <regfree@plt>
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
add    $0x1,%eax
mov    %rsp,%rcx
mov    %rcx,-0x48(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdi
sub    %rcx,%rdi
mov    %rdi,-0x70(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x50(%rbp)
mov    -0x10(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x68(%rbp),%rcx
mov    -0x70(%rbp),%rax
mov    0x10(%rcx),%rdx
movb   $0x0,(%rax,%rdx,1)
movslq 0x18(%rcx),%rcx
add    %rcx,%rax
mov    %rax,-0x58(%rbp)
mov    -0x58(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,%edi
add    $0x1,%edi
call   1090 <malloc@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1070 <assert@plt>
mov    -0x40(%rbp),%rdi
mov    -0x58(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x40(%rbp),%rax
mov    %rax,-0x8(%rbp)
movl   $0x1,-0x5c(%rbp)
mov    -0x48(%rbp),%rax
mov    %rax,%rsp
jmp    1305 <capture+0x175>
mov    $0x1,%edi
call   1090 <malloc@plt>
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x40(%rbp),%rax
mov    %rax,-0x8(%rbp)
movl   $0x1,-0x5c(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,%rsp
mov    -0x8(%rbp),%rax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<capture>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x30,%rsp
mov    %rsi,%rbx
mov    %rdi,%r14
mov    %rsp,%r15
mov    0x2e21(%rip),%rax
mov    (%rax),%edx
lea    -0x24(%rbp),%rdi
call   1060 <regcomp@plt>
test   %rax,%rax
jne    1276 <capture+0xe6>
lea    -0x24(%rbp),%rdi
lea    -0x50(%rbp),%rcx
mov    $0x2,%edx
mov    %r14,%rsi
xor    %r8d,%r8d
call   10a0 <regexec@plt>
test   %rax,%rax
je     11f2 <capture+0x62>
mov    $0x1,%edi
call   1090 <malloc@plt>
mov    %rax,%rbx
movb   $0x0,(%rax)
jmp    1263 <capture+0xd3>
lea    -0x24(%rbp),%rdi
call   1040 <regfree@plt>
mov    %r14,%rdi
call   1050 <strlen@plt>
mov    %rsp,%r12
add    $0x1,%eax
mov    %rsp,%rbx
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
mov    %rbx,%rdi
mov    %r14,%rsi
call   1030 <strcpy@plt>
mov    -0x40(%rbp),%rax
movb   $0x0,(%rax,%rbx,1)
movslq -0x38(%rbp),%r14
add    %rbx,%r14
mov    %r14,%rdi
call   1050 <strlen@plt>
lea    0x1(%rax),%edi
call   1090 <malloc@plt>
mov    %rax,%rbx
xor    %edi,%edi
test   %rax,%rax
setne  %dil
call   1070 <assert@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
call   1030 <strcpy@plt>
mov    %r12,%rsp
mov    %r15,%rsp
mov    %rbx,%rax
lea    -0x20(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2d63(%rip),%rax
mov    (%rax),%edi
lea    0xd7a(%rip),%rsi
mov    %rbx,%rdx
call   1080 <fprintf@plt>
mov    0x2d43(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
%struct.TYPE_3__ = type { i64, i32 }
@REG_EXTENDED = global i32 0
@stderr = global i32 0
@.str = [45 x i8] c"Could not compile regular expression: '%s'.\0A\00"
@EXIT_FAILURE = global i32 0
define i8* @capture(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i64
%10 = alloca i8*
%11 = alloca i8*
%12 = alloca i64
%13 = alloca i8*
%14 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
store i64 2, i64* %6
%15 = load i64, i64* %6
%16 = call i8* @llvm.stacksave()
store i8* %16, i8** %8
%17 = alloca %struct.TYPE_3__, i64 %15
store i64 %15, i64* %9
%18 = load i8*, i8** %5
%19 = load i32, i32* @REG_EXTENDED
%20 = call i64 @regcomp(i32* %7, i8* %18, i32 %19)
%21 = icmp ne i64 %20, 0
br i1 %21, label %22, label %28
22: ; preds = %2
%23 = load i32, i32* @stderr
%24 = load i8*, i8** %5
%25 = call i32 @fprintf(i32 %23, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str, i64 0, i64 0), i8* %24)
%26 = load i32, i32* @EXIT_FAILURE
%27 = call i32 @exit(i32 %26)
unreachable
28: ; preds = %2
%29 = load i8*, i8** %4
%30 = load i64, i64* %6
%31 = call i64 @regexec(i32* %7, i8* %29, i64 %30, %struct.TYPE_3__* %17, i32 0)
%32 = icmp eq i64 %31, 0
br i1 %32, label %33, label %65
33: ; preds = %28
%34 = call i32 @regfree(i32* %7)
%35 = load i8*, i8** %4
%36 = call i32 @strlen(i8* %35)
%37 = add nsw i32 %36, 1
%38 = zext i32 %37 to i64
%39 = call i8* @llvm.stacksave()
store i8* %39, i8** %11
%40 = alloca i8, i64 %38
store i64 %38, i64* %12
%41 = load i8*, i8** %4
%42 = call i32 @strcpy(i8* %40, i8* %41)
%43 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %17, i64 1
%44 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %43, i32 0, i32 0
%45 = load i64, i64* %44
%46 = getelementptr inbounds i8, i8* %40, i64 %45
store i8 0, i8* %46
%47 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %17, i64 1
%48 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %47, i32 0, i32 1
%49 = load i32, i32* %48
%50 = sext i32 %49 to i64
%51 = getelementptr inbounds i8, i8* %40, i64 %50
store i8* %51, i8** %13
%52 = load i8*, i8** %13
%53 = call i32 @strlen(i8* %52)
%54 = add nsw i32 %53, 1
%55 = call i8* @malloc(i32 %54)
store i8* %55, i8** %10
%56 = load i8*, i8** %10
%57 = icmp ne i8* %56, null
%58 = zext i1 %57 to i32
%59 = call i32 @assert(i32 %58)
%60 = load i8*, i8** %10
%61 = load i8*, i8** %13
%62 = call i32 @strcpy(i8* %60, i8* %61)
%63 = load i8*, i8** %10
store i8* %63, i8** %3
store i32 1, i32* %14
%64 = load i8*, i8** %11
br label %70
65: ; preds = %28
%66 = call i8* @malloc(i32 1)
store i8* %66, i8** %10
%67 = load i8*, i8** %10
%68 = getelementptr inbounds i8, i8* %67, i64 0
store i8 0, i8* %68
%69 = load i8*, i8** %10
store i8* %69, i8** %3
store i32 1, i32* %14
br label %70
70: ; preds = %65, %33
%71 = load i8*, i8** %8
%72 = load i8*, i8** %3
ret i8* %72
}

LLVM IR (-O2):
%struct.TYPE_3__ = type { i64, i32 }
@REG_EXTENDED = global i32 0
@stderr = global i32 0
@.str = [45 x i8] c"Could not compile regular expression: '%s'.\0A\00"
@EXIT_FAILURE = global i32 0
define i8* @capture(i8* %0, i8* %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
%5 = tail call i8* @llvm.stacksave()
%6 = alloca [2 x %struct.TYPE_3__]
%7 = load i32, i32* @REG_EXTENDED
%8 = call i64 @regcomp(i32* %3, i8* %1, i32 %7)
%9 = icmp eq i64 %8, 0
br i1 %9, label %15, label %10
10: ; preds = %2
%11 = load i32, i32* @stderr
%12 = call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str, i64 0, i64 0), i8* %1)
%13 = load i32, i32* @EXIT_FAILURE
%14 = call i32 @exit(i32 %13)
unreachable
15: ; preds = %2
%16 = getelementptr inbounds [2 x %struct.TYPE_3__], [2 x %struct.TYPE_3__]* %6, i64 0, i64 0
%17 = call i64 @regexec(i32* %3, i8* %0, i64 2, %struct.TYPE_3__* %16, i32 0)
%18 = icmp eq i64 %17, 0
br i1 %18, label %19, label %41
19: ; preds = %15
%20 = call i32 @regfree(i32* %3)
%21 = call i32 @strlen(i8* %0)
%22 = add nsw i32 %21, 1
%23 = zext i32 %22 to i64
%24 = call i8* @llvm.stacksave()
%25 = alloca i8, i64 %23
%26 = call i32 @strcpy(i8* %25, i8* %0)
%27 = getelementptr inbounds [2 x %struct.TYPE_3__], [2 x %struct.TYPE_3__]* %6, i64 0, i64 1, i32 0
%28 = load i64, i64* %27
%29 = getelementptr inbounds i8, i8* %25, i64 %28
store i8 0, i8* %29
%30 = getelementptr inbounds [2 x %struct.TYPE_3__], [2 x %struct.TYPE_3__]* %6, i64 0, i64 1, i32 1
%31 = load i32, i32* %30
%32 = sext i32 %31 to i64
%33 = getelementptr inbounds i8, i8* %25, i64 %32
%34 = call i32 @strlen(i8* %33)
%35 = add nsw i32 %34, 1
%36 = call i8* @malloc(i32 %35)
%37 = icmp ne i8* %36, null
%38 = zext i1 %37 to i32
%39 = call i32 @assert(i32 %38)
%40 = call i32 @strcpy(i8* %36, i8* %33)
br label %43
41: ; preds = %15
%42 = call dereferenceable_or_null(1) i8* @malloc(i32 1)
store i8 0, i8* %42
br label %43
43: ; preds = %41, %19
%44 = phi i8* [ %36, %19 ], [ %42, %41 ]
ret i8* %44
}