#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*/ CU_ASSERT(int);
char *calloc(int, int);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ sprintf(char *, char *);
int /*<<< orphan*/ strcmp(char *, char *);
int /*<<< orphan*/ strlen(char *);
int /*<<< orphan*/ substr_replace(char **, int *, int *, int,
                                  int /*<<< orphan*/, char *, int);
void test_substr_replace_unmoving_insertion(void) {
  char *source = calloc(1, 1024);
  char *dest = calloc(1, 1024);
  int slen = -1, dlen = -1, dalloc = 1024;
  sprintf(source, "world");
  sprintf(dest, "hello there");
  substr_replace(&dest, &dlen, &dalloc, 6, strlen(dest), source, slen);
  CU_ASSERT(!strcmp(dest, "hello world"));
  CU_ASSERT(dalloc == 1024);
  CU_ASSERT(dlen == 11);
  free(source);
  free(dest);
}


x86 assembly (clang, -O0):
<test_substr_replace_unmoving_insertion>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    $0x1,%edi
mov    $0x400,%esi
call   1060 <calloc@plt>
mov    %rax,-0x8(%rbp)
mov    $0x1,%edi
mov    $0x400,%esi
call   1060 <calloc@plt>
mov    %rax,-0x10(%rbp)
movl   $0xffffffff,-0x14(%rbp)
movl   $0xffffffff,-0x18(%rbp)
movl   $0x400,-0x1c(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe54(%rip),%rsi
call   1090 <sprintf@plt>
mov    -0x10(%rbp),%rdi
lea    0xe32(%rip),%rsi
call   1090 <sprintf@plt>
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,%r8d
mov    -0x8(%rbp),%r9
mov    -0x14(%rbp),%eax
lea    -0x10(%rbp),%rdi
lea    -0x18(%rbp),%rsi
lea    -0x1c(%rbp),%rdx
mov    $0x6,%ecx
mov    %eax,(%rsp)
call   1040 <substr_replace@plt>
mov    -0x10(%rbp),%rdi
lea    0xe02(%rip),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
and    $0x1,%al
movzbl %al,%edi
call   1080 <CU_ASSERT@plt>
cmpl   $0x400,-0x1c(%rbp)
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1080 <CU_ASSERT@plt>
cmpl   $0xb,-0x18(%rbp)
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1080 <CU_ASSERT@plt>
mov    -0x8(%rbp),%rdi
call   1030 <free@plt>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_substr_replace_unmoving_insertion>:
push   %rbx
sub    $0x20,%rsp
mov    $0x1,%edi
mov    $0x400,%esi
call   1060 <calloc@plt>
mov    %rax,%rbx
mov    $0x1,%edi
mov    $0x400,%esi
call   1060 <calloc@plt>
mov    %rax,0x18(%rsp)
movl   $0xffffffff,0x14(%rsp)
ff
movl   $0x400,0x10(%rsp)
movw   $0x64,0x4(%rbx)
movl   $0x6c726f77,(%rbx)
movabs $0x6874206f6c6c6568,%rcx
20 74 68
mov    %rcx,(%rax)
movl   $0x657265,0x8(%rax)
mov    %rax,%rdi
call   1050 <strlen@plt>
movl   $0xffffffff,(%rsp)
lea    0x18(%rsp),%rdi
lea    0x14(%rsp),%rsi
lea    0x10(%rsp),%rdx
mov    $0x6,%ecx
mov    %eax,%r8d
mov    %rbx,%r9
call   1040 <substr_replace@plt>
mov    0x18(%rsp),%rdi
lea    0xe17(%rip),%rsi
call   1070 <strcmp@plt>
xor    %edi,%edi
test   %eax,%eax
sete   %dil
call   1080 <CU_ASSERT@plt>
xor    %edi,%edi
cmpl   $0x400,0x10(%rsp)
sete   %dil
call   1080 <CU_ASSERT@plt>
xor    %edi,%edi
cmpl   $0xb,0x14(%rsp)
sete   %dil
call   1080 <CU_ASSERT@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    0x18(%rsp),%rdi
call   1030 <free@plt>
add    $0x20,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@.str = [6 x i8] c"world\00"
@.str.1 = [12 x i8] c"hello there\00"
@.str.2 = [12 x i8] c"hello world\00"
define void @test_substr_replace_unmoving_insertion() {
%1 = alloca i8*
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = call i8* @calloc(i32 1, i32 1024)
store i8* %6, i8** %1
%7 = call i8* @calloc(i32 1, i32 1024)
store i8* %7, i8** %2
store i32 -1, i32* %3
store i32 -1, i32* %4
store i32 1024, i32* %5
%8 = load i8*, i8** %1
%9 = call i32 @sprintf(i8* %8, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%10 = load i8*, i8** %2
%11 = call i32 @sprintf(i8* %10, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
%12 = load i8*, i8** %2
%13 = call i32 @strlen(i8* %12)
%14 = load i8*, i8** %1
%15 = load i32, i32* %3
%16 = call i32 @substr_replace(i8** %2, i32* %4, i32* %5, i32 6, i32 %13, i8* %14, i32 %15)
%17 = load i8*, i8** %2
%18 = call i32 @strcmp(i8* %17, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
%19 = icmp ne i32 %18, 0
%20 = xor i1 %19, true
%21 = zext i1 %20 to i32
%22 = call i32 @CU_ASSERT(i32 %21)
%23 = load i32, i32* %5
%24 = icmp eq i32 %23, 1024
%25 = zext i1 %24 to i32
%26 = call i32 @CU_ASSERT(i32 %25)
%27 = load i32, i32* %4
%28 = icmp eq i32 %27, 11
%29 = zext i1 %28 to i32
%30 = call i32 @CU_ASSERT(i32 %29)
%31 = load i8*, i8** %1
%32 = call i32 @free(i8* %31)
%33 = load i8*, i8** %2
%34 = call i32 @free(i8* %33)
ret void
}

LLVM IR (-O2):
@.str = [6 x i8] c"world\00"
@.str.1 = [12 x i8] c"hello there\00"
@.str.2 = [12 x i8] c"hello world\00"
define void @test_substr_replace_unmoving_insertion() {
%1 = alloca i8*
%2 = alloca i32
%3 = alloca i32
%4 = tail call dereferenceable_or_null(1024) i8* @calloc(i32 1, i32 1024)
%5 = bitcast i8** %1 to i8*
%6 = tail call dereferenceable_or_null(1024) i8* @calloc(i32 1, i32 1024)
store i8* %6, i8** %1
%7 = bitcast i32* %2 to i8*
store i32 -1, i32* %2
%8 = bitcast i32* %3 to i8*
store i32 1024, i32* %3
tail call void @llvm.memcpy.p0i8.p0i8.i64(i8* align 1 dereferenceable(6) %4, i8* align 1 dereferenceable(6) getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i64 6, i1 false)
tail call void @llvm.memcpy.p0i8.p0i8.i64(i8* align 1 dereferenceable(12) %6, i8* align 1 dereferenceable(12) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i64 12, i1 false)
%9 = tail call i32 @strlen(i8* %6)
%10 = call i32 @substr_replace(i8** %1, i32* %2, i32* %3, i32 6, i32 %9, i8* %4, i32 -1)
%11 = load i8*, i8** %1
%12 = call i32 @strcmp(i8* dereferenceable(1) %11, i8* dereferenceable(12) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
%13 = icmp eq i32 %12, 0
%14 = zext i1 %13 to i32
%15 = call i32 @CU_ASSERT(i32 %14)
%16 = load i32, i32* %3
%17 = icmp eq i32 %16, 1024
%18 = zext i1 %17 to i32
%19 = call i32 @CU_ASSERT(i32 %18)
%20 = load i32, i32* %2
%21 = icmp eq i32 %20, 11
%22 = zext i1 %21 to i32
%23 = call i32 @CU_ASSERT(i32 %22)
%24 = call i32 @free(i8* %4)
%25 = load i8*, i8** %1
%26 = call i32 @free(i8* %25)
ret void
}