#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 /*<<< orphan*/ 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*/ printf(char *, ...);
scalar_t__ rematch(char *, char *, size_t, TYPE_1__ *);
int /*<<< orphan*/ stderr;
char *substitute(char *, char *, int /*<<< orphan*/, int /*<<< orphan*/);
int main() {
  char *re, *src, *sub, *trgt;
  size_t nmatch = 1;
  regmatch_t pmatch[nmatch];
  printf("Enter source string: ");
  src = d_s(src);
  printf("Enter regex: ");
  re = d_s(re);
  printf("Enter regex substitution: ");
  sub = d_s(sub);
  if (rematch(src, re, nmatch, pmatch)) {
    printf("Old source string = [%s]\n", src);
    trgt = substitute(src, sub, pmatch[0].rm_so, pmatch[0].rm_eo);
    printf("New source string = [%s]\n", trgt);
  } else {
    fprintf(stderr, "Failed to match.\n");
  }
  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)
lea    0xf(,%rax,8),%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x48(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x40(%rbp)
lea    0xe6a(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1050 <d_s@plt>
mov    %rax,-0x18(%rbp)
lea    0xe65(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x10(%rbp),%rdi
call   1050 <d_s@plt>
mov    %rax,-0x10(%rbp)
lea    0xe58(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x20(%rbp),%rdi
call   1050 <d_s@plt>
mov    -0x48(%rbp),%rcx
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x30(%rbp),%rdx
call   1060 <rematch@plt>
cmp    $0x0,%rax
je     1242 <main+0xf2>
mov    -0x18(%rbp),%rsi
lea    0xe35(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x48(%rbp),%rax
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    0x4(%rax),%edx
mov    (%rax),%ecx
call   1070 <substitute@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rsi
lea    0xe23(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    1257 <main+0x107>
mov    0x2d97(%rip),%rax
mov    (%rax),%edi
lea    0xe21(%rip),%rsi
call   1040 <fprintf@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   %rbx
sub    $0x10,%rsp
lea    0xea0(%rip),%rdi
xor    %eax,%eax
call   1030 <printf@plt>
call   1050 <d_s@plt>
mov    %rax,%r15
lea    0xea0(%rip),%rdi
xor    %eax,%eax
call   1030 <printf@plt>
call   1050 <d_s@plt>
mov    %rax,%rbx
lea    0xe98(%rip),%rdi
xor    %eax,%eax
call   1030 <printf@plt>
call   1050 <d_s@plt>
mov    %rax,%r14
mov    %rsp,%rcx
mov    $0x1,%edx
mov    %r15,%rdi
mov    %rbx,%rsi
call   1060 <rematch@plt>
test   %rax,%rax
je     11e9 <main+0x99>
lea    0xe85(%rip),%rdi
mov    %r15,%rsi
xor    %eax,%eax
call   1030 <printf@plt>
mov    (%rsp),%ecx
mov    0x4(%rsp),%edx
mov    %r15,%rdi
mov    %r14,%rsi
call   1070 <substitute@plt>
lea    0xe7c(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1030 <printf@plt>
jmp    11fe <main+0xae>
mov    0x2df0(%rip),%rax
mov    (%rax),%edi
lea    0xe7a(%rip),%rsi
call   1040 <fprintf@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.TYPE_3__ = type { i32, i32 }
@.str = [22 x i8] c"Enter source string: \00"
@.str.1 = [14 x i8] c"Enter regex: \00"
@.str.2 = [27 x i8] c"Enter regex substitution: \00"
@.str.3 = [26 x i8] c"Old source string = [%s]\0A\00"
@.str.4 = [26 x i8] c"New source 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 ([14 x i8], [14 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 ([27 x i8], [27 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 %40
26: ; preds = %0
%27 = load i8*, i8** %3
%28 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 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 i32, i32* %35
%37 = call i8* @substitute(i8* %29, i8* %30, i32 %33, i32 %36)
store i8* %37, i8** %5
%38 = load i8*, i8** %5
%39 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0), i8* %38)
br label %43
40: ; preds = %0
%41 = load i32, i32* @stderr
%42 = call i32 @fprintf(i32 %41, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.5, i64 0, i64 0))
br label %43
43: ; preds = %40, %26
store i32 0, i32* %1
%44 = load i8*, i8** %7
%45 = load i32, i32* %1
ret i32 %45
}

LLVM IR (-O2):
%struct.TYPE_3__ = type { i32, i32 }
@.str = [22 x i8] c"Enter source string: \00"
@.str.1 = [14 x i8] c"Enter regex: \00"
@.str.2 = [27 x i8] c"Enter regex substitution: \00"
@.str.3 = [26 x i8] c"Old source string = [%s]\0A\00"
@.str.4 = [26 x i8] c"New source 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 ([14 x i8], [14 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 ([27 x i8], [27 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 %18, label %10
10: ; preds = %0
%11 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 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 i32, i32* %14
%16 = call i8* @substitute(i8* %3, i8* %7, i32 %13, i32 %15)
%17 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0), i8* %16)
br label %21
18: ; preds = %0
%19 = load i32, i32* @stderr
%20 = call i32 @fprintf(i32 %19, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.5, i64 0, i64 0))
br label %21
21: ; preds = %18, %10
ret i32 0
}