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


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)
mov    -0x14(%rbp),%ecx
mov    %ecx,%eax
add    $0xffffffff,%eax
mov    %eax,-0x14(%rbp)
xor    %eax,%eax
cmp    %ecx,%eax
jae    1195 <strncpy+0x95>
mov    -0x10(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x10(%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    1190 <strncpy+0x90>
jmp    115f <strncpy+0x5f>
mov    -0x14(%rbp),%ecx
mov    %ecx,%eax
add    $0xffffffff,%eax
mov    %eax,-0x14(%rbp)
xor    %eax,%eax
cmp    %ecx,%eax
jae    118b <strncpy+0x8b>
mov    -0x20(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x20(%rbp)
movb   $0x0,(%rax)
jmp    115f <strncpy+0x5f>
jmp    1195 <strncpy+0x95>
jmp    1117 <strncpy+0x17>
mov    -0x8(%rbp),%rax
pop    %rbp
ret

x86 assembly (clang, -O2):
<strncpy>:
push   %rbx
mov    %rdi,%rbx
mov    %edx,%eax
xor    %edi,%edi
nopl   0x0(%rax,%rax,1)
cmp    %edi,%eax
je     1146 <strncpy+0x36>
movzbl (%rsi,%rdi,1),%ecx
mov    %cl,(%rbx,%rdi,1)
add    $0x1,%rdi
test   %cl,%cl
jne    1120 <strncpy+0x10>
cmp    %edi,%edx
je     1146 <strncpy+0x36>
sub    %rdi,%rax
add    %rbx,%rdi
mov    %eax,%edx
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 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 = %30, %3
%10 = load i32, i32* %6
%11 = add i32 %10, -1
store i32 %11, i32* %6
%12 = icmp ult i32 0, %10
br i1 %12, label %13, label %31
13: ; preds = %9
%14 = load i8*, i8** %5
%15 = getelementptr inbounds i8, i8* %14, i32 1
store i8* %15, i8** %5
%16 = load i8, i8* %14
%17 = load i8*, i8** %7
%18 = getelementptr inbounds i8, i8* %17, i32 1
store i8* %18, i8** %7
store i8 %16, i8* %17
%19 = sext i8 %16 to i32
%20 = icmp eq i32 %19, 0
br i1 %20, label %21, label %30
21: ; preds = %13
br label %22
22: ; preds = %26, %21
%23 = load i32, i32* %6
%24 = add i32 %23, -1
store i32 %24, i32* %6
%25 = icmp ult i32 0, %23
br i1 %25, label %26, label %29
26: ; preds = %22
%27 = load i8*, i8** %7
%28 = getelementptr inbounds i8, i8* %27, i32 1
store i8* %28, i8** %7
store i8 0, i8* %27
br label %22
29: ; preds = %22
br label %31
30: ; preds = %13
br label %9
31: ; preds = %29, %9
%32 = load i8*, i8** %4
ret i8* %32
}

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