#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*/ s_del(char *, char *, int, int /*<<< orphan*/);
char *s_insert(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ s_len(char *);
int s_pos(char *, char *);
char *s_replace(char *substr, char *newsubstr, char *str) {
  int i;
  if ((i = s_pos(substr, str)) == -1)
    return (str);
  return (s_insert(newsubstr, s_del(str, str, i, s_len(substr)), i));
}


x86 assembly (clang, -O0):
<s_replace>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1040 <s_pos@plt>
mov    %eax,-0x24(%rbp)
cmp    $0xffffffff,%eax
jne    117a <s_replace+0x3a>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11c5 <s_replace+0x85>
mov    -0x18(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x24(%rbp),%eax
mov    %eax,-0x34(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <s_len@plt>
mov    -0x48(%rbp),%rdi
mov    -0x40(%rbp),%rsi
mov    -0x34(%rbp),%edx
mov    %eax,%ecx
call   1060 <s_del@plt>
mov    -0x30(%rbp),%rdi
mov    %eax,%esi
mov    -0x24(%rbp),%edx
call   1050 <s_insert@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<s_replace>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdx,%rbx
mov    %rsi,%r14
mov    %rdi,%r15
mov    %rdx,%rsi
call   1040 <s_pos@plt>
cmp    $0xffffffff,%eax
je     118c <s_replace+0x4c>
mov    %eax,%ebp
mov    %r15,%rdi
call   1030 <s_len@plt>
mov    %rbx,%rdi
mov    %rbx,%rsi
mov    %ebp,%edx
mov    %eax,%ecx
call   1060 <s_del@plt>
mov    %r14,%rdi
mov    %eax,%esi
mov    %ebp,%edx
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1050 <s_insert@plt>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i8* @s_replace(i8* %0, i8* %1, i8* %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
store i8* %0, i8** %5
store i8* %1, i8** %6
store i8* %2, i8** %7
%9 = load i8*, i8** %5
%10 = load i8*, i8** %7
%11 = call i32 @s_pos(i8* %9, i8* %10)
store i32 %11, i32* %8
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %15
13: ; preds = %3
%14 = load i8*, i8** %7
store i8* %14, i8** %4
br label %25
15: ; preds = %3
%16 = load i8*, i8** %6
%17 = load i8*, i8** %7
%18 = load i8*, i8** %7
%19 = load i32, i32* %8
%20 = load i8*, i8** %5
%21 = call i32 @s_len(i8* %20)
%22 = call i32 @s_del(i8* %17, i8* %18, i32 %19, i32 %21)
%23 = load i32, i32* %8
%24 = call i8* @s_insert(i8* %16, i32 %22, i32 %23)
store i8* %24, i8** %4
br label %25
25: ; preds = %15, %13
%26 = load i8*, i8** %4
ret i8* %26
}

LLVM IR (-O2):
define i8* @s_replace(i8* %0, i8* %1, i8* %2) {
%4 = tail call i32 @s_pos(i8* %0, i8* %2)
%5 = icmp eq i32 %4, -1
br i1 %5, label %10, label %6
6: ; preds = %3
%7 = tail call i32 @s_len(i8* %0)
%8 = tail call i32 @s_del(i8* %2, i8* %2, i32 %4, i32 %7)
%9 = tail call i8* @s_insert(i8* %1, i32 %8, i32 %4)
br label %10
10: ; preds = %3, %6
%11 = phi i8* [ %9, %6 ], [ %2, %3 ]
ret i8* %11
}