#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*/ memcpy(char *, char *, int);
int /*<<< orphan*/ putchar(char);
int /*<<< orphan*/ puts(char *);
void my_strncat(char *first, char *src, int dest_len) {
  char dest[dest_len];
  char *first_copy = first;
  char *second_copy = src;
  signed int first_len = 0;
  signed int second_len = 0;
  while (*first) {
    first_len++;
    first++;
  }
  first = first_copy;
  first_len++;
  if (first_len - dest_len >= 0) {
    puts("Error - 1st argument is just too big!");
    return;
  }
  dest_len -= first_len;
  while (*src) {
    second_len++;
    src++;
  }
  src = second_copy;
  if (dest_len > second_len)
    dest_len = second_len;
  memcpy(dest, first, first_len);
  memcpy(dest + (first_len - 1), src, dest_len);
  dest[first_len + dest_len] = '\0';
  putchar('\n');
  puts(dest);
}


x86 assembly (clang, -O0):
<my_strncat>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    %rsp,%rcx
mov    %rcx,-0x20(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x50(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x28(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x38(%rbp)
movl   $0x0,-0x3c(%rbp)
movl   $0x0,-0x40(%rbp)
mov    -0x8(%rbp),%rax
cmpb   $0x0,(%rax)
je     11ae <my_strncat+0x7e>
mov    -0x3c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x3c(%rbp)
mov    -0x8(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x8(%rbp)
jmp    1187 <my_strncat+0x57>
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x3c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x3c(%rbp)
mov    -0x3c(%rbp),%eax
sub    -0x14(%rbp),%eax
cmp    $0x0,%eax
jl     11e6 <my_strncat+0xb6>
lea    0xe2b(%rip),%rdi
call   1040 <puts@plt>
movl   $0x1,-0x44(%rbp)
jmp    1288 <my_strncat+0x158>
mov    -0x3c(%rbp),%ecx
mov    -0x14(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%rax
cmpb   $0x0,(%rax)
je     1218 <my_strncat+0xe8>
mov    -0x40(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x40(%rbp)
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
jmp    11f1 <my_strncat+0xc1>
mov    -0x38(%rbp),%rax
mov    %rax,-0x10(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x40(%rbp),%eax
jle    1232 <my_strncat+0x102>
mov    -0x40(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    -0x50(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x3c(%rbp),%edx
call   1050 <memcpy@plt>
mov    -0x50(%rbp),%rdi
mov    -0x3c(%rbp),%eax
sub    $0x1,%eax
cltq
add    %rax,%rdi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1050 <memcpy@plt>
mov    -0x50(%rbp),%rax
mov    -0x3c(%rbp),%ecx
add    -0x14(%rbp),%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    $0xa,%edi
call   1030 <putchar@plt>
mov    -0x50(%rbp),%rdi
call   1040 <puts@plt>
movl   $0x0,-0x44(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<my_strncat>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edx,%r15d
mov    %rsi,%r13
mov    %rdi,%rsi
mov    %edx,%eax
mov    %rsp,%r14
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r14
mov    %r14,%rsp
cmpb   $0x0,(%rdi)
je     117d <my_strncat+0x4d>
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
cmpb   $0x0,0x1(%rsi,%rbx,1)
lea    0x1(%rbx),%rbx
jne    1170 <my_strncat+0x40>
jmp    117f <my_strncat+0x4f>
xor    %ebx,%ebx
lea    0x1(%rbx),%r12d
cmp    %r15d,%r12d
jge    11ae <my_strncat+0x7e>
sub    %r12d,%r15d
cmpb   $0x0,0x0(%r13)
je     11c8 <my_strncat+0x98>
xor    %eax,%eax
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
cmpb   $0x0,0x1(%r13,%rax,1)
lea    0x1(%rax),%rax
jne    11a0 <my_strncat+0x70>
jmp    11ca <my_strncat+0x9a>
lea    0xe4b(%rip),%rdi
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <puts@plt>
xor    %eax,%eax
cmp    %eax,%r15d
cmovg  %eax,%r15d
mov    %r14,%rdi
mov    %r12d,%edx
call   1050 <memcpy@plt>
mov    %ebx,%edi
add    %r14,%rdi
mov    %r13,%rsi
mov    %r15d,%edx
call   1050 <memcpy@plt>
add    %r12d,%r15d
movslq %r15d,%rax
movb   $0x0,(%r14,%rax,1)
mov    $0xa,%edi
call   1030 <putchar@plt>
mov    %r14,%rdi
call   1040 <puts@plt>
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [38 x i8] c"Error - 1st argument is just too big!\00"
define void @my_strncat(i8* %0, i8* %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
store i32 %2, i32* %6
%14 = load i32, i32* %6
%15 = zext i32 %14 to i64
%16 = call i8* @llvm.stacksave()
store i8* %16, i8** %7
%17 = alloca i8, i64 %15
store i64 %15, i64* %8
%18 = load i8*, i8** %4
store i8* %18, i8** %9
%19 = load i8*, i8** %5
store i8* %19, i8** %10
store i32 0, i32* %11
store i32 0, i32* %12
br label %20
20: ; preds = %24, %3
%21 = load i8*, i8** %4
%22 = load i8, i8* %21
%23 = icmp ne i8 %22, 0
br i1 %23, label %24, label %29
24: ; preds = %20
%25 = load i32, i32* %11
%26 = add nsw i32 %25, 1
store i32 %26, i32* %11
%27 = load i8*, i8** %4
%28 = getelementptr inbounds i8, i8* %27, i32 1
store i8* %28, i8** %4
br label %20
29: ; preds = %20
%30 = load i8*, i8** %9
store i8* %30, i8** %4
%31 = load i32, i32* %11
%32 = add nsw i32 %31, 1
store i32 %32, i32* %11
%33 = load i32, i32* %11
%34 = load i32, i32* %6
%35 = sub nsw i32 %33, %34
%36 = icmp sge i32 %35, 0
br i1 %36, label %37, label %39
37: ; preds = %29
%38 = call i32 @puts(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %13
br label %77
39: ; preds = %29
%40 = load i32, i32* %11
%41 = load i32, i32* %6
%42 = sub nsw i32 %41, %40
store i32 %42, i32* %6
br label %43
43: ; preds = %47, %39
%44 = load i8*, i8** %5
%45 = load i8, i8* %44
%46 = icmp ne i8 %45, 0
br i1 %46, label %47, label %52
47: ; preds = %43
%48 = load i32, i32* %12
%49 = add nsw i32 %48, 1
store i32 %49, i32* %12
%50 = load i8*, i8** %5
%51 = getelementptr inbounds i8, i8* %50, i32 1
store i8* %51, i8** %5
br label %43
52: ; preds = %43
%53 = load i8*, i8** %10
store i8* %53, i8** %5
%54 = load i32, i32* %6
%55 = load i32, i32* %12
%56 = icmp sgt i32 %54, %55
br i1 %56, label %57, label %59
57: ; preds = %52
%58 = load i32, i32* %12
store i32 %58, i32* %6
br label %59
59: ; preds = %57, %52
%60 = load i8*, i8** %4
%61 = load i32, i32* %11
%62 = call i32 @memcpy(i8* %17, i8* %60, i32 %61)
%63 = load i32, i32* %11
%64 = sub nsw i32 %63, 1
%65 = sext i32 %64 to i64
%66 = getelementptr inbounds i8, i8* %17, i64 %65
%67 = load i8*, i8** %5
%68 = load i32, i32* %6
%69 = call i32 @memcpy(i8* %66, i8* %67, i32 %68)
%70 = load i32, i32* %11
%71 = load i32, i32* %6
%72 = add nsw i32 %70, %71
%73 = sext i32 %72 to i64
%74 = getelementptr inbounds i8, i8* %17, i64 %73
store i8 0, i8* %74
%75 = call i32 @putchar(i8 signext 10)
%76 = call i32 @puts(i8* %17)
store i32 0, i32* %13
br label %77
77: ; preds = %59, %37
%78 = load i8*, i8** %7
%79 = load i32, i32* %13
switch i32 %79, label %81 [
i32 0, label %80
i32 1, label %80
]
80: ; preds = %77, %77
ret void
81: ; preds = %77
unreachable
}

LLVM IR (-O2):
@.str = [38 x i8] c"Error - 1st argument is just too big!\00"
define void @my_strncat(i8* %0, i8* %1, i32 %2) {
%4 = zext i32 %2 to i64
%5 = alloca i8, i64 %4
%6 = load i8, i8* %0
%7 = icmp eq i8 %6, 0
br i1 %7, label %15, label %8
8: ; preds = %3, %8
%9 = phi i32 [ %11, %8 ], [ 0, %3 ]
%10 = phi i8* [ %12, %8 ], [ %0, %3 ]
%11 = add nuw nsw i32 %9, 1
%12 = getelementptr inbounds i8, i8* %10, i64 1
%13 = load i8, i8* %12
%14 = icmp eq i8 %13, 0
br i1 %14, label %15, label %8
15: ; preds = %8, %3
%16 = phi i32 [ 0, %3 ], [ %11, %8 ]
%17 = add nuw nsw i32 %16, 1
%18 = icmp slt i32 %17, %2
br i1 %18, label %21, label %19
19: ; preds = %15
%20 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0))
br label %45
21: ; preds = %15
%22 = sub nsw i32 %2, %17
%23 = load i8, i8* %1
%24 = icmp eq i8 %23, 0
br i1 %24, label %32, label %25
25: ; preds = %21, %25
%26 = phi i32 [ %28, %25 ], [ 0, %21 ]
%27 = phi i8* [ %29, %25 ], [ %1, %21 ]
%28 = add nuw nsw i32 %26, 1
%29 = getelementptr inbounds i8, i8* %27, i64 1
%30 = load i8, i8* %29
%31 = icmp eq i8 %30, 0
br i1 %31, label %32, label %25
32: ; preds = %25, %21
%33 = phi i32 [ 0, %21 ], [ %28, %25 ]
%34 = icmp sgt i32 %22, %33
%35 = select i1 %34, i32 %33, i32 %22
%36 = call i32 @memcpy(i8* %5, i8* %0, i32 %17)
%37 = zext i32 %16 to i64
%38 = getelementptr inbounds i8, i8* %5, i64 %37
%39 = call i32 @memcpy(i8* %38, i8* %1, i32 %35)
%40 = add nsw i32 %35, %17
%41 = sext i32 %40 to i64
%42 = getelementptr inbounds i8, i8* %5, i64 %41
store i8 0, i8* %42
%43 = call i32 @putchar(i8 signext 10)
%44 = call i32 @puts(i8* %5)
br label %45
45: ; preds = %32, %19
ret void
}