#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)
    return NULL;
  memcpy(d, s, l);
  d[n] = '\0';
  return d;
}


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

x86 assembly (clang, -O2):
<strndup>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%r15
call   1030 <strlen@plt>
cltq
cmp    %r14,%rax
jae    115b <strndup+0x2b>
mov    %r15,%rdi
call   1030 <strlen@plt>
mov    %eax,%r12d
add    $0x1,%r12d
jmp    115f <strndup+0x2f>
lea    0x1(%r14),%r12
mov    %r12d,%edi
call   1050 <malloc@plt>
mov    %rax,%rbx
test   %rax,%rax
je     1182 <strndup+0x52>
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %r12d,%edx
call   1040 <memcpy@plt>
movb   $0x0,(%rbx,%r14,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 i8*
%5 = alloca i64
%6 = alloca i32
%7 = alloca i8*
store i8* %0, i8** %4
store i64 %1, i64* %5
%8 = load i64, i64* %5
%9 = load i8*, i8** %4
%10 = call i32 @strlen(i8* %9)
%11 = sext i32 %10 to i64
%12 = icmp ugt i64 %8, %11
br i1 %12, label %13, label %18
13: ; preds = %2
%14 = load i8*, i8** %4
%15 = call i32 @strlen(i8* %14)
%16 = add nsw i32 %15, 1
%17 = sext i32 %16 to i64
br label %21
18: ; preds = %2
%19 = load i64, i64* %5
%20 = add i64 %19, 1
br label %21
21: ; preds = %18, %13
%22 = phi i64 [ %17, %13 ], [ %20, %18 ]
%23 = trunc i64 %22 to i32
store i32 %23, i32* %6
%24 = load i32, i32* %6
%25 = call i8* @malloc(i32 %24)
store i8* %25, i8** %7
%26 = load i8*, i8** %7
%27 = icmp ne i8* %26, null
br i1 %27, label %29, label %28
28: ; preds = %21
store i8* null, i8** %3
br label %38
29: ; preds = %21
%30 = load i8*, i8** %7
%31 = load i8*, i8** %4
%32 = load i32, i32* %6
%33 = call i32 @memcpy(i8* %30, i8* %31, i32 %32)
%34 = load i8*, i8** %7
%35 = load i64, i64* %5
%36 = getelementptr inbounds i8, i8* %34, i64 %35
store i8 0, i8* %36
%37 = load i8*, i8** %7
store i8* %37, i8** %3
br label %38
38: ; preds = %29, %28
%39 = load i8*, i8** %3
ret i8* %39
}

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 %20, label %17
17: ; preds = %12
%18 = tail call i32 @memcpy(i8* %15, i8* %0, i32 %14)
%19 = getelementptr inbounds i8, i8* %15, i64 %1
store i8 0, i8* %19
br label %20
20: ; preds = %12, %17
ret i8* %15
}