#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 *);
int /*<<< orphan*/ strcpy(char *, char const *);
int /*<<< orphan*/ strncat(char *, char const *, int);
int test_strncat(void) {
  const char *str1 = "hello ";
  const char *str2 = "world!";
  char buffer[20];
  printf("\tstrncat");
  strcpy(buffer, str1);
  strncat(buffer, str2, 20);
  if (strcmp(buffer, "hello world!"))
    return 1;
  else
    printf(".");
  strcpy(buffer, str1);
  strncat(buffer, str2, 4);
  if (buffer[10] != '\0')
    return 1;
  else
    printf(".");
  printf(" passed.\n");
  return 0;
}


x86 assembly (clang, -O0):
<test_strncat>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
lea    0xeb1(%rip),%rax
mov    %rax,-0x10(%rbp)
lea    0xebc(%rip),%rax
mov    %rax,-0x18(%rbp)
lea    0xea2(%rip),%rdi
call   1040 <printf@plt>
lea    -0x30(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1030 <strcpy@plt>
lea    -0x30(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    $0x14,%edx
call   1050 <strncat@plt>
lea    -0x30(%rbp),%rdi
lea    0xe7c(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
je     11af <test_strncat+0x6f>
movl   $0x1,-0x4(%rbp)
jmp    1212 <test_strncat+0xd2>
lea    0xe67(%rip),%rdi
call   1040 <printf@plt>
lea    -0x30(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1030 <strcpy@plt>
lea    -0x30(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    $0x4,%edx
call   1050 <strncat@plt>
movsbl -0x26(%rbp),%eax
cmp    $0x0,%eax
je     11f3 <test_strncat+0xb3>
movl   $0x1,-0x4(%rbp)
jmp    1212 <test_strncat+0xd2>
lea    0xe23(%rip),%rdi
call   1040 <printf@plt>
lea    0xe19(%rip),%rdi
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_strncat>:
push   %r14
push   %rbx
sub    $0x18,%rsp
lea    0xe99(%rip),%rdi
call   1060 <printf@plt>
lea    0xe86(%rip),%rsi
mov    %rsp,%rbx
mov    %rbx,%rdi
call   1040 <strcpy@plt>
lea    0xe8a(%rip),%rsi
mov    %rbx,%rdi
mov    $0x14,%edx
call   1070 <strncat@plt>
lea    0xe70(%rip),%rsi
mov    %rbx,%rdi
call   1080 <strcmp@plt>
mov    $0x1,%ebx
test   %rax,%rax
jne    1201 <test_strncat+0xa1>
mov    $0x2e,%edi
call   1030 <putchar@plt>
lea    0xe3d(%rip),%rsi
mov    %rsp,%r14
mov    %r14,%rdi
call   1040 <strcpy@plt>
lea    0xe41(%rip),%rsi
mov    %r14,%rdi
mov    $0x4,%edx
call   1070 <strncat@plt>
cmpb   $0x0,0xa(%rsp)
jne    1201 <test_strncat+0xa1>
mov    $0x2e,%edi
call   1030 <putchar@plt>
lea    0xe23(%rip),%rdi
call   1050 <puts@plt>
xor    %ebx,%ebx
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret

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

LLVM IR (-O2):
@.str = [7 x i8] c"hello \00"
@.str.1 = [7 x i8] c"world!\00"
@.str.2 = [9 x i8] c"\09strncat\00"
@.str.3 = [13 x i8] c"hello world!\00"
@str = [9 x i8] c" passed.\00"
define i32 @test_strncat() {
%1 = alloca [20 x i8]
%2 = getelementptr inbounds [20 x i8], [20 x i8]* %1, i64 0, i64 0
%3 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0))
%4 = call i32 @strcpy(i8* %2, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%5 = call i32 @strncat(i8* %2, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i32 20)
%6 = call i64 @strcmp(i8* %2, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
%7 = icmp eq i64 %6, 0
br i1 %7, label %8, label %18
8: ; preds = %0
%9 = call i32 @putchar(i32 46)
%10 = call i32 @strcpy(i8* %2, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%11 = call i32 @strncat(i8* %2, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i32 4)
%12 = getelementptr inbounds [20 x i8], [20 x i8]* %1, i64 0, i64 10
%13 = load i8, i8* %12
%14 = icmp eq i8 %13, 0
br i1 %14, label %15, label %18
15: ; preds = %8
%16 = call i32 @putchar(i32 46)
%17 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @str, i64 0, i64 0))
br label %18
18: ; preds = %8, %0, %15
%19 = phi i32 [ 0, %15 ], [ 1, %0 ], [ 1, %8 ]
ret i32 %19
}