#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 */
typedef int /*<<< orphan*/ a;

/* Variables and functions */
int /*<<< orphan*/ printf(char *, char *);
int strlen(char *);
int /*<<< orphan*/ strncpy(char *, char *, int);
void strncpy_test() {
  char a[10];
  char *p = "world hello";
  strncpy(a, "hello", sizeof(a));
  strncpy(a + strlen(a), p, sizeof(a) - strlen(a) - 1);
  printf("a = %s\n", a);
}


x86 assembly (clang, -O0):
<strncpy_test>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
lea    0xec1(%rip),%rax
mov    %rax,-0x18(%rbp)
lea    -0xa(%rbp),%rdi
lea    0xeb8(%rip),%rsi
mov    $0xa,%edx
call   1030 <strncpy@plt>
lea    -0xa(%rbp),%rax
mov    %rax,-0x30(%rbp)
lea    -0xa(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x30(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
lea    -0xa(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%rsi
movslq %eax,%rcx
mov    $0xa,%eax
sub    %rcx,%rax
sub    $0x1,%rax
mov    %eax,%edx
call   1030 <strncpy@plt>
lea    -0xa(%rbp),%rsi
lea    0xe59(%rip),%rdi
call   1050 <printf@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<strncpy_test>:
push   %r14
push   %rbx
sub    $0x18,%rsp
lea    0xec8(%rip),%rsi
lea    0xe(%rsp),%r14
mov    %r14,%rdi
mov    $0xa,%edx
call   1030 <strncpy@plt>
mov    %r14,%rdi
call   1040 <strlen@plt>
cltq
lea    (%rsp,%rax,1),%rbx
add    $0xe,%rbx
mov    %r14,%rdi
call   1040 <strlen@plt>
mov    $0x9,%edx
sub    %eax,%edx
lea    0xe88(%rip),%rsi
mov    %rbx,%rdi
call   1030 <strncpy@plt>
lea    0xe85(%rip),%rdi
mov    %r14,%rsi
call   1050 <printf@plt>
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [12 x i8] c"world hello\00"
@.str.1 = [6 x i8] c"hello\00"
@.str.2 = [8 x i8] c"a = %s\0A\00"
define void @strncpy_test() {
%1 = alloca [10 x i8]
%2 = alloca i8*
store i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i8** %2
%3 = getelementptr inbounds [10 x i8], [10 x i8]* %1, i64 0, i64 0
%4 = call i32 @strncpy(i8* %3, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i32 10)
%5 = getelementptr inbounds [10 x i8], [10 x i8]* %1, i64 0, i64 0
%6 = getelementptr inbounds [10 x i8], [10 x i8]* %1, i64 0, i64 0
%7 = call i32 @strlen(i8* %6)
%8 = sext i32 %7 to i64
%9 = getelementptr inbounds i8, i8* %5, i64 %8
%10 = load i8*, i8** %2
%11 = getelementptr inbounds [10 x i8], [10 x i8]* %1, i64 0, i64 0
%12 = call i32 @strlen(i8* %11)
%13 = sext i32 %12 to i64
%14 = sub i64 10, %13
%15 = sub i64 %14, 1
%16 = trunc i64 %15 to i32
%17 = call i32 @strncpy(i8* %9, i8* %10, i32 %16)
%18 = getelementptr inbounds [10 x i8], [10 x i8]* %1, i64 0, i64 0
%19 = call i32 @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i8* %18)
ret void
}

LLVM IR (-O2):
@.str = [12 x i8] c"world hello\00"
@.str.1 = [6 x i8] c"hello\00"
@.str.2 = [8 x i8] c"a = %s\0A\00"
define void @strncpy_test() {
%1 = alloca [10 x i8]
%2 = getelementptr inbounds [10 x i8], [10 x i8]* %1, i64 0, i64 0
%3 = call i32 @strncpy(i8* %2, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i32 10)
%4 = call i32 @strlen(i8* %2)
%5 = sext i32 %4 to i64
%6 = getelementptr inbounds [10 x i8], [10 x i8]* %1, i64 0, i64 %5
%7 = call i32 @strlen(i8* %2)
%8 = sub i32 9, %7
%9 = call i32 @strncpy(i8* %6, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %8)
%10 = call i32 @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i8* %2)
ret void
}