#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 *strncpy(char *, char const *, size_t);
char *KSI_strncpy(char *destination, const char *source, size_t n) {
  char *ret;
  if (n == 0 || destination == NULL || source == NULL)
    return NULL;
  destination[n - 1] = 0;
  ret = strncpy(destination, source, n - 1);
  return ret;
}


x86 assembly (clang, -O0):
<KSI_strncpy>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     1145 <KSI_strncpy+0x35>
cmpq   $0x0,-0x10(%rbp)
je     1145 <KSI_strncpy+0x35>
cmpq   $0x0,-0x18(%rbp)
jne    1152 <KSI_strncpy+0x42>
movq   $0x0,-0x8(%rbp)
jmp    1183 <KSI_strncpy+0x73>
mov    -0x10(%rbp),%rax
mov    -0x20(%rbp),%rcx
sub    $0x1,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
sub    $0x1,%rdx
call   1030 <strncpy@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<KSI_strncpy>:
push   %rbx
xor    %eax,%eax
test   %rsi,%rsi
je     1139 <KSI_strncpy+0x29>
mov    %rdi,%rbx
test   %rdi,%rdi
je     1139 <KSI_strncpy+0x29>
test   %rdx,%rdx
je     1139 <KSI_strncpy+0x29>
movb   $0x0,-0x1(%rbx,%rdx,1)
add    $0xffffffffffffffff,%rdx
mov    %rbx,%rdi
call   1030 <strncpy@plt>
mov    %rbx,%rax
pop    %rbx
ret

LLVM IR (-O0):
define i8* @KSI_strncpy(i8* %0, i8* %1, i64 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i8*
store i8* %0, i8** %5
store i8* %1, i8** %6
store i64 %2, i64* %7
%9 = load i64, i64* %7
%10 = icmp eq i64 %9, 0
br i1 %10, label %17, label %11
11: ; preds = %3
%12 = load i8*, i8** %5
%13 = icmp eq i8* %12, null
br i1 %13, label %17, label %14
14: ; preds = %11
%15 = load i8*, i8** %6
%16 = icmp eq i8* %15, null
br i1 %16, label %17, label %18
17: ; preds = %14, %11, %3
store i8* null, i8** %4
br label %29
18: ; preds = %14
%19 = load i8*, i8** %5
%20 = load i64, i64* %7
%21 = sub i64 %20, 1
%22 = getelementptr inbounds i8, i8* %19, i64 %21
store i8 0, i8* %22
%23 = load i8*, i8** %5
%24 = load i8*, i8** %6
%25 = load i64, i64* %7
%26 = sub i64 %25, 1
%27 = call i8* @strncpy(i8* %23, i8* %24, i64 %26)
store i8* %27, i8** %8
%28 = load i8*, i8** %8
store i8* %28, i8** %4
br label %29
29: ; preds = %18, %17
%30 = load i8*, i8** %4
ret i8* %30
}

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