#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 const *, size_t);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, size_t);
size_t strlen(char const *);
int append_string(char *s, const char *s2, size_t n) {
  size_t l1 = strlen(s);
  size_t l2 = strlen(s2);
  if (l1 + l2 < n) {
    memcpy(s + l1, s2, l2);
    memset(s + l1 + l2, 0, n - l1 - l2);
    return 0;
  } else {
    return -1;
  }
}


x86 assembly (clang, -O0):
<append_string>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x30(%rbp)
mov    -0x28(%rbp),%rax
add    -0x30(%rbp),%rax
cmp    -0x20(%rbp),%rax
jae    11b0 <append_string+0x80>
mov    -0x10(%rbp),%rdi
add    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x30(%rbp),%rdx
call   1050 <memcpy@plt>
mov    -0x10(%rbp),%rdi
add    -0x28(%rbp),%rdi
add    -0x30(%rbp),%rdi
mov    -0x20(%rbp),%rdx
sub    -0x28(%rbp),%rdx
sub    -0x30(%rbp),%rdx
xor    %esi,%esi
call   1040 <memset@plt>
movl   $0x0,-0x4(%rbp)
jmp    11b7 <append_string+0x87>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<append_string>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rsi,%rbp
mov    %rdi,%rbx
call   1030 <strlen@plt>
mov    %rax,%r13
mov    %rbp,(%rsp)
mov    %rbp,%rdi
call   1030 <strlen@plt>
lea    (%rax,%r13,1),%r15
mov    $0xffffffff,%ebp
cmp    %r14,%r15
jae    1190 <append_string+0x60>
mov    %rax,%r12
add    %r13,%rbx
mov    %rbx,%rdi
mov    (%rsp),%rsi
mov    %rax,%rdx
call   1050 <memcpy@plt>
add    %r12,%rbx
sub    %r15,%r14
xor    %ebp,%ebp
mov    %rbx,%rdi
xor    %esi,%esi
mov    %r14,%rdx
call   1040 <memset@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @append_string(i8* %0, i8* %1, i64 %2) {
%4 = alloca i32
%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** %5
%11 = call i64 @strlen(i8* %10)
store i64 %11, i64* %8
%12 = load i8*, i8** %6
%13 = call i64 @strlen(i8* %12)
store i64 %13, i64* %9
%14 = load i64, i64* %8
%15 = load i64, i64* %9
%16 = add i64 %14, %15
%17 = load i64, i64* %7
%18 = icmp ult i64 %16, %17
br i1 %18, label %19, label %37
19: ; preds = %3
%20 = load i8*, i8** %5
%21 = load i64, i64* %8
%22 = getelementptr inbounds i8, i8* %20, i64 %21
%23 = load i8*, i8** %6
%24 = load i64, i64* %9
%25 = call i32 @memcpy(i8* %22, i8* %23, i64 %24)
%26 = load i8*, i8** %5
%27 = load i64, i64* %8
%28 = getelementptr inbounds i8, i8* %26, i64 %27
%29 = load i64, i64* %9
%30 = getelementptr inbounds i8, i8* %28, i64 %29
%31 = load i64, i64* %7
%32 = load i64, i64* %8
%33 = sub i64 %31, %32
%34 = load i64, i64* %9
%35 = sub i64 %33, %34
%36 = call i32 @memset(i8* %30, i32 0, i64 %35)
store i32 0, i32* %4
br label %38
37: ; preds = %3
store i32 -1, i32* %4
br label %38
38: ; preds = %37, %19
%39 = load i32, i32* %4
ret i32 %39
}

LLVM IR (-O2):
define i32 @append_string(i8* %0, i8* %1, i64 %2) {
%4 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%5 = tail call i64 @strlen(i8* dereferenceable(1) %1)
%6 = add i64 %5, %4
%7 = icmp ult i64 %6, %2
br i1 %7, label %8, label %14
8: ; preds = %3
%9 = getelementptr inbounds i8, i8* %0, i64 %4
%10 = tail call i32 @memcpy(i8* %9, i8* %1, i64 %5)
%11 = getelementptr inbounds i8, i8* %9, i64 %5
%12 = sub i64 %2, %6
%13 = tail call i32 @memset(i8* %11, i32 0, i64 %12)
br label %14
14: ; preds = %3, %8
%15 = phi i32 [ 0, %8 ], [ -1, %3 ]
ret i32 %15
}