#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*/ strcmp(char *, char *);
char *strdup(char *);
int strlen(char *);
int /*<<< orphan*/ substr_replace(char **, int *, int *, int, int, char *,
                                  int /*<<< orphan*/);
void test_substr_replace_remove(void) {
  char *source = strdup("hello world");
  int len = strlen(source);
  int size = len + 1;
  substr_replace(&source, &len, &size, 5, len, "", 0);
  CU_ASSERT(!strcmp(source, "hello"));
}


x86 assembly (clang, -O0):
<test_substr_replace_remove>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
lea    0xea1(%rip),%rdi
call   1070 <strdup@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
mov    -0xc(%rbp),%r8d
lea    -0x8(%rbp),%rdi
lea    -0xc(%rbp),%rsi
lea    -0x10(%rbp),%rdx
mov    $0x5,%ecx
lea    0xe72(%rip),%r9
xor    %eax,%eax
movl   $0x0,(%rsp)
call   1030 <substr_replace@plt>
mov    -0x8(%rbp),%rdi
lea    0xe5a(%rip),%rsi
call   1050 <strcmp@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
and    $0x1,%al
movzbl %al,%edi
call   1060 <CU_ASSERT@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_substr_replace_remove>:
sub    $0x18,%rsp
lea    0xea5(%rip),%rdi
call   1070 <strdup@plt>
mov    %rax,0x10(%rsp)
mov    %rax,%rdi
call   1040 <strlen@plt>
mov    %eax,0xc(%rsp)
lea    0x1(%rax),%ecx
mov    %ecx,0x8(%rsp)
movl   $0x0,(%rsp)
lea    0xe85(%rip),%r9
lea    0x10(%rsp),%rdi
lea    0xc(%rsp),%rsi
lea    0x8(%rsp),%rdx
mov    $0x5,%ecx
mov    %eax,%r8d
call   1030 <substr_replace@plt>
mov    0x10(%rsp),%rdi
lea    0xe5e(%rip),%rsi
call   1050 <strcmp@plt>
xor    %edi,%edi
test   %eax,%eax
sete   %dil
call   1060 <CU_ASSERT@plt>
add    $0x18,%rsp
ret

LLVM IR (-O0):
@.str = [12 x i8] c"hello world\00"
@.str.1 = [1 x i8] zeroinitializer
@.str.2 = [6 x i8] c"hello\00"
define void @test_substr_replace_remove() {
%1 = alloca i8*
%2 = alloca i32
%3 = alloca i32
%4 = call i8* @strdup(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
store i8* %4, i8** %1
%5 = load i8*, i8** %1
%6 = call i32 @strlen(i8* %5)
store i32 %6, i32* %2
%7 = load i32, i32* %2
%8 = add nsw i32 %7, 1
store i32 %8, i32* %3
%9 = load i32, i32* %2
%10 = call i32 @substr_replace(i8** %1, i32* %2, i32* %3, i32 5, i32 %9, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.1, i64 0, i64 0), i32 0)
%11 = load i8*, i8** %1
%12 = call i32 @strcmp(i8* %11, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%13 = icmp ne i32 %12, 0
%14 = xor i1 %13, true
%15 = zext i1 %14 to i32
%16 = call i32 @CU_ASSERT(i32 %15)
ret void
}

LLVM IR (-O2):
@.str = [12 x i8] c"hello world\00"
@.str.1 = [1 x i8] zeroinitializer
@.str.2 = [6 x i8] c"hello\00"
define void @test_substr_replace_remove() {
%1 = alloca i8*
%2 = alloca i32
%3 = alloca i32
%4 = bitcast i8** %1 to i8*
%5 = tail call dereferenceable_or_null(12) i8* @strdup(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
store i8* %5, i8** %1
%6 = bitcast i32* %2 to i8*
%7 = tail call i32 @strlen(i8* %5)
store i32 %7, i32* %2
%8 = bitcast i32* %3 to i8*
%9 = add nsw i32 %7, 1
store i32 %9, i32* %3
%10 = call i32 @substr_replace(i8** %1, i32* %2, i32* %3, i32 5, i32 %7, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.1, i64 0, i64 0), i32 0)
%11 = load i8*, i8** %1
%12 = call i32 @strcmp(i8* dereferenceable(1) %11, i8* dereferenceable(6) getelementptr inbounds ([6 x i8], [6 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)
ret void
}