void test(str, pat, sub) char *str;
char *pat;
char *sub;
{
  int len;
  char *found;
  struct pnode *cpat;
  char buf[110];
  int max = 100;
  printf("string:     %s\n", str);
  printf("pattern:    %s\n", pat);
  cpat = sr_compile(pat);
  found = str;
  len = sr_search(cpat, str, &found);
  sr_free_re(cpat);
  if (len == -1) {
    printf("pattern not found\n\n");
    return;
  }
  printf("found:      %.*s\n", len, found);
  sr_replace(str, found, len, buf, sub, &max);
  printf("substitute: %s\n", sub);
  printf("new str:    %s\n\n", buf);
  return;
}


x86 assembly (clang, -O0):
<test>:
push   %rbp
mov    %rsp,%rbp
sub    $0xb0,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0x64,-0xa4(%rbp)
mov    -0x8(%rbp),%rsi
lea    0xe84(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x10(%rbp),%rsi
lea    0xe82(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x10(%rbp),%rdi
mov    $0x0,%al
call   1070 <sr_compile@plt>
cltq
mov    %rax,-0x30(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x8(%rbp),%rsi
lea    -0x28(%rbp),%rdx
mov    $0x0,%al
call   1060 <sr_search@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x30(%rbp),%rdi
mov    $0x0,%al
call   1030 <sr_free_re@plt>
cmpl   $0xffffffff,-0x1c(%rbp)
jne    11ec <test+0x9c>
lea    0xe40(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    124c <test+0xfc>
mov    -0x1c(%rbp),%esi
mov    -0x28(%rbp),%rdx
lea    0xe3a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x8(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x1c(%rbp),%edx
lea    -0xa0(%rbp),%rcx
mov    -0x18(%rbp),%r8
lea    -0xa4(%rbp),%r9
mov    $0x0,%al
call   1050 <sr_replace@plt>
mov    -0x18(%rbp),%rsi
lea    0xe16(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    -0xa0(%rbp),%rsi
lea    0xe11(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
add    $0xb0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x88,%rsp
mov    %rdx,%r14
mov    %rsi,%rbp
mov    %rdi,%r15
movl   $0x64,0xc(%rsp)
lea    0xe7b(%rip),%rdi
mov    %r15,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xe7a(%rip),%rdi
mov    %rbp,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %rbp,%rdi
xor    %eax,%eax
call   1080 <sr_compile@plt>
movslq %eax,%rbx
mov    %r15,(%rsp)
mov    %rsp,%rdx
mov    %rbx,%rdi
mov    %r15,%rsi
xor    %eax,%eax
call   1070 <sr_search@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
xor    %eax,%eax
call   1040 <sr_free_re@plt>
cmp    $0xffffffff,%ebp
je     122a <test+0xca>
mov    (%rsp),%rdx
lea    0xe43(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    (%rsp),%rsi
lea    0x10(%rsp),%rbx
lea    0xc(%rsp),%r9
mov    %r15,%rdi
mov    %ebp,%edx
mov    %rbx,%rcx
mov    %r14,%r8
xor    %eax,%eax
call   1060 <sr_replace@plt>
lea    0xe25(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xe24(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    1236 <test+0xd6>
lea    0xe22(%rip),%rdi
call   1030 <puts@plt>
add    $0x88,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.pnode = type opaque
@.str = [16 x i8] c"string: %s\0A\00"
@.str.1 = [16 x i8] c"pattern: %s\0A\00"
@.str.2 = [20 x i8] c"pattern not found\0A\0A\00"
@.str.3 = [18 x i8] c"found: %.*s\0A\00"
@.str.4 = [16 x i8] c"substitute: %s\0A\00"
@.str.5 = [17 x i8] c"new str: %s\0A\0A\00"
define void @test(i8* %0, i8* %1, i8* %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i8*
%9 = alloca %struct.pnode*
%10 = alloca [110 x i8]
%11 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
store i8* %2, i8** %6
store i32 100, i32* %11
%12 = load i8*, i8** %4
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i8* %12)
%14 = load i8*, i8** %5
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i8* %14)
%16 = load i8*, i8** %5
%17 = call i32 (i8*, ...) bitcast (i32 (...)* @sr_compile to i32 (i8*, ...)*)(i8* %16)
%18 = sext i32 %17 to i64
%19 = inttoptr i64 %18 to %struct.pnode*
store %struct.pnode* %19, %struct.pnode** %9
%20 = load i8*, i8** %4
store i8* %20, i8** %8
%21 = load %struct.pnode*, %struct.pnode** %9
%22 = load i8*, i8** %4
%23 = call i32 (%struct.pnode*, i8*, i8**, ...) bitcast (i32 (...)* @sr_search to i32 (%struct.pnode*, i8*, i8**, ...)*)(%struct.pnode* %21, i8* %22, i8** %8)
store i32 %23, i32* %7
%24 = load %struct.pnode*, %struct.pnode** %9
%25 = call i32 (%struct.pnode*, ...) bitcast (i32 (...)* @sr_free_re to i32 (%struct.pnode*, ...)*)(%struct.pnode* %24)
%26 = load i32, i32* %7
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %30
28: ; preds = %3
%29 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0))
br label %44
30: ; preds = %3
%31 = load i32, i32* %7
%32 = load i8*, i8** %8
%33 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0), i32 %31, i8* %32)
%34 = load i8*, i8** %4
%35 = load i8*, i8** %8
%36 = load i32, i32* %7
%37 = getelementptr inbounds [110 x i8], [110 x i8]* %10, i64 0, i64 0
%38 = load i8*, i8** %6
%39 = call i32 (i8*, i8*, i32, i8*, i8*, i32*, ...) bitcast (i32 (...)* @sr_replace to i32 (i8*, i8*, i32, i8*, i8*, i32*, ...)*)(i8* %34, i8* %35, i32 %36, i8* %37, i8* %38, i32* %11)
%40 = load i8*, i8** %6
%41 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0), i8* %40)
%42 = getelementptr inbounds [110 x i8], [110 x i8]* %10, i64 0, i64 0
%43 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.5, i64 0, i64 0), i8* %42)
br label %44
44: ; preds = %30, %28
ret void
}

