#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, char *s2, int n) {
  register int i;
  register 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    %edx,-0x1c(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
movl   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    11a6 <strncpy+0xa6>
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    1193 <strncpy+0x93>
jmp    115d <strncpy+0x5d>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
cmp    -0x1c(%rbp),%eax
jge    1186 <strncpy+0x86>
mov    -0x10(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x10(%rbp)
movb   $0x0,(%rax)
jmp    115d <strncpy+0x5d>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11ae <strncpy+0xae>
jmp    1198 <strncpy+0x98>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    111e <strncpy+0x1e>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
pop    %rbp
ret

x86 assembly (clang, -O2):
<strncpy>:
push   %rbx
mov    %rdi,%rbx
test   %edx,%edx
jle    114f <strncpy+0x3f>
lea    -0x2(%rdx),%eax
mov    $0x1,%edi
movzbl -0x1(%rsi,%rdi,1),%ecx
mov    %cl,-0x1(%rbx,%rdi,1)
test   %cl,%cl
je     113b <strncpy+0x2b>
add    $0x1,%rdi
add    $0xffffffff,%eax
cmp    $0xfffffffe,%eax
jne    1120 <strncpy+0x10>
jmp    114f <strncpy+0x3f>
cmp    %edx,%edi
jge    114f <strncpy+0x3f>
add    %rbx,%rdi
mov    %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 i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8*
store i8* %0, i8** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%10 = load i8*, i8** %5
store i8* %10, i8** %9
store i32 0, i32* %8
br label %11
11: ; preds = %35, %3
%12 = load i32, i32* %8
%13 = load i32, i32* %7
%14 = icmp slt i32 %12, %13
br i1 %14, label %15, label %38
15: ; preds = %11
%16 = load i8*, i8** %6
%17 = getelementptr inbounds i8, i8* %16, i32 1
store i8* %17, i8** %6
%18 = load i8, i8* %16
%19 = load i8*, i8** %5
%20 = getelementptr inbounds i8, i8* %19, i32 1
store i8* %20, i8** %5
store i8 %18, i8* %19
%21 = sext i8 %18 to i32
%22 = icmp eq i32 %21, 0
br i1 %22, label %23, label %34
23: ; preds = %15
br label %24
24: ; preds = %29, %23
%25 = load i32, i32* %8
%26 = add nsw i32 %25, 1
store i32 %26, i32* %8
%27 = load i32, i32* %7
%28 = icmp slt i32 %26, %27
br i1 %28, label %29, label %32
29: ; preds = %24
%30 = load i8*, i8** %5
%31 = getelementptr inbounds i8, i8* %30, i32 1
store i8* %31, i8** %5
store i8 0, i8* %30
br label %24
32: ; preds = %24
%33 = load i8*, i8** %9
store i8* %33, i8** %4
br label %40
34: ; preds = %15
br label %35
35: ; preds = %34
%36 = load i32, i32* %8
%37 = add nsw i32 %36, 1
store i32 %37, i32* %8
br label %11
38: ; preds = %11
%39 = load i8*, i8** %9
store i8* %39, i8** %4
br label %40
40: ; preds = %38, %32
%41 = load i8*, i8** %4
ret i8* %41
}

LLVM IR (-O2):
define i8* @strncpy(i8* returned writeonly %0, i8* nocapture %1, i32 %2) {
%4 = icmp sgt i32 %2, 0
br i1 %4, label %5, label %24
5: ; preds = %3, %20
%6 = phi i32 [ %22, %20 ], [ 0, %3 ]
%7 = phi i8* [ %10, %20 ], [ %0, %3 ]
%8 = phi i8* [ %21, %20 ], [ %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 %20
12: ; preds = %5
%13 = add nuw nsw i32 %6, 1
%14 = icmp slt i32 %13, %2
br i1 %14, label %15, label %24
15: ; preds = %12
%16 = add i32 %2, -2
%17 = sub i32 %16, %6
%18 = zext i32 %17 to i64
%19 = add nuw nsw i64 %18, 1
br label %24
20: ; preds = %5
%21 = getelementptr inbounds i8, i8* %8, i64 1
%22 = add nuw nsw i32 %6, 1
%23 = icmp eq i32 %22, %2
br i1 %23, label %24, label %5
24: ; preds = %20, %15, %3, %12
ret i8* %0
}