#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(unsigned int);
int /*<<< orphan*/ perror(char *);
char *strcpy(char *, char *);
int strlen(char *);
char *StrDup(char *str) {
  char *to = NULL;
  int len = 0;
  if ((str == NULL) || ((len = strlen(str)) <= 0))
    return NULL;
  if ((to = malloc((unsigned)len + 1)) == NULL) {
    perror("StrDup: can't malloc()");
    return NULL;
  }
  return strcpy(to, str);
}


x86 assembly (clang, -O0):
<StrDup>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     117b <StrDup+0x3b>
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jg     1188 <StrDup+0x48>
movq   $0x0,-0x8(%rbp)
jmp    11cb <StrDup+0x8b>
mov    -0x1c(%rbp),%edi
add    $0x1,%edi
call   1050 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    11ba <StrDup+0x7a>
lea    0xe58(%rip),%rdi
call   1060 <perror@plt>
movq   $0x0,-0x8(%rbp)
jmp    11cb <StrDup+0x8b>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1030 <strcpy@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<StrDup>:
push   %r14
push   %rbx
push   %rax
test   %rdi,%rdi
je     1180 <StrDup+0x40>
mov    %rdi,%rbx
call   1040 <strlen@plt>
test   %eax,%eax
jle    1180 <StrDup+0x40>
add    $0x1,%eax
mov    %eax,%edi
call   1050 <malloc@plt>
test   %rax,%rax
je     1174 <StrDup+0x34>
mov    %rax,%r14
mov    %rax,%rdi
mov    %rbx,%rsi
call   1030 <strcpy@plt>
jmp    1183 <StrDup+0x43>
lea    0xe85(%rip),%rdi
call   1060 <perror@plt>
xor    %r14d,%r14d
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [23 x i8] c"StrDup: can't malloc()\00"
define i8* @StrDup(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
store i8* %0, i8** %3
store i8* null, i8** %4
store i32 0, i32* %5
%6 = load i8*, i8** %3
%7 = icmp eq i8* %6, null
br i1 %7, label %12, label %8
8: ; preds = %1
%9 = load i8*, i8** %3
%10 = call i32 @strlen(i8* %9)
store i32 %10, i32* %5
%11 = icmp sle i32 %10, 0
br i1 %11, label %12, label %13
12: ; preds = %8, %1
store i8* null, i8** %2
br label %24
13: ; preds = %8
%14 = load i32, i32* %5
%15 = add i32 %14, 1
%16 = call i8* @malloc(i32 %15)
store i8* %16, i8** %4
%17 = icmp eq i8* %16, null
br i1 %17, label %18, label %20
18: ; preds = %13
%19 = call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
store i8* null, i8** %2
br label %24
20: ; preds = %13
%21 = load i8*, i8** %4
%22 = load i8*, i8** %3
%23 = call i8* @strcpy(i8* %21, i8* %22)
store i8* %23, i8** %2
br label %24
24: ; preds = %20, %18, %12
%25 = load i8*, i8** %2
ret i8* %25
}

LLVM IR (-O2):
@.str = [23 x i8] c"StrDup: can't malloc()\00"
define i8* @StrDup(i8* %0) {
%2 = icmp eq i8* %0, null
br i1 %2, label %14, label %3
3: ; preds = %1
%4 = tail call i32 @strlen(i8* %0)
%5 = icmp slt i32 %4, 1
br i1 %5, label %14, label %6
6: ; preds = %3
%7 = add nuw i32 %4, 1
%8 = tail call i8* @malloc(i32 %7)
%9 = icmp eq i8* %8, null
br i1 %9, label %10, label %12
10: ; preds = %6
%11 = tail call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
br label %14
12: ; preds = %6
%13 = tail call i8* @strcpy(i8* %8, i8* %0)
br label %14
14: ; preds = %1, %3, %12, %10
%15 = phi i8* [ null, %10 ], [ %8, %12 ], [ null, %3 ], [ null, %1 ]
ret i8* %15
}