#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 *dscan;
  const char *sscan;
  size_t count;
  dscan = s1;
  sscan = s2;
  count = n;
  while (count > 0) {
    --count;
    if ((*dscan++ = *sscan++) == '\0')
      break;
  }
  while (count-- > 0)
    *dscan++ = '\0';
  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)
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jbe    1177 <strncpy+0x77>
mov    -0x30(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x30(%rbp)
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    1172 <strncpy+0x72>
jmp    1177 <strncpy+0x77>
jmp    1128 <strncpy+0x28>
jmp    117c <strncpy+0x7c>
mov    -0x30(%rbp),%rax
mov    %rax,%rcx
add    $0xffffffffffffffff,%rcx
mov    %rcx,-0x30(%rbp)
cmp    $0x0,%rax
jbe    11ac <strncpy+0xac>
mov    -0x20(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x20(%rbp)
movb   $0x0,(%rax)
jmp    117c <strncpy+0x7c>
mov    -0x8(%rbp),%rax
pop    %rbp
ret

x86 assembly (clang, -O2):
<strncpy>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
test   %rdx,%rdx
je     1157 <strncpy+0x47>
lea    -0x1(%rdx),%rax
xor    %ecx,%ecx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
movzbl (%rsi,%rcx,1),%ebx
mov    %bl,(%r14,%rcx,1)
lea    0x1(%rcx),%rdi
test   %bl,%bl
je     1148 <strncpy+0x38>
cmp    %rcx,%rax
mov    %rdi,%rcx
jne    1130 <strncpy+0x20>
sub    %rdi,%rdx
je     1157 <strncpy+0x47>
add    %r14,%rdi
xor    %esi,%esi
call   1030 <memset@plt>
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
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*
%8 = alloca i8*
%9 = alloca i64
store i8* %0, i8** %4
store i8* %1, i8** %5
store i64 %2, i64* %6
%10 = load i8*, i8** %4
store i8* %10, i8** %7
%11 = load i8*, i8** %5
store i8* %11, i8** %8
%12 = load i64, i64* %6
store i64 %12, i64* %9
br label %13
13: ; preds = %27, %3
%14 = load i64, i64* %9
%15 = icmp ugt i64 %14, 0
br i1 %15, label %16, label %28
16: ; preds = %13
%17 = load i64, i64* %9
%18 = add i64 %17, -1
store i64 %18, i64* %9
%19 = load i8*, i8** %8
%20 = getelementptr inbounds i8, i8* %19, i32 1
store i8* %20, i8** %8
%21 = load i8, i8* %19
%22 = load i8*, i8** %7
%23 = getelementptr inbounds i8, i8* %22, i32 1
store i8* %23, i8** %7
store i8 %21, i8* %22
%24 = sext i8 %21 to i32
%25 = icmp eq i32 %24, 0
br i1 %25, label %26, label %27
26: ; preds = %16
br label %28
27: ; preds = %16
br label %13
28: ; preds = %26, %13
br label %29
29: ; preds = %33, %28
%30 = load i64, i64* %9
%31 = add i64 %30, -1
store i64 %31, i64* %9
%32 = icmp ugt i64 %30, 0
br i1 %32, label %33, label %36
33: ; preds = %29
%34 = load i8*, i8** %7
%35 = getelementptr inbounds i8, i8* %34, i32 1
store i8* %35, i8** %7
store i8 0, i8* %34
br label %29
36: ; preds = %29
%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 %19, label %5
5: ; preds = %3, %5
%6 = phi i8* [ %12, %5 ], [ %0, %3 ]
%7 = phi i8* [ %10, %5 ], [ %1, %3 ]
%8 = phi i64 [ %9, %5 ], [ %2, %3 ]
%9 = add i64 %8, -1
%10 = getelementptr inbounds i8, i8* %7, i64 1
%11 = load i8, i8* %7
%12 = getelementptr inbounds i8, i8* %6, i64 1
store i8 %11, i8* %6
%13 = icmp ne i8 %11, 0
%14 = icmp ne i64 %9, 0
%15 = select i1 %13, i1 %14, i1 false
br i1 %15, label %5, label %16
16: ; preds = %5
%17 = icmp eq i64 %9, 0
br i1 %17, label %19, label %18
18: ; preds = %16
br label %19
19: ; preds = %3, %18, %16
ret i8* %0
}