#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 */
size_t MIN(size_t, size_t);
scalar_t__ malloc(size_t);
size_t strlen(char const *);
int /*<<< orphan*/ strncpy(char *, char const *, size_t);
char *strndup(const char *s, size_t n) {
  char *res;
  size_t len = strlen(s);
  len = MIN(len, n);
  res = (char *)malloc(len + 1);
  if (res) {
    strncpy(res, s, len);
    res[len] = '\0';
  }
  return res;
}


x86 assembly (clang, -O0):
<strndup>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
call   1050 <strlen@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1040 <MIN@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
add    $0x1,%rdi
call   1060 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     11a7 <strndup+0x67>
mov    -0x18(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1030 <strncpy@plt>
mov    -0x18(%rbp),%rax
mov    -0x20(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<strndup>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%rbx
mov    %rdi,%r14
call   1050 <strlen@plt>
mov    %rax,%rdi
mov    %rbx,%rsi
call   1040 <MIN@plt>
mov    %rax,%r15
lea    0x1(%rax),%rdi
call   1060 <malloc@plt>
mov    %rax,%rbx
test   %rax,%rax
je     1182 <strndup+0x42>
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %r15,%rdx
call   1030 <strncpy@plt>
movb   $0x0,(%rbx,%r15,1)
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

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

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