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

/* Variables and functions */
int REG_ICASE;
int REG_NEWLINE;
int REG_NOTBOL;
int REG_NOTEOL;
scalar_t__ calloc(int, int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ memcpy(char *, char const *, int);
scalar_t__ regcomp(int /*<<< orphan*/ *, char *, int);
scalar_t__ regexec(int /*<<< orphan*/ *, char const *, int, TYPE_1__ *, int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ str_replace(char *, char *, char *, int);
int /*<<< orphan*/ strncpy(char *, char const *, int);
short int shellpattern_match_grab(const char *string, const char *pattern,
                                  short int casess, char **match) {
  regex_t preg;
  int numoffsets = 50;
  regmatch_t pmatch[numoffsets];
  char regexp[1024];
  char *result = NULL;
  int flags = 0;
  strncpy(regexp, pattern, 1023);
  str_replace(regexp, ".", "\\.", 1023);
  str_replace(regexp, "*", ".*", 1023);
  str_replace(regexp, "(", "\\(", 1023);
  str_replace(regexp, ")", "\\)", 1023);
  str_replace(regexp, "?", ".", 1023);
  str_replace(regexp, "[!", "[^", 1023);
  flags = REG_NEWLINE;
  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);
  result =
      (char *)calloc(sizeof(char), (pmatch[1].rm_eo - pmatch[1].rm_so) + 2);
  memcpy(result, string + pmatch[1].rm_so, (pmatch[1].rm_eo - pmatch[1].rm_so));
  *match = result;
  return (0);
}


x86 assembly (clang, -O0):
<shellpattern_match_grab>:
push   %rbp
mov    %rsp,%rbp
sub    $0x460,%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,-0x458(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x40(%rbp)
movq   $0x0,-0x448(%rbp)
movl   $0x0,-0x44c(%rbp)
lea    -0x440(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    $0x3ff,%edx
call   1030 <strncpy@plt>
lea    -0x440(%rbp),%rdi
lea    0xe0a(%rip),%rsi
lea    0xe02(%rip),%rdx
mov    $0x3ff,%ecx
call   1070 <str_replace@plt>
lea    -0x440(%rbp),%rdi
lea    0xdee(%rip),%rsi
lea    0xde6(%rip),%rdx
mov    $0x3ff,%ecx
call   1070 <str_replace@plt>
lea    -0x440(%rbp),%rdi
lea    0xdd2(%rip),%rsi
lea    0xdca(%rip),%rdx
mov    $0x3ff,%ecx
call   1070 <str_replace@plt>
lea    -0x440(%rbp),%rdi
lea    0xdb6(%rip),%rsi
lea    0xdae(%rip),%rdx
mov    $0x3ff,%ecx
call   1070 <str_replace@plt>
lea    -0x440(%rbp),%rdi
lea    0xd99(%rip),%rsi
lea    0xd87(%rip),%rdx
mov    $0x3ff,%ecx
call   1070 <str_replace@plt>
lea    -0x440(%rbp),%rdi
lea    0xd7c(%rip),%rsi
lea    0xd78(%rip),%rdx
mov    $0x3ff,%ecx
call   1070 <str_replace@plt>
mov    0x2d26(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x44c(%rbp)
cmpw   $0x0,-0x1a(%rbp)
jne    12d2 <shellpattern_match_grab+0x162>
mov    0x2d24(%rip),%rax
mov    (%rax),%eax
or     -0x44c(%rbp),%eax
mov    %eax,-0x44c(%rbp)
lea    -0x440(%rbp),%rsi
mov    -0x44c(%rbp),%edx
lea    -0x2c(%rbp),%rdi
call   1040 <regcomp@plt>
cmp    $0x0,%rax
je     1323 <shellpattern_match_grab+0x1b3>
mov    0x2cc7(%rip),%rax
mov    (%rax),%edi
lea    -0x440(%rbp),%rdx
lea    0xd0b(%rip),%rsi
call   1060 <fprintf@plt>
movw   $0xffff,-0x2(%rbp)
movl   $0x1,-0x450(%rbp)
jmp    13d3 <shellpattern_match_grab+0x263>
mov    -0x458(%rbp),%rcx
mov    -0x10(%rbp),%rsi
mov    -0x30(%rbp),%edx
mov    0x2ca8(%rip),%rax
mov    (%rax),%r8d
mov    0x2c96(%rip),%rax
or     (%rax),%r8d
lea    -0x2c(%rbp),%rdi
call   1090 <regexec@plt>
cmp    $0x0,%rax
je     136d <shellpattern_match_grab+0x1fd>
movw   $0x1,-0x2(%rbp)
movl   $0x1,-0x450(%rbp)
jmp    13d3 <shellpattern_match_grab+0x263>
mov    -0x458(%rbp),%rax
mov    0x8(%rax),%esi
sub    0xc(%rax),%esi
add    $0x2,%esi
mov    $0x1,%edi
call   1050 <calloc@plt>
mov    %rax,%rcx
mov    -0x458(%rbp),%rax
mov    %rcx,-0x448(%rbp)
mov    -0x448(%rbp),%rdi
mov    -0x10(%rbp),%rsi
movslq 0xc(%rax),%rcx
add    %rcx,%rsi
mov    0x8(%rax),%edx
sub    0xc(%rax),%edx
call   1080 <memcpy@plt>
mov    -0x448(%rbp),%rcx
mov    -0x28(%rbp),%rax
mov    %rcx,(%rax)
movw   $0x0,-0x2(%rbp)
movl   $0x1,-0x450(%rbp)
mov    -0x38(%rbp),%rax
mov    %rax,%rsp
movswl -0x2(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<shellpattern_match_grab>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x5a8,%rsp
mov    %rcx,%r14
mov    %edx,%r12d
mov    %rdi,%r15
mov    %rsp,%r13
lea    0x1a0(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x3ff,%edx
call   1030 <strncpy@plt>
lea    0xe58(%rip),%rbp
lea    0xe50(%rip),%rdx
mov    %rbx,%rdi
mov    %rbp,%rsi
mov    $0x3ff,%ecx
call   1070 <str_replace@plt>
lea    0xe3d(%rip),%rsi
lea    0xe35(%rip),%rdx
mov    %rbx,%rdi
mov    $0x3ff,%ecx
call   1070 <str_replace@plt>
lea    0xe25(%rip),%rsi
lea    0xe1d(%rip),%rdx
mov    %rbx,%rdi
mov    $0x3ff,%ecx
call   1070 <str_replace@plt>
lea    0xe0d(%rip),%rsi
lea    0xe05(%rip),%rdx
mov    %rbx,%rdi
mov    $0x3ff,%ecx
call   1070 <str_replace@plt>
lea    0xdf4(%rip),%rsi
mov    %rbx,%rdi
mov    %rbp,%rdx
mov    $0x3ff,%ecx
call   1070 <str_replace@plt>
lea    0xddf(%rip),%rsi
lea    0xddb(%rip),%rdx
mov    %rbx,%rdi
mov    $0x3ff,%ecx
call   1070 <str_replace@plt>
mov    0x2d86(%rip),%rax
xor    %edx,%edx
test   %r12w,%r12w
jne    125b <shellpattern_match_grab+0xeb>
mov    0x2d8f(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
lea    0xc(%rsp),%rdi
mov    %rbx,%rsi
call   1040 <regcomp@plt>
test   %rax,%rax
je     1292 <shellpattern_match_grab+0x122>
mov    0x2d4a(%rip),%rax
mov    (%rax),%edi
lea    0xd95(%rip),%rsi
lea    0x1a0(%rsp),%rdx
call   1060 <fprintf@plt>
mov    $0xffff,%ax
jmp    12ff <shellpattern_match_grab+0x18f>
mov    0x2d47(%rip),%rax
mov    0x2d38(%rip),%rcx
mov    (%rcx),%r8d
or     (%rax),%r8d
lea    0xc(%rsp),%rdi
lea    0x10(%rsp),%rcx
mov    %r15,%rsi
mov    $0x32,%edx
call   1090 <regexec@plt>
mov    %rax,%rcx
mov    $0x1,%ax
test   %rcx,%rcx
jne    12ff <shellpattern_match_grab+0x18f>
mov    0x18(%rsp),%esi
sub    0x1c(%rsp),%esi
add    $0x2,%esi
mov    $0x1,%edi
call   1050 <calloc@plt>
mov    %rax,%rbx
movslq 0x1c(%rsp),%rax
add    %rax,%r15
mov    0x18(%rsp),%edx
sub    %eax,%edx
mov    %rbx,%rdi
mov    %r15,%rsi
call   1080 <memcpy@plt>
mov    %rbx,(%r14)
xor    %eax,%eax
mov    %r13,%rsp
add    $0x5a8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.TYPE_3__ = type { i32, i32 }
@.str = [2 x i8] c".\00"
@.str.1 = [3 x i8] c"\\.\00"
@.str.2 = [2 x i8] c"*\00"
@.str.3 = [3 x i8] c".*\00"
@.str.4 = [2 x i8] c"(\00"
@.str.5 = [3 x i8] c"\\(\00"
@.str.6 = [2 x i8] c")\00"
@.str.7 = [3 x i8] c"\\)\00"
@.str.8 = [2 x i8] c"?\00"
@.str.9 = [3 x i8] c"[!\00"
@.str.10 = [3 x i8] c"[^\00"
@REG_NEWLINE = global i32 0
@REG_ICASE = global i32 0
@stderr = global i32 0
@.str.11 = [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 @shellpattern_match_grab(i8* %0, i8* %1, i16 signext %2, i8** nocapture writeonly %3) {
%5 = alloca i32
%6 = alloca [1024 x i8]
%7 = bitcast i32* %5 to i8*
%8 = tail call i8* @llvm.stacksave()
%9 = alloca [50 x %struct.TYPE_3__]
%10 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%11 = call i32 @strncpy(i8* %10, i8* %1, i32 1023)
%12 = call i32 @str_replace(i8* %10, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32 1023)
%13 = call i32 @str_replace(i8* %10, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32 1023)
%14 = call i32 @str_replace(i8* %10, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i32 1023)
%15 = call i32 @str_replace(i8* %10, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.6, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.7, i64 0, i64 0), i32 1023)
%16 = call i32 @str_replace(i8* %10, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.8, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i32 1023)
%17 = call i32 @str_replace(i8* %10, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.9, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.10, i64 0, i64 0), i32 1023)
%18 = load i32, i32* @REG_NEWLINE
%19 = icmp eq i16 %2, 0
%20 = load i32, i32* @REG_ICASE
%21 = select i1 %19, i32 %20, i32 0
%22 = or i32 %21, %18
%23 = call i64 @regcomp(i32* %5, i8* %10, i32 %22)
%24 = icmp eq i64 %23, 0
br i1 %24, label %28, label %25
25: ; preds = %4
%26 = load i32, i32* @stderr
%27 = call i32 @fprintf(i32 %26, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.11, i64 0, i64 0), i8* %10)
br label %50
28: ; preds = %4
%29 = getelementptr inbounds [50 x %struct.TYPE_3__], [50 x %struct.TYPE_3__]* %9, i64 0, i64 0
%30 = load i32, i32* @REG_NOTBOL
%31 = load i32, i32* @REG_NOTEOL
%32 = or i32 %31, %30
%33 = call i64 @regexec(i32* %5, i8* %0, i32 50, %struct.TYPE_3__* %29, i32 %32)
%34 = icmp eq i64 %33, 0
br i1 %34, label %35, label %50
35: ; preds = %28
%36 = getelementptr inbounds [50 x %struct.TYPE_3__], [50 x %struct.TYPE_3__]* %9, i64 0, i64 1, i32 0
%37 = load i32, i32* %36
%38 = getelementptr inbounds [50 x %struct.TYPE_3__], [50 x %struct.TYPE_3__]* %9, i64 0, i64 1, i32 1
%39 = load i32, i32* %38
%40 = add i32 %37, 2
%41 = sub i32 %40, %39
%42 = call i64 @calloc(i32 1, i32 %41)
%43 = inttoptr i64 %42 to i8*
%44 = load i32, i32* %38
%45 = sext i32 %44 to i64
%46 = getelementptr inbounds i8, i8* %0, i64 %45
%47 = load i32, i32* %36
%48 = sub nsw i32 %47, %44
%49 = call i32 @memcpy(i8* %43, i8* %46, i32 %48)
store i8* %43, i8** %3
br label %50
50: ; preds = %28, %35, %25
%51 = phi i16 [ -1, %25 ], [ 0, %35 ], [ 1, %28 ]
ret i16 %51
}