#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;
  else
    printf(".");
  strncpy(buffer, str, 10);
  if (strcmp(buffer, str))
    return 1;
  else
    printf(".");
  if (buffer[6] != '\0')
    return 1;
  else
    printf(".");
  if (buffer[10] != '\0')
    return 1;
  else
    printf(".");
  printf(" passed.\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    1243 <test_strncpy+0x113>
lea    0xe7b(%rip),%rdi
call   1040 <printf@plt>
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     11da <test_strncpy+0xaa>
movl   $0x1,-0x4(%rbp)
jmp    1243 <test_strncpy+0x113>
lea    0xe3a(%rip),%rdi
call   1040 <printf@plt>
movsbl -0x15(%rbp),%eax
cmp    $0x0,%eax
je     11ff <test_strncpy+0xcf>
movl   $0x1,-0x4(%rbp)
jmp    1243 <test_strncpy+0x113>
lea    0xe15(%rip),%rdi
call   1040 <printf@plt>
movsbl -0x11(%rbp),%eax
cmp    $0x0,%eax
je     1224 <test_strncpy+0xf4>
movl   $0x1,-0x4(%rbp)
jmp    1243 <test_strncpy+0x113>
lea    0xdf0(%rip),%rdi
call   1040 <printf@plt>
lea    0xde6(%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    0xe9c(%rip),%rdi
call   1060 <printf@plt>
lea    0xe7e(%rip),%rsi
mov    %rsp,%rdi
mov    $0x5,%edx
call   1040 <strncpy@plt>
mov    $0x1,%ebx
cmpb   $0x61,0x5(%rsp)
jne    1209 <test_strncpy+0xb9>
mov    $0x2e,%edi
call   1030 <putchar@plt>
lea    0xe54(%rip),%r14
mov    %rsp,%r15
mov    %r15,%rdi
mov    %r14,%rsi
mov    $0xa,%edx
call   1040 <strncpy@plt>
mov    %r15,%rdi
mov    %r14,%rsi
call   1070 <strcmp@plt>
test   %rax,%rax
jne    1209 <test_strncpy+0xb9>
mov    $0x2e,%edi
call   1030 <putchar@plt>
cmpb   $0x0,0x6(%rsp)
jne    1209 <test_strncpy+0xb9>
mov    $0x2e,%edi
call   1030 <putchar@plt>
cmpb   $0x0,0xa(%rsp)
jne    1209 <test_strncpy+0xb9>
mov    $0x2e,%edi
call   1030 <putchar@plt>
lea    0xe19(%rip),%rdi
call   1050 <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 = [2 x i8] c".\00"
@.str.3 = [10 x i8] c" passed.\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 %45
14: ; preds = %0
%15 = call i32 @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
br label %16
16: ; preds = %14
%17 = getelementptr inbounds [11 x i8], [11 x i8]* %3, i64 0, i64 0
%18 = load i8*, i8** %2
%19 = call i32 @strncpy(i8* %17, i8* %18, i32 10)
%20 = getelementptr inbounds [11 x i8], [11 x i8]* %3, i64 0, i64 0
%21 = load i8*, i8** %2
%22 = call i64 @strcmp(i8* %20, i8* %21)
%23 = icmp ne i64 %22, 0
br i1 %23, label %24, label %25
24: ; preds = %16
store i32 1, i32* %1
br label %45
25: ; preds = %16
%26 = call i32 @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
br label %27
27: ; preds = %25
%28 = getelementptr inbounds [11 x i8], [11 x i8]* %3, i64 0, i64 6
%29 = load i8, i8* %28
%30 = sext i8 %29 to i32
%31 = icmp ne i32 %30, 0
br i1 %31, label %32, label %33
32: ; preds = %27
store i32 1, i32* %1
br label %45
33: ; preds = %27
%34 = call i32 @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
br label %35
35: ; preds = %33
%36 = getelementptr inbounds [11 x i8], [11 x i8]* %3, i64 0, i64 10
%37 = load i8, i8* %36
%38 = sext i8 %37 to i32
%39 = icmp ne i32 %38, 0
br i1 %39, label %40, label %41
40: ; preds = %35
store i32 1, i32* %1
br label %45
41: ; preds = %35
%42 = call i32 @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
br label %43
43: ; preds = %41
%44 = call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0))
store i32 0, i32* %1
br label %45
45: ; preds = %43, %40, %32, %24, %13
%46 = load i32, i32* %1
ret i32 %46
}

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" passed.\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 %26
8: ; preds = %0
%9 = call i32 @putchar(i32 46)
%10 = call i32 @strncpy(i8* %2, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i32 10)
%11 = call i64 @strcmp(i8* %2, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%12 = icmp eq i64 %11, 0
br i1 %12, label %13, label %26
13: ; preds = %8
%14 = call i32 @putchar(i32 46)
%15 = getelementptr inbounds [11 x i8], [11 x i8]* %1, i64 0, i64 6
%16 = load i8, i8* %15
%17 = icmp eq i8 %16, 0
br i1 %17, label %18, label %26
18: ; preds = %13
%19 = call i32 @putchar(i32 46)
%20 = getelementptr inbounds [11 x i8], [11 x i8]* %1, i64 0, i64 10
%21 = load i8, i8* %20
%22 = icmp eq i8 %21, 0
br i1 %22, label %23, label %26
23: ; preds = %18
%24 = call i32 @putchar(i32 46)
%25 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @str, i64 0, i64 0))
br label %26
26: ; preds = %18, %13, %8, %0, %23
%27 = phi i32 [ 0, %23 ], [ 1, %0 ], [ 1, %8 ], [ 1, %13 ], [ 1, %18 ]
ret i32 %27
}