#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 *alloca(int);
int strlen(char const *);
int /*<<< orphan*/ strncpy(char *, char const *, size_t);
__attribute__((always_)) char *strndupa(const char *s, size_t n) {
  char *d = alloca((strlen(s) + 1) < n ? (strlen(s) + 1) : n);
  if (s && d)
    strncpy(d, s, n);
  return d;
}


x86 assembly (clang, -O0):
<strndupa>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
add    $0x1,%eax
cltq
cmp    -0x10(%rbp),%rax
jae    116f <strndupa+0x3f>
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
add    $0x1,%eax
cltq
mov    %rax,-0x20(%rbp)
jmp    1177 <strndupa+0x47>
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    %eax,%edi
call   1050 <alloca@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x8(%rbp)
je     11ad <strndupa+0x7d>
cmpq   $0x0,-0x18(%rbp)
je     11ad <strndupa+0x7d>
mov    -0x18(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x10(%rbp),%rdx
call   1030 <strncpy@plt>
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<strndupa>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%r14
mov    %rdi,%r15
call   1040 <strlen@plt>
add    $0x1,%eax
cltq
mov    %r14,%rdi
cmp    %r14,%rax
jae    115a <strndupa+0x2a>
mov    %r15,%rdi
call   1040 <strlen@plt>
mov    %eax,%edi
add    $0x1,%edi
call   1050 <alloca@plt>
mov    %rax,%rbx
test   %r15,%r15
je     117a <strndupa+0x4a>
test   %rbx,%rbx
je     117a <strndupa+0x4a>
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %r14,%rdx
call   1030 <strncpy@plt>
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i8* @strndupa(i8* %0, i64 %1) {
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i8*
store i8* %0, i8** %3
store i64 %1, i64* %4
%6 = load i8*, i8** %3
%7 = call i32 @strlen(i8* %6)
%8 = add nsw i32 %7, 1
%9 = sext i32 %8 to i64
%10 = load i64, i64* %4
%11 = icmp ult i64 %9, %10
br i1 %11, label %12, label %17
12: ; preds = %2
%13 = load i8*, i8** %3
%14 = call i32 @strlen(i8* %13)
%15 = add nsw i32 %14, 1
%16 = sext i32 %15 to i64
br label %19
17: ; preds = %2
%18 = load i64, i64* %4
br label %19
19: ; preds = %17, %12
%20 = phi i64 [ %16, %12 ], [ %18, %17 ]
%21 = trunc i64 %20 to i32
%22 = call i8* @alloca(i32 %21)
store i8* %22, i8** %5
%23 = load i8*, i8** %3
%24 = icmp ne i8* %23, null
br i1 %24, label %25, label %33
25: ; preds = %19
%26 = load i8*, i8** %5
%27 = icmp ne i8* %26, null
br i1 %27, label %28, label %33
28: ; preds = %25
%29 = load i8*, i8** %5
%30 = load i8*, i8** %3
%31 = load i64, i64* %4
%32 = call i32 @strncpy(i8* %29, i8* %30, i64 %31)
br label %33
33: ; preds = %28, %25, %19
%34 = load i8*, i8** %5
ret i8* %34
}

LLVM IR (-O2):
define i8* @strndupa(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 ult i64 %5, %1
br i1 %6, label %7, label %11
7: ; preds = %2
%8 = tail call i32 @strlen(i8* %0)
%9 = add nsw i32 %8, 1
%10 = zext i32 %9 to i64
br label %11
11: ; preds = %2, %7
%12 = phi i64 [ %10, %7 ], [ %1, %2 ]
%13 = trunc i64 %12 to i32
%14 = tail call i8* @alloca(i32 %13)
%15 = icmp ne i8* %0, null
%16 = icmp ne i8* %14, null
%17 = select i1 %15, i1 %16, i1 false
br i1 %17, label %18, label %20
18: ; preds = %11
%19 = tail call i32 @strncpy(i8* %14, i8* %0, i64 %1)
br label %20
20: ; preds = %18, %11
ret i8* %14
}