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

/* Variables and functions */
int REG_EXTENDED;
int REG_ICASE;
int REG_NEWLINE;
int REG_NOTBOL;
int REG_NOTEOL;
scalar_t__ calloc(int, int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *);
int /*<<< orphan*/ memcpy(char *, char const *, int);
scalar_t__ regcomp(int /*<<< orphan*/ *, char const *, int);
scalar_t__ regexec(int /*<<< orphan*/ *, char const *, int, TYPE_1__ *, int);
int /*<<< orphan*/ stderr;
short int regexp_match_grab(const char *string, const char *regexp,
                            short int casess, char **matches) {
  regex_t preg;
  int numoffsets = 50;
  regmatch_t pmatch[numoffsets];
  char *result = NULL;
  int flags = 0;
  int i = 0;
  flags = REG_NEWLINE | REG_EXTENDED;
  if (!casess)
    flags |= REG_ICASE;
  if (regcomp(&preg, regexp, flags) != 0) {
    fprintf(stderr, "Error: RegExp compilation failed: \"%s\"\n", regexp);
    return (-1);
  }
  if (regexec(&preg, string, numoffsets, pmatch, REG_NOTBOL | REG_NOTEOL) != 0)
    return (1);
  i = 0;
  while (pmatch[++i].rm_so != -1) {
    int len = (pmatch[i].rm_eo - pmatch[i].rm_so);
    result = (char *)calloc(sizeof(char), len);
    memcpy(result, string + pmatch[i].rm_so, len);
    matches[i - 1] = result;
  }
  matches[i - 1] = NULL;
  return (0);
}


