#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__ mymalloc(scalar_t__);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
scalar_t__ strlen(char *);
char *clonestr(char *str) {
  char *clone;
  if (str == 0 || str[0] == 0) {
    printf("warning cloning nullstr.%p\n", str);
    str = (char *)"<nullstr>";
  }
  clone = (char *)mymalloc(strlen(str) + 1);
  strcpy(clone, str);
  return (clone);
}


x86 assembly (clang, -O0):
<clonestr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
je     1167 <clonestr+0x27>
mov    -0x8(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
jne    1182 <clonestr+0x42>
mov    -0x8(%rbp),%rsi
lea    0xe8e(%rip),%rdi
call   1050 <printf@plt>
lea    0xe9e(%rip),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,%rdi
add    $0x1,%rdi
call   1060 <mymalloc@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x10(%rbp),%rax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<clonestr>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%rbx
test   %rdi,%rdi
je     1151 <clonestr+0x11>
cmpb   $0x0,(%rbx)
jne    1167 <clonestr+0x27>
lea    0xea8(%rip),%rdi
mov    %rbx,%rsi
call   1050 <printf@plt>
lea    0xeb5(%rip),%rbx
mov    %rbx,%rdi
call   1040 <strlen@plt>
lea    0x1(%rax),%rdi
call   1060 <mymalloc@plt>
mov    %rax,%r14
mov    %rax,%rdi
mov    %rbx,%rsi
call   1030 <strcpy@plt>
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [28 x i8] c"warning cloning nullstr.%p\0A\00"
@.str.1 = [10 x i8] c"<nullstr>\00"
define i8* @clonestr(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
store i8* %0, i8** %2
%4 = load i8*, i8** %2
%5 = icmp eq i8* %4, null
br i1 %5, label %12, label %6
6: ; preds = %1
%7 = load i8*, i8** %2
%8 = getelementptr inbounds i8, i8* %7, i64 0
%9 = load i8, i8* %8
%10 = sext i8 %9 to i32
%11 = icmp eq i32 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %6, %1
%13 = load i8*, i8** %2
%14 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i8* %13)
store i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i8** %2
br label %15
15: ; preds = %12, %6
%16 = load i8*, i8** %2
%17 = call i64 @strlen(i8* %16)
%18 = add nsw i64 %17, 1
%19 = call i64 @mymalloc(i64 %18)
%20 = inttoptr i64 %19 to i8*
store i8* %20, i8** %3
%21 = load i8*, i8** %3
%22 = load i8*, i8** %2
%23 = call i32 @strcpy(i8* %21, i8* %22)
%24 = load i8*, i8** %3
ret i8* %24
}

LLVM IR (-O2):
@.str = [28 x i8] c"warning cloning nullstr.%p\0A\00"
@.str.1 = [10 x i8] c"<nullstr>\00"
define i8* @clonestr(i8* %0) {
%2 = icmp eq i8* %0, null
br i1 %2, label %6, label %3
3: ; preds = %1
%4 = load i8, i8* %0
%5 = icmp eq i8 %4, 0
br i1 %5, label %6, label %8
6: ; preds = %3, %1
%7 = tail call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %8
8: ; preds = %6, %3
%9 = phi i8* [ getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), %6 ], [ %0, %3 ]
%10 = tail call i64 @strlen(i8* dereferenceable(1) %9)
%11 = add nsw i64 %10, 1
%12 = tail call i64 @mymalloc(i64 %11)
%13 = inttoptr i64 %12 to i8*
%14 = tail call i32 @strcpy(i8* %13, i8* %9)
ret i8* %13
}