#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 */
char *malloc(size_t const);
int /*<<< orphan*/ memcpy(char *, char const *, size_t const);
size_t strlen(char const *);
char *duplicate_cstr(const char *src, size_t *length) {
  const size_t src_length = strlen(src);
  char *new_str = malloc(src_length + 1);
  if (!new_str) {
    return NULL;
  }
  memcpy(new_str, src, src_length + 1);
  if (length) {
    *length = src_length;
  }
  return new_str;
}


x86 assembly (clang, -O0):
<duplicate_cstr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
add    $0x1,%rdi
call   1050 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1176 <duplicate_cstr+0x46>
movq   $0x0,-0x8(%rbp)
jmp    11a9 <duplicate_cstr+0x79>
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x20(%rbp),%rdx
add    $0x1,%rdx
call   1040 <memcpy@plt>
cmpq   $0x0,-0x18(%rbp)
je     11a1 <duplicate_cstr+0x71>
mov    -0x20(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<duplicate_cstr>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rsi,%r14
mov    %rdi,%r12
call   1030 <strlen@plt>
mov    %rax,%r15
lea    0x1(%rax),%r13
mov    %r13,%rdi
call   1050 <malloc@plt>
mov    %rax,%rbx
test   %rax,%rax
je     1171 <duplicate_cstr+0x41>
mov    %rbx,%rdi
mov    %r12,%rsi
mov    %r13,%rdx
call   1040 <memcpy@plt>
test   %r14,%r14
je     1171 <duplicate_cstr+0x41>
mov    %r15,(%r14)
mov    %rbx,%rax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

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

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