#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 _pkgconfig_error(int, char *, int /*<<< orphan*/);
int /*<<< orphan*/ errno;
char *realloc(char *, size_t);
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
size_t strlen(char *);
int /*<<< orphan*/ strncpy(char *, char const *, size_t);
int _string_append_length(char **string, char const *append, size_t length) {
  size_t slen = (*string != NULL) ? strlen(*string) : 0;
  size_t alen = (append != NULL) ? length : 0;
  char *p;
  if (alen == 0)
    return 0;
  if ((p = realloc(*string, slen + alen + 1)) == NULL)
    return -_pkgconfig_error(1, "%s", strerror(errno));
  *string = p;
  strncpy(*string + slen, append, alen);
  (*string)[slen + alen] = '\0';
  return 0;
}


x86 assembly (clang, -O0):
<_string_append_length>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rax
cmpq   $0x0,(%rax)
je     1187 <_string_append_length+0x37>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x40(%rbp)
jmp    1192 <_string_append_length+0x42>
xor    %eax,%eax
mov    %rax,-0x40(%rbp)
jmp    1192 <_string_append_length+0x42>
mov    -0x40(%rbp),%rax
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     11b2 <_string_append_length+0x62>
mov    -0x20(%rbp),%rax
mov    %rax,-0x48(%rbp)
jmp    11bd <_string_append_length+0x6d>
xor    %eax,%eax
mov    %rax,-0x48(%rbp)
jmp    11bd <_string_append_length+0x6d>
mov    -0x48(%rbp),%rax
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    11dc <_string_append_length+0x8c>
movl   $0x0,-0x4(%rbp)
jmp    126e <_string_append_length+0x11e>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    -0x28(%rbp),%rsi
add    -0x30(%rbp),%rsi
add    $0x1,%rsi
call   1060 <realloc@plt>
mov    %rax,-0x38(%rbp)
cmp    $0x0,%rax
jne    1231 <_string_append_length+0xe1>
mov    0x2dd7(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    %eax,%edx
mov    $0x1,%edi
lea    0xde2(%rip),%rsi
call   1050 <_pkgconfig_error@plt>
mov    %eax,%ecx
xor    %eax,%eax
sub    %ecx,%eax
mov    %eax,-0x4(%rbp)
jmp    126e <_string_append_length+0x11e>
mov    -0x38(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
add    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x30(%rbp),%rdx
call   1030 <strncpy@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    -0x28(%rbp),%rcx
add    -0x30(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_string_append_length>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r13
mov    %rsi,%r12
mov    %rdi,%r15
mov    (%rdi),%rbp
xor    %r14d,%r14d
test   %rbp,%rbp
je     1181 <_string_append_length+0x31>
mov    %rbp,%rdi
call   1040 <strlen@plt>
mov    %rax,%rbx
test   %r12,%r12
jne    1188 <_string_append_length+0x38>
jmp    11e9 <_string_append_length+0x99>
xor    %ebx,%ebx
test   %r12,%r12
je     11e9 <_string_append_length+0x99>
test   %r13,%r13
je     11e9 <_string_append_length+0x99>
lea    (%rbx,%r13,1),%rsi
add    $0x1,%rsi
mov    %rbp,%rdi
call   1060 <realloc@plt>
test   %rax,%rax
je     11c2 <_string_append_length+0x72>
mov    %rax,(%r15)
add    %rbx,%rax
add    %r13,%rbx
mov    %rax,%rdi
mov    %r12,%rsi
mov    %r13,%rdx
call   1030 <strncpy@plt>
mov    (%r15),%rax
movb   $0x0,(%rax,%rbx,1)
jmp    11e9 <_string_append_length+0x99>
mov    0x2e17(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
lea    0xe29(%rip),%rsi
mov    $0x1,%edi
mov    %eax,%edx
call   1050 <_pkgconfig_error@plt>
mov    %eax,%r14d
neg    %r14d
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"%s\00"
@errno = global i32 0
define i32 @_string_append_length(i8** %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i64
%9 = alloca i64
%10 = alloca i8*
store i8** %0, i8*** %5
store i8* %1, i8** %6
store i64 %2, i64* %7
%11 = load i8**, i8*** %5
%12 = load i8*, i8** %11
%13 = icmp ne i8* %12, null
br i1 %13, label %14, label %18
14: ; preds = %3
%15 = load i8**, i8*** %5
%16 = load i8*, i8** %15
%17 = call i64 @strlen(i8* %16)
br label %19
18: ; preds = %3
br label %19
19: ; preds = %18, %14
%20 = phi i64 [ %17, %14 ], [ 0, %18 ]
store i64 %20, i64* %8
%21 = load i8*, i8** %6
%22 = icmp ne i8* %21, null
br i1 %22, label %23, label %25
23: ; preds = %19
%24 = load i64, i64* %7
br label %26
25: ; preds = %19
br label %26
26: ; preds = %25, %23
%27 = phi i64 [ %24, %23 ], [ 0, %25 ]
store i64 %27, i64* %9
%28 = load i64, i64* %9
%29 = icmp eq i64 %28, 0
br i1 %29, label %30, label %31
30: ; preds = %26
store i32 0, i32* %4
br label %61
31: ; preds = %26
%32 = load i8**, i8*** %5
%33 = load i8*, i8** %32
%34 = load i64, i64* %8
%35 = load i64, i64* %9
%36 = add i64 %34, %35
%37 = add i64 %36, 1
%38 = call i8* @realloc(i8* %33, i64 %37)
store i8* %38, i8** %10
%39 = icmp eq i8* %38, null
br i1 %39, label %40, label %45
40: ; preds = %31
%41 = load i32, i32* @errno
%42 = call i32 @strerror(i32 %41)
%43 = call i32 @_pkgconfig_error(i32 1, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %42)
%44 = sub nsw i32 0, %43
store i32 %44, i32* %4
br label %61
45: ; preds = %31
%46 = load i8*, i8** %10
%47 = load i8**, i8*** %5
store i8* %46, i8** %47
%48 = load i8**, i8*** %5
%49 = load i8*, i8** %48
%50 = load i64, i64* %8
%51 = getelementptr inbounds i8, i8* %49, i64 %50
%52 = load i8*, i8** %6
%53 = load i64, i64* %9
%54 = call i32 @strncpy(i8* %51, i8* %52, i64 %53)
%55 = load i8**, i8*** %5
%56 = load i8*, i8** %55
%57 = load i64, i64* %8
%58 = load i64, i64* %9
%59 = add i64 %57, %58
%60 = getelementptr inbounds i8, i8* %56, i64 %59
store i8 0, i8* %60
store i32 0, i32* %4
br label %61
61: ; preds = %45, %40, %30
%62 = load i32, i32* %4
ret i32 %62
}

LLVM IR (-O2):
@.str = [3 x i8] c"%s\00"
@errno = global i32 0
define i32 @_string_append_length(i8** nocapture %0, i8* %1, i64 %2) {
%4 = load i8*, i8** %0
%5 = icmp eq i8* %4, null
br i1 %5, label %8, label %6
6: ; preds = %3
%7 = tail call i64 @strlen(i8* %4)
br label %8
8: ; preds = %3, %6
%9 = phi i64 [ %7, %6 ], [ 0, %3 ]
%10 = icmp eq i8* %1, null
%11 = icmp eq i64 %2, 0
%12 = or i1 %10, %11
br i1 %12, label %28, label %13
13: ; preds = %8
%14 = add i64 %9, %2
%15 = add i64 %14, 1
%16 = tail call i8* @realloc(i8* %4, i64 %15)
%17 = icmp eq i8* %16, null
br i1 %17, label %18, label %23
18: ; preds = %13
%19 = load i32, i32* @errno
%20 = tail call i32 @strerror(i32 %19)
%21 = tail call i32 @_pkgconfig_error(i32 1, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %20)
%22 = sub nsw i32 0, %21
br label %28
23: ; preds = %13
store i8* %16, i8** %0
%24 = getelementptr inbounds i8, i8* %16, i64 %9
%25 = tail call i32 @strncpy(i8* %24, i8* %1, i64 %2)
%26 = load i8*, i8** %0
%27 = getelementptr inbounds i8, i8* %26, i64 %14
store i8 0, i8* %27
br label %28
28: ; preds = %8, %23, %18
%29 = phi i32 [ %22, %18 ], [ 0, %23 ], [ 0, %8 ]
ret i32 %29
}