#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*/ sprintf(char *, char *, char const *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
long strlen(char const *);
int /*<<< orphan*/ strncpy(char *, char *, int);
char *strstr(char *, char const *);
int replace_string(char *str, int str_length, const char *original,
                   const char *replacement) {
  char buffer[str_length];
  char *found;
  if (!(found = strstr(str, original)))
    return 0;
  if (found - str >= str_length)
    return -1;
  strncpy(buffer, str, found - str);
  buffer[found - str] = '\0';
  if (strlen(buffer) - (found - str) + strlen(replacement) +
          strlen(found + strlen(original)) >=
      str_length)
    return -1;
  sprintf(buffer + (found - str), "%s%s", replacement,
          found + strlen(original));
  strcpy(str, buffer);
  return 1;
}


x86 assembly (clang, -O0):
<replace_string>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    -0x14(%rbp),%eax
mov    %rsp,%rcx
mov    %rcx,-0x30(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x50(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x38(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1070 <strstr@plt>
mov    %rax,-0x40(%rbp)
cmp    $0x0,%rax
jne    11be <replace_string+0x6e>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x44(%rbp)
jmp    12f0 <replace_string+0x1a0>
mov    -0x40(%rbp),%rax
mov    -0x10(%rbp),%rcx
sub    %rcx,%rax
movslq -0x14(%rbp),%rcx
cmp    %rcx,%rax
jl     11e9 <replace_string+0x99>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x44(%rbp)
jmp    12f0 <replace_string+0x1a0>
mov    -0x50(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x40(%rbp),%rax
mov    -0x10(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,%edx
call   1030 <strncpy@plt>
mov    -0x50(%rbp),%rdi
mov    -0x40(%rbp),%rax
mov    -0x10(%rbp),%rcx
sub    %rcx,%rax
movb   $0x0,(%rdi,%rax,1)
call   1050 <strlen@plt>
mov    -0x40(%rbp),%rcx
mov    -0x10(%rbp),%rdx
sub    %rdx,%rcx
sub    %rcx,%rax
mov    %rax,-0x68(%rbp)
mov    -0x28(%rbp),%rdi
call   1050 <strlen@plt>
mov    %rax,%rcx
mov    -0x68(%rbp),%rax
add    %rcx,%rax
mov    %rax,-0x58(%rbp)
mov    -0x40(%rbp),%rax
mov    %rax,-0x60(%rbp)
mov    -0x20(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x60(%rbp),%rdi
add    %rax,%rdi
call   1050 <strlen@plt>
mov    %rax,%rcx
mov    -0x58(%rbp),%rax
add    %rcx,%rax
movslq -0x14(%rbp),%rcx
cmp    %rcx,%rax
jl     128b <replace_string+0x13b>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x44(%rbp)
jmp    12f0 <replace_string+0x1a0>
mov    -0x50(%rbp),%rax
mov    -0x40(%rbp),%rcx
mov    -0x10(%rbp),%rdx
sub    %rdx,%rcx
add    %rcx,%rax
mov    %rax,-0x78(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x70(%rbp)
mov    -0x40(%rbp),%rax
mov    %rax,-0x80(%rbp)
mov    -0x20(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x80(%rbp),%rcx
mov    -0x78(%rbp),%rdi
mov    -0x70(%rbp),%rdx
add    %rax,%rcx
lea    0xd30(%rip),%rsi
call   1060 <sprintf@plt>
mov    -0x50(%rbp),%rsi
mov    -0x10(%rbp),%rdi
call   1040 <strcpy@plt>
movl   $0x1,-0x4(%rbp)
movl   $0x1,-0x44(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<replace_string>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rcx,%r12
mov    %rdx,%r13
mov    %esi,%r15d
mov    %esi,%eax
mov    %rsp,%rbx
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
mov    %rdi,-0x30(%rbp)
mov    %rdx,%rsi
call   1070 <strstr@plt>
test   %rax,%rax
je     1233 <replace_string+0xe3>
mov    %rax,%r14
mov    %r12,-0x38(%rbp)
mov    %rax,%r12
sub    -0x30(%rbp),%r12
movslq %r15d,%rcx
mov    $0xffffffff,%eax
cmp    %rcx,%r12
jge    1235 <replace_string+0xe5>
mov    %rbx,%rdi
mov    -0x30(%rbp),%rsi
mov    %r12d,%edx
mov    %rcx,-0x40(%rbp)
call   1030 <strncpy@plt>
movb   $0x0,(%rbx,%r12,1)
mov    %rbx,%rdi
call   1050 <strlen@plt>
mov    %rax,%r15
sub    %r12,%r15
mov    -0x38(%rbp),%rdi
call   1050 <strlen@plt>
add    %r15,%rax
mov    %rax,%r15
mov    %r13,%rdi
call   1050 <strlen@plt>
add    %rax,%r14
mov    %r14,%rdi
call   1050 <strlen@plt>
add    %r15,%rax
cmp    -0x40(%rbp),%rax
mov    $0xffffffff,%eax
jge    1235 <replace_string+0xe5>
add    %rbx,%r12
lea    0xdef(%rip),%rsi
mov    %r12,%rdi
mov    -0x38(%rbp),%rdx
mov    %r14,%rcx
call   1060 <sprintf@plt>
mov    -0x30(%rbp),%rdi
mov    %rbx,%rsi
call   1040 <strcpy@plt>
mov    $0x1,%eax
jmp    1235 <replace_string+0xe5>
xor    %eax,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [5 x i8] c"%s%s\00"
define i32 @replace_string(i8* %0, i32 %1, i8* %2, i8* %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca i64
%12 = alloca i8*
%13 = alloca i32
store i8* %0, i8** %6
store i32 %1, i32* %7
store i8* %2, i8** %8
store i8* %3, i8** %9
%14 = load i32, i32* %7
%15 = zext i32 %14 to i64
%16 = call i8* @llvm.stacksave()
store i8* %16, i8** %10
%17 = alloca i8, i64 %15
store i64 %15, i64* %11
%18 = load i8*, i8** %6
%19 = load i8*, i8** %8
%20 = call i8* @strstr(i8* %18, i8* %19)
store i8* %20, i8** %12
%21 = icmp ne i8* %20, null
br i1 %21, label %23, label %22
22: ; preds = %4
store i32 0, i32* %5
store i32 1, i32* %13
br label %83
23: ; preds = %4
%24 = load i8*, i8** %12
%25 = load i8*, i8** %6
%26 = ptrtoint i8* %24 to i64
%27 = ptrtoint i8* %25 to i64
%28 = sub i64 %26, %27
%29 = load i32, i32* %7
%30 = sext i32 %29 to i64
%31 = icmp sge i64 %28, %30
br i1 %31, label %32, label %33
32: ; preds = %23
store i32 -1, i32* %5
store i32 1, i32* %13
br label %83
33: ; preds = %23
%34 = load i8*, i8** %6
%35 = load i8*, i8** %12
%36 = load i8*, i8** %6
%37 = ptrtoint i8* %35 to i64
%38 = ptrtoint i8* %36 to i64
%39 = sub i64 %37, %38
%40 = trunc i64 %39 to i32
%41 = call i32 @strncpy(i8* %17, i8* %34, i32 %40)
%42 = load i8*, i8** %12
%43 = load i8*, i8** %6
%44 = ptrtoint i8* %42 to i64
%45 = ptrtoint i8* %43 to i64
%46 = sub i64 %44, %45
%47 = getelementptr inbounds i8, i8* %17, i64 %46
store i8 0, i8* %47
%48 = call i64 @strlen(i8* %17)
%49 = load i8*, i8** %12
%50 = load i8*, i8** %6
%51 = ptrtoint i8* %49 to i64
%52 = ptrtoint i8* %50 to i64
%53 = sub i64 %51, %52
%54 = sub nsw i64 %48, %53
%55 = load i8*, i8** %9
%56 = call i64 @strlen(i8* %55)
%57 = add nsw i64 %54, %56
%58 = load i8*, i8** %12
%59 = load i8*, i8** %8
%60 = call i64 @strlen(i8* %59)
%61 = getelementptr inbounds i8, i8* %58, i64 %60
%62 = call i64 @strlen(i8* %61)
%63 = add nsw i64 %57, %62
%64 = load i32, i32* %7
%65 = sext i32 %64 to i64
%66 = icmp sge i64 %63, %65
br i1 %66, label %67, label %68
67: ; preds = %33
store i32 -1, i32* %5
store i32 1, i32* %13
br label %83
68: ; preds = %33
%69 = load i8*, i8** %12
%70 = load i8*, i8** %6
%71 = ptrtoint i8* %69 to i64
%72 = ptrtoint i8* %70 to i64
%73 = sub i64 %71, %72
%74 = getelementptr inbounds i8, i8* %17, i64 %73
%75 = load i8*, i8** %9
%76 = load i8*, i8** %12
%77 = load i8*, i8** %8
%78 = call i64 @strlen(i8* %77)
%79 = getelementptr inbounds i8, i8* %76, i64 %78
%80 = call i32 @sprintf(i8* %74, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* %75, i8* %79)
%81 = load i8*, i8** %6
%82 = call i32 @strcpy(i8* %81, i8* %17)
store i32 1, i32* %5
store i32 1, i32* %13
br label %83
83: ; preds = %68, %67, %32, %22
%84 = load i8*, i8** %10
%85 = load i32, i32* %5
ret i32 %85
}

LLVM IR (-O2):
@.str = [5 x i8] c"%s%s\00"
define i32 @replace_string(i8* %0, i32 %1, i8* nocapture %2, i8* %3) {
%5 = zext i32 %1 to i64
%6 = alloca i8, i64 %5
%7 = tail call i8* @strstr(i8* dereferenceable(1) %0, i8* dereferenceable(1) %2)
%8 = icmp eq i8* %7, null
br i1 %8, label %31, label %9
9: ; preds = %4
%10 = ptrtoint i8* %7 to i64
%11 = ptrtoint i8* %0 to i64
%12 = sub i64 %10, %11
%13 = sext i32 %1 to i64
%14 = icmp slt i64 %12, %13
br i1 %14, label %15, label %31
15: ; preds = %9
%16 = trunc i64 %12 to i32
%17 = call i32 @strncpy(i8* %6, i8* %0, i32 %16)
%18 = getelementptr inbounds i8, i8* %6, i64 %12
store i8 0, i8* %18
%19 = call i64 @strlen(i8* %6)
%20 = sub i64 %19, %12
%21 = call i64 @strlen(i8* dereferenceable(1) %3)
%22 = add nsw i64 %20, %21
%23 = call i64 @strlen(i8* dereferenceable(1) %2)
%24 = getelementptr inbounds i8, i8* %7, i64 %23
%25 = call i64 @strlen(i8* %24)
%26 = add nsw i64 %22, %25
%27 = icmp slt i64 %26, %13
br i1 %27, label %28, label %31
28: ; preds = %15
%29 = call i32 @sprintf(i8* %18, i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* %3, i8* %24)
%30 = call i32 @strcpy(i8* %0, i8* %6)
br label %31
31: ; preds = %15, %9, %4, %28
%32 = phi i32 [ 1, %28 ], [ 0, %4 ], [ -1, %9 ], [ -1, %15 ]
ret i32 %32
}