#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(char const *);
void *calloc(int, size_t);
scalar_t__ memcpy(void *, char const *, size_t);
size_t strlen(char const *);
char *bm_strdup(const char *string) {
  assert(string);
  size_t len = strlen(string);
  if (len == 0)
    return NULL;
  void *copy = calloc(1, len + 1);
  if (copy == NULL)
    return NULL;
  return (char *)memcpy(copy, string, len);
}


x86 assembly (clang, -O0):
<bm_strdup>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <assert@plt>
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    117a <bm_strdup+0x3a>
movq   $0x0,-0x8(%rbp)
jmp    11bd <bm_strdup+0x7d>
mov    -0x18(%rbp),%rsi
add    $0x1,%rsi
mov    $0x1,%edi
call   1040 <calloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11a8 <bm_strdup+0x68>
movq   $0x0,-0x8(%rbp)
jmp    11bd <bm_strdup+0x7d>
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):
<bm_strdup>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
call   1050 <assert@plt>
mov    %r14,%rdi
call   1030 <strlen@plt>
test   %rax,%rax
je     117f <bm_strdup+0x3f>
mov    %rax,%rbx
lea    0x1(%rax),%rsi
mov    $0x1,%edi
call   1040 <calloc@plt>
test   %rax,%rax
je     117f <bm_strdup+0x3f>
mov    %rax,%rdi
mov    %r14,%rsi
mov    %rbx,%rdx
call   1060 <memcpy@plt>
jmp    1181 <bm_strdup+0x41>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

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

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