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


x86 assembly (clang, -O0):
<pcp_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     1176 <pcp_strndup+0x46>
mov    -0x20(%rbp),%rax
mov    -0x8(%rbp),%rcx
sub    %rcx,%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
jmp    1182 <pcp_strndup+0x52>
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     11bb <pcp_strndup+0x8b>
mov    -0x18(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x10(%rbp),%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):
<pcp_strndup>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%r15
xor    %esi,%esi
mov    %r14,%rdx
call   1030 <memchr@plt>
mov    %rax,%rbx
sub    %r15,%rbx
test   %rax,%rax
cmove  %r14,%rbx
lea    0x1(%rbx),%r14
mov    %r14,%rdi
call   1050 <malloc@plt>
mov    %rax,%r12
test   %rax,%rax
je     117c <pcp_strndup+0x4c>
mov    %r12,%rdi
mov    %r15,%rsi
mov    %r14,%rdx
call   1040 <memcpy@plt>
movb   $0x0,(%r12,%rbx,1)
mov    %r12,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i8* @pcp_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 %22
19: ; preds = %2
%20 = load i64, i64* %4
%21 = add i64 %20, 1
store i64 %21, i64* %4
br label %22
22: ; preds = %19, %12
%23 = load i64, i64* %4
%24 = call i8* @malloc(i64 %23)
store i8* %24, i8** %5
%25 = load i8*, i8** %5
%26 = icmp ne i8* %25, null
br i1 %26, label %27, label %36
27: ; preds = %22
%28 = load i8*, i8** %5
%29 = load i8*, i8** %3
%30 = load i64, i64* %4
%31 = call i32 @memcpy(i8* %28, i8* %29, i64 %30)
%32 = load i8*, i8** %5
%33 = load i64, i64* %4
%34 = sub i64 %33, 1
%35 = getelementptr inbounds i8, i8* %32, i64 %34
store i8 0, i8* %35
br label %36
36: ; preds = %27, %22
%37 = load i8*, i8** %5
ret i8* %37
}

LLVM IR (-O2):
define i8* @pcp_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 %5, %6
%8 = select i1 %4, i64 %1, i64 %7
%9 = add i64 %8, 1
%10 = tail call i8* @malloc(i64 %9)
%11 = icmp eq i8* %10, null
br i1 %11, label %15, label %12
12: ; preds = %2
%13 = tail call i32 @memcpy(i8* %10, i8* %0, i64 %9)
%14 = getelementptr inbounds i8, i8* %10, i64 %8
store i8 0, i8* %14
br label %15
15: ; preds = %12, %2
ret i8* %10
}