#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 */
scalar_t__ malloc(int);
int strlen(char const *);
char *strrpl(const char *str, char oldchar, char newchar) {
  char *rpl = (char *)malloc(sizeof(char) * (1 + strlen(str)));
  char *begin = rpl;
  char c;
  while ((c = *str++)) {
    if (c == oldchar) {
      c = newchar;
    }
    *rpl++ = c;
  }
  *rpl = '\0';
  return begin;
}


x86 assembly (clang, -O0):
<strrpl>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %dl,%al
mov    %sil,%cl
mov    %rdi,-0x8(%rbp)
mov    %cl,-0x9(%rbp)
mov    %al,-0xa(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
add    $0x1,%eax
cltq
shl    $0x0,%rax
mov    %eax,%edi
call   1040 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x8(%rbp)
mov    (%rax),%al
mov    %al,-0x21(%rbp)
cmp    $0x0,%al
je     11a7 <strrpl+0x87>
movsbl -0x21(%rbp),%eax
movsbl -0x9(%rbp),%ecx
cmp    %ecx,%eax
jne    118e <strrpl+0x6e>
mov    -0xa(%rbp),%al
mov    %al,-0x21(%rbp)
mov    -0x21(%rbp),%cl
mov    -0x18(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x18(%rbp)
mov    %cl,(%rax)
jmp    115c <strrpl+0x3c>
mov    -0x18(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x20(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<strrpl>:
push   %rbp
push   %r14
push   %rbx
mov    %edx,%r14d
mov    %esi,%ebp
mov    %rdi,%rbx
call   1030 <strlen@plt>
lea    0x1(%rax),%edi
call   1040 <malloc@plt>
mov    (%rbx),%cl
test   %cl,%cl
je     116c <strrpl+0x4c>
add    $0x1,%rbx
movzbl %r14b,%esi
mov    %rax,%rdx
nopw   0x0(%rax,%rax,1)
cmp    %bpl,%cl
movzbl %cl,%ecx
cmove  %esi,%ecx
mov    %cl,(%rdx)
add    $0x1,%rdx
movzbl (%rbx),%ecx
add    $0x1,%rbx
test   %cl,%cl
jne    1150 <strrpl+0x30>
jmp    116f <strrpl+0x4f>
mov    %rax,%rdx
movb   $0x0,(%rdx)
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
define i8* @strrpl(i8* %0, i8 signext %1, i8 signext %2) {
%4 = alloca i8*
%5 = alloca i8
%6 = alloca i8
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i8
store i8* %0, i8** %4
store i8 %1, i8* %5
store i8 %2, i8* %6
%10 = load i8*, i8** %4
%11 = call i32 @strlen(i8* %10)
%12 = add nsw i32 1, %11
%13 = sext i32 %12 to i64
%14 = mul i64 1, %13
%15 = trunc i64 %14 to i32
%16 = call i64 @malloc(i32 %15)
%17 = inttoptr i64 %16 to i8*
store i8* %17, i8** %7
%18 = load i8*, i8** %7
store i8* %18, i8** %8
br label %19
19: ; preds = %32, %3
%20 = load i8*, i8** %4
%21 = getelementptr inbounds i8, i8* %20, i32 1
store i8* %21, i8** %4
%22 = load i8, i8* %20
store i8 %22, i8* %9
%23 = icmp ne i8 %22, 0
br i1 %23, label %24, label %36
24: ; preds = %19
%25 = load i8, i8* %9
%26 = sext i8 %25 to i32
%27 = load i8, i8* %5
%28 = sext i8 %27 to i32
%29 = icmp eq i32 %26, %28
br i1 %29, label %30, label %32
30: ; preds = %24
%31 = load i8, i8* %6
store i8 %31, i8* %9
br label %32
32: ; preds = %30, %24
%33 = load i8, i8* %9
%34 = load i8*, i8** %7
%35 = getelementptr inbounds i8, i8* %34, i32 1
store i8* %35, i8** %7
store i8 %33, i8* %34
br label %19
36: ; preds = %19
%37 = load i8*, i8** %7
store i8 0, i8* %37
%38 = load i8*, i8** %8
ret i8* %38
}

LLVM IR (-O2):
define i8* @strrpl(i8* %0, i8 signext %1, i8 signext %2) {
%4 = tail call i32 @strlen(i8* %0)
%5 = add nsw i32 %4, 1
%6 = tail call i64 @malloc(i32 %5)
%7 = inttoptr i64 %6 to i8*
%8 = load i8, i8* %0
%9 = icmp eq i8 %8, 0
br i1 %9, label %20, label %10
10: ; preds = %3, %10
%11 = phi i8 [ %18, %10 ], [ %8, %3 ]
%12 = phi i8* [ %14, %10 ], [ %0, %3 ]
%13 = phi i8* [ %17, %10 ], [ %7, %3 ]
%14 = getelementptr inbounds i8, i8* %12, i64 1
%15 = icmp eq i8 %11, %1
%16 = select i1 %15, i8 %2, i8 %11
%17 = getelementptr inbounds i8, i8* %13, i64 1
store i8 %16, i8* %13
%18 = load i8, i8* %14
%19 = icmp eq i8 %18, 0
br i1 %19, label %20, label %10
20: ; preds = %10, %3
%21 = phi i8* [ %7, %3 ], [ %17, %10 ]
store i8 0, i8* %21
ret i8* %7
}