#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 /*<<< orphan*/ assert(int);
void mystrncpy(char *dest, const char *src, int n) {
  enum { FILLINGBYTE = 'y' };
  int c;
  assert(n > 0);
  assert(NULL != dest);
  assert(NULL != src);
  c = '\0';
  while (n > 1) {
    n--;
    c = *dest++ = *src++;
    if (c == '\0')
      break;
  }
  if (c != '\0') {
    n--;
    *dest++ = '\0';
  }
  while (n > 0) {
    n--;
    *dest++ = FILLINGBYTE;
  }
}


x86 assembly (clang, -O0):
<mystrncpy>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
setg   %al
and    $0x1,%al
movzbl %al,%edi
call   1030 <assert@plt>
xor    %eax,%eax
cmp    -0x8(%rbp),%rax
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1030 <assert@plt>
xor    %eax,%eax
cmp    -0x10(%rbp),%rax
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1030 <assert@plt>
movl   $0x0,-0x18(%rbp)
cmpl   $0x1,-0x14(%rbp)
jle    11b0 <mystrncpy+0xa0>
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
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jne    11ab <mystrncpy+0x9b>
jmp    11b0 <mystrncpy+0xa0>
jmp    1161 <mystrncpy+0x51>
cmpl   $0x0,-0x18(%rbp)
je     11d5 <mystrncpy+0xc5>
mov    -0x14(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x14(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x8(%rbp)
movb   $0x0,(%rax)
jmp    11da <mystrncpy+0xca>
cmpl   $0x0,-0x14(%rbp)
jle    1204 <mystrncpy+0xf4>
mov    -0x14(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x14(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x8(%rbp)
movb   $0x79,(%rax)
jmp    11da <mystrncpy+0xca>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mystrncpy>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%r14d
mov    %rsi,%r15
mov    %rdi,%rbp
xor    %edi,%edi
test   %edx,%edx
setg   %dil
call   1040 <assert@plt>
xor    %edi,%edi
test   %rbp,%rbp
setne  %dil
call   1040 <assert@plt>
xor    %edi,%edi
test   %r15,%r15
setne  %dil
call   1040 <assert@plt>
cmp    $0x2,%r14d
jl     11a2 <mystrncpy+0x82>
xor    %eax,%eax
mov    %r14d,%edx
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
lea    -0x1(%rdx),%ecx
movzbl (%r15,%rax,1),%ebx
mov    %bl,0x0(%rbp)
add    $0x1,%rbp
add    $0x1,%rax
test   %bl,%bl
je     118e <mystrncpy+0x6e>
cmp    $0x2,%edx
mov    %ecx,%edx
ja     1170 <mystrncpy+0x50>
test   %bl,%bl
je     11c2 <mystrncpy+0xa2>
not    %eax
add    %r14d,%eax
movb   $0x0,0x0(%rbp)
add    $0x1,%rbp
mov    %eax,%r14d
test   %r14d,%r14d
jle    11b7 <mystrncpy+0x97>
mov    %r14d,%edx
mov    %rbp,%rdi
mov    $0x79,%esi
call   1030 <memset@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %ecx,%r14d
test   %r14d,%r14d
jg     11a7 <mystrncpy+0x87>
jmp    11b7 <mystrncpy+0x97>

LLVM IR (-O0):
define void @mystrncpy(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 i32, i32* %6
%9 = icmp sgt i32 %8, 0
%10 = zext i1 %9 to i32
%11 = call i32 @assert(i32 %10)
%12 = load i8*, i8** %4
%13 = icmp ne i8* null, %12
%14 = zext i1 %13 to i32
%15 = call i32 @assert(i32 %14)
%16 = load i8*, i8** %5
%17 = icmp ne i8* null, %16
%18 = zext i1 %17 to i32
%19 = call i32 @assert(i32 %18)
store i32 0, i32* %7
br label %20
20: ; preds = %35, %3
%21 = load i32, i32* %6
%22 = icmp sgt i32 %21, 1
br i1 %22, label %23, label %36
23: ; preds = %20
%24 = load i32, i32* %6
%25 = add nsw i32 %24, -1
store i32 %25, i32* %6
%26 = load i8*, i8** %5
%27 = getelementptr inbounds i8, i8* %26, i32 1
store i8* %27, i8** %5
%28 = load i8, i8* %26
%29 = load i8*, i8** %4
%30 = getelementptr inbounds i8, i8* %29, i32 1
store i8* %30, i8** %4
store i8 %28, i8* %29
%31 = sext i8 %28 to i32
store i32 %31, i32* %7
%32 = load i32, i32* %7
%33 = icmp eq i32 %32, 0
br i1 %33, label %34, label %35
34: ; preds = %23
br label %36
35: ; preds = %23
br label %20
36: ; preds = %34, %20
%37 = load i32, i32* %7
%38 = icmp ne i32 %37, 0
br i1 %38, label %39, label %44
39: ; preds = %36
%40 = load i32, i32* %6
%41 = add nsw i32 %40, -1
store i32 %41, i32* %6
%42 = load i8*, i8** %4
%43 = getelementptr inbounds i8, i8* %42, i32 1
store i8* %43, i8** %4
store i8 0, i8* %42
br label %44
44: ; preds = %39, %36
br label %45
45: ; preds = %48, %44
%46 = load i32, i32* %6
%47 = icmp sgt i32 %46, 0
br i1 %47, label %48, label %53
48: ; preds = %45
%49 = load i32, i32* %6
%50 = add nsw i32 %49, -1
store i32 %50, i32* %6
%51 = load i8*, i8** %4
%52 = getelementptr inbounds i8, i8* %51, i32 1
store i8* %52, i8** %4
store i8 121, i8* %51
br label %45
53: ; preds = %45
ret void
}

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