#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 *ls_data_malloc(size_t);
size_t ls_strlen(char const *);
int /*<<< orphan*/ memcpy(char *, char const *, size_t);
char *ls_data_strdup(const char *src) {
  char *ret = NULL;
  if (src) {
    size_t len = ls_strlen(src);
    ret = ls_data_malloc(len + 1);
    if (!ret) {
      return NULL;
    }
    memcpy(ret, src, len + 1);
  }
  return ret;
}


x86 assembly (clang, -O0):
<ls_data_strdup>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     119a <ls_data_strdup+0x6a>
mov    -0x10(%rbp),%rdi
call   1040 <ls_strlen@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
add    $0x1,%rdi
call   1030 <ls_data_malloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1185 <ls_data_strdup+0x55>
movq   $0x0,-0x8(%rbp)
jmp    11a2 <ls_data_strdup+0x72>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x20(%rbp),%rdx
add    $0x1,%rdx
call   1050 <memcpy@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ls_data_strdup>:
push   %r15
push   %r14
push   %rbx
test   %rdi,%rdi
je     1169 <ls_data_strdup+0x39>
mov    %rdi,%r14
call   1040 <ls_strlen@plt>
mov    %rax,%rbx
add    $0x1,%rbx
mov    %rbx,%rdi
call   1030 <ls_data_malloc@plt>
test   %rax,%rax
je     1169 <ls_data_strdup+0x39>
mov    %rax,%r15
mov    %rax,%rdi
mov    %r14,%rsi
mov    %rbx,%rdx
call   1050 <memcpy@plt>
jmp    116c <ls_data_strdup+0x3c>
xor    %r15d,%r15d
mov    %r15,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

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

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