#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 *malloc(int);
int /*<<< orphan*/ memcpy(char *, char const *, int);
int strlen(char const *);
char *strndup(const char *s, size_t n) {
  int l = n > strlen(s) ? strlen(s) + 1 : n + 1;
  char *d = malloc(l);
  if (d)
    memcpy(d, s, l);
  d[n] = '\0';
  return d;
}


x86 assembly (clang, -O0):
<strndup>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x28(%rbp),%rax
movslq %ecx,%rcx
cmp    %rcx,%rax
jbe    117a <strndup+0x4a>
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
add    $0x1,%eax
cltq
mov    %rax,-0x30(%rbp)
jmp    1186 <strndup+0x56>
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%edi
call   1050 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     11b4 <strndup+0x84>
mov    -0x20(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1040 <memcpy@plt>
mov    -0x20(%rbp),%rax
mov    -0x10(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x20(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<strndup>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r15
mov    %rdi,%r14
call   1030 <strlen@plt>
cltq
cmp    %r15,%rax
jae    115b <strndup+0x2b>
mov    %r14,%rdi
call   1030 <strlen@plt>
mov    %eax,%r12d
add    $0x1,%r12d
jmp    115f <strndup+0x2f>
lea    0x1(%r15),%r12
mov    %r12d,%edi
call   1050 <malloc@plt>
mov    %rax,%rbx
test   %rax,%rax
je     117d <strndup+0x4d>
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %r12d,%edx
call   1040 <memcpy@plt>
movb   $0x0,(%rbx,%r15,1)
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i8* @strndup(i8* %0, i64 %1) {
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i32
%6 = alloca i8*
store i8* %0, i8** %3
store i64 %1, i64* %4
%7 = load i64, i64* %4
%8 = load i8*, i8** %3
%9 = call i32 @strlen(i8* %8)
%10 = sext i32 %9 to i64
%11 = icmp ugt i64 %7, %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 %20
17: ; preds = %2
%18 = load i64, i64* %4
%19 = add i64 %18, 1
br label %20
20: ; preds = %17, %12
%21 = phi i64 [ %16, %12 ], [ %19, %17 ]
%22 = trunc i64 %21 to i32
store i32 %22, i32* %5
%23 = load i32, i32* %5
%24 = call i8* @malloc(i32 %23)
store i8* %24, i8** %6
%25 = load i8*, i8** %6
%26 = icmp ne i8* %25, null
br i1 %26, label %27, label %32
27: ; preds = %20
%28 = load i8*, i8** %6
%29 = load i8*, i8** %3
%30 = load i32, i32* %5
%31 = call i32 @memcpy(i8* %28, i8* %29, i32 %30)
br label %32
32: ; preds = %27, %20
%33 = load i8*, i8** %6
%34 = load i64, i64* %4
%35 = getelementptr inbounds i8, i8* %33, i64 %34
store i8 0, i8* %35
%36 = load i8*, i8** %6
ret i8* %36
}

LLVM IR (-O2):
define i8* @strndup(i8* %0, i64 %1) {
%3 = tail call i32 @strlen(i8* %0)
%4 = sext i32 %3 to i64
%5 = icmp ult i64 %4, %1
br i1 %5, label %6, label %10
6: ; preds = %2
%7 = tail call i32 @strlen(i8* %0)
%8 = add nsw i32 %7, 1
%9 = zext i32 %8 to i64
br label %12
10: ; preds = %2
%11 = add i64 %1, 1
br label %12
12: ; preds = %10, %6
%13 = phi i64 [ %9, %6 ], [ %11, %10 ]
%14 = trunc i64 %13 to i32
%15 = tail call i8* @malloc(i32 %14)
%16 = icmp eq i8* %15, null
br i1 %16, label %19, label %17
17: ; preds = %12
%18 = tail call i32 @memcpy(i8* %15, i8* %0, i32 %14)
br label %19
19: ; preds = %17, %12
%20 = getelementptr inbounds i8, i8* %15, i64 %1
store i8 0, i8* %20
ret i8* %15
}