#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 *s1, const char *s2, size_t n) {
  char *s = s1;
  while (n > 0 && *s2 != '\0') {
    *s++ = *s2++;
    --n;
  }
  while (n > 0) {
    *s++ = '\0';
    --n;
  }
  return s1;
}


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    1138 <strncpy+0x38>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
setne  %al
mov    %al,-0x21(%rbp)
mov    -0x21(%rbp),%al
test   $0x1,%al
jne    1148 <strncpy+0x48>
jmp    117b <strncpy+0x7b>
mov    -0x10(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x10(%rbp)
mov    (%rax),%cl
mov    -0x20(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x20(%rbp)
mov    %cl,(%rax)
mov    -0x18(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x18(%rbp)
jmp    1118 <strncpy+0x18>
jmp    1180 <strncpy+0x80>
cmpq   $0x0,-0x18(%rbp)
jbe    11ae <strncpy+0xae>
mov    -0x20(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x20(%rbp)
movb   $0x0,(%rax)
mov    -0x18(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x18(%rbp)
jmp    1180 <strncpy+0x80>
mov    -0x8(%rbp),%rax
pop    %rbp
ret

x86 assembly (clang, -O2):
<strncpy>:
push   %rbx
mov    %rdi,%rbx
test   %rdx,%rdx
je     1144 <strncpy+0x34>
xor    %eax,%eax
nopl   0x0(%rax,%rax,1)
movzbl (%rsi,%rax,1),%ecx
test   %cl,%cl
je     1136 <strncpy+0x26>
mov    %cl,(%rbx,%rax,1)
add    $0x1,%rax
cmp    %rax,%rdx
jne    1120 <strncpy+0x10>
jmp    1144 <strncpy+0x34>
lea    (%rbx,%rax,1),%rdi
sub    %rax,%rdx
xor    %esi,%esi
call   1030 <memset@plt>
mov    %rbx,%rax
pop    %rbx
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 = %19, %3
%10 = load i64, i64* %6
%11 = icmp ugt i64 %10, 0
br i1 %11, label %12, label %17
12: ; preds = %9
%13 = load i8*, i8** %5
%14 = load i8, i8* %13
%15 = sext i8 %14 to i32
%16 = icmp ne i32 %15, 0
br label %17
17: ; preds = %12, %9
%18 = phi i1 [ false, %9 ], [ %16, %12 ]
br i1 %18, label %19, label %27
19: ; preds = %17
%20 = load i8*, i8** %5
%21 = getelementptr inbounds i8, i8* %20, i32 1
store i8* %21, i8** %5
%22 = load i8, i8* %20
%23 = load i8*, i8** %7
%24 = getelementptr inbounds i8, i8* %23, i32 1
store i8* %24, i8** %7
store i8 %22, i8* %23
%25 = load i64, i64* %6
%26 = add i64 %25, -1
store i64 %26, i64* %6
br label %9
27: ; preds = %17
br label %28
28: ; preds = %31, %27
%29 = load i64, i64* %6
%30 = icmp ugt i64 %29, 0
br i1 %30, label %31, label %36
31: ; preds = %28
%32 = load i8*, i8** %7
%33 = getelementptr inbounds i8, i8* %32, i32 1
store i8* %33, i8** %7
store i8 0, i8* %32
%34 = load i64, i64* %6
%35 = add i64 %34, -1
store i64 %35, i64* %6
br label %28
36: ; preds = %28
%37 = load i8*, i8** %4
ret i8* %37
}

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, %11
%6 = phi i8* [ %13, %11 ], [ %0, %3 ]
%7 = phi i64 [ %14, %11 ], [ %2, %3 ]
%8 = phi i8* [ %12, %11 ], [ %1, %3 ]
%9 = load i8, i8* %8
%10 = icmp eq i8 %9, 0
br i1 %10, label %16, label %11
11: ; preds = %5
%12 = getelementptr inbounds i8, i8* %8, i64 1
%13 = getelementptr inbounds i8, i8* %6, i64 1
store i8 %9, i8* %6
%14 = add i64 %7, -1
%15 = icmp eq i64 %14, 0
br i1 %15, label %17, label %5
16: ; preds = %5
br label %17
17: ; preds = %11, %3, %16
ret i8* %0
}