#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);
scalar_t__ memcpy(char *, char const *, size_t);
size_t strnlen(char const *, size_t const);
char *strndup(const char *s, size_t const n) {
  size_t len = strnlen(s, n);
  char *news = (char *)malloc(len + 1);
  if (news == NULL) {
    return NULL;
  }
  news[len] = '\0';
  return (char *)memcpy(news, s, len);
}


x86 assembly (clang, -O0):
<strndup>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1030 <strnlen@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
add    $0x1,%rdi
call   1050 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    117a <strndup+0x4a>
movq   $0x0,-0x8(%rbp)
jmp    119b <strndup+0x6b>
mov    -0x28(%rbp),%rax
mov    -0x20(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1040 <memcpy@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<strndup>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
call   1030 <strnlen@plt>
mov    %rax,%rbx
lea    0x1(%rax),%rdi
call   1050 <malloc@plt>
test   %rax,%rax
je     1161 <strndup+0x31>
movb   $0x0,(%rax,%rbx,1)
mov    %rax,%rdi
mov    %r14,%rsi
mov    %rbx,%rdx
call   1040 <memcpy@plt>
jmp    1163 <strndup+0x33>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

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

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