#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 */
size_t strlen(char const *);
int /*<<< orphan*/ strncat(char *, char const *, size_t);
size_t append_str(char *dst, const char *src, size_t size) {
  size_t srclen = strlen(src);
  size_t unused = size - strlen(dst) - 1;
  if (unused < srclen) {
    return -1;
  }
  strncat(dst, src, unused);
  return srclen;
}


x86 assembly (clang, -O0):
<append_str>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,%rcx
mov    -0x38(%rbp),%rax
sub    %rcx,%rax
sub    $0x1,%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
cmp    -0x28(%rbp),%rax
jae    117f <append_str+0x5f>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    1198 <append_str+0x78>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x30(%rbp),%rdx
call   1040 <strncat@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<append_str>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r12
mov    %rsi,%r14
mov    %rdi,%r15
mov    %rsi,%rdi
call   1030 <strlen@plt>
mov    %rax,%rbx
mov    %r15,%rdi
call   1030 <strlen@plt>
not    %rax
add    %r12,%rax
mov    $0xffffffffffffffff,%rcx
cmp    %rbx,%rax
jb     1167 <append_str+0x47>
mov    %r15,%rdi
mov    %r14,%rsi
mov    %rax,%rdx
call   1040 <strncat@plt>
mov    %rbx,%rcx
mov    %rcx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i64 @append_str(i8* %0, i8* %1, i64 %2) {
%4 = alloca i64
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i64
%9 = alloca i64
store i8* %0, i8** %5
store i8* %1, i8** %6
store i64 %2, i64* %7
%10 = load i8*, i8** %6
%11 = call i64 @strlen(i8* %10)
store i64 %11, i64* %8
%12 = load i64, i64* %7
%13 = load i8*, i8** %5
%14 = call i64 @strlen(i8* %13)
%15 = sub i64 %12, %14
%16 = sub i64 %15, 1
store i64 %16, i64* %9
%17 = load i64, i64* %9
%18 = load i64, i64* %8
%19 = icmp ult i64 %17, %18
br i1 %19, label %20, label %21
20: ; preds = %3
store i64 -1, i64* %4
br label %27
21: ; preds = %3
%22 = load i8*, i8** %5
%23 = load i8*, i8** %6
%24 = load i64, i64* %9
%25 = call i32 @strncat(i8* %22, i8* %23, i64 %24)
%26 = load i64, i64* %8
store i64 %26, i64* %4
br label %27
27: ; preds = %21, %20
%28 = load i64, i64* %4
ret i64 %28
}

LLVM IR (-O2):
define i64 @append_str(i8* %0, i8* %1, i64 %2) {
%4 = tail call i64 @strlen(i8* dereferenceable(1) %1)
%5 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%6 = xor i64 %5, -1
%7 = add i64 %6, %2
%8 = icmp ult i64 %7, %4
br i1 %8, label %11, label %9
9: ; preds = %3
%10 = tail call i32 @strncat(i8* %0, i8* %1, i64 %7)
br label %11
11: ; preds = %3, %9
%12 = phi i64 [ %4, %9 ], [ -1, %3 ]
ret i64 %12
}