#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*/ EXIT_FAILURE;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
char *malloc(int);
int /*<<< orphan*/ memcpy(char *, char const *, size_t);
int /*<<< orphan*/ stderr;
unsigned int strlen(char const *);
char *str_replace(const char *s, unsigned int start, unsigned int lenght,
                  const char *ct) {
  char *new_s = NULL;
  if (s != NULL && ct != NULL && start >= 0 && lenght > 0) {
    size_t size = strlen(s);
    new_s = malloc(sizeof(*new_s) * (size - lenght + strlen(ct) + 1));
    if (new_s != NULL) {
      memcpy(new_s, s, start);
      memcpy(&new_s[start], ct, strlen(ct));
      memcpy(&new_s[start + strlen(ct)], &s[start + lenght],
             size - lenght - start + 1);
    }
  } else {
    fprintf(stderr, "Memoire insuffisante\n");
    exit(EXIT_FAILURE);
  }
  return new_s;
}


x86 assembly (clang, -O0):
<str_replace>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    %rcx,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
cmpq   $0x0,-0x8(%rbp)
je     127d <str_replace+0x12d>
cmpq   $0x0,-0x18(%rbp)
je     127d <str_replace+0x12d>
cmpl   $0x0,-0xc(%rbp)
jb     127d <str_replace+0x12d>
cmpl   $0x0,-0x10(%rbp)
jbe    127d <str_replace+0x12d>
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%eax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    -0x10(%rbp),%ecx
sub    %rcx,%rax
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x30(%rbp),%rax
mov    %ecx,%ecx
add    %rcx,%rax
add    $0x1,%rax
shl    $0x0,%rax
mov    %eax,%edi
call   1060 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     1278 <str_replace+0x128>
mov    -0x20(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0xc(%rbp),%eax
mov    %eax,%edx
call   1050 <memcpy@plt>
mov    -0x20(%rbp),%rax
mov    -0xc(%rbp),%ecx
add    %rcx,%rax
mov    %rax,-0x50(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x50(%rbp),%rdi
mov    -0x48(%rbp),%rsi
mov    %eax,%eax
mov    %eax,%edx
call   1050 <memcpy@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x34(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x40(%rbp),%rdi
mov    %eax,%ecx
mov    -0x34(%rbp),%eax
add    %ecx,%eax
mov    %eax,%eax
add    %rax,%rdi
mov    -0x8(%rbp),%rsi
mov    -0xc(%rbp),%eax
add    -0x10(%rbp),%eax
mov    %eax,%eax
add    %rax,%rsi
mov    -0x28(%rbp),%rdx
mov    -0x10(%rbp),%eax
sub    %rax,%rdx
mov    -0xc(%rbp),%eax
sub    %rax,%rdx
add    $0x1,%rdx
call   1050 <memcpy@plt>
jmp    12a0 <str_replace+0x150>
mov    0x2d5c(%rip),%rax
mov    (%rax),%edi
lea    0xd73(%rip),%rsi
call   1040 <fprintf@plt>
mov    0x2d3f(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
mov    -0x20(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<str_replace>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
test   %edx,%edx
je     1220 <str_replace+0xd0>
mov    %rdi,%r14
test   %rdi,%rdi
je     1220 <str_replace+0xd0>
mov    %rcx,%r13
test   %rcx,%rcx
je     1220 <str_replace+0xd0>
mov    %edx,%r12d
mov    %esi,%r15d
mov    %r14,%rdi
call   1030 <strlen@plt>
mov    %eax,%ebx
mov    %r12d,%eax
sub    %rax,%rbx
mov    %r13,%rdi
call   1030 <strlen@plt>
lea    (%rax,%rbx,1),%edi
add    $0x1,%edi
call   1060 <malloc@plt>
mov    %rax,%rbp
test   %rax,%rax
je     120e <str_replace+0xbe>
mov    %r15d,%edx
mov    %rbp,%rdi
mov    %r14,%rsi
mov    %r14,0x10(%rsp)
mov    %rdx,%r14
call   1050 <memcpy@plt>
lea    (%r14,%rbp,1),%rax
mov    %rax,0x8(%rsp)
mov    %r13,%rdi
call   1030 <strlen@plt>
mov    %eax,%edx
mov    0x8(%rsp),%rdi
mov    %r13,%rsi
call   1050 <memcpy@plt>
mov    %r13,%rdi
call   1030 <strlen@plt>
lea    (%rax,%r15,1),%edi
add    %rbp,%rdi
add    %r15d,%r12d
add    0x10(%rsp),%r12
sub    %r14,%rbx
add    $0x1,%rbx
mov    %r12,%rsi
mov    %rbx,%rdx
call   1050 <memcpy@plt>
mov    %rbp,%rax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2db9(%rip),%rax
mov    (%rax),%edi
lea    0xdd0(%rip),%rsi
call   1040 <fprintf@plt>
mov    0x2d9c(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>

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

LLVM IR (-O2):
@stderr = global i32 0
@.str = [22 x i8] c"Memoire insuffisante\0A\00"
@EXIT_FAILURE = global i32 0
define i8* @str_replace(i8* %0, i32 %1, i32 %2, i8* %3) {
%5 = icmp ne i8* %0, null
%6 = icmp ne i8* %3, null
%7 = and i1 %5, %6
%8 = icmp ne i32 %2, 0
%9 = and i1 %8, %7
br i1 %9, label %10, label %39
10: ; preds = %4
%11 = tail call i32 @strlen(i8* %0)
%12 = zext i32 %11 to i64
%13 = zext i32 %2 to i64
%14 = sub nsw i64 %12, %13
%15 = tail call i32 @strlen(i8* %3)
%16 = trunc i64 %14 to i32
%17 = add i32 %15, 1
%18 = add i32 %17, %16
%19 = tail call i8* @malloc(i32 %18)
%20 = icmp eq i8* %19, null
br i1 %20, label %38, label %21
21: ; preds = %10
%22 = zext i32 %1 to i64
%23 = tail call i32 @memcpy(i8* %19, i8* %0, i64 %22)
%24 = getelementptr inbounds i8, i8* %19, i64 %22
%25 = tail call i32 @strlen(i8* %3)
%26 = zext i32 %25 to i64
%27 = tail call i32 @memcpy(i8* %24, i8* %3, i64 %26)
%28 = tail call i32 @strlen(i8* %3)
%29 = add i32 %28, %1
%30 = zext i32 %29 to i64
%31 = getelementptr inbounds i8, i8* %19, i64 %30
%32 = add i32 %2, %1
%33 = zext i32 %32 to i64
%34 = getelementptr inbounds i8, i8* %0, i64 %33
%35 = sub nsw i64 1, %22
%36 = add nsw i64 %35, %14
%37 = tail call i32 @memcpy(i8* %31, i8* %34, i64 %36)
br label %38
38: ; preds = %21, %10
ret i8* %19
39: ; preds = %4
%40 = load i32, i32* @stderr
%41 = tail call i32 @fprintf(i32 %40, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
%42 = load i32, i32* @EXIT_FAILURE
%43 = tail call i32 @exit(i32 %42)
unreachable
}