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


x86 assembly (clang, -O0):
<StrUtil_copyNTString>:
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   1040 <strlen@plt>
mov    %eax,-0x28(%rbp)
mov    -0x28(%rbp),%eax
sub    $0x1,%eax
cmp    -0x1c(%rbp),%eax
jge    11f5 <StrUtil_copyNTString+0x85>
mov    $0x1,%edi
call   1080 <malloc@plt>
mov    -0x10(%rbp),%rcx
mov    %rax,(%rcx)
cmp    $0x0,%rax
jne    11e6 <StrUtil_copyNTString+0x76>
mov    $0xd,%edi
lea    0xe38(%rip),%rsi
mov    $0x2,%edx
lea    0xe56(%rip),%rcx
call   1050 <Error_write@plt>
movq   $0x0,-0x8(%rbp)
jmp    12ce <StrUtil_copyNTString+0x15e>
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
movb   $0x0,(%rax)
jmp    12c3 <StrUtil_copyNTString+0x153>
mov    -0x20(%rbp),%edi
add    $0x1,%edi
mov    $0x1,%esi
call   1070 <calloc@plt>
mov    -0x10(%rbp),%rcx
mov    %rax,(%rcx)
cmp    $0x0,%rax
jne    1240 <StrUtil_copyNTString+0xd0>
mov    $0xd,%edi
lea    0xdf2(%rip),%rsi
mov    $0x2,%edx
lea    0xdfb(%rip),%rcx
call   1050 <Error_write@plt>
movq   $0x0,-0x8(%rbp)
jmp    12ce <StrUtil_copyNTString+0x15e>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    -0x18(%rbp),%rsi
movslq -0x1c(%rbp),%rax
add    %rax,%rsi
mov    -0x20(%rbp),%edx
call   1060 <StrUtil_strncpy@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1030 <StrUtil_rmEndSpaces@plt>
mov    %eax,-0x24(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    -0x24(%rbp),%eax
add    $0x1,%eax
cltq
shl    $0x0,%rax
mov    %eax,%esi
call   1090 <realloc@plt>
mov    -0x10(%rbp),%rcx
mov    %rax,(%rcx)
cmp    $0x0,%rax
jne    12be <StrUtil_copyNTString+0x14e>
mov    $0xf,%edi
lea    0xd8d(%rip),%rsi
mov    $0x2,%edx
lea    0xd7e(%rip),%rcx
call   1050 <Error_write@plt>
movq   $0x0,-0x8(%rbp)
jmp    12ce <StrUtil_copyNTString+0x15e>
jmp    12c3 <StrUtil_copyNTString+0x153>
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_copyNTString>:
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   1040 <strlen@plt>
cmp    %ebp,%eax
jle    11e8 <StrUtil_copyNTString+0x78>
lea    0x1(%r14),%edi
mov    $0x1,%esi
call   1070 <calloc@plt>
mov    %rax,(%r15)
test   %rax,%rax
je     1202 <StrUtil_copyNTString+0x92>
movslq %ebp,%rcx
add    %rcx,%rbx
mov    %rax,%rdi
mov    %rbx,%rsi
mov    %r14d,%edx
call   1060 <StrUtil_strncpy@plt>
mov    (%r15),%rdi
call   1030 <StrUtil_rmEndSpaces@plt>
mov    (%r15),%rdi
lea    0x1(%rax),%esi
call   1090 <realloc@plt>
mov    %rax,(%r15)
test   %rax,%rax
jne    1231 <StrUtil_copyNTString+0xc1>
lea    0xe53(%rip),%rsi
lea    0xe49(%rip),%rcx
mov    $0xf,%edi
jmp    1225 <StrUtil_copyNTString+0xb5>
mov    $0x1,%edi
call   1080 <malloc@plt>
mov    %rax,(%r15)
test   %rax,%rax
je     1212 <StrUtil_copyNTString+0xa2>
movb   $0x0,(%rax)
mov    (%r15),%rax
jmp    1231 <StrUtil_copyNTString+0xc1>
lea    0xe0b(%rip),%rsi
lea    0xe19(%rip),%rcx
jmp    1220 <StrUtil_copyNTString+0xb0>
lea    0xde7(%rip),%rsi
lea    0xe0a(%rip),%rcx
mov    $0xd,%edi
mov    $0x2,%edx
call   1050 <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_copyNTSring\00"
@.str.1 = [3 x i8] c"to\00"
@.str.2 = [21 x i8] c"StrUtil_copyNTString\00"
@.str.3 = [4 x i8] c"*to\00"
@.str.4 = [20 x i8] c"StrUtil_copyNTtring\00"
define i8* @StrUtil_copyNTString(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
%11 = alloca i32
store i8** %0, i8*** %6
store i8* %1, i8** %7
store i32 %2, i32* %8
store i32 %3, i32* %9
%12 = load i8*, i8** %7
%13 = call i32 @strlen(i8* %12)
store i32 %13, i32* %11
%14 = load i32, i32* %11
%15 = sub nsw i32 %14, 1
%16 = load i32, i32* %8
%17 = icmp slt i32 %15, %16
br i1 %17, label %18, label %29
18: ; preds = %4
%19 = call i64 @malloc(i32 1)
%20 = inttoptr i64 %19 to i8*
%21 = load i8**, i8*** %6
store i8* %20, i8** %21
%22 = icmp eq i8* %20, null
br i1 %22, label %23, label %25
23: ; preds = %18
%24 = 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 %67
25: ; preds = %18
%26 = load i8**, i8*** %6
%27 = load i8*, i8** %26
%28 = getelementptr inbounds i8, i8* %27, i64 0
store i8 0, i8* %28
br label %64
29: ; preds = %4
%30 = load i32, i32* %9
%31 = add nsw i32 %30, 1
%32 = call i64 @calloc(i32 %31, i32 1)
%33 = inttoptr i64 %32 to i8*
%34 = load i8**, i8*** %6
store i8* %33, i8** %34
%35 = icmp eq i8* %33, null
br i1 %35, label %36, label %38
36: ; preds = %29
%37 = call i32 @Error_write(i32 13, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), i32 2, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0))
store i8* null, i8** %5
br label %67
38: ; preds = %29
%39 = load i8**, i8*** %6
%40 = load i8*, i8** %39
%41 = load i8*, i8** %7
%42 = load i32, i32* %8
%43 = sext i32 %42 to i64
%44 = getelementptr inbounds i8, i8* %41, i64 %43
%45 = load i32, i32* %9
%46 = call i32 @StrUtil_strncpy(i8* %40, i8* %44, i32 %45)
%47 = load i8**, i8*** %6
%48 = load i8*, i8** %47
%49 = call i32 @StrUtil_rmEndSpaces(i8* %48)
store i32 %49, i32* %10
%50 = load i8**, i8*** %6
%51 = load i8*, i8** %50
%52 = load i32, i32* %10
%53 = add nsw i32 %52, 1
%54 = sext i32 %53 to i64
%55 = mul i64 %54, 1
%56 = trunc i64 %55 to i32
%57 = call i64 @realloc(i8* %51, i32 %56)
%58 = inttoptr i64 %57 to i8*
%59 = load i8**, i8*** %6
store i8* %58, i8** %59
%60 = icmp eq i8* %58, null
br i1 %60, label %61, label %63
61: ; preds = %38
%62 = call i32 @Error_write(i32 15, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, 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 %67
63: ; preds = %38
br label %64
64: ; preds = %63, %25
%65 = load i8**, i8*** %6
%66 = load i8*, i8** %65
store i8* %66, i8** %5
br label %67
67: ; preds = %64, %61, %36, %23
%68 = load i8*, i8** %5
ret i8* %68
}

