#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 my_strlen(char *);
char *my_strncat(char *dest, char *src, int nb) {
  int i;
  int j;
  i = my_strlen(dest);
  j = 0;
  while (j < my_strlen(src) && j < nb) {
    dest[i] = src[j];
    i++;
    j++;
  }
  dest[i] = '\0';
  return (dest);
}


x86 assembly (clang, -O0):
<my_strncat>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <my_strlen@plt>
mov    %eax,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <my_strlen@plt>
mov    -0x24(%rbp),%ecx
mov    %eax,%edx
xor    %eax,%eax
cmp    %edx,%ecx
mov    %al,-0x1d(%rbp)
jge    1163 <my_strncat+0x53>
mov    -0x1c(%rbp),%eax
cmp    -0x14(%rbp),%eax
setl   %al
mov    %al,-0x1d(%rbp)
mov    -0x1d(%rbp),%al
test   $0x1,%al
jne    1173 <my_strncat+0x63>
jmp    11a0 <my_strncat+0x90>
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    (%rax,%rcx,1),%dl
mov    -0x8(%rbp),%rax
movslq -0x18(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1136 <my_strncat+0x26>
mov    -0x8(%rbp),%rax
movslq -0x18(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<my_strncat>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edx,%ebx
mov    %rsi,%rbp
mov    %rdi,%r15
call   1030 <my_strlen@plt>
mov    %eax,%r14d
mov    %rbp,%rdi
call   1030 <my_strlen@plt>
test   %ebx,%ebx
jle    1175 <my_strncat+0x65>
test   %eax,%eax
jle    1175 <my_strncat+0x65>
mov    %ebx,%r12d
movslq %r14d,%r13
add    %r15,%r13
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
movzbl 0x0(%rbp,%rbx,1),%eax
mov    %al,0x0(%r13,%rbx,1)
add    $0x1,%rbx
mov    %rbp,%rdi
call   1030 <my_strlen@plt>
cmp    %r12,%rbx
jae    1172 <my_strncat+0x62>
cltq
cmp    %rax,%rbx
jl     1150 <my_strncat+0x40>
add    %ebx,%r14d
movslq %r14d,%rax
movb   $0x0,(%r15,%rax,1)
mov    %r15,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i8* @my_strncat(i8* %0, i8* %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
store i32 %2, i32* %6
%9 = load i8*, i8** %4
%10 = call i32 @my_strlen(i8* %9)
store i32 %10, i32* %7
store i32 0, i32* %8
br label %11
11: ; preds = %22, %3
%12 = load i32, i32* %8
%13 = load i8*, i8** %5
%14 = call i32 @my_strlen(i8* %13)
%15 = icmp slt i32 %12, %14
br i1 %15, label %16, label %20
16: ; preds = %11
%17 = load i32, i32* %8
%18 = load i32, i32* %6
%19 = icmp slt i32 %17, %18
br label %20
20: ; preds = %16, %11
%21 = phi i1 [ false, %11 ], [ %19, %16 ]
br i1 %21, label %22, label %36
22: ; preds = %20
%23 = load i8*, i8** %5
%24 = load i32, i32* %8
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds i8, i8* %23, i64 %25
%27 = load i8, i8* %26
%28 = load i8*, i8** %4
%29 = load i32, i32* %7
%30 = sext i32 %29 to i64
%31 = getelementptr inbounds i8, i8* %28, i64 %30
store i8 %27, i8* %31
%32 = load i32, i32* %7
%33 = add nsw i32 %32, 1
store i32 %33, i32* %7
%34 = load i32, i32* %8
%35 = add nsw i32 %34, 1
store i32 %35, i32* %8
br label %11
36: ; preds = %20
%37 = load i8*, i8** %4
%38 = load i32, i32* %7
%39 = sext i32 %38 to i64
%40 = getelementptr inbounds i8, i8* %37, i64 %39
store i8 0, i8* %40
%41 = load i8*, i8** %4
ret i8* %41
}

LLVM IR (-O2):
define i8* @my_strncat(i8* returned %0, i8* %1, i32 %2) {
%4 = tail call i32 @my_strlen(i8* %0)
%5 = tail call i32 @my_strlen(i8* %1)
%6 = icmp sgt i32 %5, 0
%7 = icmp sgt i32 %2, 0
%8 = and i1 %7, %6
br i1 %8, label %9, label %27
9: ; preds = %3
%10 = zext i32 %2 to i64
%11 = sext i32 %4 to i64
br label %12
12: ; preds = %9, %12
%13 = phi i64 [ %11, %9 ], [ %18, %12 ]
%14 = phi i64 [ 0, %9 ], [ %19, %12 ]
%15 = getelementptr inbounds i8, i8* %1, i64 %14
%16 = load i8, i8* %15
%17 = getelementptr inbounds i8, i8* %0, i64 %13
store i8 %16, i8* %17
%18 = add nsw i64 %13, 1
%19 = add nuw nsw i64 %14, 1
%20 = tail call i32 @my_strlen(i8* %1)
%21 = sext i32 %20 to i64
%22 = icmp slt i64 %19, %21
%23 = icmp ult i64 %19, %10
%24 = and i1 %23, %22
br i1 %24, label %12, label %25
25: ; preds = %12
%26 = trunc i64 %18 to i32
br label %27
27: ; preds = %25, %3
%28 = phi i32 [ %4, %3 ], [ %26, %25 ]
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds i8, i8* %0, i64 %29
store i8 0, i8* %30
ret i8* %0
}