#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 */
void strn_cpy(char *s, char *ct, size_t n) {
  int i = 0;
  while ((*s++ = *ct++) && (i++ < n))
    ;
  while (i++ < n)
    *s++ = '\0';
  *--s = '\0';
}


x86 assembly (clang, -O0):
<strn_cpy>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0x0,-0x1c(%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,%ecx
xor    %eax,%eax
cmp    $0x0,%ecx
mov    %al,-0x1d(%rbp)
je     1161 <strn_cpy+0x61>
mov    -0x1c(%rbp),%eax
mov    %eax,%ecx
add    $0x1,%ecx
mov    %ecx,-0x1c(%rbp)
cltq
cmp    -0x18(%rbp),%rax
setb   %al
mov    %al,-0x1d(%rbp)
mov    -0x1d(%rbp),%al
test   $0x1,%al
jne    1171 <strn_cpy+0x71>
jmp    1176 <strn_cpy+0x76>
jmp    1117 <strn_cpy+0x17>
jmp    117b <strn_cpy+0x7b>
mov    -0x1c(%rbp),%eax
mov    %eax,%ecx
add    $0x1,%ecx
mov    %ecx,-0x1c(%rbp)
cltq
cmp    -0x18(%rbp),%rax
jae    11a9 <strn_cpy+0xa9>
mov    -0x8(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x8(%rbp)
movb   $0x0,(%rax)
jmp    117b <strn_cpy+0x7b>
mov    -0x8(%rbp),%rax
mov    %rax,%rcx
add    $0xffffffffffffffff,%rcx
mov    %rcx,-0x8(%rbp)
movb   $0x0,-0x1(%rax)
pop    %rbp
ret

x86 assembly (clang, -O2):
<strn_cpy>:
push   %r15
push   %r14
push   %rbx
mov    %rdx,%r14
mov    %rdi,%r15
lea    0x1(%rdx),%eax
mov    %rdx,%rcx
not    %rcx
xor    %edx,%edx
cs nopw 0x0(%rax,%rax,1)
mov    %rdx,%rbx
movzbl (%rsi,%rdx,1),%edx
mov    %dl,(%r15,%rbx,1)
test   %dl,%dl
je     114d <strn_cpy+0x3d>
lea    0x1(%rbx),%rdx
mov    %rcx,%rdi
add    %rdx,%rdi
jne    1130 <strn_cpy+0x20>
jmp    114f <strn_cpy+0x3f>
mov    %ebx,%eax
movslq %eax,%rcx
cmp    %r14,%rcx
jae    1186 <strn_cpy+0x76>
add    $0x1,%eax
cltq
cmp    %rax,%r14
cmovbe %rax,%r14
lea    (%r15,%rbx,1),%rdi
add    $0x1,%rdi
sub    %rax,%r14
lea    0x1(%r14),%rdx
xor    %esi,%esi
call   1030 <memset@plt>
add    %r15,%r14
lea    (%rbx,%r14,1),%r15
add    $0x1,%r15
jmp    1189 <strn_cpy+0x79>
add    %rbx,%r15
movb   $0x0,(%r15)
pop    %rbx
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
define void @strn_cpy(i8* nocapture writeonly %0, i8* nocapture %1, i64 %2) {
%4 = trunc i64 %2 to i32
%5 = add i32 %4, 1
br label %6
6: ; preds = %13, %3
%7 = phi i64 [ %15, %13 ], [ 0, %3 ]
%8 = phi i8* [ %14, %13 ], [ %1, %3 ]
%9 = phi i8* [ %11, %13 ], [ %0, %3 ]
%10 = load i8, i8* %8
%11 = getelementptr inbounds i8, i8* %9, i64 1
store i8 %10, i8* %9
%12 = icmp eq i8 %10, 0
br i1 %12, label %17, label %13
13: ; preds = %6
%14 = getelementptr inbounds i8, i8* %8, i64 1
%15 = add nuw i64 %7, 1
%16 = icmp eq i64 %7, %2
br i1 %16, label %19, label %6
17: ; preds = %6
%18 = trunc i64 %7 to i32
br label %19
19: ; preds = %13, %17
%20 = phi i32 [ %18, %17 ], [ %5, %13 ]
%21 = sext i32 %20 to i64
%22 = icmp ult i64 %21, %2
br i1 %22, label %23, label %31
23: ; preds = %19
%24 = add i32 %20, 1
%25 = sext i32 %24 to i64
%26 = call i64 @llvm.umax.i64(i64 %2, i64 %25)
%27 = add i64 %26, 1
%28 = sub i64 %27, %25
%29 = sub i64 %26, %25
%30 = getelementptr i8, i8* %11, i64 %29
br label %31
31: ; preds = %23, %19
%32 = phi i8* [ %9, %19 ], [ %30, %23 ]
store i8 0, i8* %32
ret void
}