#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 */
int /*<<< orphan*/ exit(int);
scalar_t__ malloc(int);
int /*<<< orphan*/ memcpy(char *, char const *, int);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int);
char *StrNDup(const char *src, int len) {
  char *dst = (char *)malloc(sizeof(char) * (len + 1));
  if (!dst) {
    perror("strndup");
    printf("\nerror:\tStrNDup(src, len = %d\n", len);
    exit(2);
  }
  if (!src) {
    memset(dst, 0, len + 1);
  } else {
    memcpy(dst, src, len);
    dst[len] = 0;
  }
  return dst;
}


x86 assembly (clang, -O0):
<StrNDup>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0xc(%rbp),%eax
add    $0x1,%eax
cltq
shl    $0x0,%rax
mov    %eax,%edi
call   1060 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11b6 <StrNDup+0x56>
lea    0xe68(%rip),%rdi
call   1070 <perror@plt>
mov    -0xc(%rbp),%esi
lea    0xe61(%rip),%rdi
call   1030 <printf@plt>
mov    $0x2,%edi
call   1080 <exit@plt>
cmpq   $0x0,-0x8(%rbp)
jne    11d7 <StrNDup+0x77>
mov    -0x18(%rbp),%rdi
mov    -0xc(%rbp),%edx
add    $0x1,%edx
xor    %esi,%esi
call   1040 <memset@plt>
jmp    11f3 <StrNDup+0x93>
mov    -0x18(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0xc(%rbp),%edx
call   1050 <memcpy@plt>
mov    -0x18(%rbp),%rax
movslq -0xc(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<StrNDup>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %esi,%r14d
mov    %rdi,%rbp
lea    0x1(%r14),%r15d
mov    %r15d,%edi
call   1060 <malloc@plt>
test   %rax,%rax
je     11b5 <StrNDup+0x55>
mov    %rax,%rbx
mov    %rax,%rdi
test   %rbp,%rbp
je     119d <StrNDup+0x3d>
mov    %rbp,%rsi
mov    %r14d,%edx
call   1050 <memcpy@plt>
movslq %r14d,%rax
movb   $0x0,(%rbx,%rax,1)
jmp    11a7 <StrNDup+0x47>
xor    %esi,%esi
mov    %r15d,%edx
call   1040 <memset@plt>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe44(%rip),%rdi
call   1070 <perror@plt>
lea    0xe40(%rip),%rdi
mov    %r14d,%esi
call   1030 <printf@plt>
mov    $0x2,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [8 x i8] c"strndup\00"
@.str.1 = [31 x i8] c"\0Aerror:\09StrNDup(src, len = %d\0A\00"
define i8* @StrNDup(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i8*
store i8* %0, i8** %3
store i32 %1, i32* %4
%6 = load i32, i32* %4
%7 = add nsw i32 %6, 1
%8 = sext i32 %7 to i64
%9 = mul i64 1, %8
%10 = trunc i64 %9 to i32
%11 = call i64 @malloc(i32 %10)
%12 = inttoptr i64 %11 to i8*
store i8* %12, i8** %5
%13 = load i8*, i8** %5
%14 = icmp ne i8* %13, null
br i1 %14, label %20, label %15
15: ; preds = %2
%16 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%17 = load i32, i32* %4
%18 = call i32 @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0), i32 %17)
%19 = call i32 @exit(i32 2)
unreachable
20: ; preds = %2
%21 = load i8*, i8** %3
%22 = icmp ne i8* %21, null
br i1 %22, label %28, label %23
23: ; preds = %20
%24 = load i8*, i8** %5
%25 = load i32, i32* %4
%26 = add nsw i32 %25, 1
%27 = call i32 @memset(i8* %24, i32 0, i32 %26)
br label %37
28: ; preds = %20
%29 = load i8*, i8** %5
%30 = load i8*, i8** %3
%31 = load i32, i32* %4
%32 = call i32 @memcpy(i8* %29, i8* %30, i32 %31)
%33 = load i8*, i8** %5
%34 = load i32, i32* %4
%35 = sext i32 %34 to i64
%36 = getelementptr inbounds i8, i8* %33, i64 %35
store i8 0, i8* %36
br label %37
37: ; preds = %28, %23
%38 = load i8*, i8** %5
ret i8* %38
}

LLVM IR (-O2):
@.str = [8 x i8] c"strndup\00"
@.str.1 = [31 x i8] c"\0Aerror:\09StrNDup(src, len = %d\0A\00"
define i8* @StrNDup(i8* %0, i32 %1) {
%3 = add nsw i32 %1, 1
%4 = tail call i64 @malloc(i32 %3)
%5 = inttoptr i64 %4 to i8*
%6 = icmp eq i64 %4, 0
br i1 %6, label %7, label %11
7: ; preds = %2
%8 = tail call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%9 = tail call i32 @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0), i32 %1)
%10 = tail call i32 @exit(i32 2)
unreachable
11: ; preds = %2
%12 = icmp eq i8* %0, null
br i1 %12, label %13, label %15
13: ; preds = %11
%14 = tail call i32 @memset(i8* %5, i32 0, i32 %3)
br label %19
15: ; preds = %11
%16 = tail call i32 @memcpy(i8* %5, i8* %0, i32 %1)
%17 = sext i32 %1 to i64
%18 = getelementptr inbounds i8, i8* %5, i64 %17
store i8 0, i8* %18
br label %19
19: ; preds = %15, %13
ret i8* %5
}