#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*/ assert(int);
scalar_t__ realloc(char *, size_t);
size_t strlen(char *);
int /*<<< orphan*/ strncpy(char *, char *, size_t);
void __strCopySub(char **destPtr, char *src, size_t index, size_t len) {
  assert(destPtr != NULL);
  assert(*destPtr != NULL);
  assert(src != NULL);
  assert(index >= 0 && index <= strlen(src));
  assert(len >= 0 && len <= strlen(src) - index);
  *destPtr = (char *)realloc(*destPtr, len + 1);
  strncpy(*destPtr, &src[index], len);
  (*destPtr)[len] = 0;
}


x86 assembly (clang, -O0):
<__strCopySub>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
cmpq   $0x0,-0x8(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
mov    -0x8(%rbp),%rax
cmpq   $0x0,(%rax)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
cmpq   $0x0,-0x10(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
xor    %eax,%eax
cmpq   $0x0,-0x18(%rbp)
mov    %al,-0x21(%rbp)
jb     11c2 <__strCopySub+0x82>
mov    -0x18(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,%rcx
mov    -0x30(%rbp),%rax
cmp    %rcx,%rax
setbe  %al
mov    %al,-0x21(%rbp)
mov    -0x21(%rbp),%al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
xor    %eax,%eax
cmpq   $0x0,-0x20(%rbp)
mov    %al,-0x31(%rbp)
jb     1204 <__strCopySub+0xc4>
mov    -0x20(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,%rcx
mov    -0x40(%rbp),%rax
sub    -0x18(%rbp),%rcx
cmp    %rcx,%rax
setbe  %al
mov    %al,-0x31(%rbp)
mov    -0x31(%rbp),%al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
mov    -0x20(%rbp),%rsi
add    $0x1,%rsi
call   1060 <realloc@plt>
mov    %rax,%rcx
mov    -0x8(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
mov    -0x10(%rbp),%rsi
add    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1030 <strncpy@plt>
mov    -0x8(%rbp),%rax
mov    (%rax),%rax
mov    -0x20(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<__strCopySub>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rcx,%r14
mov    %rdx,%r12
mov    %rsi,%rbx
mov    %rdi,%r15
xor    %edi,%edi
test   %r15,%r15
setne  %dil
call   1050 <assert@plt>
xor    %edi,%edi
cmpq   $0x0,(%r15)
setne  %dil
call   1050 <assert@plt>
xor    %edi,%edi
test   %rbx,%rbx
setne  %dil
call   1050 <assert@plt>
mov    %rbx,%rdi
call   1040 <strlen@plt>
xor    %edi,%edi
cmp    %r12,%rax
setae  %dil
call   1050 <assert@plt>
mov    %rbx,%rdi
call   1040 <strlen@plt>
sub    %r12,%rax
xor    %edi,%edi
cmp    %r14,%rax
setae  %dil
call   1050 <assert@plt>
mov    (%r15),%rdi
lea    0x1(%r14),%rsi
call   1060 <realloc@plt>
mov    %rax,(%r15)
add    %r12,%rbx
mov    %rax,%rdi
mov    %rbx,%rsi
mov    %r14,%rdx
call   1030 <strncpy@plt>
mov    (%r15),%rax
movb   $0x0,(%rax,%r14,1)
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define void @__strCopySub(i8** %0, i8* %1, i64 %2, i64 %3) {
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i64
store i8** %0, i8*** %5
store i8* %1, i8** %6
store i64 %2, i64* %7
store i64 %3, i64* %8
%9 = load i8**, i8*** %5
%10 = icmp ne i8** %9, null
%11 = zext i1 %10 to i32
%12 = call i32 @assert(i32 %11)
%13 = load i8**, i8*** %5
%14 = load i8*, i8** %13
%15 = icmp ne i8* %14, null
%16 = zext i1 %15 to i32
%17 = call i32 @assert(i32 %16)
%18 = load i8*, i8** %6
%19 = icmp ne i8* %18, null
%20 = zext i1 %19 to i32
%21 = call i32 @assert(i32 %20)
%22 = load i64, i64* %7
%23 = icmp uge i64 %22, 0
br i1 %23, label %24, label %29
24: ; preds = %4
%25 = load i64, i64* %7
%26 = load i8*, i8** %6
%27 = call i64 @strlen(i8* %26)
%28 = icmp ule i64 %25, %27
br label %29
29: ; preds = %24, %4
%30 = phi i1 [ false, %4 ], [ %28, %24 ]
%31 = zext i1 %30 to i32
%32 = call i32 @assert(i32 %31)
%33 = load i64, i64* %8
%34 = icmp uge i64 %33, 0
br i1 %34, label %35, label %42
35: ; preds = %29
%36 = load i64, i64* %8
%37 = load i8*, i8** %6
%38 = call i64 @strlen(i8* %37)
%39 = load i64, i64* %7
%40 = sub i64 %38, %39
%41 = icmp ule i64 %36, %40
br label %42
42: ; preds = %35, %29
%43 = phi i1 [ false, %29 ], [ %41, %35 ]
%44 = zext i1 %43 to i32
%45 = call i32 @assert(i32 %44)
%46 = load i8**, i8*** %5
%47 = load i8*, i8** %46
%48 = load i64, i64* %8
%49 = add i64 %48, 1
%50 = call i64 @realloc(i8* %47, i64 %49)
%51 = inttoptr i64 %50 to i8*
%52 = load i8**, i8*** %5
store i8* %51, i8** %52
%53 = load i8**, i8*** %5
%54 = load i8*, i8** %53
%55 = load i8*, i8** %6
%56 = load i64, i64* %7
%57 = getelementptr inbounds i8, i8* %55, i64 %56
%58 = load i64, i64* %8
%59 = call i32 @strncpy(i8* %54, i8* %57, i64 %58)
%60 = load i8**, i8*** %5
%61 = load i8*, i8** %60
%62 = load i64, i64* %8
%63 = getelementptr inbounds i8, i8* %61, i64 %62
store i8 0, i8* %63
ret void
}

LLVM IR (-O2):
define void @__strCopySub(i8** %0, i8* %1, i64 %2, i64 %3) {
%5 = icmp ne i8** %0, null
%6 = zext i1 %5 to i32
%7 = tail call i32 @assert(i32 %6)
%8 = load i8*, i8** %0
%9 = icmp ne i8* %8, null
%10 = zext i1 %9 to i32
%11 = tail call i32 @assert(i32 %10)
%12 = icmp ne i8* %1, null
%13 = zext i1 %12 to i32
%14 = tail call i32 @assert(i32 %13)
%15 = tail call i64 @strlen(i8* dereferenceable(1) %1)
%16 = icmp uge i64 %15, %2
%17 = zext i1 %16 to i32
%18 = tail call i32 @assert(i32 %17)
%19 = tail call i64 @strlen(i8* dereferenceable(1) %1)
%20 = sub i64 %19, %2
%21 = icmp uge i64 %20, %3
%22 = zext i1 %21 to i32
%23 = tail call i32 @assert(i32 %22)
%24 = load i8*, i8** %0
%25 = add i64 %3, 1
%26 = tail call i64 @realloc(i8* %24, i64 %25)
%27 = inttoptr i64 %26 to i8*
store i8* %27, i8** %0
%28 = getelementptr inbounds i8, i8* %1, i64 %2
%29 = tail call i32 @strncpy(i8* %27, i8* %28, i64 %3)
%30 = load i8*, i8** %0
%31 = getelementptr inbounds i8, i8* %30, i64 %3
store i8 0, i8* %31
ret void
}