#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 *strncpy(char *s, const char *t, int n) {
  char *os;
  os = s;
  while (n-- > 0 && (*s++ = *t++) != 0)
    ;
  while (n-- > 0)
    *s++ = 0;
  return os;
}


x86 assembly (clang, -O0):
<strncpy>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x14(%rbp),%ecx
mov    %ecx,%eax
add    $0xffffffff,%eax
mov    %eax,-0x14(%rbp)
xor    %eax,%eax
cmp    $0x0,%ecx
mov    %al,-0x21(%rbp)
jle    115e <strncpy+0x5e>
mov    -0x10(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x10(%rbp)
mov    (%rax),%al
mov    -0x8(%rbp),%rcx
mov    %rcx,%rdx
add    $0x1,%rdx
mov    %rdx,-0x8(%rbp)
mov    %al,(%rcx)
movsbl %al,%eax
cmp    $0x0,%eax
setne  %al
mov    %al,-0x21(%rbp)
mov    -0x21(%rbp),%al
test   $0x1,%al
jne    116e <strncpy+0x6e>
jmp    1173 <strncpy+0x73>
jmp    1117 <strncpy+0x17>
jmp    1178 <strncpy+0x78>
mov    -0x14(%rbp),%eax
mov    %eax,%ecx
add    $0xffffffff,%ecx
mov    %ecx,-0x14(%rbp)
cmp    $0x0,%eax
jle    11a3 <strncpy+0xa3>
mov    -0x8(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x8(%rbp)
movb   $0x0,(%rax)
jmp    1178 <strncpy+0x78>
mov    -0x20(%rbp),%rax
pop    %rbp
ret

x86 assembly (clang, -O2):
<strncpy>:
push   %rbx
mov    %rdi,%rbx
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
test   %edx,%edx
jle    1157 <strncpy+0x47>
movzbl (%rsi),%eax
add    $0x1,%rsi
mov    %al,(%rdi)
add    $0x1,%rdi
add    $0xffffffff,%edx
test   %al,%al
jne    1120 <strncpy+0x10>
lea    0x1(%rdx),%eax
cmp    $0x2,%eax
jb     1157 <strncpy+0x47>
test   %edx,%edx
mov    $0x1,%eax
cmovle %edx,%eax
sub    %eax,%edx
add    $0x1,%rdx
xor    %esi,%esi
call   1030 <memset@plt>
mov    %rbx,%rax
pop    %rbx
ret

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

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