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


x86 assembly (clang, -O0):
<test_substr_replace_alloc_and_insert_from_null_dest>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
lea    0xea1(%rip),%rax
mov    %rax,-0x8(%rbp)
movq   $0x0,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x8(%rbp),%r9
lea    -0x10(%rbp),%rdi
lea    -0x14(%rbp),%rsi
lea    -0x18(%rbp),%rdx
xor    %r8d,%r8d
mov    %r8d,%ecx
movl   $0xffffffff,(%rsp)
call   1040 <substr_replace@plt>
mov    -0x10(%rbp),%rdi
mov    -0x8(%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    -0x14(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x8(%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    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
setg   %al
and    $0x1,%al
movzbl %al,%edi
call   1070 <CU_ASSERT@plt>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_substr_replace_alloc_and_insert_from_null_dest>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
movq   $0x0,0x10(%rsp)
movl   $0x0,0x8(%rsp)
movl   $0x0,0xc(%rsp)
movl   $0xffffffff,(%rsp)
lea    0xe83(%rip),%rbx
lea    0x10(%rsp),%rdi
lea    0x8(%rsp),%rsi
lea    0xc(%rsp),%rdx
xor    %ecx,%ecx
xor    %r8d,%r8d
mov    %rbx,%r9
call   1040 <substr_replace@plt>
mov    0x10(%rsp),%rdi
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    %rbp
ret

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

LLVM IR (-O2):
@.str = [12 x i8] c"hello world\00"
define void @test_substr_replace_alloc_and_insert_from_null_dest() {
%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 ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 -1)
%8 = load i8*, i8** %1
%9 = call i32 @strcmp(i8* dereferenceable(1) %8, i8* dereferenceable(12) getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%10 = icmp eq i32 %9, 0
%11 = zext i1 %10 to i32
%12 = call i32 @CU_ASSERT(i32 %11)
%13 = load i32, i32* %2
%14 = call i32 @strlen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%15 = icmp eq i32 %13, %14
%16 = zext i1 %15 to i32
%17 = call i32 @CU_ASSERT(i32 %16)
%18 = load i32, i32* %3
%19 = load i32, i32* %2
%20 = icmp sgt i32 %18, %19
%21 = zext i1 %20 to i32
%22 = call i32 @CU_ASSERT(i32 %21)
%23 = load i8*, i8** %1
%24 = call i32 @free(i8* %23)
ret void
}