#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 *);
char *strncpy(char *, char *, unsigned int);
char *strmncpy(char *dest, char *source, unsigned int len) {
  char *pt;
  if (dest == NULL || source == NULL)
    return NULL;
  pt = strncpy(dest, source, len - 1);
  if (strlen(source) + 1 >= len)
    dest[len - 1] = 0;
  return pt;
}


x86 assembly (clang, -O0):
<strmncpy>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1149 <strmncpy+0x29>
cmpq   $0x0,-0x18(%rbp)
jne    1156 <strmncpy+0x36>
movq   $0x0,-0x8(%rbp)
jmp    119a <strmncpy+0x7a>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
sub    $0x1,%edx
call   1030 <strncpy@plt>
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
add    $0x1,%eax
cmp    -0x1c(%rbp),%eax
jb     1192 <strmncpy+0x72>
mov    -0x10(%rbp),%rax
mov    -0x1c(%rbp),%ecx
sub    $0x1,%ecx
mov    %ecx,%ecx
movb   $0x0,(%rax,%rcx,1)
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<strmncpy>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
xor    %r14d,%r14d
test   %rdi,%rdi
je     1166 <strmncpy+0x46>
mov    %rsi,%rbx
test   %rsi,%rsi
je     1166 <strmncpy+0x46>
mov    %edx,%r15d
mov    %rdi,%r12
lea    -0x1(%r15),%ebp
mov    %rbx,%rsi
mov    %ebp,%edx
call   1030 <strncpy@plt>
mov    %rax,%r14
mov    %rbx,%rdi
call   1040 <strlen@plt>
add    $0x1,%eax
cmp    %r15d,%eax
jb     1166 <strmncpy+0x46>
mov    %ebp,%eax
movb   $0x0,(%r12,%rax,1)
mov    %r14,%rax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
define i8* @strmncpy(i8* %0, i8* %1, i32 %2) {
%4 = icmp eq i8* %0, null
%5 = icmp eq i8* %1, null
%6 = or i1 %4, %5
br i1 %6, label %16, label %7
7: ; preds = %3
%8 = add i32 %2, -1
%9 = tail call i8* @strncpy(i8* %0, i8* %1, i32 %8)
%10 = tail call i32 @strlen(i8* %1)
%11 = add nsw i32 %10, 1
%12 = icmp ult i32 %11, %2
br i1 %12, label %16, label %13
13: ; preds = %7
%14 = zext i32 %8 to i64
%15 = getelementptr inbounds i8, i8* %0, i64 %14
store i8 0, i8* %15
br label %16
16: ; preds = %7, %13, %3
%17 = phi i8* [ null, %3 ], [ %9, %13 ], [ %9, %7 ]
ret i8* %17
}