#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 */
char **bc_str_split(char const *, char const, int /*<<< orphan*/);
char *bc_strdup(char const *);
int /*<<< orphan*/ bc_strv_free(char **);
char *bc_strv_join(char **, char const *);
char *bc_str_replace(const char *str, const char search, const char *replace) {
  char **pieces = bc_str_split(str, search, 0);
  if (pieces == NULL)
    return NULL;
  char *rv = bc_strv_join(pieces, replace);
  bc_strv_free(pieces);
  if (rv == NULL)
    return bc_strdup(str);
  return rv;
}


x86 assembly (clang, -O0):
<bc_str_replace>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %sil,%al
mov    %rdi,-0x10(%rbp)
mov    %al,-0x11(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
xor    %edx,%edx
movsbl -0x11(%rbp),%esi
call   1030 <bc_str_split@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1181 <bc_str_replace+0x41>
movq   $0x0,-0x8(%rbp)
jmp    11c0 <bc_str_replace+0x80>
mov    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1060 <bc_strv_join@plt>
mov    %rax,-0x30(%rbp)
mov    -0x28(%rbp),%rdi
call   1040 <bc_strv_free@plt>
cmpq   $0x0,-0x30(%rbp)
jne    11b8 <bc_str_replace+0x78>
mov    -0x10(%rbp),%rdi
call   1050 <bc_strdup@plt>
mov    %rax,-0x8(%rbp)
jmp    11c0 <bc_str_replace+0x80>
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bc_str_replace>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r15
mov    %rdi,%r14
xor    %r12d,%r12d
xor    %edx,%edx
call   1030 <bc_str_split@plt>
test   %rax,%rax
je     117b <bc_str_replace+0x3b>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r15,%rsi
call   1060 <bc_strv_join@plt>
mov    %rax,%r12
mov    %rbx,%rdi
call   1040 <bc_strv_free@plt>
test   %r12,%r12
je     118a <bc_str_replace+0x4a>
mov    %r12,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
mov    %r14,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
jmp    1050 <bc_strdup@plt>

LLVM IR (-O0):
define i8* @bc_str_replace(i8* %0, i8 signext %1, i8* %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8
%7 = alloca i8*
%8 = alloca i8**
%9 = alloca i8*
store i8* %0, i8** %5
store i8 %1, i8* %6
store i8* %2, i8** %7
%10 = load i8*, i8** %5
%11 = load i8, i8* %6
%12 = call i8** @bc_str_split(i8* %10, i8 signext %11, i32 0)
store i8** %12, i8*** %8
%13 = load i8**, i8*** %8
%14 = icmp eq i8** %13, null
br i1 %14, label %15, label %16
15: ; preds = %3
store i8* null, i8** %4
br label %29
16: ; preds = %3
%17 = load i8**, i8*** %8
%18 = load i8*, i8** %7
%19 = call i8* @bc_strv_join(i8** %17, i8* %18)
store i8* %19, i8** %9
%20 = load i8**, i8*** %8
%21 = call i32 @bc_strv_free(i8** %20)
%22 = load i8*, i8** %9
%23 = icmp eq i8* %22, null
br i1 %23, label %24, label %27
24: ; preds = %16
%25 = load i8*, i8** %5
%26 = call i8* @bc_strdup(i8* %25)
store i8* %26, i8** %4
br label %29
27: ; preds = %16
%28 = load i8*, i8** %9
store i8* %28, i8** %4
br label %29
29: ; preds = %27, %24, %15
%30 = load i8*, i8** %4
ret i8* %30
}

LLVM IR (-O2):
define i8* @bc_str_replace(i8* %0, i8 signext %1, i8* %2) {
%4 = tail call i8** @bc_str_split(i8* %0, i8 signext %1, i32 0)
%5 = icmp eq i8** %4, null
br i1 %5, label %12, label %6
6: ; preds = %3
%7 = tail call i8* @bc_strv_join(i8** %4, i8* %2)
%8 = tail call i32 @bc_strv_free(i8** %4)
%9 = icmp eq i8* %7, null
br i1 %9, label %10, label %12
10: ; preds = %6
%11 = tail call i8* @bc_strdup(i8* %0)
br label %12
12: ; preds = %10, %6, %3
%13 = phi i8* [ null, %3 ], [ %11, %10 ], [ %7, %6 ]
ret i8* %13
}