#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 *dstrinit(char, size_t);
char *dstrpos(char const *, size_t);
int strlen(char const *);
char *strncpy(char *, char const *, size_t);
char *dstrndup(const char *src, size_t n) {
  char *str;
  char *end = dstrpos(src, n);
  if (end == NULL)
    end = (char *)(src + strlen(src));
  n = end - src;
  str = dstrinit('\0', n);
  return strncpy(str, src, n);
}


x86 assembly (clang, -O0):
<dstrndup>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1050 <dstrpos@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    118d <dstrndup+0x4d>
mov    -0x8(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x28(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    -0x8(%rbp),%rcx
sub    %rcx,%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rsi
xor    %edi,%edi
call   1060 <dstrinit@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x10(%rbp),%rdx
call   1030 <strncpy@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dstrndup>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
call   1050 <dstrpos@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    1162 <dstrndup+0x22>
mov    %r14,%rdi
call   1040 <strlen@plt>
movslq %eax,%rbx
add    %r14,%rbx
sub    %r14,%rbx
xor    %edi,%edi
mov    %rbx,%rsi
call   1060 <dstrinit@plt>
mov    %rax,%rdi
mov    %r14,%rsi
mov    %rbx,%rdx
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1030 <strncpy@plt>

LLVM IR (-O0):
define i8* @dstrndup(i8* %0, i64 %1) {
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i8*
%6 = alloca i8*
store i8* %0, i8** %3
store i64 %1, i64* %4
%7 = load i8*, i8** %3
%8 = load i64, i64* %4
%9 = call i8* @dstrpos(i8* %7, i64 %8)
store i8* %9, i8** %6
%10 = load i8*, i8** %6
%11 = icmp eq i8* %10, null
br i1 %11, label %12, label %18
12: ; preds = %2
%13 = load i8*, i8** %3
%14 = load i8*, i8** %3
%15 = call i32 @strlen(i8* %14)
%16 = sext i32 %15 to i64
%17 = getelementptr inbounds i8, i8* %13, i64 %16
store i8* %17, i8** %6
br label %18
18: ; preds = %12, %2
%19 = load i8*, i8** %6
%20 = load i8*, i8** %3
%21 = ptrtoint i8* %19 to i64
%22 = ptrtoint i8* %20 to i64
%23 = sub i64 %21, %22
store i64 %23, i64* %4
%24 = load i64, i64* %4
%25 = call i8* @dstrinit(i8 signext 0, i64 %24)
store i8* %25, i8** %5
%26 = load i8*, i8** %5
%27 = load i8*, i8** %3
%28 = load i64, i64* %4
%29 = call i8* @strncpy(i8* %26, i8* %27, i64 %28)
ret i8* %29
}

LLVM IR (-O2):
define i8* @dstrndup(i8* %0, i64 %1) {
%3 = tail call i8* @dstrpos(i8* %0, i64 %1)
%4 = icmp eq i8* %3, null
br i1 %4, label %5, label %9
5: ; preds = %2
%6 = tail call i32 @strlen(i8* %0)
%7 = sext i32 %6 to i64
%8 = getelementptr inbounds i8, i8* %0, i64 %7
br label %9
9: ; preds = %5, %2
%10 = phi i8* [ %8, %5 ], [ %3, %2 ]
%11 = ptrtoint i8* %10 to i64
%12 = ptrtoint i8* %0 to i64
%13 = sub i64 %11, %12
%14 = tail call i8* @dstrinit(i8 signext 0, i64 %13)
%15 = tail call i8* @strncpy(i8* dereferenceable(1) %14, i8* %0, i64 %13)
ret i8* %14
}