#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*/ assert(int /*<<< orphan*/);
scalar_t__ malloc(size_t);
scalar_t__ memmove(char *, char const *, size_t);
size_t strlen(char const *);
char *cvec_strdup(const char *str) {
  size_t len;
  char *temp;
  if (!str)
    return NULL;
  len = strlen(str);
  temp = (char *)malloc(len + 1);
  if (!temp) {
    assert(temp != NULL);
    return NULL;
  }
  temp[len] = 0;
  return (char *)memmove(temp, str, len);
}


x86 assembly (clang, -O0):
<cvec_strdup>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1164 <cvec_strdup+0x24>
movq   $0x0,-0x8(%rbp)
jmp    11cd <cvec_strdup+0x8d>
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
add    $0x1,%rdi
call   1050 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11ac <cvec_strdup+0x6c>
cmpq   $0x0,-0x20(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
movq   $0x0,-0x8(%rbp)
jmp    11cd <cvec_strdup+0x8d>
mov    -0x20(%rbp),%rax
mov    -0x18(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1060 <memmove@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cvec_strdup>:
push   %r14
push   %rbx
push   %rax
test   %rdi,%rdi
je     117a <cvec_strdup+0x3a>
mov    %rdi,%rbx
call   1030 <strlen@plt>
mov    %rax,%r14
lea    0x1(%rax),%rdi
call   1050 <malloc@plt>
test   %rax,%rax
je     117e <cvec_strdup+0x3e>
movb   $0x0,(%rax,%r14,1)
mov    %rax,%rdi
mov    %rbx,%rsi
mov    %r14,%rdx
call   1060 <memmove@plt>
mov    %rax,%rbx
jmp    1187 <cvec_strdup+0x47>
xor    %ebx,%ebx
jmp    1187 <cvec_strdup+0x47>
xor    %ebx,%ebx
xor    %edi,%edi
call   1040 <assert@plt>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
define i8* @cvec_strdup(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 ne i8* %6, null
br i1 %7, label %9, label %8
8: ; preds = %1
store i8* null, i8** %2
br label %32
9: ; preds = %1
%10 = load i8*, i8** %3
%11 = call i64 @strlen(i8* %10)
store i64 %11, i64* %4
%12 = load i64, i64* %4
%13 = add i64 %12, 1
%14 = call i64 @malloc(i64 %13)
%15 = inttoptr i64 %14 to i8*
store i8* %15, i8** %5
%16 = load i8*, i8** %5
%17 = icmp ne i8* %16, null
br i1 %17, label %23, label %18
18: ; preds = %9
%19 = load i8*, i8** %5
%20 = icmp ne i8* %19, null
%21 = zext i1 %20 to i32
%22 = call i32 @assert(i32 %21)
store i8* null, i8** %2
br label %32
23: ; preds = %9
%24 = load i8*, i8** %5
%25 = load i64, i64* %4
%26 = getelementptr inbounds i8, i8* %24, i64 %25
store i8 0, i8* %26
%27 = load i8*, i8** %5
%28 = load i8*, i8** %3
%29 = load i64, i64* %4
%30 = call i64 @memmove(i8* %27, i8* %28, i64 %29)
%31 = inttoptr i64 %30 to i8*
store i8* %31, i8** %2
br label %32
32: ; preds = %23, %18, %8
%33 = load i8*, i8** %2
ret i8* %33
}

LLVM IR (-O2):
define i8* @cvec_strdup(i8* %0) {
%2 = icmp eq i8* %0, null
br i1 %2, label %15, label %3
3: ; preds = %1
%4 = tail call i64 @strlen(i8* %0)
%5 = add i64 %4, 1
%6 = tail call i64 @malloc(i64 %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 @assert(i32 0)
br label %15
11: ; preds = %3
%12 = getelementptr inbounds i8, i8* %7, i64 %4
store i8 0, i8* %12
%13 = tail call i64 @memmove(i8* %7, i8* %0, i64 %4)
%14 = inttoptr i64 %13 to i8*
br label %15
15: ; preds = %1, %11, %9
%16 = phi i8* [ %14, %11 ], [ null, %9 ], [ null, %1 ]
ret i8* %16
}