LLVM IR (-O2):
@.str = [20 x i8] c"StrUtil_copyNTSring\00"
@.str.1 = [3 x i8] c"to\00"
@.str.2 = [21 x i8] c"StrUtil_copyNTString\00"
@.str.3 = [4 x i8] c"*to\00"
@.str.4 = [20 x i8] c"StrUtil_copyNTtring\00"
define i8* @StrUtil_copyNTString(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 @malloc(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 %35
13: ; preds = %7
store i8 0, i8* %9
%14 = load i8*, i8** %0
br label %35
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 ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), i32 2, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0))
br label %35
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 @StrUtil_rmEndSpaces(i8* %26)
%28 = load i8*, i8** %0
%29 = add nsw i32 %27, 1
%30 = tail call i64 @realloc(i8* %28, i32 %29)
%31 = inttoptr i64 %30 to i8*
store i8* %31, i8** %0
%32 = icmp eq i64 %30, 0
br i1 %32, label %33, label %35
33: ; preds = %22
%34 = tail call i32 @Error_write(i32 15, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0), i32 2, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
br label %35
35: ; preds = %13, %22, %33, %20, %11
%36 = phi i8* [ null, %11 ], [ null, %20 ], [ null, %33 ], [ %31, %22 ], [ %14, %13 ]
ret i8* %36
}