#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 *strncpy(char *, char const *, size_t);
char *euca_strncpy(char *restrict to, const char *restrict from, size_t size) {
  char *sRet = NULL;
  if (to != NULL) {
    if ((from == NULL) || (size == 0)) {
      (*to) = '\0';
      return (to);
    }
    sRet = strncpy(to, from, size);
    sRet[size - 1] = '\0';
  }
  return (sRet);
}


x86 assembly (clang, -O0):
<euca_strncpy>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1186 <euca_strncpy+0x76>
cmpq   $0x0,-0x18(%rbp)
je     114d <euca_strncpy+0x3d>
cmpq   $0x0,-0x20(%rbp)
jne    1161 <euca_strncpy+0x51>
mov    -0x10(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    118e <euca_strncpy+0x7e>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1030 <strncpy@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    -0x20(%rbp),%rcx
sub    $0x1,%rcx
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):
<euca_strncpy>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%rbx
test   %rdi,%rdi
je     1145 <euca_strncpy+0x35>
mov    %rbx,%rax
test   %rsi,%rsi
je     1142 <euca_strncpy+0x32>
mov    %rdx,%r14
mov    %rbx,%rax
test   %rdx,%rdx
je     1142 <euca_strncpy+0x32>
mov    %rbx,%rdi
mov    %r14,%rdx
call   1030 <strncpy@plt>
lea    (%r14,%rbx,1),%rax
add    $0xffffffffffffffff,%rax
movb   $0x0,(%rax)
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

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

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