#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__ malloc(size_t);
int strlen(char const *);
int /*<<< orphan*/ strncpy(char *, char const *, size_t);
char *strndup(const char *str, size_t len) {
  int n = strlen(str);
  char *ns = (void *)malloc((n + 1) > len ? len : (n + 1));
  if (ns == NULL)
    return NULL;
  strncpy(ns, str, (n + 1) > len ? len : (n + 1));
  return ns;
}


x86 assembly (clang, -O0):
<strndup>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
cltq
cmp    -0x18(%rbp),%rax
jbe    116b <strndup+0x3b>
mov    -0x18(%rbp),%rax
mov    %rax,-0x30(%rbp)
jmp    1177 <strndup+0x47>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
cltq
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
call   1050 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    119c <strndup+0x6c>
movq   $0x0,-0x8(%rbp)
jmp    11f0 <strndup+0xc0>
mov    -0x28(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
cltq
cmp    -0x18(%rbp),%rax
jbe    11cb <strndup+0x9b>
mov    -0x18(%rbp),%rax
mov    %rax,-0x48(%rbp)
jmp    11d7 <strndup+0xa7>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
cltq
mov    %rax,-0x48(%rbp)
mov    -0x38(%rbp),%rsi
mov    -0x40(%rbp),%rdi
mov    -0x48(%rbp),%rdx
call   1030 <strncpy@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<strndup>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%r15
mov    %rdi,%r14
call   1040 <strlen@plt>
add    $0x1,%eax
movslq %eax,%rbx
cmp    %r15,%rbx
cmova  %r15,%rbx
mov    %rbx,%rdi
call   1050 <malloc@plt>
test   %rax,%rax
je     116d <strndup+0x3d>
mov    %rax,%r15
mov    %rax,%rdi
mov    %r14,%rsi
mov    %rbx,%rdx
call   1030 <strncpy@plt>
jmp    1170 <strndup+0x40>
xor    %r15d,%r15d
mov    %r15,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i8* @strndup(i8* %0, i64 %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i32
%7 = alloca i8*
store i8* %0, i8** %4
store i64 %1, i64* %5
%8 = load i8*, i8** %4
%9 = call i32 @strlen(i8* %8)
store i32 %9, i32* %6
%10 = load i32, i32* %6
%11 = add nsw i32 %10, 1
%12 = sext i32 %11 to i64
%13 = load i64, i64* %5
%14 = icmp ugt i64 %12, %13
br i1 %14, label %15, label %17
15: ; preds = %2
%16 = load i64, i64* %5
br label %21
17: ; preds = %2
%18 = load i32, i32* %6
%19 = add nsw i32 %18, 1
%20 = sext i32 %19 to i64
br label %21
21: ; preds = %17, %15
%22 = phi i64 [ %16, %15 ], [ %20, %17 ]
%23 = call i64 @malloc(i64 %22)
%24 = inttoptr i64 %23 to i8*
store i8* %24, i8** %7
%25 = load i8*, i8** %7
%26 = icmp eq i8* %25, null
br i1 %26, label %27, label %28
27: ; preds = %21
store i8* null, i8** %3
br label %46
28: ; preds = %21
%29 = load i8*, i8** %7
%30 = load i8*, i8** %4
%31 = load i32, i32* %6
%32 = add nsw i32 %31, 1
%33 = sext i32 %32 to i64
%34 = load i64, i64* %5
%35 = icmp ugt i64 %33, %34
br i1 %35, label %36, label %38
36: ; preds = %28
%37 = load i64, i64* %5
br label %42
38: ; preds = %28
%39 = load i32, i32* %6
%40 = add nsw i32 %39, 1
%41 = sext i32 %40 to i64
br label %42
42: ; preds = %38, %36
%43 = phi i64 [ %37, %36 ], [ %41, %38 ]
%44 = call i32 @strncpy(i8* %29, i8* %30, i64 %43)
%45 = load i8*, i8** %7
store i8* %45, i8** %3
br label %46
46: ; preds = %42, %27
%47 = load i8*, i8** %3
ret i8* %47
}

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