#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*/ strcpy(char *, char const *);
unsigned int strlen(char const *);
char *strdup(const char *src) {
  char *dup;
  unsigned int len;
  if (!src)
    return NULL;
  len = strlen(src);
  if (len == 0)
    return NULL;
  dup = malloc(len + 1);
  if (dup)
    strcpy(dup, src);
  return dup;
}


x86 assembly (clang, -O0):
<strdup>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1154 <strdup+0x24>
movq   $0x0,-0x8(%rbp)
jmp    11a6 <strdup+0x76>
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jne    1177 <strdup+0x47>
movq   $0x0,-0x8(%rbp)
jmp    11a6 <strdup+0x76>
mov    -0x1c(%rbp),%edi
add    $0x1,%edi
call   1050 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     119e <strdup+0x6e>
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):
<strdup>:
push   %r14
push   %rbx
push   %rax
test   %rdi,%rdi
je     1164 <strdup+0x34>
mov    %rdi,%rbx
call   1040 <strlen@plt>
test   %eax,%eax
je     1164 <strdup+0x34>
add    $0x1,%eax
mov    %eax,%edi
call   1050 <malloc@plt>
test   %rax,%rax
je     1164 <strdup+0x34>
mov    %rax,%r14
mov    %rax,%rdi
mov    %rbx,%rsi
call   1030 <strcpy@plt>
jmp    1167 <strdup+0x37>
xor    %r14d,%r14d
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

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

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