#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);
int /*<<< orphan*/ memcpy(char *, char const *, size_t);
size_t strnlen(char const *, size_t);
char *bc_strndup(const char *s, size_t n) {
  if (s == NULL)
    return NULL;
  size_t l = strnlen(s, n);
  char *tmp = malloc(l + 1);
  if (tmp == NULL)
    return NULL;
  memcpy(tmp, s, l);
  tmp[l] = '\0';
  return tmp;
}


x86 assembly (clang, -O0):
<bc_strndup>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1158 <bc_strndup+0x28>
movq   $0x0,-0x8(%rbp)
jmp    11b7 <bc_strndup+0x87>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1030 <strnlen@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
add    $0x1,%rdi
call   1050 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1192 <bc_strndup+0x62>
movq   $0x0,-0x8(%rbp)
jmp    11b7 <bc_strndup+0x87>
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1040 <memcpy@plt>
mov    -0x28(%rbp),%rax
mov    -0x20(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bc_strndup>:
push   %r15
push   %r14
push   %rbx
test   %rdi,%rdi
je     116b <bc_strndup+0x3b>
mov    %rdi,%r14
call   1030 <strnlen@plt>
mov    %rax,%r15
lea    0x1(%rax),%rdi
call   1050 <malloc@plt>
test   %rax,%rax
je     116b <bc_strndup+0x3b>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r14,%rsi
mov    %r15,%rdx
call   1040 <memcpy@plt>
movb   $0x0,(%rbx,%r15,1)
jmp    116d <bc_strndup+0x3d>
xor    %ebx,%ebx
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i8* @bc_strndup(i8* %0, i64 %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i64
%7 = alloca i8*
store i8* %0, i8** %4
store i64 %1, i64* %5
%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 %30
11: ; preds = %2
%12 = load i8*, i8** %4
%13 = load i64, i64* %5
%14 = call i64 @strnlen(i8* %12, i64 %13)
store i64 %14, i64* %6
%15 = load i64, i64* %6
%16 = add i64 %15, 1
%17 = call i8* @malloc(i64 %16)
store i8* %17, i8** %7
%18 = load i8*, i8** %7
%19 = icmp eq i8* %18, null
br i1 %19, label %20, label %21
20: ; preds = %11
store i8* null, i8** %3
br label %30
21: ; preds = %11
%22 = load i8*, i8** %7
%23 = load i8*, i8** %4
%24 = load i64, i64* %6
%25 = call i32 @memcpy(i8* %22, i8* %23, i64 %24)
%26 = load i8*, i8** %7
%27 = load i64, i64* %6
%28 = getelementptr inbounds i8, i8* %26, i64 %27
store i8 0, i8* %28
%29 = load i8*, i8** %7
store i8* %29, i8** %3
br label %30
30: ; preds = %21, %20, %10
%31 = load i8*, i8** %3
ret i8* %31
}

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