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


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
mov    -0x10(%rbp),%rdx
xor    %esi,%esi
call   1030 <memchr@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     1171 <strndup+0x41>
mov    -0x20(%rbp),%rax
mov    -0x8(%rbp),%rcx
sub    %rcx,%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     11ae <strndup+0x7e>
mov    -0x18(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x10(%rbp),%rdx
sub    $0x1,%rdx
call   1040 <memcpy@plt>
mov    -0x18(%rbp),%rax
mov    -0x10(%rbp),%rcx
sub    $0x1,%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,%r15
mov    %rdi,%r14
xor    %esi,%esi
mov    %r15,%rdx
call   1030 <memchr@plt>
mov    %rax,%rbx
sub    %r14,%rbx
add    $0x1,%rbx
test   %rax,%rax
cmove  %r15,%rbx
mov    %rbx,%rdi
call   1050 <malloc@plt>
mov    %rax,%r15
test   %rbx,%rbx
je     117b <strndup+0x4b>
lea    -0x1(%rbx),%rdx
mov    %r15,%rdi
mov    %r14,%rsi
call   1040 <memcpy@plt>
movb   $0x0,-0x1(%r15,%rbx,1)
mov    %r15,%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 i8*
store i8* %0, i8** %3
store i64 %1, i64* %4
%7 = load i8*, i8** %3
%8 = load i64, i64* %4
%9 = call i8* @memchr(i8* %7, i32 0, i64 %8)
store i8* %9, i8** %6
%10 = load i8*, i8** %6
%11 = icmp ne i8* %10, null
br i1 %11, label %12, label %19
12: ; preds = %2
%13 = load i8*, i8** %6
%14 = load i8*, i8** %3
%15 = ptrtoint i8* %13 to i64
%16 = ptrtoint i8* %14 to i64
%17 = sub i64 %15, %16
%18 = add nsw i64 %17, 1
store i64 %18, i64* %4
br label %19
19: ; preds = %12, %2
%20 = load i64, i64* %4
%21 = call i8* @malloc(i64 %20)
store i8* %21, i8** %5
%22 = load i64, i64* %4
%23 = icmp ne i64 %22, 0
br i1 %23, label %24, label %34
24: ; preds = %19
%25 = load i8*, i8** %5
%26 = load i8*, i8** %3
%27 = load i64, i64* %4
%28 = sub i64 %27, 1
%29 = call i32 @memcpy(i8* %25, i8* %26, i64 %28)
%30 = load i8*, i8** %5
%31 = load i64, i64* %4
%32 = sub i64 %31, 1
%33 = getelementptr inbounds i8, i8* %30, i64 %32
store i8 0, i8* %33
br label %34
34: ; preds = %24, %19
%35 = load i8*, i8** %5
ret i8* %35
}

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