char *Substitute(char *string, char old, char n) {
  register char *ptr;
  static char area[1024];
  ptr = area;
  while (*string) {
    *(ptr++) = (*string == old ? n : *string);
    string++;
  }
  *ptr = '\0';
  return (area);
}


x86 assembly (clang, -O0):
<Substitute>:
push   %rbp
mov    %rsp,%rbp
mov    %dl,%al
mov    %sil,%cl
mov    %rdi,-0x8(%rbp)
mov    %cl,-0x9(%rbp)
mov    %al,-0xa(%rbp)
lea    0x2f16(%rip),%rax
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%rax
cmpb   $0x0,(%rax)
je     117b <Substitute+0x7b>
mov    -0x8(%rbp),%rax
movsbl (%rax),%eax
movsbl -0x9(%rbp),%ecx
cmp    %ecx,%eax
jne    114a <Substitute+0x4a>
movsbl -0xa(%rbp),%eax
mov    %eax,-0x1c(%rbp)
jmp    1154 <Substitute+0x54>
mov    -0x8(%rbp),%rax
movsbl (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    %al,%cl
mov    -0x18(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x18(%rbp)
mov    %cl,(%rax)
mov    -0x8(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x8(%rbp)
jmp    111e <Substitute+0x1e>
mov    -0x18(%rbp),%rax
movb   $0x0,(%rax)
lea    0x2ea7(%rip),%rax
pop    %rbp
ret

x86 assembly (clang, -O2):
<Substitute>:
mov    (%rdi),%cl
test   %cl,%cl
je     113c <Substitute+0x3c>
add    $0x1,%rdi
lea    0x2f1f(%rip),%rax
movzbl %dl,%edx
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
cmp    %sil,%cl
movzbl %cl,%ecx
cmove  %edx,%ecx
mov    %cl,(%rax)
add    $0x1,%rax
movzbl (%rdi),%ecx
add    $0x1,%rdi
test   %cl,%cl
jne    1120 <Substitute+0x20>
jmp    1143 <Substitute+0x43>
lea    0x2eed(%rip),%rax
movb   $0x0,(%rax)
lea    0x2ee3(%rip),%rax
ret

LLVM IR (-O0):
@Substitute.area = internal global [1024 x i8] zeroinitializer
define i8* @Substitute(i8* %0, i8 signext %1, i8 signext %2) {
%4 = alloca i8*
%5 = alloca i8
%6 = alloca i8
%7 = alloca i8*
store i8* %0, i8** %4
store i8 %1, i8* %5
store i8 %2, i8* %6
store i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @Substitute.area, i64 0, i64 0), i8** %7
br label %8
8: ; preds = %26, %3
%9 = load i8*, i8** %4
%10 = load i8, i8* %9
%11 = icmp ne i8 %10, 0
br i1 %11, label %12, label %33
12: ; preds = %8
%13 = load i8*, i8** %4
%14 = load i8, i8* %13
%15 = sext i8 %14 to i32
%16 = load i8, i8* %5
%17 = sext i8 %16 to i32
%18 = icmp eq i32 %15, %17
br i1 %18, label %19, label %22
19: ; preds = %12
%20 = load i8, i8* %6
%21 = sext i8 %20 to i32
br label %26
22: ; preds = %12
%23 = load i8*, i8** %4
%24 = load i8, i8* %23
%25 = sext i8 %24 to i32
br label %26
26: ; preds = %22, %19
%27 = phi i32 [ %21, %19 ], [ %25, %22 ]
%28 = trunc i32 %27 to i8
%29 = load i8*, i8** %7
%30 = getelementptr inbounds i8, i8* %29, i32 1
store i8* %30, i8** %7
store i8 %28, i8* %29
%31 = load i8*, i8** %4
%32 = getelementptr inbounds i8, i8* %31, i32 1
store i8* %32, i8** %4
br label %8
33: ; preds = %8
%34 = load i8*, i8** %7
store i8 0, i8* %34
ret i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @Substitute.area, i64 0, i64 0)
}

LLVM IR (-O2):
@Substitute.area = internal global [1024 x i8] zeroinitializer
define i8* @Substitute(i8* nocapture %0, i8 signext %1, i8 signext %2) {
%4 = load i8, i8* %0
%5 = icmp eq i8 %4, 0
br i1 %5, label %16, label %6
6: ; preds = %3, %6
%7 = phi i8 [ %14, %6 ], [ %4, %3 ]
%8 = phi i8* [ %12, %6 ], [ getelementptr inbounds ([1024 x i8], [1024 x i8]* @Substitute.area, i64 0, i64 0), %3 ]
%9 = phi i8* [ %13, %6 ], [ %0, %3 ]
%10 = icmp eq i8 %7, %1
%11 = select i1 %10, i8 %2, i8 %7
%12 = getelementptr inbounds i8, i8* %8, i64 1
store i8 %11, i8* %8
%13 = getelementptr inbounds i8, i8* %9, i64 1
%14 = load i8, i8* %13
%15 = icmp eq i8 %14, 0
br i1 %15, label %16, label %6
16: ; preds = %6, %3
%17 = phi i8* [ getelementptr inbounds ([1024 x i8], [1024 x i8]* @Substitute.area, i64 0, i64 0), %3 ], [ %12, %6 ]
store i8 0, i8* %17
ret i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @Substitute.area, i64 0, i64 0)
}