#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*/ printf(char *);
scalar_t__ strcmp(char *, char const *);
int /*<<< orphan*/ strncpy(char *, char const *, int);
int test_strncpy(void) {
  const char *str = "string";
  char buffer[11] = "aaaaaaaaaa";
  printf("\tstrncpy");
  strncpy(buffer, str, 5);
  if (buffer[5] != 'a')
    return 1;
  strncpy(buffer, str, 10);
  if (strcmp(buffer, str))
    return 1;
  if (buffer[6] != '\0')
    return 1;
  if (buffer[10] != '\0')
    return 1;
  printf("\tpassed.\n");
  return 0;
}


x86 assembly (clang, -O0):
<test_strncpy>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
lea    0xec1(%rip),%rax
mov    %rax,-0x10(%rbp)
mov    0xebd(%rip),%rax
mov    %rax,-0x1b(%rbp)
mov    0xeba(%rip),%ax
mov    %ax,-0x13(%rbp)
mov    0xeb2(%rip),%al
mov    %al,-0x11(%rbp)
lea    0xea9(%rip),%rdi
call   1040 <printf@plt>
lea    -0x1b(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    $0x5,%edx
call   1030 <strncpy@plt>
movsbl -0x16(%rbp),%eax
cmp    $0x61,%eax
je     1199 <test_strncpy+0x69>
movl   $0x1,-0x4(%rbp)
jmp    1213 <test_strncpy+0xe3>
lea    -0x1b(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    $0xa,%edx
call   1030 <strncpy@plt>
lea    -0x1b(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1050 <strcmp@plt>
cmp    $0x0,%rax
je     11ce <test_strncpy+0x9e>
movl   $0x1,-0x4(%rbp)
jmp    1213 <test_strncpy+0xe3>
movsbl -0x15(%rbp),%eax
cmp    $0x0,%eax
je     11e7 <test_strncpy+0xb7>
movl   $0x1,-0x4(%rbp)
jmp    1213 <test_strncpy+0xe3>
movsbl -0x11(%rbp),%eax
cmp    $0x0,%eax
je     1200 <test_strncpy+0xd0>
movl   $0x1,-0x4(%rbp)
jmp    1213 <test_strncpy+0xe3>
lea    0xe14(%rip),%rdi
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_strncpy>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
movabs $0x6161616161616161,%rax
61 61 61
mov    %rax,(%rsp)
movl   $0x616161,0x7(%rsp)
lea    0xeac(%rip),%rdi
call   1050 <printf@plt>
lea    0xe8e(%rip),%rsi
mov    %rsp,%rdi
mov    $0x5,%edx
call   1030 <strncpy@plt>
mov    $0x1,%ebx
cmpb   $0x61,0x5(%rsp)
jne    11d1 <test_strncpy+0x91>
lea    0xe6e(%rip),%r14
mov    %rsp,%r15
mov    %r15,%rdi
mov    %r14,%rsi
mov    $0xa,%edx
call   1030 <strncpy@plt>
mov    %r15,%rdi
mov    %r14,%rsi
call   1060 <strcmp@plt>
test   %rax,%rax
jne    11d1 <test_strncpy+0x91>
cmpb   $0x0,0x6(%rsp)
jne    11d1 <test_strncpy+0x91>
cmpb   $0x0,0xa(%rsp)
jne    11d1 <test_strncpy+0x91>
lea    0xe51(%rip),%rdi
call   1040 <puts@plt>
xor    %ebx,%ebx
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [7 x i8] c"string\00"
@__const.test_strncpy.buffer = [11 x i8] c"aaaaaaaaaa\00"
@.str.1 = [9 x i8] c"\09strncpy\00"
@.str.2 = [10 x i8] c"\09passed.\0A\00"
define i32 @test_strncpy() {
%1 = alloca i32
%2 = alloca i8*
%3 = alloca [11 x i8]
store i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8** %2
%4 = bitcast [11 x i8]* %3 to i8*
%5 = call i32 @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
%6 = getelementptr inbounds [11 x i8], [11 x i8]* %3, i64 0, i64 0
%7 = load i8*, i8** %2
%8 = call i32 @strncpy(i8* %6, i8* %7, i32 5)
%9 = getelementptr inbounds [11 x i8], [11 x i8]* %3, i64 0, i64 5
%10 = load i8, i8* %9
%11 = sext i8 %10 to i32
%12 = icmp ne i32 %11, 97
br i1 %12, label %13, label %14
13: ; preds = %0
store i32 1, i32* %1
br label %37
14: ; preds = %0
%15 = getelementptr inbounds [11 x i8], [11 x i8]* %3, i64 0, i64 0
%16 = load i8*, i8** %2
%17 = call i32 @strncpy(i8* %15, i8* %16, i32 10)
%18 = getelementptr inbounds [11 x i8], [11 x i8]* %3, i64 0, i64 0
%19 = load i8*, i8** %2
%20 = call i64 @strcmp(i8* %18, i8* %19)
%21 = icmp ne i64 %20, 0
br i1 %21, label %22, label %23
22: ; preds = %14
store i32 1, i32* %1
br label %37
23: ; preds = %14
%24 = getelementptr inbounds [11 x i8], [11 x i8]* %3, i64 0, i64 6
%25 = load i8, i8* %24
%26 = sext i8 %25 to i32
%27 = icmp ne i32 %26, 0
br i1 %27, label %28, label %29
28: ; preds = %23
store i32 1, i32* %1
br label %37
29: ; preds = %23
%30 = getelementptr inbounds [11 x i8], [11 x i8]* %3, i64 0, i64 10
%31 = load i8, i8* %30
%32 = sext i8 %31 to i32
%33 = icmp ne i32 %32, 0
br i1 %33, label %34, label %35
34: ; preds = %29
store i32 1, i32* %1
br label %37
35: ; preds = %29
%36 = call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0))
store i32 0, i32* %1
br label %37
37: ; preds = %35, %34, %28, %22, %13
%38 = load i32, i32* %1
ret i32 %38
}

LLVM IR (-O2):
@.str = [7 x i8] c"string\00"
@__const.test_strncpy.buffer = [11 x i8] c"aaaaaaaaaa\00"
@.str.1 = [9 x i8] c"\09strncpy\00"
@str = [9 x i8] c"\09passed.\00"
define i32 @test_strncpy() {
%1 = alloca [11 x i8]
%2 = getelementptr inbounds [11 x i8], [11 x i8]* %1, i64 0, i64 0
%3 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
%4 = call i32 @strncpy(i8* %2, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i32 5)
%5 = getelementptr inbounds [11 x i8], [11 x i8]* %1, i64 0, i64 5
%6 = load i8, i8* %5
%7 = icmp eq i8 %6, 97
br i1 %7, label %8, label %22
8: ; preds = %0
%9 = call i32 @strncpy(i8* %2, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i32 10)
%10 = call i64 @strcmp(i8* %2, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%11 = icmp eq i64 %10, 0
%12 = getelementptr inbounds [11 x i8], [11 x i8]* %1, i64 0, i64 6
%13 = load i8, i8* %12
%14 = icmp eq i8 %13, 0
%15 = select i1 %11, i1 %14, i1 false
%16 = getelementptr inbounds [11 x i8], [11 x i8]* %1, i64 0, i64 10
%17 = load i8, i8* %16
%18 = icmp eq i8 %17, 0
%19 = select i1 %15, i1 %18, i1 false
br i1 %19, label %20, label %22
20: ; preds = %8
%21 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @str, i64 0, i64 0))
br label %22
22: ; preds = %8, %0, %20
%23 = phi i32 [ 0, %20 ], [ 1, %0 ], [ 1, %8 ]
ret i32 %23
}