#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 */

/* Type definitions */

/* Variables and functions */
int /*<<< orphan*/ outputreplace(char *, char *, char *, char *);
char *replacebegin(char *, char *);
int replace(char *from, char *to, char *text) {
  char *bak, *s, *e;
  bak = text;
  if (from[0] == '^') {
    s = text;
    e = replacebegin(from + 1, text);
    outputreplace(bak, s, e, to);
    return 1;
  }
  do {
    s = text;
    if ((e = replacebegin(from, text)) != 0) {
      outputreplace(bak, s, e, to);
      return 1;
    }
  } while (*text++ != '\0');
  return 0;
}


x86 assembly (clang, -O0):
<replace>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x5e,%eax
jne    118a <replace+0x6a>
mov    -0x20(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
add    $0x1,%rdi
mov    -0x20(%rbp),%rsi
call   1040 <replacebegin@plt>
mov    %rax,-0x38(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    -0x38(%rbp),%rdx
mov    -0x18(%rbp),%rcx
call   1030 <outputreplace@plt>
movl   $0x1,-0x4(%rbp)
jmp    11fa <replace+0xda>
jmp    118f <replace+0x6f>
mov    -0x20(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1040 <replacebegin@plt>
mov    %rax,-0x38(%rbp)
cmp    $0x0,%rax
je     11d3 <replace+0xb3>
mov    -0x28(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    -0x38(%rbp),%rdx
mov    -0x18(%rbp),%rcx
call   1030 <outputreplace@plt>
movl   $0x1,-0x4(%rbp)
jmp    11fa <replace+0xda>
jmp    11d8 <replace+0xb8>
mov    -0x20(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x20(%rbp)
movsbl (%rax),%eax
cmp    $0x0,%eax
jne    118f <replace+0x6f>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<replace>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r15
mov    %rsi,%r14
mov    %rdi,%r12
cmpb   $0x5e,(%rdi)
jne    114d <replace+0x2d>
add    $0x1,%r12
mov    %r12,%rdi
mov    %r15,%rsi
call   1040 <replacebegin@plt>
mov    %r15,%rdi
mov    %r15,%rsi
jmp    1173 <replace+0x53>
mov    %r15,%rbx
mov    %r12,%rdi
mov    %rbx,%rsi
call   1040 <replacebegin@plt>
test   %rax,%rax
jne    116d <replace+0x4d>
cmpb   $0x0,(%rbx)
lea    0x1(%rbx),%rbx
jne    1150 <replace+0x30>
xor    %eax,%eax
jmp    1183 <replace+0x63>
mov    %r15,%rdi
mov    %rbx,%rsi
mov    %rax,%rdx
mov    %r14,%rcx
call   1030 <outputreplace@plt>
mov    $0x1,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i32 @replace(i8* %0, i8* %1, i8* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i8*
store i8* %0, i8** %5
store i8* %1, i8** %6
store i8* %2, i8** %7
%11 = load i8*, i8** %7
store i8* %11, i8** %8
%12 = load i8*, i8** %5
%13 = getelementptr inbounds i8, i8* %12, i64 0
%14 = load i8, i8* %13
%15 = sext i8 %14 to i32
%16 = icmp eq i32 %15, 94
br i1 %16, label %17, label %28
17: ; preds = %3
%18 = load i8*, i8** %7
store i8* %18, i8** %9
%19 = load i8*, i8** %5
%20 = getelementptr inbounds i8, i8* %19, i64 1
%21 = load i8*, i8** %7
%22 = call i8* @replacebegin(i8* %20, i8* %21)
store i8* %22, i8** %10
%23 = load i8*, i8** %8
%24 = load i8*, i8** %9
%25 = load i8*, i8** %10
%26 = load i8*, i8** %6
%27 = call i32 @outputreplace(i8* %23, i8* %24, i8* %25, i8* %26)
store i32 1, i32* %4
br label %49
28: ; preds = %3
br label %29
29: ; preds = %42, %28
%30 = load i8*, i8** %7
store i8* %30, i8** %9
%31 = load i8*, i8** %5
%32 = load i8*, i8** %7
%33 = call i8* @replacebegin(i8* %31, i8* %32)
store i8* %33, i8** %10
%34 = icmp ne i8* %33, null
br i1 %34, label %35, label %41
35: ; preds = %29
%36 = load i8*, i8** %8
%37 = load i8*, i8** %9
%38 = load i8*, i8** %10
%39 = load i8*, i8** %6
%40 = call i32 @outputreplace(i8* %36, i8* %37, i8* %38, i8* %39)
store i32 1, i32* %4
br label %49
41: ; preds = %29
br label %42
42: ; preds = %41
%43 = load i8*, i8** %7
%44 = getelementptr inbounds i8, i8* %43, i32 1
store i8* %44, i8** %7
%45 = load i8, i8* %43
%46 = sext i8 %45 to i32
%47 = icmp ne i32 %46, 0
br i1 %47, label %29, label %48
48: ; preds = %42
store i32 0, i32* %4
br label %49
49: ; preds = %48, %35, %17
%50 = load i32, i32* %4
ret i32 %50
}

LLVM IR (-O2):
define i32 @replace(i8* %0, i8* %1, i8* %2) {
%4 = load i8, i8* %0
%5 = icmp eq i8 %4, 94
br i1 %5, label %6, label %10
6: ; preds = %3
%7 = getelementptr inbounds i8, i8* %0, i64 1
%8 = tail call i8* @replacebegin(i8* %7, i8* %2)
%9 = tail call i32 @outputreplace(i8* %2, i8* %2, i8* %8, i8* %1)
br label %20
10: ; preds = %3, %16
%11 = phi i8* [ %17, %16 ], [ %2, %3 ]
%12 = tail call i8* @replacebegin(i8* %0, i8* %11)
%13 = icmp eq i8* %12, null
br i1 %13, label %16, label %14
14: ; preds = %10
%15 = tail call i32 @outputreplace(i8* %2, i8* %11, i8* %12, i8* %1)
br label %20
16: ; preds = %10
%17 = getelementptr inbounds i8, i8* %11, i64 1
%18 = load i8, i8* %11
%19 = icmp eq i8 %18, 0
br i1 %19, label %20, label %10
20: ; preds = %16, %14, %6
%21 = phi i32 [ 1, %6 ], [ 1, %14 ], [ 0, %16 ]
ret i32 %21
}