#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 *safestrncpy(char *dst, const char *src, size_t n) {
  if (n > 0) {
    char *d = dst;
    const char *s = src;
    d[--n] = '\0';
    for (; n > 0; --n) {
      if ((*d++ = *s++) == '\0') {
        while (--n > 0)
          *d++ = '\0';
        break;
      }
    }
  }
  return dst;
}


x86 assembly (clang, -O0):
<safestrncpy>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jbe    11ce <safestrncpy+0xce>
mov    -0x8(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rax
mov    -0x18(%rbp),%rcx
mov    %rcx,%rdx
add    $0xffffffffffffffff,%rdx
mov    %rdx,-0x18(%rbp)
movb   $0x0,-0x1(%rax,%rcx,1)
cmpq   $0x0,-0x18(%rbp)
jbe    11c9 <safestrncpy+0xc9>
mov    -0x28(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x28(%rbp)
mov    (%rax),%al
mov    -0x20(%rbp),%rcx
mov    %rcx,%rdx
add    $0x1,%rdx
mov    %rdx,-0x20(%rbp)
mov    %al,(%rcx)
movsbl %al,%eax
cmp    $0x0,%eax
jne    11b3 <safestrncpy+0xb3>
jmp    1181 <safestrncpy+0x81>
mov    -0x18(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jbe    11ae <safestrncpy+0xae>
mov    -0x20(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x20(%rbp)
movb   $0x0,(%rax)
jmp    1181 <safestrncpy+0x81>
jmp    11c9 <safestrncpy+0xc9>
jmp    11b8 <safestrncpy+0xb8>
mov    -0x18(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x18(%rbp)
jmp    1143 <safestrncpy+0x43>
jmp    11ce <safestrncpy+0xce>
mov    -0x8(%rbp),%rax
pop    %rbp
ret

x86 assembly (clang, -O2):
<safestrncpy>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
test   %rdx,%rdx
je     115f <safestrncpy+0x4f>
mov    %rdx,%rcx
movb   $0x0,-0x1(%r14,%rdx,1)
add    $0xffffffffffffffff,%rcx
je     115f <safestrncpy+0x4f>
xor    %eax,%eax
nopl   (%rax)
movzbl (%rsi,%rax,1),%ebx
mov    %bl,(%r14,%rax,1)
test   %bl,%bl
je     1147 <safestrncpy+0x37>
add    $0x1,%rax
cmp    %rax,%rcx
jne    1130 <safestrncpy+0x20>
jmp    115f <safestrncpy+0x4f>
add    $0xfffffffffffffffe,%rdx
sub    %rax,%rdx
je     115f <safestrncpy+0x4f>
lea    (%r14,%rax,1),%rdi
add    $0x1,%rdi
xor    %esi,%esi
call   1030 <memset@plt>
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
define i8* @safestrncpy(i8* %0, i8* %1, i64 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i8*
%8 = alloca i8*
store i8* %0, i8** %4
store i8* %1, i8** %5
store i64 %2, i64* %6
%9 = load i64, i64* %6
%10 = icmp ugt i64 %9, 0
br i1 %10, label %11, label %43
11: ; preds = %3
%12 = load i8*, i8** %4
store i8* %12, i8** %7
%13 = load i8*, i8** %5
store i8* %13, i8** %8
%14 = load i8*, i8** %7
%15 = load i64, i64* %6
%16 = add i64 %15, -1
store i64 %16, i64* %6
%17 = getelementptr inbounds i8, i8* %14, i64 %16
store i8 0, i8* %17
br label %18
18: ; preds = %39, %11
%19 = load i64, i64* %6
%20 = icmp ugt i64 %19, 0
br i1 %20, label %21, label %42
21: ; preds = %18
%22 = load i8*, i8** %8
%23 = getelementptr inbounds i8, i8* %22, i32 1
store i8* %23, i8** %8
%24 = load i8, i8* %22
%25 = load i8*, i8** %7
%26 = getelementptr inbounds i8, i8* %25, i32 1
store i8* %26, i8** %7
store i8 %24, i8* %25
%27 = sext i8 %24 to i32
%28 = icmp eq i32 %27, 0
br i1 %28, label %29, label %38
29: ; preds = %21
br label %30
30: ; preds = %34, %29
%31 = load i64, i64* %6
%32 = add i64 %31, -1
store i64 %32, i64* %6
%33 = icmp ugt i64 %32, 0
br i1 %33, label %34, label %37
34: ; preds = %30
%35 = load i8*, i8** %7
%36 = getelementptr inbounds i8, i8* %35, i32 1
store i8* %36, i8** %7
store i8 0, i8* %35
br label %30
37: ; preds = %30
br label %42
38: ; preds = %21
br label %39
39: ; preds = %38
%40 = load i64, i64* %6
%41 = add i64 %40, -1
store i64 %41, i64* %6
br label %18
42: ; preds = %37, %18
br label %43
43: ; preds = %42, %3
%44 = load i8*, i8** %4
ret i8* %44
}

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