#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__ calloc(size_t, int);
scalar_t__ memcpy(char *, char const *, size_t);
size_t strlen(char const *);
char *mystrdup(const char *str) {
  size_t len = strlen(str);
  char *temp = (char *)calloc(len + 1, sizeof(char));
  if (!temp) {
    assert(temp != NULL);
    return NULL;
  }
  return (char *)memcpy(temp, str, len);
}


x86 assembly (clang, -O0):
<mystrdup>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
add    $0x1,%rdi
mov    $0x1,%esi
call   1040 <calloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1199 <mystrdup+0x59>
cmpq   $0x0,-0x20(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
movq   $0x0,-0x8(%rbp)
jmp    11ae <mystrdup+0x6e>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1060 <memcpy@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mystrdup>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
call   1030 <strlen@plt>
mov    %rax,%rbx
lea    0x1(%rax),%rdi
mov    $0x1,%esi
call   1040 <calloc@plt>
test   %rax,%rax
je     1175 <mystrdup+0x35>
mov    %rax,%rdi
mov    %r14,%rsi
mov    %rbx,%rdx
call   1060 <memcpy@plt>
mov    %rax,%rbx
jmp    117e <mystrdup+0x3e>
xor    %ebx,%ebx
xor    %edi,%edi
call   1050 <assert@plt>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

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

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