#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 */
int strlen(char const *);
int /*<<< orphan*/ strncpy(char *, char const *, int);
void ytht_strsncpy(char *s1, const char *s2, int n) {
  int l = strlen(s2);
  if (n < 0)
    return;
  if (n > l + 1)
    n = l + 1;
  strncpy(s1, s2, n - 1);
  s1[n - 1] = 0;
}


x86 assembly (clang, -O0):
<ytht_strsncpy>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    114e <ytht_strsncpy+0x2e>
jmp    118c <ytht_strsncpy+0x6c>
mov    -0x14(%rbp),%eax
mov    -0x18(%rbp),%ecx
add    $0x1,%ecx
cmp    %ecx,%eax
jle    1168 <ytht_strsncpy+0x48>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
sub    $0x1,%edx
call   1030 <strncpy@plt>
mov    -0x8(%rbp),%rax
mov    -0x14(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ytht_strsncpy>:
push   %rbp
push   %r14
push   %rbx
mov    %edx,%ebp
mov    %rsi,%rbx
mov    %rdi,%r14
mov    %rsi,%rdi
call   1040 <strlen@plt>
test   %ebp,%ebp
js     1158 <ytht_strsncpy+0x38>
add    $0x1,%eax
cmp    %ebp,%eax
cmovge %ebp,%eax
movslq %eax,%rbp
lea    -0x1(%rbp),%rdx
mov    %r14,%rdi
mov    %rbx,%rsi
call   1030 <strncpy@plt>
movb   $0x0,-0x1(%r14,%rbp,1)
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
define void @ytht_strsncpy(i8* %0, i8* %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
store i32 %2, i32* %6
%8 = load i8*, i8** %5
%9 = call i32 @strlen(i8* %8)
store i32 %9, i32* %7
%10 = load i32, i32* %6
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %13
12: ; preds = %3
br label %32
13: ; preds = %3
%14 = load i32, i32* %6
%15 = load i32, i32* %7
%16 = add nsw i32 %15, 1
%17 = icmp sgt i32 %14, %16
br i1 %17, label %18, label %21
18: ; preds = %13
%19 = load i32, i32* %7
%20 = add nsw i32 %19, 1
store i32 %20, i32* %6
br label %21
21: ; preds = %18, %13
%22 = load i8*, i8** %4
%23 = load i8*, i8** %5
%24 = load i32, i32* %6
%25 = sub nsw i32 %24, 1
%26 = call i32 @strncpy(i8* %22, i8* %23, i32 %25)
%27 = load i8*, i8** %4
%28 = load i32, i32* %6
%29 = sub nsw i32 %28, 1
%30 = sext i32 %29 to i64
%31 = getelementptr inbounds i8, i8* %27, i64 %30
store i8 0, i8* %31
br label %32
32: ; preds = %21, %12
ret void
}

LLVM IR (-O2):
define void @ytht_strsncpy(i8* %0, i8* %1, i32 %2) {
%4 = tail call i32 @strlen(i8* %1)
%5 = icmp slt i32 %2, 0
br i1 %5, label %14, label %6
6: ; preds = %3
%7 = add nsw i32 %4, 1
%8 = icmp slt i32 %7, %2
%9 = select i1 %8, i32 %7, i32 %2
%10 = add nsw i32 %9, -1
%11 = tail call i32 @strncpy(i8* %0, i8* %1, i32 %10)
%12 = sext i32 %10 to i64
%13 = getelementptr inbounds i8, i8* %0, i64 %12
store i8 0, i8* %13
br label %14
14: ; preds = %3, %6
ret void
}