#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 strlen(char const *);
int /*<<< orphan*/ strncpy(char *, char const *, size_t);
scalar_t__ vktrace_malloc(int);
char *vktrace_allocate_and_copy(const char *_src) {
  if (_src == NULL) {
    return NULL;
  } else {
    size_t bufferSize = 1 + strlen(_src);
    char *retVal = (char *)vktrace_malloc(sizeof(char) * bufferSize);
    strncpy(retVal, _src, bufferSize);
    return retVal;
  }
}


x86 assembly (clang, -O0):
<vktrace_allocate_and_copy>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1154 <vktrace_allocate_and_copy+0x24>
movq   $0x0,-0x8(%rbp)
jmp    1192 <vktrace_allocate_and_copy+0x62>
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
add    $0x1,%eax
cltq
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
shl    $0x0,%rax
mov    %eax,%edi
call   1030 <vktrace_malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1040 <strncpy@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<vktrace_allocate_and_copy>:
push   %r15
push   %r14
push   %rbx
test   %rdi,%rdi
je     1163 <vktrace_allocate_and_copy+0x33>
mov    %rdi,%rbx
call   1050 <strlen@plt>
add    $0x1,%eax
movslq %eax,%r15
mov    %r15d,%edi
call   1030 <vktrace_malloc@plt>
mov    %rax,%r14
mov    %rax,%rdi
mov    %rbx,%rsi
mov    %r15,%rdx
call   1040 <strncpy@plt>
jmp    1166 <vktrace_allocate_and_copy+0x36>
xor    %r14d,%r14d
mov    %r14,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i8* @vktrace_allocate_and_copy(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i64
%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 %24
9: ; preds = %1
%10 = load i8*, i8** %3
%11 = call i32 @strlen(i8* %10)
%12 = add nsw i32 1, %11
%13 = sext i32 %12 to i64
store i64 %13, i64* %4
%14 = load i64, i64* %4
%15 = mul i64 1, %14
%16 = trunc i64 %15 to i32
%17 = call i64 @vktrace_malloc(i32 %16)
%18 = inttoptr i64 %17 to i8*
store i8* %18, i8** %5
%19 = load i8*, i8** %5
%20 = load i8*, i8** %3
%21 = load i64, i64* %4
%22 = call i32 @strncpy(i8* %19, i8* %20, i64 %21)
%23 = load i8*, i8** %5
store i8* %23, i8** %2
br label %24
24: ; preds = %9, %8
%25 = load i8*, i8** %2
ret i8* %25
}

LLVM IR (-O2):
define i8* @vktrace_allocate_and_copy(i8* %0) {
%2 = icmp eq i8* %0, null
br i1 %2, label %10, label %3
3: ; preds = %1
%4 = tail call i32 @strlen(i8* %0)
%5 = add nsw i32 %4, 1
%6 = sext i32 %5 to i64
%7 = tail call i64 @vktrace_malloc(i32 %5)
%8 = inttoptr i64 %7 to i8*
%9 = tail call i32 @strncpy(i8* %8, i8* %0, i64 %6)
br label %10
10: ; preds = %1, %3
%11 = phi i8* [ %8, %3 ], [ null, %1 ]
ret i8* %11
}