#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*/ memcpy(char *, char const *, int);
int strlen(char const *);
int createStringCopy(const char *source, int sourceLen, char **target) {
  char *stringCopy;
  if (sourceLen == -1)
    sourceLen = strlen(source);
  stringCopy = (char *)malloc(sourceLen + 1);
  if (stringCopy == NULL)
    return -1;
  memcpy(stringCopy, source, sourceLen);
  stringCopy[sourceLen] = '\0';
  *target = stringCopy;
  return 0;
}


x86 assembly (clang, -O0):
<createStringCopy>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    1159 <createStringCopy+0x29>
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%edi
add    $0x1,%edi
call   1050 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    117f <createStringCopy+0x4f>
movl   $0xffffffff,-0x4(%rbp)
jmp    11ad <createStringCopy+0x7d>
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1040 <memcpy@plt>
mov    -0x28(%rbp),%rax
movslq -0x14(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x28(%rbp),%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<createStringCopy>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %esi,%r12d
mov    %rdi,%r15
cmp    $0xffffffff,%esi
jne    1151 <createStringCopy+0x21>
mov    %r15,%rdi
call   1030 <strlen@plt>
mov    %eax,%r12d
lea    0x1(%r12),%edi
call   1050 <malloc@plt>
test   %rax,%rax
je     117f <createStringCopy+0x4f>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r15,%rsi
mov    %r12d,%edx
call   1040 <memcpy@plt>
movslq %r12d,%rax
movb   $0x0,(%rbx,%rax,1)
mov    %rbx,(%r14)
xor    %eax,%eax
jmp    1184 <createStringCopy+0x54>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
define i32 @createStringCopy(i8* %0, i32 %1, i8** nocapture writeonly %2) {
%4 = icmp eq i32 %1, -1
br i1 %4, label %5, label %7
5: ; preds = %3
%6 = tail call i32 @strlen(i8* %0)
br label %7
7: ; preds = %5, %3
%8 = phi i32 [ %6, %5 ], [ %1, %3 ]
%9 = add nsw i32 %8, 1
%10 = tail call i64 @malloc(i32 %9)
%11 = inttoptr i64 %10 to i8*
%12 = icmp eq i64 %10, 0
br i1 %12, label %17, label %13
13: ; preds = %7
%14 = tail call i32 @memcpy(i8* %11, i8* %0, i32 %8)
%15 = sext i32 %8 to i64
%16 = getelementptr inbounds i8, i8* %11, i64 %15
store i8 0, i8* %16
store i8* %11, i8** %2
br label %17
17: ; preds = %7, %13
%18 = phi i32 [ 0, %13 ], [ -1, %7 ]
ret i32 %18
}