#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*/ memmove(char *, char *, size_t);
size_t str_len(char *);
char *str_reserve(char *, size_t);
char *str_create(char *src) {
  char *new_str = NULL;
  size_t capacity = 1;
  size_t src_len = 0;
  if (src != NULL)
    capacity = str_len(src);
  new_str = str_reserve(new_str, capacity);
  if (new_str == NULL)
    return (NULL);
  new_str[0] = '\0';
  if (src != NULL) {
    src_len = str_len(src);
    memmove(new_str, src, src_len + 1);
  }
  return (new_str);
}


x86 assembly (clang, -O0):
<str_create>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
movq   $0x1,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     116c <str_create+0x3c>
mov    -0x10(%rbp),%rdi
call   1050 <str_len@plt>
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1030 <str_reserve@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1195 <str_create+0x65>
movq   $0x0,-0x8(%rbp)
jmp    11d1 <str_create+0xa1>
mov    -0x18(%rbp),%rax
movb   $0x0,(%rax)
cmpq   $0x0,-0x10(%rbp)
je     11c9 <str_create+0x99>
mov    -0x10(%rbp),%rdi
call   1050 <str_len@plt>
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x28(%rbp),%rdx
add    $0x1,%rdx
call   1040 <memmove@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<str_create>:
push   %r15
push   %r14
push   %rbx
test   %rdi,%rdi
je     1176 <str_create+0x46>
mov    %rdi,%rbx
call   1050 <str_len@plt>
xor    %r14d,%r14d
xor    %edi,%edi
mov    %rax,%rsi
call   1030 <str_reserve@plt>
test   %rax,%rax
je     1190 <str_create+0x60>
mov    %rax,%r15
movb   $0x0,(%rax)
mov    %rbx,%rdi
call   1050 <str_len@plt>
lea    0x1(%rax),%rdx
mov    %r15,%rdi
mov    %rbx,%rsi
call   1040 <memmove@plt>
mov    %r15,%r14
jmp    1190 <str_create+0x60>
xor    %r14d,%r14d
mov    $0x1,%esi
xor    %edi,%edi
call   1030 <str_reserve@plt>
test   %rax,%rax
je     1190 <str_create+0x60>
movb   $0x0,(%rax)
mov    %rax,%r14
mov    %r14,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
define i8* @str_create(i8* %0) {
%2 = icmp eq i8* %0, null
br i1 %2, label %3, label %6
3: ; preds = %1
%4 = tail call i8* @str_reserve(i8* null, i64 1)
%5 = icmp eq i8* %4, null
br i1 %5, label %15, label %10
6: ; preds = %1
%7 = tail call i64 @str_len(i8* %0)
%8 = tail call i8* @str_reserve(i8* null, i64 %7)
%9 = icmp eq i8* %8, null
br i1 %9, label %15, label %11
10: ; preds = %3
store i8 0, i8* %4
br label %15
11: ; preds = %6
store i8 0, i8* %8
%12 = tail call i64 @str_len(i8* %0)
%13 = add i64 %12, 1
%14 = tail call i32 @memmove(i8* %8, i8* %0, i64 %13)
br label %15
15: ; preds = %10, %6, %11, %3
%16 = phi i8* [ null, %3 ], [ %8, %11 ], [ %4, %10 ], [ null, %6 ]
ret i8* %16
}