#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*/ bzero(char *, short);
char *malloc(short);
int /*<<< orphan*/ strcat(char *, char *);
char *strdup(char *);
int strlen(char *);
int /*<<< orphan*/ strncpy(char *, char *, short);
char *strstr(char *, char *);
char *strrpl(char *str, char *cut, char *paste) {
  char paste_length, cut_length;
  short cut_index, newstr_length, tail_length;
  char *cut_ptr;
  char *newstr;
  cut_ptr = strstr(str, cut);
  if (cut_ptr == NULL)
    return strdup(str);
  paste_length = strlen(paste);
  cut_length = strlen(cut);
  cut_index = cut_ptr - str;
  tail_length = strlen(cut_ptr) - strlen(cut);
  newstr_length = strlen(str) - strlen(cut) + strlen(paste) + 1;
  newstr = malloc(newstr_length);
  bzero(newstr, newstr_length);
  strncpy(newstr, str, cut_index);
  strcat(newstr, paste);
  strcat(newstr, cut_ptr + cut_length);
  return newstr;
}


x86 assembly (clang, -O0):
<strrpl>:
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    -0x18(%rbp),%rsi
call   1090 <strstr@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    11b2 <strrpl+0x42>
mov    -0x10(%rbp),%rdi
call   1080 <strdup@plt>
mov    %rax,-0x8(%rbp)
jmp    1285 <strrpl+0x115>
mov    -0x20(%rbp),%rdi
call   1040 <strlen@plt>
mov    %al,-0x21(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    %al,-0x22(%rbp)
mov    -0x30(%rbp),%rax
mov    -0x10(%rbp),%rcx
sub    %rcx,%rax
mov    %ax,-0x24(%rbp)
mov    -0x30(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x44(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x44(%rbp),%eax
sub    %ecx,%eax
mov    %ax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x40(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x40(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x3c(%rbp)
mov    -0x20(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x3c(%rbp),%eax
add    %ecx,%eax
add    $0x1,%eax
mov    %ax,-0x26(%rbp)
movswl -0x26(%rbp),%edi
call   1050 <malloc@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rdi
movswl -0x26(%rbp),%esi
call   1060 <bzero@plt>
mov    -0x38(%rbp),%rdi
mov    -0x10(%rbp),%rsi
movswl -0x24(%rbp),%edx
call   1030 <strncpy@plt>
mov    -0x38(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1070 <strcat@plt>
mov    -0x38(%rbp),%rdi
mov    -0x30(%rbp),%rsi
movsbl -0x22(%rbp),%eax
cltq
add    %rax,%rsi
call   1070 <strcat@plt>
mov    -0x38(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<strrpl>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rsi,%rbp
mov    %rdi,%r13
call   1090 <strstr@plt>
test   %rax,%rax
je     1236 <strrpl+0xc6>
mov    %rax,%r15
mov    %r14,%rdi
call   1040 <strlen@plt>
mov    %rbp,%rdi
call   1040 <strlen@plt>
mov    %eax,0x4(%rsp)
mov    %r15d,%r12d
sub    %r13d,%r12d
mov    %r15,%rdi
call   1040 <strlen@plt>
mov    %rbp,%rdi
call   1040 <strlen@plt>
mov    %r13,%rdi
call   1040 <strlen@plt>
mov    %eax,%ebx
mov    %rbp,%rdi
call   1040 <strlen@plt>
sub    %eax,%ebx
mov    %r14,%rdi
call   1040 <strlen@plt>
add    %ebx,%eax
add    $0x1,%eax
movswl %ax,%ebx
mov    %ebx,%edi
call   1050 <malloc@plt>
mov    %rax,%rbp
mov    %rax,%rdi
mov    %ebx,%esi
call   1060 <bzero@plt>
movswl %r12w,%edx
mov    %rbp,%rdi
mov    %r13,%rsi
call   1030 <strncpy@plt>
mov    %rbp,%rdi
mov    %r14,%rsi
call   1070 <strcat@plt>
movsbl 0x4(%rsp),%eax
movslq %eax,%rsi
add    %r15,%rsi
mov    %rbp,%rdi
call   1070 <strcat@plt>
mov    %rbp,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %r13,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1080 <strdup@plt>

LLVM IR (-O0):
define i8* @strrpl(i8* %0, i8* %1, i8* %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8
%9 = alloca i8
%10 = alloca i16
%11 = alloca i16
%12 = alloca i16
%13 = alloca i8*
%14 = alloca i8*
store i8* %0, i8** %5
store i8* %1, i8** %6
store i8* %2, i8** %7
%15 = load i8*, i8** %5
%16 = load i8*, i8** %6
%17 = call i8* @strstr(i8* %15, i8* %16)
store i8* %17, i8** %13
%18 = load i8*, i8** %13
%19 = icmp eq i8* %18, null
br i1 %19, label %20, label %23
20: ; preds = %3
%21 = load i8*, i8** %5
%22 = call i8* @strdup(i8* %21)
store i8* %22, i8** %4
br label %72
23: ; preds = %3
%24 = load i8*, i8** %7
%25 = call i32 @strlen(i8* %24)
%26 = trunc i32 %25 to i8
store i8 %26, i8* %8
%27 = load i8*, i8** %6
%28 = call i32 @strlen(i8* %27)
%29 = trunc i32 %28 to i8
store i8 %29, i8* %9
%30 = load i8*, i8** %13
%31 = load i8*, i8** %5
%32 = ptrtoint i8* %30 to i64
%33 = ptrtoint i8* %31 to i64
%34 = sub i64 %32, %33
%35 = trunc i64 %34 to i16
store i16 %35, i16* %10
%36 = load i8*, i8** %13
%37 = call i32 @strlen(i8* %36)
%38 = load i8*, i8** %6
%39 = call i32 @strlen(i8* %38)
%40 = sub nsw i32 %37, %39
%41 = trunc i32 %40 to i16
store i16 %41, i16* %12
%42 = load i8*, i8** %5
%43 = call i32 @strlen(i8* %42)
%44 = load i8*, i8** %6
%45 = call i32 @strlen(i8* %44)
%46 = sub nsw i32 %43, %45
%47 = load i8*, i8** %7
%48 = call i32 @strlen(i8* %47)
%49 = add nsw i32 %46, %48
%50 = add nsw i32 %49, 1
%51 = trunc i32 %50 to i16
store i16 %51, i16* %11
%52 = load i16, i16* %11
%53 = call i8* @malloc(i16 signext %52)
store i8* %53, i8** %14
%54 = load i8*, i8** %14
%55 = load i16, i16* %11
%56 = call i32 @bzero(i8* %54, i16 signext %55)
%57 = load i8*, i8** %14
%58 = load i8*, i8** %5
%59 = load i16, i16* %10
%60 = call i32 @strncpy(i8* %57, i8* %58, i16 signext %59)
%61 = load i8*, i8** %14
%62 = load i8*, i8** %7
%63 = call i32 @strcat(i8* %61, i8* %62)
%64 = load i8*, i8** %14
%65 = load i8*, i8** %13
%66 = load i8, i8* %9
%67 = sext i8 %66 to i32
%68 = sext i32 %67 to i64
%69 = getelementptr inbounds i8, i8* %65, i64 %68
%70 = call i32 @strcat(i8* %64, i8* %69)
%71 = load i8*, i8** %14
store i8* %71, i8** %4
br label %72
72: ; preds = %23, %20
%73 = load i8*, i8** %4
ret i8* %73
}

LLVM IR (-O2):
define i8* @strrpl(i8* %0, i8* %1, i8* %2) {
%4 = tail call i8* @strstr(i8* dereferenceable(1) %0, i8* dereferenceable(1) %1)
%5 = icmp eq i8* %4, null
br i1 %5, label %6, label %8
6: ; preds = %3
%7 = tail call i8* @strdup(i8* %0)
br label %33
8: ; preds = %3
%9 = tail call i32 @strlen(i8* %2)
%10 = tail call i32 @strlen(i8* %1)
%11 = ptrtoint i8* %4 to i64
%12 = ptrtoint i8* %0 to i64
%13 = sub i64 %11, %12
%14 = trunc i64 %13 to i16
%15 = tail call i32 @strlen(i8* %4)
%16 = tail call i32 @strlen(i8* %1)
%17 = tail call i32 @strlen(i8* %0)
%18 = tail call i32 @strlen(i8* %1)
%19 = sub i32 %17, %18
%20 = tail call i32 @strlen(i8* %2)
%21 = add nsw i32 %19, %20
%22 = trunc i32 %21 to i16
%23 = add i16 %22, 1
%24 = tail call i8* @malloc(i16 signext %23)
%25 = tail call i32 @bzero(i8* %24, i16 signext %23)
%26 = tail call i32 @strncpy(i8* %24, i8* %0, i16 signext %14)
%27 = tail call i32 @strcat(i8* %24, i8* %2)
%28 = shl i32 %10, 24
%29 = ashr exact i32 %28, 24
%30 = sext i32 %29 to i64
%31 = getelementptr inbounds i8, i8* %4, i64 %30
%32 = tail call i32 @strcat(i8* %24, i8* %31)
br label %33
33: ; preds = %8, %6
%34 = phi i8* [ %7, %6 ], [ %24, %8 ]
ret i8* %34
}