#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);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ strcmp(char *, char const *);
int strlen(char const *);
int /*<<< orphan*/ substr_replace(char **, int *, int *, int,
                                  int /*<<< orphan*/, char *, int);
void test_substr_replace_multiple(void) {
  char *dest = NULL;
  int dlen = 0, dalloc = 0;
  substr_replace(&dest, &dlen, &dalloc, 0, 0, "hello", -1);
  substr_replace(&dest, &dlen, &dalloc, dlen, 0, " ", -1);
  substr_replace(&dest, &dlen, &dalloc, dlen, 0, "world", -1);
  const char *expect = "hello world";
  CU_ASSERT(!strcmp(dest, expect));
  CU_ASSERT(dlen == strlen(expect));
  CU_ASSERT(dalloc > dlen);
  free(dest);
}


x86 assembly (clang, -O0):
<test_substr_replace_multiple>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movq   $0x0,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
lea    -0x8(%rbp),%rdi
lea    -0xc(%rbp),%rsi
lea    -0x10(%rbp),%rdx
xor    %r8d,%r8d
lea    0xe7c(%rip),%r9
mov    %r8d,%ecx
movl   $0xffffffff,(%rsp)
call   1040 <substr_replace@plt>
mov    -0xc(%rbp),%ecx
lea    -0x8(%rbp),%rdi
lea    -0xc(%rbp),%rsi
lea    -0x10(%rbp),%rdx
xor    %r8d,%r8d
lea    0xe5a(%rip),%r9
movl   $0xffffffff,(%rsp)
call   1040 <substr_replace@plt>
mov    -0xc(%rbp),%ecx
lea    -0x8(%rbp),%rdi
lea    -0xc(%rbp),%rsi
lea    -0x10(%rbp),%rdx
xor    %r8d,%r8d
lea    0xe3d(%rip),%r9
movl   $0xffffffff,(%rsp)
call   1040 <substr_replace@plt>
lea    0xe24(%rip),%rax
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
and    $0x1,%al
movzbl %al,%edi
call   1070 <CU_ASSERT@plt>
mov    -0xc(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,%ecx
mov    -0x1c(%rbp),%eax
cmp    %ecx,%eax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1070 <CU_ASSERT@plt>
mov    -0x10(%rbp),%eax
cmp    -0xc(%rbp),%eax
setg   %al
and    $0x1,%al
movzbl %al,%edi
call   1070 <CU_ASSERT@plt>
mov    -0x8(%rbp),%rdi
call   1030 <free@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_substr_replace_multiple>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
movq   $0x0,0x10(%rsp)
movl   $0x0,0x8(%rsp)
movl   $0x0,0xc(%rsp)
movl   $0xffffffff,(%rsp)
lea    0xe7f(%rip),%r9
lea    0x10(%rsp),%rbx
lea    0x8(%rsp),%r14
lea    0xc(%rsp),%r15
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %r15,%rdx
xor    %ecx,%ecx
xor    %r8d,%r8d
call   1040 <substr_replace@plt>
mov    0x8(%rsp),%ecx
movl   $0xffffffff,(%rsp)
lea    0xe51(%rip),%r9
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %r15,%rdx
xor    %r8d,%r8d
call   1040 <substr_replace@plt>
mov    0x8(%rsp),%ecx
movl   $0xffffffff,(%rsp)
lea    0xe36(%rip),%r9
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %r15,%rdx
xor    %r8d,%r8d
call   1040 <substr_replace@plt>
mov    0x10(%rsp),%rdi
lea    0xe13(%rip),%rbx
mov    %rbx,%rsi
call   1060 <strcmp@plt>
xor    %edi,%edi
test   %eax,%eax
sete   %dil
call   1070 <CU_ASSERT@plt>
mov    0x8(%rsp),%ebp
mov    %rbx,%rdi
call   1050 <strlen@plt>
xor    %edi,%edi
cmp    %eax,%ebp
sete   %dil
call   1070 <CU_ASSERT@plt>
mov    0xc(%rsp),%eax
xor    %edi,%edi
cmp    0x8(%rsp),%eax
setg   %dil
call   1070 <CU_ASSERT@plt>
mov    0x10(%rsp),%rdi
call   1030 <free@plt>
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

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