#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 */
void strncpy(char *s, char *t, int n) {
  while (*t && n-- > 0)
    *s++ = *t++;
  while (n-- > 0)
    *s++ = '\0';
}


x86 assembly (clang, -O0):
<strncpy>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x10(%rbp),%rax
movsbl (%rax),%ecx
xor    %eax,%eax
cmp    $0x0,%ecx
mov    %al,-0x15(%rbp)
je     1138 <strncpy+0x38>
mov    -0x14(%rbp),%eax
mov    %eax,%ecx
add    $0xffffffff,%ecx
mov    %ecx,-0x14(%rbp)
cmp    $0x0,%eax
setg   %al
mov    %al,-0x15(%rbp)
mov    -0x15(%rbp),%al
test   $0x1,%al
jne    1148 <strncpy+0x48>
jmp    116f <strncpy+0x6f>
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    110f <strncpy+0xf>
jmp    1174 <strncpy+0x74>
mov    -0x14(%rbp),%eax
mov    %eax,%ecx
add    $0xffffffff,%ecx
mov    %ecx,-0x14(%rbp)
cmp    $0x0,%eax
jle    119f <strncpy+0x9f>
mov    -0x8(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x8(%rbp)
movb   $0x0,(%rax)
jmp    1174 <strncpy+0x74>
pop    %rbp
ret

x86 assembly (clang, -O2):
<strncpy>:
push   %rax
mov    (%rsi),%cl
test   %cl,%cl
je     1148 <strncpy+0x38>
xor    %eax,%eax
test   %edx,%edx
cmovs  %edx,%eax
add    $0xffffffff,%eax
add    $0x1,%rsi
cs nopw 0x0(%rax,%rax,1)
nop
test   %edx,%edx
jle    1157 <strncpy+0x47>
mov    %cl,(%rdi)
add    $0x1,%rdi
movzbl (%rsi),%ecx
add    $0x1,%rsi
add    $0xffffffff,%edx
test   %cl,%cl
jne    1130 <strncpy+0x20>
test   %edx,%edx
jle    1155 <strncpy+0x45>
mov    %edx,%edx
xor    %esi,%esi
call   1030 <memset@plt>
pop    %rax
ret
mov    %eax,%edx
test   %edx,%edx
jg     114c <strncpy+0x3c>
jmp    1155 <strncpy+0x45>

LLVM IR (-O0):
define void @strncpy(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
br label %7
7: ; preds = %18, %3
%8 = load i8*, i8** %5
%9 = load i8, i8* %8
%10 = sext i8 %9 to i32
%11 = icmp ne i32 %10, 0
br i1 %11, label %12, label %16
12: ; preds = %7
%13 = load i32, i32* %6
%14 = add nsw i32 %13, -1
store i32 %14, i32* %6
%15 = icmp sgt i32 %13, 0
br label %16
16: ; preds = %12, %7
%17 = phi i1 [ false, %7 ], [ %15, %12 ]
br i1 %17, label %18, label %24
18: ; preds = %16
%19 = load i8*, i8** %5
%20 = getelementptr inbounds i8, i8* %19, i32 1
store i8* %20, i8** %5
%21 = load i8, i8* %19
%22 = load i8*, i8** %4
%23 = getelementptr inbounds i8, i8* %22, i32 1
store i8* %23, i8** %4
store i8 %21, i8* %22
br label %7
24: ; preds = %16
br label %25
25: ; preds = %29, %24
%26 = load i32, i32* %6
%27 = add nsw i32 %26, -1
store i32 %27, i32* %6
%28 = icmp sgt i32 %26, 0
br i1 %28, label %29, label %32
29: ; preds = %25
%30 = load i8*, i8** %4
%31 = getelementptr inbounds i8, i8* %30, i32 1
store i8* %31, i8** %4
store i8 0, i8* %30
br label %25
32: ; preds = %25
ret void
}

LLVM IR (-O2):
define void @strncpy(i8* nocapture writeonly %0, i8* nocapture %1, i32 %2) {
%4 = load i8, i8* %1
%5 = icmp eq i8 %4, 0
br i1 %5, label %21, label %6
6: ; preds = %3
%7 = call i32 @llvm.smin.i32(i32 %2, i32 0)
%8 = add i32 %7, -1
br label %9
9: ; preds = %6, %16
%10 = phi i8 [ %19, %16 ], [ %4, %6 ]
%11 = phi i32 [ %14, %16 ], [ %2, %6 ]
%12 = phi i8* [ %17, %16 ], [ %1, %6 ]
%13 = phi i8* [ %18, %16 ], [ %0, %6 ]
%14 = add nsw i32 %11, -1
%15 = icmp sgt i32 %11, 0
br i1 %15, label %16, label %21
16: ; preds = %9
%17 = getelementptr inbounds i8, i8* %12, i64 1
%18 = getelementptr inbounds i8, i8* %13, i64 1
store i8 %10, i8* %13
%19 = load i8, i8* %17
%20 = icmp eq i8 %19, 0
br i1 %20, label %21, label %9
21: ; preds = %9, %16, %3
%22 = phi i8* [ %0, %3 ], [ %13, %9 ], [ %18, %16 ]
%23 = phi i32 [ %2, %3 ], [ %8, %9 ], [ %14, %16 ]
%24 = icmp sgt i32 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %21
%26 = zext i32 %23 to i64
br label %27
27: ; preds = %25, %21
ret void
}