#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, size_t n) {
  char *os = s;
  while (n > 0 && (*s++ = *t++) != 0)
    n--;
  if (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    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x20(%rbp)
xor    %eax,%eax
cmpq   $0x0,-0x18(%rbp)
mov    %al,-0x21(%rbp)
jbe    1156 <strncpy+0x56>
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    1166 <strncpy+0x66>
jmp    1177 <strncpy+0x77>
mov    -0x18(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x18(%rbp)
jmp    1118 <strncpy+0x18>
cmpq   $0x0,-0x18(%rbp)
jbe    1189 <strncpy+0x89>
mov    -0x8(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x20(%rbp),%rax
pop    %rbp
ret

x86 assembly (clang, -O2):
<strncpy>:
mov    %rdi,%rax
test   %rdx,%rdx
je     1126 <strncpy+0x26>
xor    %ecx,%ecx
nopw   0x0(%rax,%rax,1)
movzbl (%rsi,%rcx,1),%edi
mov    %dil,(%rax,%rcx,1)
test   %dil,%dil
je     1127 <strncpy+0x27>
add    $0x1,%rcx
cmp    %rcx,%rdx
jne    1110 <strncpy+0x10>
ret
movb   $0x0,0x1(%rax,%rcx,1)
ret

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

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