#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*/ osBZero(char *, size_t);
size_t strlen(char *);
scalar_t__ strncpy(char *, char *, size_t);
char *utilStrNCpy(char *sDst, char *sSrc, size_t ulCnt) {
  size_t ulLen;
  if (strncpy(sDst, sSrc, ulCnt) == 0)
    return 0;
  ulLen = strlen(sSrc);
  if ((ulLen + 1) < ulCnt)
    osBZero(sDst + ulLen + 1, ulCnt - ulLen - 1);
  return sDst;
}


x86 assembly (clang, -O0):
<utilStrNCpy>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1030 <strncpy@plt>
cmp    $0x0,%rax
jne    116c <utilStrNCpy+0x3c>
movq   $0x0,-0x8(%rbp)
jmp    11b0 <utilStrNCpy+0x80>
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
add    $0x1,%rax
cmp    -0x20(%rbp),%rax
jae    11a8 <utilStrNCpy+0x78>
mov    -0x10(%rbp),%rdi
add    -0x28(%rbp),%rdi
add    $0x1,%rdi
mov    -0x20(%rbp),%rsi
sub    -0x28(%rbp),%rsi
sub    $0x1,%rsi
call   1050 <osBZero@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<utilStrNCpy>:
push   %r15
push   %r14
push   %rbx
mov    %rdx,%r15
mov    %rsi,%rbx
mov    %rdi,%r14
call   1030 <strncpy@plt>
test   %rax,%rax
je     1171 <utilStrNCpy+0x41>
mov    %rbx,%rdi
call   1040 <strlen@plt>
lea    0x1(%rax),%rcx
cmp    %r15,%rcx
jae    1174 <utilStrNCpy+0x44>
lea    (%r14,%rax,1),%rdi
add    $0x1,%rdi
not    %rax
add    %r15,%rax
mov    %rax,%rsi
call   1050 <osBZero@plt>
jmp    1174 <utilStrNCpy+0x44>
xor    %r14d,%r14d
mov    %r14,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

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

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