#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*/ free(char *);
scalar_t__ malloc(int);
int /*<<< orphan*/ memcpy(char *, char const *, int);
int /*<<< orphan*/ memset(char *, char, int);
char *myStrncpy(char *dest, const char *src, size_t n) {
  int size = sizeof(char) * (n + 1);
  char *tmp = (char *)malloc(size);
  if (tmp) {
    memset(tmp, '\0', size);
    memcpy(tmp, src, size - 1);
    memcpy(dest, tmp, size);
    free(tmp);
    return dest;
  } else {
    return NULL;
  }
}


x86 assembly (clang, -O0):
<myStrncpy>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x20(%rbp),%rax
add    $0x1,%rax
shl    $0x0,%rax
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%edi
call   1060 <malloc@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
je     11c1 <myStrncpy+0x81>
mov    -0x30(%rbp),%rdi
mov    -0x24(%rbp),%edx
xor    %esi,%esi
call   1040 <memset@plt>
mov    -0x30(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x24(%rbp),%edx
sub    $0x1,%edx
call   1050 <memcpy@plt>
mov    -0x10(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    -0x24(%rbp),%edx
call   1050 <memcpy@plt>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11c9 <myStrncpy+0x89>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<myStrncpy>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rdx,%r15
mov    %rsi,%r12
mov    %rdi,%r14
lea    0x1(%rdx),%ebp
mov    %ebp,%edi
call   1060 <malloc@plt>
test   %rax,%rax
je     1194 <myStrncpy+0x54>
mov    %rax,%rbx
mov    %rax,%rdi
xor    %esi,%esi
mov    %ebp,%edx
call   1040 <memset@plt>
mov    %rbx,%rdi
mov    %r12,%rsi
mov    %r15d,%edx
call   1050 <memcpy@plt>
mov    %r14,%rdi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1050 <memcpy@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
jmp    1197 <myStrncpy+0x57>
xor    %r14d,%r14d
mov    %r14,%rax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i8* @myStrncpy(i8* %0, i8* %1, i64 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32
%9 = alloca i8*
store i8* %0, i8** %5
store i8* %1, i8** %6
store i64 %2, i64* %7
%10 = load i64, i64* %7
%11 = add i64 %10, 1
%12 = mul i64 1, %11
%13 = trunc i64 %12 to i32
store i32 %13, i32* %8
%14 = load i32, i32* %8
%15 = call i64 @malloc(i32 %14)
%16 = inttoptr i64 %15 to i8*
store i8* %16, i8** %9
%17 = load i8*, i8** %9
%18 = icmp ne i8* %17, null
br i1 %18, label %19, label %35
19: ; preds = %3
%20 = load i8*, i8** %9
%21 = load i32, i32* %8
%22 = call i32 @memset(i8* %20, i8 signext 0, i32 %21)
%23 = load i8*, i8** %9
%24 = load i8*, i8** %6
%25 = load i32, i32* %8
%26 = sub nsw i32 %25, 1
%27 = call i32 @memcpy(i8* %23, i8* %24, i32 %26)
%28 = load i8*, i8** %5
%29 = load i8*, i8** %9
%30 = load i32, i32* %8
%31 = call i32 @memcpy(i8* %28, i8* %29, i32 %30)
%32 = load i8*, i8** %9
%33 = call i32 @free(i8* %32)
%34 = load i8*, i8** %5
store i8* %34, i8** %4
br label %36
35: ; preds = %3
store i8* null, i8** %4
br label %36
36: ; preds = %35, %19
%37 = load i8*, i8** %4
ret i8* %37
}

LLVM IR (-O2):
define i8* @myStrncpy(i8* %0, i8* %1, i64 %2) {
%4 = trunc i64 %2 to i32
%5 = add i32 %4, 1
%6 = tail call i64 @malloc(i32 %5)
%7 = inttoptr i64 %6 to i8*
%8 = icmp eq i64 %6, 0
br i1 %8, label %14, label %9
9: ; preds = %3
%10 = tail call i32 @memset(i8* %7, i8 signext 0, i32 %5)
%11 = tail call i32 @memcpy(i8* %7, i8* %1, i32 %4)
%12 = tail call i32 @memcpy(i8* %0, i8* %7, i32 %5)
%13 = tail call i32 @free(i8* %7)
br label %14
14: ; preds = %3, %9
%15 = phi i8* [ %0, %9 ], [ null, %3 ]
ret i8* %15
}