#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*/ memcpy(char *, char const *, size_t);
size_t strlen(char const *);
char *strcdup(const char *str, void *(*mmalloc)(void *user_data, size_t n),
              void *user_data) {
  size_t n;
  char *p;
  n = strlen(str);
  if ((p = (char *)mmalloc(user_data, n)) == NULL)
    return (NULL);
  memcpy(p, str, n);
  p[n] = '\0';
  return (p);
}


x86 assembly (clang, -O0):
<strcdup>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rax
mov    -0x20(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   *%rax
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
jne    116a <strcdup+0x4a>
movq   $0x0,-0x8(%rbp)
jmp    118f <strcdup+0x6f>
mov    -0x30(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   1040 <memcpy@plt>
mov    -0x30(%rbp),%rax
mov    -0x28(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<strcdup>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdx,%rbx
mov    %rsi,%r12
mov    %rdi,%r14
call   1030 <strlen@plt>
mov    %rax,%r15
mov    %rbx,%rdi
mov    %rax,%rsi
call   *%r12
mov    %rax,%rbx
test   %rax,%rax
je     115d <strcdup+0x3d>
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %r15,%rdx
call   1040 <memcpy@plt>
movb   $0x0,(%rbx,%r15,1)
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
define i8* @strcdup(i8* %0, i8* (i8*, i64)* nocapture %1, i8* %2) {
%4 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%5 = tail call i8* %1(i8* %2, i64 %4)
%6 = icmp eq i8* %5, null
br i1 %6, label %10, label %7
7: ; preds = %3
%8 = tail call i32 @memcpy(i8* %5, i8* %0, i64 %4)
%9 = getelementptr inbounds i8, i8* %5, i64 %4
store i8 0, i8* %9
br label %10
10: ; preds = %3, %7
ret i8* %5
}