#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 */
scalar_t__ malloc(int);
int /*<<< orphan*/ perror(char *);
int strlen(char const *);
int /*<<< orphan*/ strncpy(char *, char const *, int);
char *createStringClone(const char *str) {
  if (str == NULL)
    return NULL;
  int strLen = strlen(str);
  char *strClone = (char *)malloc((strLen + 1) * sizeof(char));
  if (strClone == NULL) {
    perror("String clone failed");
    return NULL;
  }
  strncpy(strClone, str, strLen);
  strClone[strLen] = '\0';
  return strClone;
}


x86 assembly (clang, -O0):
<createStringClone>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1164 <createStringClone+0x24>
movq   $0x0,-0x8(%rbp)
jmp    11cf <createStringClone+0x8f>
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
add    $0x1,%eax
cltq
shl    $0x0,%rax
mov    %eax,%edi
call   1050 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11ab <createStringClone+0x6b>
lea    0xe67(%rip),%rdi
call   1060 <perror@plt>
movq   $0x0,-0x8(%rbp)
jmp    11cf <createStringClone+0x8f>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1030 <strncpy@plt>
mov    -0x20(%rbp),%rax
movslq -0x14(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<createStringClone>:
push   %r15
push   %r14
push   %rbx
test   %rdi,%rdi
je     1189 <createStringClone+0x49>
mov    %rdi,%r14
call   1040 <strlen@plt>
mov    %eax,%r15d
lea    0x1(%r15),%edi
call   1050 <malloc@plt>
test   %rax,%rax
je     117d <createStringClone+0x3d>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r14,%rsi
mov    %r15d,%edx
call   1030 <strncpy@plt>
movslq %r15d,%rax
movb   $0x0,(%rbx,%rax,1)
jmp    118b <createStringClone+0x4b>
lea    0xe7c(%rip),%rdi
call   1060 <perror@plt>
xor    %ebx,%ebx
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [20 x i8] c"String clone failed\00"
define i8* @createStringClone(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i8*
store i8* %0, i8** %3
%6 = load i8*, i8** %3
%7 = icmp eq i8* %6, null
br i1 %7, label %8, label %9
8: ; preds = %1
store i8* null, i8** %2
br label %33
9: ; preds = %1
%10 = load i8*, i8** %3
%11 = call i32 @strlen(i8* %10)
store i32 %11, i32* %4
%12 = load i32, i32* %4
%13 = add nsw i32 %12, 1
%14 = sext i32 %13 to i64
%15 = mul i64 %14, 1
%16 = trunc i64 %15 to i32
%17 = call i64 @malloc(i32 %16)
%18 = inttoptr i64 %17 to i8*
store i8* %18, i8** %5
%19 = load i8*, i8** %5
%20 = icmp eq i8* %19, null
br i1 %20, label %21, label %23
21: ; preds = %9
%22 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
store i8* null, i8** %2
br label %33
23: ; preds = %9
%24 = load i8*, i8** %5
%25 = load i8*, i8** %3
%26 = load i32, i32* %4
%27 = call i32 @strncpy(i8* %24, i8* %25, i32 %26)
%28 = load i8*, i8** %5
%29 = load i32, i32* %4
%30 = sext i32 %29 to i64
%31 = getelementptr inbounds i8, i8* %28, i64 %30
store i8 0, i8* %31
%32 = load i8*, i8** %5
store i8* %32, i8** %2
br label %33
33: ; preds = %23, %21, %8
%34 = load i8*, i8** %2
ret i8* %34
}

LLVM IR (-O2):
@.str = [20 x i8] c"String clone failed\00"
define i8* @createStringClone(i8* %0) {
%2 = icmp eq i8* %0, null
br i1 %2, label %15, label %3
3: ; preds = %1
%4 = tail call i32 @strlen(i8* %0)
%5 = add nsw i32 %4, 1
%6 = tail call i64 @malloc(i32 %5)
%7 = inttoptr i64 %6 to i8*
%8 = icmp eq i64 %6, 0
br i1 %8, label %9, label %11
9: ; preds = %3
%10 = tail call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
br label %15
11: ; preds = %3
%12 = tail call i32 @strncpy(i8* %7, i8* %0, i32 %4)
%13 = sext i32 %4 to i64
%14 = getelementptr inbounds i8, i8* %7, i64 %13
store i8 0, i8* %14
br label %15
15: ; preds = %9, %11, %1
%16 = phi i8* [ null, %1 ], [ null, %9 ], [ %7, %11 ]
ret i8* %16
}