#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*/ G_Error(char *);
int /*<<< orphan*/ strncpy(char *, char const *, int);
void Q_strncpyz(char *dest, const char *src, int destsize) {
  if (!dest) {
    G_Error("Q_strncpyz: NULL dest");
  }
  if (!src) {
    G_Error("Q_strncpyz: NULL src");
  }
  if (destsize < 1) {
    G_Error("Q_strncpyz: destsize < 1");
  }
  strncpy(dest, src, destsize - 1);
  dest[destsize - 1] = 0;
}


x86 assembly (clang, -O0):
<Q_strncpyz>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    114a <Q_strncpyz+0x2a>
lea    0xebb(%rip),%rdi
call   1040 <G_Error@plt>
cmpq   $0x0,-0x10(%rbp)
jne    1161 <Q_strncpyz+0x41>
lea    0xeba(%rip),%rdi
call   1040 <G_Error@plt>
cmpl   $0x1,-0x14(%rbp)
jge    1177 <Q_strncpyz+0x57>
lea    0xeb9(%rip),%rdi
call   1040 <G_Error@plt>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
sub    $0x1,%edx
call   1030 <strncpy@plt>
mov    -0x8(%rbp),%rax
mov    -0x14(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<Q_strncpyz>:
push   %rbp
push   %r14
push   %rbx
mov    %edx,%ebp
mov    %rsi,%r14
mov    %rdi,%rbx
test   %rdi,%rdi
je     1156 <Q_strncpyz+0x36>
test   %r14,%r14
je     1167 <Q_strncpyz+0x47>
test   %ebp,%ebp
jle    1177 <Q_strncpyz+0x57>
movslq %ebp,%rbp
lea    -0x1(%rbp),%rdx
mov    %rbx,%rdi
mov    %r14,%rsi
call   1030 <strncpy@plt>
movb   $0x0,-0x1(%rbx,%rbp,1)
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xea3(%rip),%rdi
call   1040 <G_Error@plt>
test   %r14,%r14
jne    1136 <Q_strncpyz+0x16>
lea    0xea8(%rip),%rdi
call   1040 <G_Error@plt>
test   %ebp,%ebp
jg     113a <Q_strncpyz+0x1a>
lea    0xead(%rip),%rdi
call   1040 <G_Error@plt>
jmp    113a <Q_strncpyz+0x1a>

LLVM IR (-O0):
@.str = [22 x i8] c"Q_strncpyz: NULL dest\00"
@.str.1 = [21 x i8] c"Q_strncpyz: NULL src\00"
@.str.2 = [25 x i8] c"Q_strncpyz: destsize < 1\00"
define void @Q_strncpyz(i8* %0, i8* %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
store i32 %2, i32* %6
%7 = load i8*, i8** %4
%8 = icmp ne i8* %7, null
br i1 %8, label %11, label %9
9: ; preds = %3
%10 = call i32 @G_Error(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
br label %11
11: ; preds = %9, %3
%12 = load i8*, i8** %5
%13 = icmp ne i8* %12, null
br i1 %13, label %16, label %14
14: ; preds = %11
%15 = call i32 @G_Error(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
br label %16
16: ; preds = %14, %11
%17 = load i32, i32* %6
%18 = icmp slt i32 %17, 1
br i1 %18, label %19, label %21
19: ; preds = %16
%20 = call i32 @G_Error(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0))
br label %21
21: ; preds = %19, %16
%22 = load i8*, i8** %4
%23 = load i8*, i8** %5
%24 = load i32, i32* %6
%25 = sub nsw i32 %24, 1
%26 = call i32 @strncpy(i8* %22, i8* %23, i32 %25)
%27 = load i8*, i8** %4
%28 = load i32, i32* %6
%29 = sub nsw i32 %28, 1
%30 = sext i32 %29 to i64
%31 = getelementptr inbounds i8, i8* %27, i64 %30
store i8 0, i8* %31
ret void
}

LLVM IR (-O2):
@.str = [22 x i8] c"Q_strncpyz: NULL dest\00"
@.str.1 = [21 x i8] c"Q_strncpyz: NULL src\00"
@.str.2 = [25 x i8] c"Q_strncpyz: destsize < 1\00"
define void @Q_strncpyz(i8* %0, i8* %1, i32 %2) {
%4 = icmp eq i8* %0, null
br i1 %4, label %5, label %7
5: ; preds = %3
%6 = tail call i32 @G_Error(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
br label %7
7: ; preds = %5, %3
%8 = icmp eq i8* %1, null
br i1 %8, label %9, label %11
9: ; preds = %7
%10 = tail call i32 @G_Error(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
br label %11
11: ; preds = %9, %7
%12 = icmp slt i32 %2, 1
br i1 %12, label %13, label %15
13: ; preds = %11
%14 = tail call i32 @G_Error(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0))
br label %15
15: ; preds = %13, %11
%16 = add nsw i32 %2, -1
%17 = tail call i32 @strncpy(i8* %0, i8* %1, i32 %16)
%18 = sext i32 %16 to i64
%19 = getelementptr inbounds i8, i8* %0, i64 %18
store i8 0, i8* %19
ret void
}