#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(int);
int /*<<< orphan*/ strncpy(char *, char const *, int);
char *mystrndup(const char *str1, int n) {
  char *dest;
  if ((dest = (char *)malloc(n + 1))) {
    if (str1)
      strncpy(dest, str1, n);
    else
      dest[0] = 0;
    dest[n] = 0;
  }
  return dest;
}


x86 assembly (clang, -O0):
<mystrndup>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0xc(%rbp),%edi
add    $0x1,%edi
call   1040 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
je     117b <mystrndup+0x5b>
cmpq   $0x0,-0x8(%rbp)
je     1168 <mystrndup+0x48>
mov    -0x18(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0xc(%rbp),%edx
call   1030 <strncpy@plt>
jmp    116f <mystrndup+0x4f>
mov    -0x18(%rbp),%rax
movb   $0x0,(%rax)
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):
<mystrndup>:
push   %r15
push   %r14
push   %rbx
mov    %esi,%r14d
mov    %rdi,%r15
lea    0x1(%r14),%edi
call   1040 <malloc@plt>
mov    %rax,%rbx
test   %rax,%rax
je     115b <mystrndup+0x3b>
test   %r15,%r15
je     1151 <mystrndup+0x31>
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %r14d,%edx
call   1030 <strncpy@plt>
jmp    1154 <mystrndup+0x34>
movb   $0x0,(%rbx)
movslq %r14d,%rax
movb   $0x0,(%rbx,%rax,1)
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i8* @mystrndup(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 = call i64 @malloc(i32 %7)
%9 = inttoptr i64 %8 to i8*
store i8* %9, i8** %5
%10 = icmp ne i8* %9, null
br i1 %10, label %11, label %27
11: ; preds = %2
%12 = load i8*, i8** %3
%13 = icmp ne i8* %12, null
br i1 %13, label %14, label %19
14: ; preds = %11
%15 = load i8*, i8** %5
%16 = load i8*, i8** %3
%17 = load i32, i32* %4
%18 = call i32 @strncpy(i8* %15, i8* %16, i32 %17)
br label %22
19: ; preds = %11
%20 = load i8*, i8** %5
%21 = getelementptr inbounds i8, i8* %20, i64 0
store i8 0, i8* %21
br label %22
22: ; preds = %19, %14
%23 = load i8*, i8** %5
%24 = load i32, i32* %4
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds i8, i8* %23, i64 %25
store i8 0, i8* %26
br label %27
27: ; preds = %22, %2
%28 = load i8*, i8** %5
ret i8* %28
}

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