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


x86 assembly (clang, -O0):
<mystrncpy>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%rax
add    -0x18(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rcx
xor    %eax,%eax
cmp    -0x20(%rbp),%rcx
mov    %al,-0x29(%rbp)
jae    1169 <mystrncpy+0x69>
mov    -0x10(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x10(%rbp)
mov    (%rax),%al
mov    -0x28(%rbp),%rcx
mov    %rcx,%rdx
add    $0x1,%rdx
mov    %rdx,-0x28(%rbp)
mov    %al,(%rcx)
movsbl %al,%eax
cmp    $0x0,%eax
setne  %al
mov    %al,-0x29(%rbp)
mov    -0x29(%rbp),%al
test   $0x1,%al
jne    1179 <mystrncpy+0x79>
jmp    117e <mystrncpy+0x7e>
jmp    1128 <mystrncpy+0x28>
mov    -0x28(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x8(%rbp),%rax
pop    %rbp
ret

x86 assembly (clang, -O2):
<mystrncpy>:
mov    %rdi,%rax
lea    (%rdi,%rdx,1),%r8
add    $0xffffffffffffffff,%r8
xor    %ecx,%ecx
nopl   (%rax)
lea    (%rax,%rcx,1),%rdi
cmp    %r8,%rdi
jae    112d <mystrncpy+0x2d>
movzbl (%rsi,%rcx,1),%edx
mov    %dl,(%rdi)
add    $0x1,%rcx
test   %dl,%dl
jne    1110 <mystrncpy+0x10>
add    %rax,%rcx
mov    %rcx,%rdi
movb   $0x0,(%rdi)
ret

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

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