LLVM IR (-O2):
%struct.pnode = type opaque
@.str = [16 x i8] c"string: %s\0A\00"
@.str.1 = [16 x i8] c"pattern: %s\0A\00"
@.str.3 = [18 x i8] c"found: %.*s\0A\00"
@.str.4 = [16 x i8] c"substitute: %s\0A\00"
@.str.5 = [17 x i8] c"new str: %s\0A\0A\00"
@str = [19 x i8] c"pattern not found\0A\00"
define void @test(i8* %0, i8* %1, i8* %2) {
%4 = alloca i8*
%5 = alloca [110 x i8]
%6 = alloca i32
%7 = bitcast i8** %4 to i8*
%8 = getelementptr inbounds [110 x i8], [110 x i8]* %5, i64 0, i64 0
%9 = bitcast i32* %6 to i8*
store i32 100, i32* %6
%10 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i8* %0)
%11 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i8* %1)
%12 = tail call i32 (i8*, ...) bitcast (i32 (...)* @sr_compile to i32 (i8*, ...)*)(i8* %1)
%13 = sext i32 %12 to i64
%14 = inttoptr i64 %13 to %struct.pnode*
store i8* %0, i8** %4
%15 = call i32 (%struct.pnode*, i8*, i8**, ...) bitcast (i32 (...)* @sr_search to i32 (%struct.pnode*, i8*, i8**, ...)*)(%struct.pnode* %14, i8* %0, i8** %4)
%16 = call i32 (%struct.pnode*, ...) bitcast (i32 (...)* @sr_free_re to i32 (%struct.pnode*, ...)*)(%struct.pnode* %14)
%17 = icmp eq i32 %15, -1
br i1 %17, label %18, label %20
18: ; preds = %3
%19 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str, i64 0, i64 0))
br label %27
20: ; preds = %3
%21 = load i8*, i8** %4
%22 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0), i32 %15, i8* %21)
%23 = load i8*, i8** %4
%24 = call i32 (i8*, i8*, i32, i8*, i8*, i32*, ...) bitcast (i32 (...)* @sr_replace to i32 (i8*, i8*, i32, i8*, i8*, i32*, ...)*)(i8* %0, i8* %23, i32 %15, i8* %8, i8* %2, i32* %6)
%25 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0), i8* %2)
%26 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str.5, i64 0, i64 0), i8* %8)
br label %27
27: ; preds = %20, %18
ret void
}