#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, unsigned n) {
  char *res = s1;
  while (n) {
    n--;
    if ((*s1++ = *s2++) == '\0')
      break;
  }
  while (n--)
    *s1++ = '\0';
  return res;
}


x86 assembly (clang, -O0):
<strncpy>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x20(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     1162 <strncpy+0x62>
mov    -0x14(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x10(%rbp)
mov    (%rax),%al
mov    -0x8(%rbp),%rcx
mov    %rcx,%rdx
add    $0x1,%rdx
mov    %rdx,-0x8(%rbp)
mov    %al,(%rcx)
movsbl %al,%eax
cmp    $0x0,%eax
jne    115d <strncpy+0x5d>
jmp    1162 <strncpy+0x62>
jmp    1117 <strncpy+0x17>
jmp    1167 <strncpy+0x67>
mov    -0x14(%rbp),%eax
mov    %eax,%ecx
add    $0xffffffff,%ecx
mov    %ecx,-0x14(%rbp)
cmp    $0x0,%eax
je     1192 <strncpy+0x92>
mov    -0x8(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x8(%rbp)
movb   $0x0,(%rax)
jmp    1167 <strncpy+0x67>
mov    -0x20(%rbp),%rax
pop    %rbp
ret

x86 assembly (clang, -O2):
<strncpy>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
test   %edx,%edx
je     1160 <strncpy+0x50>
mov    %edx,%eax
add    $0xffffffffffffffff,%rax
xor    %edi,%edi
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
movzbl (%rsi,%rdi,1),%ebx
mov    %bl,(%r14,%rdi,1)
lea    0x1(%rdi),%rcx
test   %bl,%bl
je     1147 <strncpy+0x37>
cmp    %edi,%eax
mov    %rcx,%rdi
jne    1130 <strncpy+0x20>
cmp    %ecx,%edx
je     1160 <strncpy+0x50>
lea    (%r14,%rcx,1),%rdi
add    $0xffffffff,%edx
sub    %rcx,%rdx
add    $0x1,%rdx
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, i32 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i8*
store i8* %0, i8** %4
store i8* %1, i8** %5
store i32 %2, i32* %6
%8 = load i8*, i8** %4
store i8* %8, i8** %7
br label %9
9: ; preds = %23, %3
%10 = load i32, i32* %6
%11 = icmp ne i32 %10, 0
br i1 %11, label %12, label %24
12: ; preds = %9
%13 = load i32, i32* %6
%14 = add i32 %13, -1
store i32 %14, i32* %6
%15 = load i8*, i8** %5
%16 = getelementptr inbounds i8, i8* %15, i32 1
store i8* %16, i8** %5
%17 = load i8, i8* %15
%18 = load i8*, i8** %4
%19 = getelementptr inbounds i8, i8* %18, i32 1
store i8* %19, i8** %4
store i8 %17, i8* %18
%20 = sext i8 %17 to i32
%21 = icmp eq i32 %20, 0
br i1 %21, label %22, label %23
22: ; preds = %12
br label %24
23: ; preds = %12
br label %9
24: ; preds = %22, %9
br label %25
25: ; preds = %29, %24
%26 = load i32, i32* %6
%27 = add i32 %26, -1
store i32 %27, i32* %6
%28 = icmp ne i32 %26, 0
br i1 %28, label %29, label %32
29: ; preds = %25
%30 = load i8*, i8** %4
%31 = getelementptr inbounds i8, i8* %30, i32 1
store i8* %31, i8** %4
store i8 0, i8* %30
br label %25
32: ; preds = %25
%33 = load i8*, i8** %7
ret i8* %33
}

LLVM IR (-O2):
define i8* @strncpy(i8* returned writeonly %0, i8* nocapture %1, i32 %2) {
%4 = icmp eq i32 %2, 0
br i1 %4, label %20, label %5
5: ; preds = %3, %5
%6 = phi i8* [ %10, %5 ], [ %1, %3 ]
%7 = phi i32 [ %9, %5 ], [ %2, %3 ]
%8 = phi i8* [ %12, %5 ], [ %0, %3 ]
%9 = add i32 %7, -1
%10 = getelementptr inbounds i8, i8* %6, i64 1
%11 = load i8, i8* %6
%12 = getelementptr inbounds i8, i8* %8, i64 1
store i8 %11, i8* %8
%13 = icmp ne i8 %11, 0
%14 = icmp ne i32 %9, 0
%15 = select i1 %13, i1 %14, i1 false
br i1 %15, label %5, label %16
16: ; preds = %5
%17 = icmp eq i32 %9, 0
br i1 %17, label %20, label %18
18: ; preds = %16
%19 = zext i32 %9 to i64
br label %20
20: ; preds = %3, %18, %16
ret i8* %0
}