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


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
call   1030 <strlen@plt>
cmp    -0x18(%rbp),%rax
jae    1165 <strndup+0x35>
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x30(%rbp)
jmp    116d <strndup+0x3d>
mov    -0x18(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
call   1050 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    119d <strndup+0x6d>
movq   $0x0,-0x8(%rbp)
jmp    11c6 <strndup+0x96>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   1040 <memcpy@plt>
mov    -0x20(%rbp),%rax
mov    -0x28(%rbp),%rcx
sub    $0x1,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%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,%rbx
mov    %rdi,%r14
call   1030 <strlen@plt>
cmp    %rbx,%rax
cmovb  %rax,%rbx
lea    0x1(%rbx),%r12
mov    %r12,%rdi
call   1050 <malloc@plt>
test   %rax,%rax
je     1173 <strndup+0x43>
mov    %rax,%r15
mov    %rax,%rdi
mov    %r14,%rsi
mov    %r12,%rdx
call   1040 <memcpy@plt>
movb   $0x0,(%r15,%rbx,1)
jmp    1176 <strndup+0x46>
xor    %r15d,%r15d
mov    %r15,%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 i8*
%7 = alloca i64
store i8* %0, i8** %4
store i64 %1, i64* %5
%8 = load i8*, i8** %4
%9 = call i64 @strlen(i8* %8)
%10 = load i64, i64* %5
%11 = icmp ult i64 %9, %10
br i1 %11, label %12, label %15
12: ; preds = %2
%13 = load i8*, i8** %4
%14 = call i64 @strlen(i8* %13)
br label %17
15: ; preds = %2
%16 = load i64, i64* %5
br label %17
17: ; preds = %15, %12
%18 = phi i64 [ %14, %12 ], [ %16, %15 ]
%19 = add i64 %18, 1
store i64 %19, i64* %7
%20 = load i64, i64* %7
%21 = call i64 @malloc(i64 %20)
%22 = inttoptr i64 %21 to i8*
store i8* %22, i8** %6
%23 = icmp eq i8* %22, null
br i1 %23, label %24, label %25
24: ; preds = %17
store i8* null, i8** %3
br label %35
25: ; preds = %17
%26 = load i8*, i8** %6
%27 = load i8*, i8** %4
%28 = load i64, i64* %7
%29 = call i32 @memcpy(i8* %26, i8* %27, i64 %28)
%30 = load i8*, i8** %6
%31 = load i64, i64* %7
%32 = sub i64 %31, 1
%33 = getelementptr inbounds i8, i8* %30, i64 %32
store i8 0, i8* %33
%34 = load i8*, i8** %6
store i8* %34, i8** %3
br label %35
35: ; preds = %25, %24
%36 = load i8*, i8** %3
ret i8* %36
}

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