x86 assembly (clang, -O0):
<regexp_match_grab>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %dx,%ax
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %ax,-0x1a(%rbp)
mov    %rcx,-0x28(%rbp)
movl   $0x32,-0x30(%rbp)
mov    -0x30(%rbp),%eax
mov    %rsp,%rcx
mov    %rcx,-0x38(%rbp)
lea    0xf(,%rax,8),%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x60(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x40(%rbp)
movq   $0x0,-0x48(%rbp)
movl   $0x0,-0x4c(%rbp)
movl   $0x0,-0x50(%rbp)
mov    0x2e1a(%rip),%rax
mov    (%rax),%eax
mov    0x2df9(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x4c(%rbp)
cmpw   $0x0,-0x1a(%rbp)
jne    11de <regexp_match_grab+0x8e>
mov    0x2e12(%rip),%rax
mov    (%rax),%eax
or     -0x4c(%rbp),%eax
mov    %eax,-0x4c(%rbp)
mov    -0x18(%rbp),%rsi
mov    -0x4c(%rbp),%edx
lea    -0x2c(%rbp),%rdi
call   1030 <regcomp@plt>
cmp    $0x0,%rax
je     1223 <regexp_match_grab+0xd3>
mov    0x2dc1(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rdx
lea    0xdf4(%rip),%rsi
call   1050 <fprintf@plt>
movw   $0xffff,-0x2(%rbp)
movl   $0x1,-0x54(%rbp)
jmp    130d <regexp_match_grab+0x1bd>
mov    -0x60(%rbp),%rcx
mov    -0x10(%rbp),%rsi
mov    -0x30(%rbp),%edx
mov    0x2dab(%rip),%rax
mov    (%rax),%r8d
mov    0x2d99(%rip),%rax
or     (%rax),%r8d
lea    -0x2c(%rbp),%rdi
call   1070 <regexec@plt>
cmp    $0x0,%rax
je     1267 <regexp_match_grab+0x117>
movw   $0x1,-0x2(%rbp)
movl   $0x1,-0x54(%rbp)
jmp    130d <regexp_match_grab+0x1bd>
movl   $0x0,-0x50(%rbp)
mov    -0x60(%rbp),%rax
mov    -0x50(%rbp),%ecx
add    $0x1,%ecx
mov    %ecx,-0x50(%rbp)
movslq %ecx,%rcx
cmpl   $0xffffffff,(%rax,%rcx,8)
je     12eb <regexp_match_grab+0x19b>
mov    -0x60(%rbp),%rcx
movslq -0x50(%rbp),%rax
mov    0x4(%rcx,%rax,8),%eax
movslq -0x50(%rbp),%rdx
sub    (%rcx,%rdx,8),%eax
mov    %eax,-0x58(%rbp)
mov    -0x58(%rbp),%esi
mov    $0x1,%edi
call   1040 <calloc@plt>
mov    %rax,%rcx
mov    -0x60(%rbp),%rax
mov    %rcx,-0x48(%rbp)
mov    -0x48(%rbp),%rdi
mov    -0x10(%rbp),%rsi
movslq -0x50(%rbp),%rcx
movslq (%rax,%rcx,8),%rax
add    %rax,%rsi
mov    -0x58(%rbp),%edx
call   1060 <memcpy@plt>
mov    -0x48(%rbp),%rdx
mov    -0x28(%rbp),%rax
mov    -0x50(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
mov    %rdx,(%rax,%rcx,8)
jmp    126e <regexp_match_grab+0x11e>
mov    -0x28(%rbp),%rax
mov    -0x50(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movq   $0x0,(%rax,%rcx,8)
movw   $0x0,-0x2(%rbp)
movl   $0x1,-0x54(%rbp)
mov    -0x38(%rbp),%rax
mov    %rax,%rsp
movswl -0x2(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<regexp_match_grab>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x1a8,%rsp
mov    %rcx,%r14
mov    %edx,%eax
mov    %rsi,%rbx
mov    %rdi,%r15
mov    %rsp,%rbp
mov    0x2e5a(%rip),%rcx
mov    0x2e3b(%rip),%rdx
mov    (%rdx),%edx
or     (%rcx),%edx
xor    %ecx,%ecx
test   %ax,%ax
jne    1191 <regexp_match_grab+0x41>
mov    0x2e59(%rip),%rax
mov    (%rax),%ecx
or     %ecx,%edx
lea    0x4(%rsp),%rdi
mov    %rbx,%rsi
call   1030 <regcomp@plt>
test   %rax,%rax
je     11c6 <regexp_match_grab+0x76>
mov    0x2e14(%rip),%rax
mov    (%rax),%edi
lea    0xe4b(%rip),%rsi
mov    %rbx,%rdx
call   1050 <fprintf@plt>
mov    $0xffff,%ax
jmp    1261 <regexp_match_grab+0x111>
mov    0x2e13(%rip),%rax
mov    0x2e04(%rip),%rcx
mov    (%rcx),%r8d
or     (%rax),%r8d
lea    0x4(%rsp),%rdi
lea    0x10(%rsp),%rcx
mov    %r15,%rsi
mov    $0x32,%edx
call   1070 <regexec@plt>
mov    %rax,%rcx
mov    $0x1,%ax
test   %rcx,%rcx
jne    1261 <regexp_match_grab+0x111>
mov    0x18(%rsp),%eax
cmp    $0xffffffff,%eax
je     1255 <regexp_match_grab+0x105>
mov    %rbp,0x8(%rsp)
xor    %r13d,%r13d
xchg   %ax,%ax
mov    %r13,%rbp
add    $0x1,%r13
mov    0x1c(%rsp,%rbp,8),%ebx
sub    %eax,%ebx
mov    $0x1,%edi
mov    %ebx,%esi
call   1040 <calloc@plt>
mov    %rax,%r12
movslq 0x18(%rsp,%rbp,8),%rsi
add    %r15,%rsi
mov    %rax,%rdi
mov    %ebx,%edx
call   1060 <memcpy@plt>
mov    %r12,(%r14,%rbp,8)
mov    0x20(%rsp,%rbp,8),%eax
cmp    $0xffffffff,%eax
jne    1210 <regexp_match_grab+0xc0>
mov    %r13d,%eax
mov    0x8(%rsp),%rbp
jmp    1257 <regexp_match_grab+0x107>
xor    %eax,%eax
movq   $0x0,(%r14,%rax,8)
xor    %eax,%eax
mov    %rbp,%rsp
add    $0x1a8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.TYPE_3__ = type { i32, i32 }
@REG_NEWLINE = global i32 0
@REG_EXTENDED = global i32 0
@REG_ICASE = global i32 0
@stderr = global i32 0
@.str = [40 x i8] c"Error: RegExp compilation failed: \22%s\22\0A\00"
@REG_NOTBOL = global i32 0
@REG_NOTEOL = global i32 0
define signext i16 @regexp_match_grab(i8* %0, i8* %1, i16 signext %2, i8** %3) {
%5 = alloca i16
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i16
%9 = alloca i8**
%10 = alloca i32
%11 = alloca i32
%12 = alloca i8*
%13 = alloca i64
%14 = alloca i8*
%15 = alloca i32
%16 = alloca i32
%17 = alloca i32
%18 = alloca i32
store i8* %0, i8** %6
store i8* %1, i8** %7
store i16 %2, i16* %8
store i8** %3, i8*** %9
store i32 50, i32* %11
%19 = load i32, i32* %11
%20 = zext i32 %19 to i64
%21 = call i8* @llvm.stacksave()
store i8* %21, i8** %12
%22 = alloca %struct.TYPE_3__, i64 %20
store i64 %20, i64* %13
store i8* null, i8** %14
store i32 0, i32* %15
store i32 0, i32* %16
%23 = load i32, i32* @REG_NEWLINE
%24 = load i32, i32* @REG_EXTENDED
%25 = or i32 %23, %24
store i32 %25, i32* %15
%26 = load i16, i16* %8
%27 = icmp ne i16 %26, 0
br i1 %27, label %32, label %28
28: ; preds = %4
%29 = load i32, i32* @REG_ICASE
%30 = load i32, i32* %15
%31 = or i32 %30, %29
store i32 %31, i32* %15
br label %32
32: ; preds = %28, %4
%33 = load i8*, i8** %7
%34 = load i32, i32* %15
%35 = call i64 @regcomp(i32* %10, i8* %33, i32 %34)
%36 = icmp ne i64 %35, 0
br i1 %36, label %37, label %41
37: ; preds = %32
%38 = load i32, i32* @stderr
%39 = load i8*, i8** %7
%40 = call i32 @fprintf(i32 %38, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0), i8* %39)
store i16 -1, i16* %5
store i32 1, i32* %17
br label %97
41: ; preds = %32
%42 = load i8*, i8** %6
%43 = load i32, i32* %11
%44 = load i32, i32* @REG_NOTBOL
%45 = load i32, i32* @REG_NOTEOL
%46 = or i32 %44, %45
%47 = call i64 @regexec(i32* %10, i8* %42, i32 %43, %struct.TYPE_3__* %22, i32 %46)
%48 = icmp ne i64 %47, 0
br i1 %48, label %49, label %50
49: ; preds = %41
store i16 1, i16* %5
store i32 1, i32* %17
br label %97
50: ; preds = %41
store i32 0, i32* %16
br label %51
51: ; preds = %59, %50
%52 = load i32, i32* %16
%53 = add nsw i32 %52, 1
store i32 %53, i32* %16
%54 = sext i32 %53 to i64
%55 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %22, i64 %54
%56 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %55, i32 0, i32 0
%57 = load i32, i32* %56
%58 = icmp ne i32 %57, -1
br i1 %58, label %59, label %91
59: ; preds = %51
%60 = load i32, i32* %16
%61 = sext i32 %60 to i64
%62 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %22, i64 %61
%63 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %62, i32 0, i32 1
%64 = load i32, i32* %63
%65 = load i32, i32* %16
%66 = sext i32 %65 to i64
%67 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %22, i64 %66
%68 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %67, i32 0, i32 0
%69 = load i32, i32* %68
%70 = sub nsw i32 %64, %69
store i32 %70, i32* %18
%71 = load i32, i32* %18
%72 = call i64 @calloc(i32 1, i32 %71)
%73 = inttoptr i64 %72 to i8*
store i8* %73, i8** %14
%74 = load i8*, i8** %14
%75 = load i8*, i8** %6
%76 = load i32, i32* %16
%77 = sext i32 %76 to i64
%78 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %22, i64 %77
%79 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %78, i32 0, i32 0
%80 = load i32, i32* %79
%81 = sext i32 %80 to i64
%82 = getelementptr inbounds i8, i8* %75, i64 %81
%83 = load i32, i32* %18
%84 = call i32 @memcpy(i8* %74, i8* %82, i32 %83)
%85 = load i8*, i8** %14
%86 = load i8**, i8*** %9
%87 = load i32, i32* %16
%88 = sub nsw i32 %87, 1
%89 = sext i32 %88 to i64
%90 = getelementptr inbounds i8*, i8** %86, i64 %89
store i8* %85, i8** %90
br label %51
91: ; preds = %51
%92 = load i8**, i8*** %9
%93 = load i32, i32* %16
%94 = sub nsw i32 %93, 1
%95 = sext i32 %94 to i64
%96 = getelementptr inbounds i8*, i8** %92, i64 %95
store i8* null, i8** %96
store i16 0, i16* %5
store i32 1, i32* %17
br label %97
97: ; preds = %91, %49, %37
%98 = load i8*, i8** %12
%99 = load i16, i16* %5
ret i16 %99
}

LLVM IR (-O2):
%struct.TYPE_3__ = type { i32, i32 }
@REG_NEWLINE = global i32 0
@REG_EXTENDED = global i32 0
@REG_ICASE = global i32 0
@stderr = global i32 0
@.str = [40 x i8] c"Error: RegExp compilation failed: \22%s\22\0A\00"
@REG_NOTBOL = global i32 0
@REG_NOTEOL = global i32 0
define signext i16 @regexp_match_grab(i8* %0, i8* %1, i16 signext %2, i8** nocapture writeonly %3) {
%5 = alloca i32
%6 = bitcast i32* %5 to i8*
%7 = tail call i8* @llvm.stacksave()
%8 = alloca [50 x %struct.TYPE_3__]
%9 = load i32, i32* @REG_NEWLINE
%10 = load i32, i32* @REG_EXTENDED
%11 = or i32 %10, %9
%12 = icmp eq i16 %2, 0
%13 = load i32, i32* @REG_ICASE
%14 = select i1 %12, i32 %13, i32 0
%15 = or i32 %11, %14
%16 = call i64 @regcomp(i32* %5, i8* %1, i32 %15)
%17 = icmp eq i64 %16, 0
br i1 %17, label %21, label %18
18: ; preds = %4
%19 = load i32, i32* @stderr
%20 = call i32 @fprintf(i32 %19, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0), i8* %1)
br label %57
21: ; preds = %4
%22 = getelementptr inbounds [50 x %struct.TYPE_3__], [50 x %struct.TYPE_3__]* %8, i64 0, i64 0
%23 = load i32, i32* @REG_NOTBOL
%24 = load i32, i32* @REG_NOTEOL
%25 = or i32 %24, %23
%26 = call i64 @regexec(i32* %5, i8* %0, i32 50, %struct.TYPE_3__* %22, i32 %25)
%27 = icmp eq i64 %26, 0
br i1 %27, label %28, label %57
28: ; preds = %21
%29 = getelementptr inbounds [50 x %struct.TYPE_3__], [50 x %struct.TYPE_3__]* %8, i64 0, i64 1, i32 0
%30 = load i32, i32* %29
%31 = icmp eq i32 %30, -1
br i1 %31, label %54, label %32
32: ; preds = %28, %32
%33 = phi i64 [ %37, %32 ], [ 0, %28 ]
%34 = phi i64 [ %48, %32 ], [ 1, %28 ]
%35 = phi i32 [ %50, %32 ], [ %30, %28 ]
%36 = phi i32* [ %49, %32 ], [ %29, %28 ]
%37 = add nuw nsw i64 %33, 1
%38 = getelementptr inbounds [50 x %struct.TYPE_3__], [50 x %struct.TYPE_3__]* %8, i64 0, i64 %34, i32 1
%39 = load i32, i32* %38
%40 = sub nsw i32 %39, %35
%41 = call i64 @calloc(i32 1, i32 %40)
%42 = inttoptr i64 %41 to i8*
%43 = load i32, i32* %36
%44 = sext i32 %43 to i64
%45 = getelementptr inbounds i8, i8* %0, i64 %44
%46 = call i32 @memcpy(i8* %42, i8* %45, i32 %40)
%47 = getelementptr inbounds i8*, i8** %3, i64 %33
store i8* %42, i8** %47
%48 = add nuw i64 %34, 1
%49 = getelementptr inbounds [50 x %struct.TYPE_3__], [50 x %struct.TYPE_3__]* %8, i64 0, i64 %48, i32 0
%50 = load i32, i32* %49
%51 = icmp eq i32 %50, -1
br i1 %51, label %52, label %32
52: ; preds = %32
%53 = and i64 %34, 4294967295
br label %54
54: ; preds = %52, %28
%55 = phi i64 [ 0, %28 ], [ %53, %52 ]
%56 = getelementptr inbounds i8*, i8** %3, i64 %55
store i8* null, i8** %56
br label %57
57: ; preds = %21, %54, %18
%58 = phi i16 [ -1, %18 ], [ 0, %54 ], [ 1, %21 ]
ret i16 %58
}