#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);
char *strndup(const char *s, size_t n) {
  size_t len = strnlen(s, n);
  char *new = (char *)malloc((len + 1) * sizeof(char));
  if (!new)
    return NULL;
  new[len] = '\0';
  return (char *)memcpy(new, 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
shl    $0x0,%rdi
call   1050 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    117e <strndup+0x4e>
movq   $0x0,-0x8(%rbp)
jmp    119f <strndup+0x6f>
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 = mul i64 %12, 1
%14 = call i64 @malloc(i64 %13)
%15 = inttoptr i64 %14 to i8*
store i8* %15, i8** %7
%16 = load i8*, i8** %7
%17 = icmp ne i8* %16, null
br i1 %17, label %19, label %18
18: ; preds = %2
store i8* null, i8** %3
br label %28
19: ; preds = %2
%20 = load i8*, i8** %7
%21 = load i64, i64* %6
%22 = getelementptr inbounds i8, i8* %20, i64 %21
store i8 0, i8* %22
%23 = load i8*, i8** %7
%24 = load i8*, i8** %4
%25 = load i64, i64* %6
%26 = call i64 @memcpy(i8* %23, i8* %24, i64 %25)
%27 = inttoptr i64 %26 to i8*
store i8* %27, i8** %3
br label %28
28: ; preds = %19, %18
%29 = load i8*, i8** %3
ret i8* %29
}

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
}