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

/* Variables and functions */
char *d_s(char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ printf(char *, ...);
scalar_t__ rematch(char *, char *, size_t, TYPE_1__ *);
int /*<<< orphan*/ stderr;
char *sub(char *, char *, int /*<<< orphan*/, scalar_t__);
int main() {
  char *re, *src, *sbst, *trgt;
  size_t nmatch = 1;
  regmatch_t pmatch[nmatch];
  printf("Enter source string: ");
  src = d_s(src);
  printf("Enter regex string: ");
  re = d_s(re);
  printf("Enter regex subststitution: ");
  sbst = d_s(sbst);
  if (rematch(src, re, nmatch, pmatch)) {
    printf("Source string = [%s]\n", src);
    trgt = sub(src, sbst, pmatch[0].rm_so, pmatch[0].rm_eo - 1);
    printf("Target string = [%s]\n", trgt);
  } else {
    fprintf(stderr, "Failed to match.\n");
  }
  free(src);
  free(re);
  free(sbst);
  free(trgt);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
movq   $0x1,-0x30(%rbp)
mov    -0x30(%rbp),%rax
mov    %rsp,%rcx
mov    %rcx,-0x38(%rbp)
mov    %rax,%rdx
shl    $0x4,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x48(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x40(%rbp)
lea    0xe5f(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1060 <d_s@plt>
mov    %rax,-0x18(%rbp)
lea    0xe5a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x10(%rbp),%rdi
call   1060 <d_s@plt>
mov    %rax,-0x10(%rbp)
lea    0xe54(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x20(%rbp),%rdi
call   1060 <d_s@plt>
mov    -0x48(%rbp),%rcx
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x30(%rbp),%rdx
call   1070 <rematch@plt>
cmp    $0x0,%rax
je     1252 <main+0xf2>
mov    -0x18(%rbp),%rsi
lea    0xe33(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x48(%rbp),%rax
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    0x8(%rax),%edx
mov    (%rax),%rcx
sub    $0x1,%rcx
call   1080 <sub@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rsi
lea    0xe18(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1267 <main+0x107>
mov    0x2d87(%rip),%rax
mov    (%rax),%edi
lea    0xe12(%rip),%rsi
call   1050 <fprintf@plt>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x38(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x18,%rsp
lea    0xe8e(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
call   1060 <d_s@plt>
mov    %rax,%r12
lea    0xe8e(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
call   1060 <d_s@plt>
mov    %rax,%r14
lea    0xe8d(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
call   1060 <d_s@plt>
mov    %rax,%r15
mov    %rsp,%rcx
mov    $0x1,%edx
mov    %r12,%rdi
mov    %r14,%rsi
call   1070 <rematch@plt>
test   %rax,%rax
je     122f <main+0xcf>
lea    0xe7c(%rip),%rdi
mov    %r12,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
mov    0x8(%rsp),%edx
mov    (%rsp),%rcx
add    $0xffffffffffffffff,%rcx
mov    %r12,%rdi
mov    %r15,%rsi
call   1080 <sub@plt>
mov    %rax,%rbx
lea    0xe67(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %r12,%rdi
call   1030 <free@plt>
mov    %r14,%rdi
call   1030 <free@plt>
mov    %r15,%rdi
call   1030 <free@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
mov    0x2daa(%rip),%rax
mov    (%rax),%edi
lea    0xe35(%rip),%rsi
call   1050 <fprintf@plt>

LLVM IR (-O0):
%struct.TYPE_3__ = type { i64, i32 }
@.str = [22 x i8] c"Enter source string: \00"
@.str.1 = [21 x i8] c"Enter regex string: \00"
@.str.2 = [29 x i8] c"Enter regex subststitution: \00"
@.str.3 = [22 x i8] c"Source string = [%s]\0A\00"
@.str.4 = [22 x i8] c"Target string = [%s]\0A\00"
@stderr = global i32 0
@.str.5 = [18 x i8] c"Failed to match.\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i8*
%8 = alloca i64
store i32 0, i32* %1
store i64 1, i64* %6
%9 = load i64, i64* %6
%10 = call i8* @llvm.stacksave()
store i8* %10, i8** %7
%11 = alloca %struct.TYPE_3__, i64 %9
store i64 %9, i64* %8
%12 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
%13 = load i8*, i8** %3
%14 = call i8* @d_s(i8* %13)
store i8* %14, i8** %3
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
%16 = load i8*, i8** %2
%17 = call i8* @d_s(i8* %16)
store i8* %17, i8** %2
%18 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0))
%19 = load i8*, i8** %4
%20 = call i8* @d_s(i8* %19)
store i8* %20, i8** %4
%21 = load i8*, i8** %3
%22 = load i8*, i8** %2
%23 = load i64, i64* %6
%24 = call i64 @rematch(i8* %21, i8* %22, i64 %23, %struct.TYPE_3__* %11)
%25 = icmp ne i64 %24, 0
br i1 %25, label %26, label %41
26: ; preds = %0
%27 = load i8*, i8** %3
%28 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0), i8* %27)
%29 = load i8*, i8** %3
%30 = load i8*, i8** %4
%31 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %11, i64 0
%32 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %31, i32 0, i32 1
%33 = load i32, i32* %32
%34 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %11, i64 0
%35 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %34, i32 0, i32 0
%36 = load i64, i64* %35
%37 = sub nsw i64 %36, 1
%38 = call i8* @sub(i8* %29, i8* %30, i32 %33, i64 %37)
store i8* %38, i8** %5
%39 = load i8*, i8** %5
%40 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0), i8* %39)
br label %44
41: ; preds = %0
%42 = load i32, i32* @stderr
%43 = call i32 @fprintf(i32 %42, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.5, i64 0, i64 0))
br label %44
44: ; preds = %41, %26
%45 = load i8*, i8** %3
%46 = call i32 @free(i8* %45)
%47 = load i8*, i8** %2
%48 = call i32 @free(i8* %47)
%49 = load i8*, i8** %4
%50 = call i32 @free(i8* %49)
%51 = load i8*, i8** %5
%52 = call i32 @free(i8* %51)
store i32 0, i32* %1
%53 = load i8*, i8** %7
%54 = load i32, i32* %1
ret i32 %54
}

LLVM IR (-O2):
%struct.TYPE_3__ = type { i64, i32 }
@.str = [22 x i8] c"Enter source string: \00"
@.str.1 = [21 x i8] c"Enter regex string: \00"
@.str.2 = [29 x i8] c"Enter regex subststitution: \00"
@.str.3 = [22 x i8] c"Source string = [%s]\0A\00"
@.str.4 = [22 x i8] c"Target string = [%s]\0A\00"
@stderr = global i32 0
@.str.5 = [18 x i8] c"Failed to match.\0A\00"
define i32 @main() {
%1 = alloca %struct.TYPE_3__
%2 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
%3 = tail call i8* @d_s(i8* undef)
%4 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
%5 = tail call i8* @d_s(i8* undef)
%6 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0))
%7 = tail call i8* @d_s(i8* undef)
%8 = call i64 @rematch(i8* %3, i8* %5, i64 1, %struct.TYPE_3__* %1)
%9 = icmp eq i64 %8, 0
br i1 %9, label %23, label %10
10: ; preds = %0
%11 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0), i8* %3)
%12 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %1, i64 0, i32 1
%13 = load i32, i32* %12
%14 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %1, i64 0, i32 0
%15 = load i64, i64* %14
%16 = add nsw i64 %15, -1
%17 = call i8* @sub(i8* %3, i8* %7, i32 %13, i64 %16)
%18 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0), i8* %17)
%19 = call i32 @free(i8* %3)
%20 = call i32 @free(i8* %5)
%21 = call i32 @free(i8* %7)
%22 = call i32 @free(i8* %17)
ret i32 0
23: ; preds = %0
%24 = load i32, i32* @stderr
%25 = call i32 @fprintf(i32 %24, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.5, i64 0, i64 0))
unreachable
}