#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 *alloc_mem(size_t);
int /*<<< orphan*/ memcpy(char *, char const *, size_t);
size_t strlen(char const *);
char *copy_cstring_with_length(const char *str, size_t *length) {
  char *new_string;
  *length = strlen(str);
  new_string = alloc_mem(*length + 1);
  if (!new_string) {
    return NULL;
  }
  memcpy(new_string, str, *length);
  new_string[*length] = '\0';
  return new_string;
}


x86 assembly (clang, -O0):
<copy_cstring_with_length>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
add    $0x1,%rdi
call   1030 <alloc_mem@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    117f <copy_cstring_with_length+0x4f>
movq   $0x0,-0x8(%rbp)
jmp    11aa <copy_cstring_with_length+0x7a>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rax
mov    (%rax),%rdx
call   1050 <memcpy@plt>
mov    -0x20(%rbp),%rax
mov    -0x18(%rbp),%rcx
mov    (%rcx),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<copy_cstring_with_length>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%r15
mov    %rdi,%r14
call   1040 <strlen@plt>
mov    %rax,(%r15)
lea    0x1(%rax),%rdi
call   1030 <alloc_mem@plt>
mov    %rax,%rbx
test   %rax,%rax
je     1169 <copy_cstring_with_length+0x39>
mov    (%r15),%rdx
mov    %rbx,%rdi
mov    %r14,%rsi
call   1050 <memcpy@plt>
mov    (%r15),%rax
movb   $0x0,(%rbx,%rax,1)
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

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

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