#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 *strncpyz(char *dest, const char *src, size_t bufsize) {
  char *destend = dest + bufsize - 1;
  while (dest < destend && *src)
    *dest++ = *src++;
  *dest = 0;
  return dest;
}


x86 assembly (clang, -O0):
<strncpyz>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%rax
add    -0x18(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%rcx
xor    %eax,%eax
cmp    -0x20(%rbp),%rcx
mov    %al,-0x21(%rbp)
jae    1143 <strncpyz+0x43>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
setne  %al
mov    %al,-0x21(%rbp)
mov    -0x21(%rbp),%al
test   $0x1,%al
jne    1153 <strncpyz+0x53>
jmp    117a <strncpyz+0x7a>
mov    -0x10(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x10(%rbp)
mov    (%rax),%cl
mov    -0x8(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x8(%rbp)
mov    %cl,(%rax)
jmp    1120 <strncpyz+0x20>
mov    -0x8(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x8(%rbp),%rax
pop    %rbp
ret

x86 assembly (clang, -O2):
<strncpyz>:
mov    %rdi,%rax
lea    (%rdi,%rdx,1),%r8
add    $0xffffffffffffffff,%r8
cmp    %rdi,%r8
jbe    113f <strncpyz+0x3f>
add    $0xffffffffffffffff,%rdx
xor    %edi,%edi
cs nopw 0x0(%rax,%rax,1)
movzbl (%rsi,%rdi,1),%ecx
test   %cl,%cl
je     113c <strncpyz+0x3c>
mov    %cl,(%rax,%rdi,1)
add    $0x1,%rdi
cmp    %rdi,%rdx
jne    1120 <strncpyz+0x20>
mov    %r8,%rax
movb   $0x0,(%r8)
ret
add    %rdi,%rax
movb   $0x0,(%rax)
ret

LLVM IR (-O0):
define i8* @strncpyz(i8* %0, i8* %1, i64 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i8*
store i8* %0, i8** %4
store i8* %1, i8** %5
store i64 %2, i64* %6
%8 = load i8*, i8** %4
%9 = load i64, i64* %6
%10 = getelementptr inbounds i8, i8* %8, i64 %9
%11 = getelementptr inbounds i8, i8* %10, i64 -1
store i8* %11, i8** %7
br label %12
12: ; preds = %23, %3
%13 = load i8*, i8** %4
%14 = load i8*, i8** %7
%15 = icmp ult i8* %13, %14
br i1 %15, label %16, label %21
16: ; preds = %12
%17 = load i8*, i8** %5
%18 = load i8, i8* %17
%19 = sext i8 %18 to i32
%20 = icmp ne i32 %19, 0
br label %21
21: ; preds = %16, %12
%22 = phi i1 [ false, %12 ], [ %20, %16 ]
br i1 %22, label %23, label %29
23: ; preds = %21
%24 = load i8*, i8** %5
%25 = getelementptr inbounds i8, i8* %24, i32 1
store i8* %25, i8** %5
%26 = load i8, i8* %24
%27 = load i8*, i8** %4
%28 = getelementptr inbounds i8, i8* %27, i32 1
store i8* %28, i8** %4
store i8 %26, i8* %27
br label %12
29: ; preds = %21
%30 = load i8*, i8** %4
store i8 0, i8* %30
%31 = load i8*, i8** %4
ret i8* %31
}

LLVM IR (-O2):
define i8* @strncpyz(i8* writeonly %0, i8* nocapture %1, i64 %2) {
%4 = getelementptr inbounds i8, i8* %0, i64 %2
%5 = getelementptr i8, i8* %4, i64 -1
%6 = icmp ugt i8* %5, %0
br i1 %6, label %7, label %16
7: ; preds = %3, %12
%8 = phi i8* [ %14, %12 ], [ %0, %3 ]
%9 = phi i8* [ %13, %12 ], [ %1, %3 ]
%10 = load i8, i8* %9
%11 = icmp eq i8 %10, 0
br i1 %11, label %16, label %12
12: ; preds = %7
%13 = getelementptr inbounds i8, i8* %9, i64 1
%14 = getelementptr inbounds i8, i8* %8, i64 1
store i8 %10, i8* %8
%15 = icmp eq i8* %14, %5
br i1 %15, label %16, label %7
16: ; preds = %7, %12, %3
%17 = phi i8* [ %0, %3 ], [ %5, %12 ], [ %8, %7 ]
store i8 0, i8* %17
ret i8* %17
}