#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*/ ENOSPC;
int /*<<< orphan*/ addlen(size_t, char **, size_t *);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ memcpy(char *, char const *, size_t);
int addstr(const char *src, size_t len, char **buf, size_t *buflen) {
  if (len > *buflen) {
    errno = ENOSPC;
    return (-1);
  }
  memcpy(*buf, src, len);
  addlen(len, buf, buflen);
  **buf = '\0';
  return (0);
}


x86 assembly (clang, -O0):
<addstr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    -0x18(%rbp),%rax
mov    -0x28(%rbp),%rcx
cmp    (%rcx),%rax
jbe    1167 <addstr+0x47>
mov    0x2e88(%rip),%rax
mov    (%rax),%ecx
mov    0x2e87(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    119d <addstr+0x7d>
mov    -0x20(%rbp),%rax
mov    (%rax),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1030 <memcpy@plt>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   1040 <addlen@plt>
mov    -0x20(%rbp),%rax
mov    (%rax),%rax
movb   $0x0,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<addstr>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%rbx
cmp    %rsi,(%rcx)
jae    1146 <addstr+0x26>
mov    0x2ea4(%rip),%rax
mov    (%rax),%eax
mov    0x2ea3(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffff,%eax
jmp    1170 <addstr+0x50>
mov    %rcx,%r15
mov    %rdx,%r14
mov    %rdi,%rsi
mov    (%rdx),%rdi
mov    %rbx,%rdx
call   1030 <memcpy@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %r15,%rdx
call   1040 <addlen@plt>
mov    (%r14),%rax
movb   $0x0,(%rax)
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
@ENOSPC = global i32 0
@errno = global i32 0
define i32 @addstr(i8* %0, i64 %1, i8** %2, i64* %3) {
%5 = load i64, i64* %3
%6 = icmp ult i64 %5, %1
br i1 %6, label %7, label %9
7: ; preds = %4
%8 = load i32, i32* @ENOSPC
store i32 %8, i32* @errno
br label %14
9: ; preds = %4
%10 = load i8*, i8** %2
%11 = tail call i32 @memcpy(i8* %10, i8* %0, i64 %1)
%12 = tail call i32 @addlen(i64 %1, i8** %2, i64* %3)
%13 = load i8*, i8** %2
store i8 0, i8* %13
br label %14
14: ; preds = %9, %7
%15 = phi i32 [ -1, %7 ], [ 0, %9 ]
ret i32 %15
}