#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 */
char *malloc(int);
int strlen(char const *);
char *StringTools_createString(const char *string) {
  int length = strlen(string);
  char *newString = malloc(sizeof(char) * (length + 1));
  for (int k = 0; k < length; k++) {
    newString[k] = string[k];
  }
  newString[length] = '\0';
  return newString;
}


x86 assembly (clang, -O0):
<StringTools_createString>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%eax
add    $0x1,%eax
cltq
shl    $0x0,%rax
mov    %eax,%edi
call   1040 <malloc@plt>
mov    %rax,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    1186 <StringTools_createString+0x66>
mov    -0x8(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    (%rax,%rcx,1),%dl
mov    -0x18(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1156 <StringTools_createString+0x36>
mov    -0x18(%rbp),%rax
movslq -0xc(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<StringTools_createString>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%r14
call   1030 <strlen@plt>
mov    %eax,%ebx
lea    0x1(%rbx),%edi
call   1050 <malloc@plt>
mov    %rax,%r15
test   %ebx,%ebx
jle    115b <StringTools_createString+0x2b>
mov    %ebx,%edx
mov    %r15,%rdi
mov    %r14,%rsi
call   1040 <memcpy@plt>
movslq %ebx,%rax
movb   $0x0,(%r15,%rax,1)
mov    %r15,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i8* @StringTools_createString(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
store i8* %0, i8** %2
%6 = load i8*, i8** %2
%7 = call i32 @strlen(i8* %6)
store i32 %7, i32* %3
%8 = load i32, i32* %3
%9 = add nsw i32 %8, 1
%10 = sext i32 %9 to i64
%11 = mul i64 1, %10
%12 = trunc i64 %11 to i32
%13 = call i8* @malloc(i32 %12)
store i8* %13, i8** %4
store i32 0, i32* %5
br label %14
14: ; preds = %28, %1
%15 = load i32, i32* %5
%16 = load i32, i32* %3
%17 = icmp slt i32 %15, %16
br i1 %17, label %18, label %31
18: ; preds = %14
%19 = load i8*, i8** %2
%20 = load i32, i32* %5
%21 = sext i32 %20 to i64
%22 = getelementptr inbounds i8, i8* %19, i64 %21
%23 = load i8, i8* %22
%24 = load i8*, i8** %4
%25 = load i32, i32* %5
%26 = sext i32 %25 to i64
%27 = getelementptr inbounds i8, i8* %24, i64 %26
store i8 %23, i8* %27
br label %28
28: ; preds = %18
%29 = load i32, i32* %5
%30 = add nsw i32 %29, 1
store i32 %30, i32* %5
br label %14
31: ; preds = %14
%32 = load i8*, i8** %4
%33 = load i32, i32* %3
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds i8, i8* %32, i64 %34
store i8 0, i8* %35
%36 = load i8*, i8** %4
ret i8* %36
}

LLVM IR (-O2):
define noalias i8* @StringTools_createString(i8* %0) {
%2 = tail call i32 @strlen(i8* %0)
%3 = add nsw i32 %2, 1
%4 = tail call i8* @malloc(i32 %3)
%5 = icmp sgt i32 %2, 0
br i1 %5, label %6, label %8
6: ; preds = %1
%7 = zext i32 %2 to i64
br label %8
8: ; preds = %6, %1
%9 = sext i32 %2 to i64
%10 = getelementptr inbounds i8, i8* %4, i64 %9
store i8 0, i8* %10
ret i8* %4
}