#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;
  strcpy(buffer, str1);
  strncat(buffer, str2, 4);
  if (buffer[10] != '\0')
    return 1;
  printf("\tpassed.\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    11fa <test_strncat+0xba>
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     11e7 <test_strncat+0xa7>
movl   $0x1,-0x4(%rbp)
jmp    11fa <test_strncat+0xba>
lea    0xe2f(%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    0xea9(%rip),%rdi
call   1050 <printf@plt>
lea    0xe96(%rip),%rsi
mov    %rsp,%rbx
mov    %rbx,%rdi
call   1030 <strcpy@plt>
lea    0xe9a(%rip),%rsi
mov    %rbx,%rdi
mov    $0x14,%edx
call   1060 <strncat@plt>
lea    0xe80(%rip),%rsi
mov    %rbx,%rdi
call   1070 <strcmp@plt>
mov    $0x1,%ebx
test   %rax,%rax
jne    11dd <test_strncat+0x8d>
lea    0xe57(%rip),%rsi
mov    %rsp,%r14
mov    %r14,%rdi
call   1030 <strcpy@plt>
lea    0xe5b(%rip),%rsi
mov    %r14,%rdi
mov    $0x4,%edx
call   1060 <strncat@plt>
cmpb   $0x0,0xa(%rsp)
jne    11dd <test_strncat+0x8d>
lea    0xe47(%rip),%rdi
call   1040 <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 = [10 x i8] c"\09passed.\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 %30
16: ; preds = %0
%17 = getelementptr inbounds [20 x i8], [20 x i8]* %4, i64 0, i64 0
%18 = load i8*, i8** %2
%19 = call i32 @strcpy(i8* %17, i8* %18)
%20 = getelementptr inbounds [20 x i8], [20 x i8]* %4, i64 0, i64 0
%21 = load i8*, i8** %3
%22 = call i32 @strncat(i8* %20, i8* %21, i32 4)
%23 = getelementptr inbounds [20 x i8], [20 x i8]* %4, i64 0, i64 10
%24 = load i8, i8* %23
%25 = sext i8 %24 to i32
%26 = icmp ne i32 %25, 0
br i1 %26, label %27, label %28
27: ; preds = %16
store i32 1, i32* %1
br label %30
28: ; preds = %16
%29 = call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0))
store i32 0, i32* %1
br label %30
30: ; preds = %28, %27, %15
%31 = load i32, i32* %1
ret i32 %31
}

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"\09passed.\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 %16
8: ; preds = %0
%9 = call i32 @strcpy(i8* %2, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%10 = call i32 @strncat(i8* %2, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i32 4)
%11 = getelementptr inbounds [20 x i8], [20 x i8]* %1, i64 0, i64 10
%12 = load i8, i8* %11
%13 = icmp eq i8 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %8
%15 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @str, i64 0, i64 0))
br label %16
16: ; preds = %8, %0, %14
%17 = phi i32 [ 0, %14 ], [ 1, %0 ], [ 1, %8 ]
ret i32 %17
}