#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 *smartstrncpy(char *dest, char *src, size_t max_chars) {
  size_t copied;
  for (copied = 0; copied < max_chars; copied++, dest++, src++) {
    *dest = *src;
    if (*dest == 0)
      break;
  }
  *dest = 0;
  return dest;
}


x86 assembly (clang, -O0):
<smartstrncpy>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%rax
cmp    -0x18(%rbp),%rax
jae    1175 <smartstrncpy+0x75>
mov    -0x10(%rbp),%rax
mov    (%rax),%cl
mov    -0x8(%rbp),%rax
mov    %cl,(%rax)
mov    -0x8(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
jne    1147 <smartstrncpy+0x47>
jmp    1175 <smartstrncpy+0x75>
jmp    114c <smartstrncpy+0x4c>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x8(%rbp)
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
jmp    1118 <smartstrncpy+0x18>
mov    -0x8(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x8(%rbp),%rax
pop    %rbp
ret

x86 assembly (clang, -O2):
<smartstrncpy>:
mov    %rdi,%rax
test   %rdx,%rdx
je     1129 <smartstrncpy+0x29>
xor    %ecx,%ecx
nopw   0x0(%rax,%rax,1)
movzbl (%rsi,%rcx,1),%edi
mov    %dil,(%rax,%rcx,1)
test   %dil,%dil
je     1126 <smartstrncpy+0x26>
add    $0x1,%rcx
cmp    %rcx,%rdx
jne    1110 <smartstrncpy+0x10>
add    %rcx,%rax
movb   $0x0,(%rax)
ret

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

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