#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*/ Error_write(int, char *, int, char *);
int /*<<< orphan*/ StrUtil_strncpy(char *, char *, int);
scalar_t__ calloc(int, int);
scalar_t__ realloc(char *, int);
int strlen(char *);
char *StrUtil_copyNString(char **to, char *from, int Start, int length) {
  int lenFrom = strlen(from);
  if (lenFrom - 1 < Start) {
    if ((*to = (char *)calloc(1, sizeof(char))) == NULL) {
      Error_write(13, "StrUtil_copyNString", 2, "to");
      return NULL;
    }
    (*to)[0] = '\0';
  } else {
    if ((*to = (char *)calloc(length + 1, sizeof(char))) == NULL) {
      Error_write(13, "StrUtil_copyNString", 2, "*to");
      return NULL;
    }
    StrUtil_strncpy(*to, &(from[Start]), length);
    if ((*to = (char *)realloc(*to, (strlen(*to) + 1) * sizeof(char))) ==
        NULL) {
      Error_write(13, "StrUtil_copyNString", 2, "to");
      return NULL;
    }
  }
  return *to;
}


x86 assembly (clang, -O0):
<StrUtil_copyNString>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    %ecx,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
sub    $0x1,%eax
cmp    -0x1c(%rbp),%eax
jge    11d7 <StrUtil_copyNString+0x87>
mov    $0x1,%esi
mov    %esi,%edi
call   1060 <calloc@plt>
mov    -0x10(%rbp),%rcx
mov    %rax,(%rcx)
cmp    $0x0,%rax
jne    11c8 <StrUtil_copyNString+0x78>
mov    $0xd,%edi
lea    0xe56(%rip),%rsi
mov    $0x2,%edx
lea    0xe5f(%rip),%rcx
call   1040 <Error_write@plt>
movq   $0x0,-0x8(%rbp)
jmp    12b2 <StrUtil_copyNString+0x162>
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
movb   $0x0,(%rax)
jmp    12a7 <StrUtil_copyNString+0x157>
mov    -0x20(%rbp),%edi
add    $0x1,%edi
mov    $0x1,%esi
call   1060 <calloc@plt>
mov    -0x10(%rbp),%rcx
mov    %rax,(%rcx)
cmp    $0x0,%rax
jne    1222 <StrUtil_copyNString+0xd2>
mov    $0xd,%edi
lea    0xdfc(%rip),%rsi
mov    $0x2,%edx
lea    0xe04(%rip),%rcx
call   1040 <Error_write@plt>
movq   $0x0,-0x8(%rbp)
jmp    12b2 <StrUtil_copyNString+0x162>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    -0x18(%rbp),%rsi
movslq -0x1c(%rbp),%rax
add    %rax,%rsi
mov    -0x20(%rbp),%edx
call   1050 <StrUtil_strncpy@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1030 <strlen@plt>
mov    -0x30(%rbp),%rdi
add    $0x1,%eax
cltq
shl    $0x0,%rax
mov    %eax,%esi
call   1070 <realloc@plt>
mov    -0x10(%rbp),%rcx
mov    %rax,(%rcx)
cmp    $0x0,%rax
jne    12a2 <StrUtil_copyNString+0x152>
mov    $0xd,%edi
lea    0xd7c(%rip),%rsi
mov    $0x2,%edx
lea    0xd85(%rip),%rcx
call   1040 <Error_write@plt>
movq   $0x0,-0x8(%rbp)
jmp    12b2 <StrUtil_copyNString+0x162>
jmp    12a7 <StrUtil_copyNString+0x157>
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<StrUtil_copyNString>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %ecx,%r14d
mov    %edx,%ebp
mov    %rsi,%rbx
mov    %rdi,%r15
mov    %rsi,%rdi
call   1030 <strlen@plt>
cmp    %ebp,%eax
jle    11b8 <StrUtil_copyNString+0x68>
lea    0x1(%r14),%edi
mov    $0x1,%esi
call   1060 <calloc@plt>
mov    %rax,(%r15)
test   %rax,%rax
je     11e7 <StrUtil_copyNString+0x97>
movslq %ebp,%rcx
add    %rcx,%rbx
mov    %rax,%rdi
mov    %rbx,%rsi
mov    %r14d,%edx
call   1050 <StrUtil_strncpy@plt>
mov    (%r15),%rbx
mov    %rbx,%rdi
call   1030 <strlen@plt>
lea    0x1(%rax),%esi
mov    %rbx,%rdi
call   1070 <realloc@plt>
mov    %rax,(%r15)
test   %rax,%rax
jne    1206 <StrUtil_copyNString+0xb6>
jmp    11d7 <StrUtil_copyNString+0x87>
mov    $0x1,%edi
mov    $0x1,%esi
call   1060 <calloc@plt>
mov    %rax,(%r15)
test   %rax,%rax
je     11d7 <StrUtil_copyNString+0x87>
movb   $0x0,(%rax)
mov    (%r15),%rax
jmp    1206 <StrUtil_copyNString+0xb6>
lea    0xe22(%rip),%rsi
lea    0xe30(%rip),%rcx
jmp    11f5 <StrUtil_copyNString+0xa5>
lea    0xe12(%rip),%rsi
lea    0xe1f(%rip),%rcx
mov    $0xd,%edi
mov    $0x2,%edx
call   1040 <Error_write@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [20 x i8] c"StrUtil_copyNString\00"
@.str.1 = [3 x i8] c"to\00"
@.str.2 = [4 x i8] c"*to\00"
define i8* @StrUtil_copyNString(i8** %0, i8* %1, i32 %2, i32 %3) {
%5 = alloca i8*
%6 = alloca i8**
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i8** %0, i8*** %6
store i8* %1, i8** %7
store i32 %2, i32* %8
store i32 %3, i32* %9
%11 = load i8*, i8** %7
%12 = call i32 @strlen(i8* %11)
store i32 %12, i32* %10
%13 = load i32, i32* %10
%14 = sub nsw i32 %13, 1
%15 = load i32, i32* %8
%16 = icmp slt i32 %14, %15
br i1 %16, label %17, label %28
17: ; preds = %4
%18 = call i64 @calloc(i32 1, i32 1)
%19 = inttoptr i64 %18 to i8*
%20 = load i8**, i8*** %6
store i8* %19, i8** %20
%21 = icmp eq i8* %19, null
br i1 %21, label %22, label %24
22: ; preds = %17
%23 = call i32 @Error_write(i32 13, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i32 2, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i8* null, i8** %5
br label %65
24: ; preds = %17
%25 = load i8**, i8*** %6
%26 = load i8*, i8** %25
%27 = getelementptr inbounds i8, i8* %26, i64 0
store i8 0, i8* %27
br label %62
28: ; preds = %4
%29 = load i32, i32* %9
%30 = add nsw i32 %29, 1
%31 = call i64 @calloc(i32 %30, i32 1)
%32 = inttoptr i64 %31 to i8*
%33 = load i8**, i8*** %6
store i8* %32, i8** %33
%34 = icmp eq i8* %32, null
br i1 %34, label %35, label %37
35: ; preds = %28
%36 = call i32 @Error_write(i32 13, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i32 2, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0))
store i8* null, i8** %5
br label %65
37: ; preds = %28
%38 = load i8**, i8*** %6
%39 = load i8*, i8** %38
%40 = load i8*, i8** %7
%41 = load i32, i32* %8
%42 = sext i32 %41 to i64
%43 = getelementptr inbounds i8, i8* %40, i64 %42
%44 = load i32, i32* %9
%45 = call i32 @StrUtil_strncpy(i8* %39, i8* %43, i32 %44)
%46 = load i8**, i8*** %6
%47 = load i8*, i8** %46
%48 = load i8**, i8*** %6
%49 = load i8*, i8** %48
%50 = call i32 @strlen(i8* %49)
%51 = add nsw i32 %50, 1
%52 = sext i32 %51 to i64
%53 = mul i64 %52, 1
%54 = trunc i64 %53 to i32
%55 = call i64 @realloc(i8* %47, i32 %54)
%56 = inttoptr i64 %55 to i8*
%57 = load i8**, i8*** %6
store i8* %56, i8** %57
%58 = icmp eq i8* %56, null
br i1 %58, label %59, label %61
59: ; preds = %37
%60 = call i32 @Error_write(i32 13, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i32 2, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i8* null, i8** %5
br label %65
61: ; preds = %37
br label %62
62: ; preds = %61, %24
%63 = load i8**, i8*** %6
%64 = load i8*, i8** %63
store i8* %64, i8** %5
br label %65
65: ; preds = %62, %59, %35, %22
%66 = load i8*, i8** %5
ret i8* %66
}

LLVM IR (-O2):
@.str = [20 x i8] c"StrUtil_copyNString\00"
@.str.1 = [3 x i8] c"to\00"
@.str.2 = [4 x i8] c"*to\00"
define i8* @StrUtil_copyNString(i8** nocapture %0, i8* %1, i32 %2, i32 %3) {
%5 = tail call i32 @strlen(i8* %1)
%6 = icmp sgt i32 %5, %2
br i1 %6, label %15, label %7
7: ; preds = %4
%8 = tail call i64 @calloc(i32 1, i32 1)
%9 = inttoptr i64 %8 to i8*
store i8* %9, i8** %0
%10 = icmp eq i64 %8, 0
br i1 %10, label %11, label %13
11: ; preds = %7
%12 = tail call i32 @Error_write(i32 13, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i32 2, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
br label %34
13: ; preds = %7
store i8 0, i8* %9
%14 = load i8*, i8** %0
br label %34
15: ; preds = %4
%16 = add nsw i32 %3, 1
%17 = tail call i64 @calloc(i32 %16, i32 1)
%18 = inttoptr i64 %17 to i8*
store i8* %18, i8** %0
%19 = icmp eq i64 %17, 0
br i1 %19, label %20, label %22
20: ; preds = %15
%21 = tail call i32 @Error_write(i32 13, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i32 2, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0))
br label %34
22: ; preds = %15
%23 = sext i32 %2 to i64
%24 = getelementptr inbounds i8, i8* %1, i64 %23
%25 = tail call i32 @StrUtil_strncpy(i8* %18, i8* %24, i32 %3)
%26 = load i8*, i8** %0
%27 = tail call i32 @strlen(i8* %26)
%28 = add nsw i32 %27, 1
%29 = tail call i64 @realloc(i8* %26, i32 %28)
%30 = inttoptr i64 %29 to i8*
store i8* %30, i8** %0
%31 = icmp eq i64 %29, 0
br i1 %31, label %32, label %34
32: ; preds = %22
%33 = tail call i32 @Error_write(i32 13, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i32 2, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
br label %34
34: ; preds = %13, %22, %32, %20, %11
%35 = phi i8* [ null, %11 ], [ null, %20 ], [ null, %32 ], [ %30, %22 ], [ %14, %13 ]
ret i8* %35
}