#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);
char *strdup(char *);
size_t strlen(char const *);
char *br_strndup(const char *str, size_t size) {
  char *result = (char *)((void *)0);
  size_t len;
  if (str == (const char *)((void *)0))
    return (char *)((void *)0);
  len = strlen(str);
  if (len == 0)
    return strdup("");
  if (size > len)
    size = len;
  result = (char *)malloc(len + 1);
  memcpy(result, str, size);
  result[size] = '\0';
  return result;
}


x86 assembly (clang, -O0):
<br_strndup>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1170 <br_strndup+0x30>
movq   $0x0,-0x8(%rbp)
jmp    11e9 <br_strndup+0xa9>
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    119d <br_strndup+0x5d>
lea    0xe71(%rip),%rdi
call   1060 <strdup@plt>
mov    %rax,-0x8(%rbp)
jmp    11e9 <br_strndup+0xa9>
mov    -0x18(%rbp),%rax
cmp    -0x28(%rbp),%rax
jbe    11b3 <br_strndup+0x73>
mov    -0x28(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x28(%rbp),%rdi
add    $0x1,%rdi
call   1050 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1040 <memcpy@plt>
mov    -0x20(%rbp),%rax
mov    -0x18(%rbp),%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):
<br_strndup>:
push   %r15
push   %r14
push   %rbx
test   %rdi,%rdi
je     1185 <br_strndup+0x45>
mov    %rsi,%r14
mov    %rdi,%rbx
call   1030 <strlen@plt>
test   %rax,%rax
je     1191 <br_strndup+0x51>
cmp    %r14,%rax
cmovb  %rax,%r14
add    $0x1,%rax
mov    %rax,%rdi
call   1050 <malloc@plt>
mov    %rax,%r15
mov    %rax,%rdi
mov    %rbx,%rsi
mov    %r14,%rdx
call   1040 <memcpy@plt>
movb   $0x0,(%r15,%r14,1)
jmp    1188 <br_strndup+0x48>
xor    %r15d,%r15d
mov    %r15,%rax
pop    %rbx
pop    %r14
pop    %r15
ret
lea    0xe68(%rip),%rdi
pop    %rbx
pop    %r14
pop    %r15
jmp    1060 <strdup@plt>

LLVM IR (-O0):
@.str = [1 x i8] zeroinitializer
define i8* @br_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
store i8* null, i8** %6
%8 = load i8*, i8** %4
%9 = icmp eq i8* %8, null
br i1 %9, label %10, label %11
10: ; preds = %2
store i8* null, i8** %3
br label %37
11: ; preds = %2
%12 = load i8*, i8** %4
%13 = call i64 @strlen(i8* %12)
store i64 %13, i64* %7
%14 = load i64, i64* %7
%15 = icmp eq i64 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %11
%17 = call i8* @strdup(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0))
store i8* %17, i8** %3
br label %37
18: ; preds = %11
%19 = load i64, i64* %5
%20 = load i64, i64* %7
%21 = icmp ugt i64 %19, %20
br i1 %21, label %22, label %24
22: ; preds = %18
%23 = load i64, i64* %7
store i64 %23, i64* %5
br label %24
24: ; preds = %22, %18
%25 = load i64, i64* %7
%26 = add i64 %25, 1
%27 = call i64 @malloc(i64 %26)
%28 = inttoptr i64 %27 to i8*
store i8* %28, i8** %6
%29 = load i8*, i8** %6
%30 = load i8*, i8** %4
%31 = load i64, i64* %5
%32 = call i32 @memcpy(i8* %29, i8* %30, i64 %31)
%33 = load i8*, i8** %6
%34 = load i64, i64* %5
%35 = getelementptr inbounds i8, i8* %33, i64 %34
store i8 0, i8* %35
%36 = load i8*, i8** %6
store i8* %36, i8** %3
br label %37
37: ; preds = %24, %16, %10
%38 = load i8*, i8** %3
ret i8* %38
}

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