#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*/ __set_errno(int /*<<< orphan*/);
int /*<<< orphan*/ addlen(size_t, char **, size_t *);
int /*<<< orphan*/ memcpy(char *, char const *, size_t);
int addstr(const char *src, size_t len, char **buf, size_t *buflen) {
  if (len >= *buflen) {
    __set_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
jb     1173 <addstr+0x43>
mov    0x2e80(%rip),%rax
mov    (%rax),%edi
call   1030 <__set_errno@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11a9 <addstr+0x79>
mov    -0x20(%rbp),%rax
mov    (%rax),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1040 <memcpy@plt>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   1050 <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)
jbe    1169 <addstr+0x39>
mov    %rcx,%r15
mov    %rdx,%r14
mov    %rdi,%rsi
mov    (%rdx),%rdi
mov    %rbx,%rdx
call   1040 <memcpy@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %r15,%rdx
call   1050 <addlen@plt>
mov    (%r14),%rax
movb   $0x0,(%rax)
xor    %eax,%eax
jmp    117c <addstr+0x4c>
mov    0x2e70(%rip),%rax
mov    (%rax),%edi
call   1030 <__set_errno@plt>
mov    $0xffffffff,%eax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@ENOSPC = 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 uge i64 %10, %12
br i1 %13, label %14, label %17
14: ; preds = %4
%15 = load i32, i32* @ENOSPC
%16 = call i32 @__set_errno(i32 %15)
store i32 -1, i32* %5
br label %29
17: ; preds = %4
%18 = load i8**, i8*** %8
%19 = load i8*, i8** %18
%20 = load i8*, i8** %6
%21 = load i64, i64* %7
%22 = call i32 @memcpy(i8* %19, i8* %20, i64 %21)
%23 = load i64, i64* %7
%24 = load i8**, i8*** %8
%25 = load i64*, i64** %9
%26 = call i32 @addlen(i64 %23, i8** %24, i64* %25)
%27 = load i8**, i8*** %8
%28 = load i8*, i8** %27
store i8 0, i8* %28
store i32 0, i32* %5
br label %29
29: ; preds = %17, %14
%30 = load i32, i32* %5
ret i32 %30
}

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