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

/* Variables and functions */
int /*<<< orphan*/ errno;
int /*<<< orphan*/ regcomp(int /*<<< orphan*/ *, char *, int);
int /*<<< orphan*/ regexec(int /*<<< orphan*/ *, char *, int, TYPE_1__ *,
                           int /*<<< orphan*/);
int /*<<< orphan*/ regfree(int /*<<< orphan*/ *);
int /*<<< orphan*/ strncpy(char *, char *, size_t);
char *reg_getsubm(char *rs_p, char *pattern, int cflags, char *output,
                  size_t max_out) {
  regex_t preg;
  output[0] = 0x0;
  if ((errno = regcomp(&preg, pattern, cflags))) {
    return NULL;
  }
  regmatch_t rm[4];
  if (!regexec(&preg, rs_p, 2, rm, 0)) {
    size_t l_r = rm[0].rm_eo - rm[0].rm_so;
    if (l_r < 1) {
      regfree(&preg);
      return output;
    }
    if (l_r > max_out) {
      l_r = max_out;
    }
    strncpy(output, &rs_p[rm[0].rm_so], l_r);
    output[l_r] = 0x0;
  }
  regfree(&preg);
  return output;
}


x86 assembly (clang, -O0):
<reg_getsubm>:
push   %rbp
mov    %rsp,%rbp
sub    $0x90,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    %rcx,-0x28(%rbp)
mov    %r8,-0x30(%rbp)
mov    -0x28(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
lea    -0x34(%rbp),%rdi
call   1050 <regcomp@plt>
mov    0x2e64(%rip),%rcx
mov    %eax,(%rcx)
cmp    $0x0,%eax
je     1194 <reg_getsubm+0x54>
movq   $0x0,-0x8(%rbp)
jmp    123d <reg_getsubm+0xfd>
mov    -0x10(%rbp),%rsi
lea    -0x80(%rbp),%rcx
lea    -0x34(%rbp),%rdi
mov    $0x2,%edx
xor    %r8d,%r8d
call   1060 <regexec@plt>
cmp    $0x0,%eax
jne    122c <reg_getsubm+0xec>
mov    -0x80(%rbp),%rax
sub    -0x78(%rbp),%rax
mov    %rax,-0x88(%rbp)
cmpq   $0x1,-0x88(%rbp)
01
jae    11e9 <reg_getsubm+0xa9>
lea    -0x34(%rbp),%rdi
call   1040 <regfree@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    123d <reg_getsubm+0xfd>
mov    -0x88(%rbp),%rax
cmp    -0x30(%rbp),%rax
jbe    1205 <reg_getsubm+0xc5>
mov    -0x30(%rbp),%rax
mov    %rax,-0x88(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rsi
add    -0x78(%rbp),%rsi
mov    -0x88(%rbp),%rdx
call   1030 <strncpy@plt>
mov    -0x28(%rbp),%rax
mov    -0x88(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
lea    -0x34(%rbp),%rdi
call   1040 <regfree@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x90,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<reg_getsubm>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x58,%rsp
mov    %r8,%r15
mov    %rcx,%rbx
mov    %rdi,%r14
movb   $0x0,(%rcx)
lea    0xc(%rsp),%rdi
call   1050 <regcomp@plt>
mov    0x2e78(%rip),%rcx
mov    %eax,(%rcx)
test   %eax,%eax
je     1172 <reg_getsubm+0x32>
xor    %ebx,%ebx
jmp    11c6 <reg_getsubm+0x86>
lea    0xc(%rsp),%rdi
lea    0x10(%rsp),%rcx
mov    %r14,%rsi
mov    $0x2,%edx
xor    %r8d,%r8d
call   1060 <regexec@plt>
test   %eax,%eax
jne    11bc <reg_getsubm+0x7c>
mov    0x10(%rsp),%r12
mov    0x18(%rsp),%rax
sub    %rax,%r12
je     11bc <reg_getsubm+0x7c>
cmp    %r15,%r12
cmova  %r15,%r12
add    %rax,%r14
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %r12,%rdx
call   1030 <strncpy@plt>
movb   $0x0,(%rbx,%r12,1)
lea    0xc(%rsp),%rdi
call   1040 <regfree@plt>
mov    %rbx,%rax
add    $0x58,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.TYPE_3__ = type { i64, i64 }
@errno = global i32 0
define i8* @reg_getsubm(i8* %0, i8* %1, i32 %2, i8* %3, i64 %4) {
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i8*
%11 = alloca i64
%12 = alloca i32
%13 = alloca [4 x %struct.TYPE_3__]
%14 = alloca i64
store i8* %0, i8** %7
store i8* %1, i8** %8
store i32 %2, i32* %9
store i8* %3, i8** %10
store i64 %4, i64* %11
%15 = load i8*, i8** %10
%16 = getelementptr inbounds i8, i8* %15, i64 0
store i8 0, i8* %16
%17 = load i8*, i8** %8
%18 = load i32, i32* %9
%19 = call i32 @regcomp(i32* %12, i8* %17, i32 %18)
store i32 %19, i32* @errno
%20 = icmp ne i32 %19, 0
br i1 %20, label %21, label %22
21: ; preds = %5
store i8* null, i8** %6
br label %61
22: ; preds = %5
%23 = load i8*, i8** %7
%24 = getelementptr inbounds [4 x %struct.TYPE_3__], [4 x %struct.TYPE_3__]* %13, i64 0, i64 0
%25 = call i32 @regexec(i32* %12, i8* %23, i32 2, %struct.TYPE_3__* %24, i32 0)
%26 = icmp ne i32 %25, 0
br i1 %26, label %58, label %27
27: ; preds = %22
%28 = getelementptr inbounds [4 x %struct.TYPE_3__], [4 x %struct.TYPE_3__]* %13, i64 0, i64 0
%29 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %28, i32 0, i32 0
%30 = load i64, i64* %29
%31 = getelementptr inbounds [4 x %struct.TYPE_3__], [4 x %struct.TYPE_3__]* %13, i64 0, i64 0
%32 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %31, i32 0, i32 1
%33 = load i64, i64* %32
%34 = sub i64 %30, %33
store i64 %34, i64* %14
%35 = load i64, i64* %14
%36 = icmp ult i64 %35, 1
br i1 %36, label %37, label %40
37: ; preds = %27
%38 = call i32 @regfree(i32* %12)
%39 = load i8*, i8** %10
store i8* %39, i8** %6
br label %61
40: ; preds = %27
%41 = load i64, i64* %14
%42 = load i64, i64* %11
%43 = icmp ugt i64 %41, %42
br i1 %43, label %44, label %46
44: ; preds = %40
%45 = load i64, i64* %11
store i64 %45, i64* %14
br label %46
46: ; preds = %44, %40
%47 = load i8*, i8** %10
%48 = load i8*, i8** %7
%49 = getelementptr inbounds [4 x %struct.TYPE_3__], [4 x %struct.TYPE_3__]* %13, i64 0, i64 0
%50 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %49, i32 0, i32 1
%51 = load i64, i64* %50
%52 = getelementptr inbounds i8, i8* %48, i64 %51
%53 = load i64, i64* %14
%54 = call i32 @strncpy(i8* %47, i8* %52, i64 %53)
%55 = load i8*, i8** %10
%56 = load i64, i64* %14
%57 = getelementptr inbounds i8, i8* %55, i64 %56
store i8 0, i8* %57
br label %58
58: ; preds = %46, %22
%59 = call i32 @regfree(i32* %12)
%60 = load i8*, i8** %10
store i8* %60, i8** %6
br label %61
61: ; preds = %58, %37, %21
%62 = load i8*, i8** %6
ret i8* %62
}

LLVM IR (-O2):
%struct.TYPE_3__ = type { i64, i64 }
@errno = global i32 0
define i8* @reg_getsubm(i8* %0, i8* %1, i32 %2, i8* %3, i64 %4) {
%6 = alloca i32
%7 = alloca [4 x %struct.TYPE_3__]
%8 = bitcast i32* %6 to i8*
store i8 0, i8* %3
%9 = call i32 @regcomp(i32* %6, i8* %1, i32 %2)
store i32 %9, i32* @errno
%10 = icmp eq i32 %9, 0
br i1 %10, label %11, label %31
11: ; preds = %5
%12 = bitcast [4 x %struct.TYPE_3__]* %7 to i8*
%13 = getelementptr inbounds [4 x %struct.TYPE_3__], [4 x %struct.TYPE_3__]* %7, i64 0, i64 0
%14 = call i32 @regexec(i32* %6, i8* %0, i32 2, %struct.TYPE_3__* %13, i32 0)
%15 = icmp eq i32 %14, 0
br i1 %15, label %16, label %29
16: ; preds = %11
%17 = getelementptr inbounds [4 x %struct.TYPE_3__], [4 x %struct.TYPE_3__]* %7, i64 0, i64 0, i32 0
%18 = load i64, i64* %17
%19 = getelementptr inbounds [4 x %struct.TYPE_3__], [4 x %struct.TYPE_3__]* %7, i64 0, i64 0, i32 1
%20 = load i64, i64* %19
%21 = sub i64 %18, %20
%22 = icmp eq i64 %21, 0
br i1 %22, label %29, label %23
23: ; preds = %16
%24 = icmp ugt i64 %21, %4
%25 = select i1 %24, i64 %4, i64 %21
%26 = getelementptr inbounds i8, i8* %0, i64 %20
%27 = call i32 @strncpy(i8* %3, i8* %26, i64 %25)
%28 = getelementptr inbounds i8, i8* %3, i64 %25
store i8 0, i8* %28
br label %29
29: ; preds = %11, %23, %16
%30 = call i32 @regfree(i32* %6)
br label %31
31: ; preds = %5, %29
%32 = phi i8* [ %3, %29 ], [ null, %5 ]
ret i8* %32
}