#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 *)((char *)0);
  size_t len;
  if (str == (const char *)((char *)0))
    return (char *)((char *)0);
  len = strlen(str);
  if (len == 0)
    return strdup("");
  if (size > len)
    size = len;
  result = (char *)malloc(len + 1);
  if (result) {
    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    11f4 <br_strndup+0xb4>
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    11f4 <br_strndup+0xb4>
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)
cmpq   $0x0,-0x20(%rbp)
je     11ec <br_strndup+0xac>
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     118a <br_strndup+0x4a>
mov    %rsi,%r14
mov    %rdi,%rbx
call   1030 <strlen@plt>
test   %rax,%rax
je     1196 <br_strndup+0x56>
cmp    %r14,%rax
cmovb  %rax,%r14
add    $0x1,%rax
mov    %rax,%rdi
call   1050 <malloc@plt>
mov    %rax,%r15
test   %rax,%rax
je     118d <br_strndup+0x4d>
mov    %r15,%rdi
mov    %rbx,%rsi
mov    %r14,%rdx
call   1040 <memcpy@plt>
movb   $0x0,(%r15,%r14,1)
jmp    118d <br_strndup+0x4d>
xor    %r15d,%r15d
mov    %r15,%rax
pop    %rbx
pop    %r14
pop    %r15
ret
lea    0xe63(%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 %41
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 %41
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 = icmp ne i8* %29, null
br i1 %30, label %31, label %39
31: ; preds = %24
%32 = load i8*, i8** %6
%33 = load i8*, i8** %4
%34 = load i64, i64* %5
%35 = call i32 @memcpy(i8* %32, i8* %33, i64 %34)
%36 = load i8*, i8** %6
%37 = load i64, i64* %5
%38 = getelementptr inbounds i8, i8* %36, i64 %37
store i8 0, i8* %38
br label %39
39: ; preds = %31, %24
%40 = load i8*, i8** %6
store i8* %40, i8** %3
br label %41
41: ; preds = %39, %16, %10
%42 = load i8*, i8** %3
ret i8* %42
}

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 %19, 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 %19
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 = icmp eq i64 %13, 0
br i1 %15, label %19, label %16
16: ; preds = %9
%17 = tail call i32 @memcpy(i8* %14, i8* %0, i64 %11)
%18 = getelementptr inbounds i8, i8* %14, i64 %11
store i8 0, i8* %18
br label %19
19: ; preds = %9, %16, %2, %7
%20 = phi i8* [ %8, %7 ], [ null, %2 ], [ %14, %16 ], [ %14, %9 ]
ret i8* %20
}