#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 */
scalar_t__ malloc(scalar_t__);
int /*<<< orphan*/ strcpy(char *, char *);
scalar_t__ strlen(char *);
char *newstr(const char *string) {
  char *value;
  if (!string)
    return ((char *)NULL);
  if (!(value = (char *)malloc(
            (string == (char *)NULL ? 0 : strlen((char *)string)) + 1)))
    return ((char *)NULL);
  (void)strcpy((char *)value, (char *)string);
  return (value);
}


x86 assembly (clang, -O0):
<newstr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1154 <newstr+0x24>
movq   $0x0,-0x8(%rbp)
jmp    11b4 <newstr+0x84>
cmpq   $0x0,-0x10(%rbp)
jne    116a <newstr+0x3a>
xor    %eax,%eax
mov    %rax,-0x20(%rbp)
jmp    1177 <newstr+0x47>
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
add    $0x1,%rdi
call   1050 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    119f <newstr+0x6f>
movq   $0x0,-0x8(%rbp)
jmp    11b4 <newstr+0x84>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<newstr>:
push   %r14
push   %rbx
push   %rax
test   %rdi,%rdi
je     115f <newstr+0x2f>
mov    %rdi,%r14
call   1040 <strlen@plt>
lea    0x1(%rax),%rdi
call   1050 <malloc@plt>
test   %rax,%rax
je     115f <newstr+0x2f>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r14,%rsi
call   1030 <strcpy@plt>
jmp    1161 <newstr+0x31>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

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

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