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


x86 assembly (clang, -O0):
<sequence_replace>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    %rcx,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    $0x3d090,%edi
call   1070 <malloc@plt>
mov    %rax,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
cmpq   $0x0,-0x8(%rbp)
je     1298 <sequence_replace+0x138>
cmpl   $0x0,-0xc(%rbp)
jl     1298 <sequence_replace+0x138>
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x28(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     1293 <sequence_replace+0x133>
mov    -0x20(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0xc(%rbp),%edx
call   1060 <memcpy@plt>
mov    -0xc(%rbp),%eax
add    -0x10(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x28(%rbp),%eax
jl     1218 <sequence_replace+0xb8>
mov    -0x20(%rbp),%rax
movslq -0xc(%rbp),%rcx
add    %rcx,%rax
mov    %rax,-0x38(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x38(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    %eax,%edx
call   1060 <memcpy@plt>
jmp    128e <sequence_replace+0x12e>
mov    -0x20(%rbp),%rax
movslq -0xc(%rbp),%rcx
add    %rcx,%rax
mov    %rax,-0x58(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x58(%rbp),%rdi
mov    -0x50(%rbp),%rsi
mov    %eax,%edx
call   1060 <memcpy@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x48(%rbp),%rdi
mov    %eax,%ecx
mov    -0x3c(%rbp),%eax
add    %ecx,%eax
cltq
add    %rax,%rdi
mov    -0x8(%rbp),%rsi
mov    -0xc(%rbp),%eax
add    -0x10(%rbp),%eax
cltq
add    %rax,%rsi
mov    -0x28(%rbp),%edx
sub    -0x10(%rbp),%edx
sub    -0xc(%rbp),%edx
add    $0x1,%edx
call   1060 <memcpy@plt>
jmp    1293 <sequence_replace+0x133>
jmp    12c7 <sequence_replace+0x167>
mov    0x2d41(%rip),%rax
mov    (%rax),%edi
lea    0xd58(%rip),%rsi
call   1050 <fprintf@plt>
lea    0xd4c(%rip),%rdi
call   1040 <printf@plt>
mov    0x2d18(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
mov    -0x20(%rbp),%rax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sequence_replace>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rcx,%r13
mov    %edx,%r15d
mov    %esi,%ebp
mov    %rdi,%r14
mov    $0x3d090,%edi
call   1070 <malloc@plt>
test   %r14,%r14
je     1218 <sequence_replace+0xb8>
test   %ebp,%ebp
js     1218 <sequence_replace+0xb8>
mov    %rax,%rbx
mov    %r14,%rdi
call   1040 <strlen@plt>
test   %rbx,%rbx
je     1206 <sequence_replace+0xa6>
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %ebp,%edx
mov    %eax,0x4(%rsp)
call   1060 <memcpy@plt>
add    %ebp,%r15d
mov    %ebp,%r12d
add    %rbx,%r12
mov    %r13,%rdi
call   1040 <strlen@plt>
mov    %r12,%rdi
mov    %r13,%rsi
mov    %eax,%edx
call   1060 <memcpy@plt>
mov    0x4(%rsp),%eax
sub    %r15d,%eax
jle    1206 <sequence_replace+0xa6>
mov    %r15d,%r12d
mov    %r13,%rdi
mov    %eax,%r15d
call   1040 <strlen@plt>
add    %ebp,%eax
cltq
mov    %rbx,%rdi
add    %rax,%rdi
movslq %r12d,%rax
add    %rax,%r14
add    $0x1,%r15d
mov    %r14,%rsi
mov    %r15d,%edx
call   1060 <memcpy@plt>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2dc1(%rip),%rax
mov    (%rax),%edi
lea    0xdd8(%rip),%rsi
call   1050 <fprintf@plt>
lea    0xde1(%rip),%rdi
call   1030 <puts@plt>
mov    0x2d98(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>

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

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