#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_strnlen(char const *, size_t);
int /*<<< orphan*/ memcpy(char *, char const *, size_t);
char *ls_data_strndup(const char *src, size_t len) {
  char *ret = NULL;
  if (src) {
    len = ls_strnlen(src, len);
    ret = ls_data_malloc(len + 1);
    if (!ret) {
      return NULL;
    }
    memcpy(ret, src, len);
    ret[len] = '\0';
  }
  return ret;
}


x86 assembly (clang, -O0):
<ls_data_strndup>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     11aa <ls_data_strndup+0x7a>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1050 <ls_strnlen@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
add    $0x1,%rdi
call   1030 <ls_data_malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    118d <ls_data_strndup+0x5d>
movq   $0x0,-0x8(%rbp)
jmp    11b2 <ls_data_strndup+0x82>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1040 <memcpy@plt>
mov    -0x20(%rbp),%rax
mov    -0x18(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ls_data_strndup>:
push   %r15
push   %r14
push   %rbx
test   %rdi,%rdi
je     116b <ls_data_strndup+0x3b>
mov    %rdi,%r14
call   1050 <ls_strnlen@plt>
mov    %rax,%r15
lea    0x1(%rax),%rdi
call   1030 <ls_data_malloc@plt>
test   %rax,%rax
je     116b <ls_data_strndup+0x3b>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r14,%rsi
mov    %r15,%rdx
call   1040 <memcpy@plt>
movb   $0x0,(%rbx,%r15,1)
jmp    116d <ls_data_strndup+0x3d>
xor    %ebx,%ebx
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

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

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