#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) {
  int i;
  char *os1;
  os1 = s1;
  for (i = 0; i < n; i++)
    if ((*s1++ = *s2++) == 0) {
      while (++i < n)
        *s1++ = 0;
      return os1;
    }
  return os1;
}


x86 assembly (clang, -O0):
<strncpy>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
movl   $0x0,-0x24(%rbp)
movslq -0x24(%rbp),%rax
cmp    -0x20(%rbp),%rax
jae    11ac <strncpy+0xac>
mov    -0x18(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x18(%rbp)
mov    (%rax),%al
mov    -0x10(%rbp),%rcx
mov    %rcx,%rdx
add    $0x1,%rdx
mov    %rdx,-0x10(%rbp)
mov    %al,(%rcx)
movsbl %al,%eax
cmp    $0x0,%eax
jne    1199 <strncpy+0x99>
jmp    1160 <strncpy+0x60>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
cltq
cmp    -0x20(%rbp),%rax
jae    118c <strncpy+0x8c>
mov    -0x10(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x10(%rbp)
movb   $0x0,(%rax)
jmp    1160 <strncpy+0x60>
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11b4 <strncpy+0xb4>
jmp    119e <strncpy+0x9e>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    111f <strncpy+0x1f>
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
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     1177 <strncpy+0x67>
mov    %rdx,%rax
neg    %rax
mov    $0x1,%ecx
nopw   0x0(%rax,%rax,1)
movzbl -0x1(%rsi,%rcx,1),%ebx
mov    %bl,-0x1(%r14,%rcx,1)
test   %bl,%bl
je     1152 <strncpy+0x42>
lea    (%rax,%rcx,1),%rdi
add    $0x1,%rdi
add    $0x1,%rcx
cmp    $0x1,%rdi
jne    1130 <strncpy+0x20>
jmp    1177 <strncpy+0x67>
mov    %ecx,%eax
cmp    %rdx,%rax
jae    1177 <strncpy+0x67>
lea    (%r14,%rcx,1),%rdi
add    $0x1,%ecx
xor    %eax,%eax
sub    %rcx,%rdx
cmovae %rdx,%rax
add    $0x1,%rax
xor    %esi,%esi
mov    %rax,%rdx
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 i8*
%7 = alloca i64
%8 = alloca i32
%9 = alloca i8*
store i8* %0, i8** %5
store i8* %1, i8** %6
store i64 %2, i64* %7
%10 = load i8*, i8** %5
store i8* %10, i8** %9
store i32 0, i32* %8
br label %11
11: ; preds = %37, %3
%12 = load i32, i32* %8
%13 = sext i32 %12 to i64
%14 = load i64, i64* %7
%15 = icmp ult i64 %13, %14
br i1 %15, label %16, label %40
16: ; preds = %11
%17 = load i8*, i8** %6
%18 = getelementptr inbounds i8, i8* %17, i32 1
store i8* %18, i8** %6
%19 = load i8, i8* %17
%20 = load i8*, i8** %5
%21 = getelementptr inbounds i8, i8* %20, i32 1
store i8* %21, i8** %5
store i8 %19, i8* %20
%22 = sext i8 %19 to i32
%23 = icmp eq i32 %22, 0
br i1 %23, label %24, label %36
24: ; preds = %16
br label %25
25: ; preds = %31, %24
%26 = load i32, i32* %8
%27 = add nsw i32 %26, 1
store i32 %27, i32* %8
%28 = sext i32 %27 to i64
%29 = load i64, i64* %7
%30 = icmp ult i64 %28, %29
br i1 %30, label %31, label %34
31: ; preds = %25
%32 = load i8*, i8** %5
%33 = getelementptr inbounds i8, i8* %32, i32 1
store i8* %33, i8** %5
store i8 0, i8* %32
br label %25
34: ; preds = %25
%35 = load i8*, i8** %9
store i8* %35, i8** %4
br label %42
36: ; preds = %16
br label %37
37: ; preds = %36
%38 = load i32, i32* %8
%39 = add nsw i32 %38, 1
store i32 %39, i32* %8
br label %11
40: ; preds = %11
%41 = load i8*, i8** %9
store i8* %41, i8** %4
br label %42
42: ; preds = %40, %34
%43 = load i8*, i8** %4
ret i8* %43
}

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 %26, label %5
5: ; preds = %3, %22
%6 = phi i64 [ %24, %22 ], [ 0, %3 ]
%7 = phi i8* [ %10, %22 ], [ %0, %3 ]
%8 = phi i8* [ %23, %22 ], [ %1, %3 ]
%9 = load i8, i8* %8
%10 = getelementptr inbounds i8, i8* %7, i64 1
store i8 %9, i8* %7
%11 = icmp eq i8 %9, 0
br i1 %11, label %12, label %22
12: ; preds = %5
%13 = add i64 %6, 1
%14 = and i64 %13, 4294967295
%15 = icmp ult i64 %14, %2
br i1 %15, label %16, label %26
16: ; preds = %12
%17 = add i64 %6, 2
%18 = and i64 %17, 4294967295
%19 = call i64 @llvm.umax.i64(i64 %2, i64 %18)
%20 = add i64 %19, 1
%21 = sub i64 %20, %18
br label %26
22: ; preds = %5
%23 = getelementptr inbounds i8, i8* %8, i64 1
%24 = add nuw i64 %6, 1
%25 = icmp eq i64 %24, %2
br i1 %25, label %26, label %5
26: ; preds = %22, %16, %3, %12
ret i8* %0
}