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

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


x86 assembly (clang, -O0):
<reg_sub_d>:
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    -0x28(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
lea    -0x2c(%rbp),%rdi
call   1060 <regcomp@plt>
mov    0x2e48(%rip),%rcx
mov    %eax,(%rcx)
cmp    $0x0,%eax
je     11b0 <reg_sub_d+0x50>
movq   $0x0,-0x8(%rbp)
jmp    1277 <reg_sub_d+0x117>
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
add    $0x1,%eax
cltq
mov    %rax,-0x38(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x38(%rbp),%rdx
call   1030 <strncpy@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x88(%rbp)
mov    -0x88(%rbp),%rsi
lea    -0x80(%rbp),%rcx
lea    -0x2c(%rbp),%rdi
mov    $0x2,%edx
xor    %r8d,%r8d
call   1070 <regexec@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    1213 <reg_sub_d+0xb3>
jmp    1266 <reg_sub_d+0x106>
mov    -0x80(%rbp),%rax
cmp    -0x78(%rbp),%rax
jne    1237 <reg_sub_d+0xd7>
lea    -0x2c(%rbp),%rdi
call   1040 <regfree@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1277 <reg_sub_d+0x117>
mov    -0x88(%rbp),%rdi
add    -0x80(%rbp),%rdi
mov    -0x88(%rbp),%rsi
add    -0x78(%rbp),%rsi
mov    -0x38(%rbp),%rdx
sub    -0x78(%rbp),%rdx
call   1080 <memmove@plt>
mov    %rax,-0x88(%rbp)
jmp    11e2 <reg_sub_d+0x82>
lea    -0x2c(%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_sub_d>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x50,%rsp
mov    %rcx,%r13
mov    %rdi,%rbx
movb   $0x0,(%rcx)
lea    0xc(%rsp),%rdi
call   1060 <regcomp@plt>
mov    0x2e59(%rip),%rcx
mov    %eax,(%rcx)
test   %eax,%eax
je     1195 <reg_sub_d+0x35>
xor    %r13d,%r13d
jmp    122a <reg_sub_d+0xca>
mov    %rbx,%rdi
call   1050 <strlen@plt>
add    $0x1,%eax
movslq %eax,%r14
mov    %r13,%rdi
mov    %rbx,%rsi
mov    %r14,%rdx
call   1030 <strncpy@plt>
lea    0xc(%rsp),%rdi
lea    0x10(%rsp),%rcx
mov    %r13,%rsi
mov    $0x2,%edx
xor    %r8d,%r8d
call   1070 <regexec@plt>
test   %eax,%eax
jne    1220 <reg_sub_d+0xc0>
lea    0xc(%rsp),%r15
lea    0x10(%rsp),%r12
mov    %r13,%rsi
nopl   0x0(%rax)
mov    0x10(%rsp),%rbx
mov    0x18(%rsp),%rax
cmp    %rax,%rbx
je     1220 <reg_sub_d+0xc0>
add    %rsi,%rbx
add    %rax,%rsi
mov    %r14,%rdx
sub    %rax,%rdx
mov    %rbx,%rdi
call   1080 <memmove@plt>
mov    %r15,%rdi
mov    %rbx,%rsi
mov    $0x2,%edx
mov    %r12,%rcx
xor    %r8d,%r8d
call   1070 <regexec@plt>
mov    %rbx,%rsi
test   %eax,%eax
je     11e0 <reg_sub_d+0x80>
lea    0xc(%rsp),%rdi
call   1040 <regfree@plt>
mov    %r13,%rax
add    $0x50,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

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

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