#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 */
typedef char int8_t;

/* Variables and functions */
int /*<<< orphan*/ assert(char const *);
scalar_t__ malloc(size_t);
size_t strlen(char const *);
int /*<<< orphan*/ strncpy(char *, char const *, size_t);
int8_t *str_dup(int8_t const *src) {
  int8_t *dst = 0;
  size_t len = strlen((char const *)src);
  assert(src);
  if (!src || !len)
    return 0;
  dst = (int8_t *)malloc(len + 1);
  strncpy((char *)dst, (char const *)src, len);
  dst[len] = '\0';
  return dst;
}


x86 assembly (clang, -O0):
<str_dup>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <assert@plt>
cmpq   $0x0,-0x10(%rbp)
je     1180 <str_dup+0x40>
cmpq   $0x0,-0x20(%rbp)
jne    118d <str_dup+0x4d>
movq   $0x0,-0x8(%rbp)
jmp    11c3 <str_dup+0x83>
mov    -0x20(%rbp),%rdi
add    $0x1,%rdi
call   1060 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1030 <strncpy@plt>
mov    -0x18(%rbp),%rax
mov    -0x20(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<str_dup>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%rbx
call   1040 <strlen@plt>
mov    %rax,%r14
mov    %rbx,%rdi
call   1050 <assert@plt>
xor    %r15d,%r15d
test   %rbx,%rbx
je     1187 <str_dup+0x47>
test   %r14,%r14
je     1187 <str_dup+0x47>
mov    %r14,%rdi
add    $0x1,%rdi
call   1060 <malloc@plt>
mov    %rax,%r15
mov    %rax,%rdi
mov    %rbx,%rsi
mov    %r14,%rdx
call   1030 <strncpy@plt>
movb   $0x0,(%r15,%r14,1)
mov    %r15,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
define i8* @str_dup(i8* %0) {
%2 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%3 = tail call i32 @assert(i8* %0)
%4 = icmp ne i8* %0, null
%5 = icmp ne i64 %2, 0
%6 = select i1 %4, i1 %5, i1 false
br i1 %6, label %7, label %13
7: ; preds = %1
%8 = add i64 %2, 1
%9 = tail call i64 @malloc(i64 %8)
%10 = inttoptr i64 %9 to i8*
%11 = tail call i32 @strncpy(i8* %10, i8* %0, i64 %2)
%12 = getelementptr inbounds i8, i8* %10, i64 %2
store i8 0, i8* %12
br label %13
13: ; preds = %1, %7
%14 = phi i8* [ %10, %7 ], [ null, %1 ]
ret i8* %14
}