#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*/ err(int, char *);
char *realloc(char *, size_t);
char *strdup(char const *);
int /*<<< orphan*/ strlcat(char *, char const *, size_t);
int /*<<< orphan*/ strlcpy(char *, char *, size_t);
size_t strlen(char const *);
void astrcat(char **s, const char *append) {
  static size_t offset = 0;
  size_t newsiz;
  static const char *oldstr = NULL;
  char *newstr;
  if (!*s) {
    if (!(*s = strdup(append)))
      err(2, "astrcat");
    offset = strlen(*s);
    oldstr = *s;
    return;
  }
  if (oldstr != *s) {
    offset = strlen(*s);
    oldstr = *s;
  }
  newsiz = offset + 1 + strlen(append) + 1;
  newstr = realloc(*s, newsiz);
  if (newstr == NULL)
    err(2, "astrcat");
  *s = newstr;
  strlcpy(*s + offset, "\n", newsiz - offset);
  strlcat(*s + offset, append, newsiz - offset);
  offset = newsiz - 1;
  oldstr = *s;
}


x86 assembly (clang, -O0):
<astrcat>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rax
cmpq   $0x0,(%rax)
jne    11cf <astrcat+0x6f>
mov    -0x10(%rbp),%rdi
call   1080 <strdup@plt>
mov    -0x8(%rbp),%rcx
mov    %rax,(%rcx)
cmp    $0x0,%rax
jne    11a9 <astrcat+0x49>
mov    $0x2,%edi
lea    0xe5c(%rip),%rsi
call   1050 <err@plt>
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
call   1040 <strlen@plt>
mov    %rax,0x2e9c(%rip)
mov    -0x8(%rbp),%rax
mov    (%rax),%rax
mov    %rax,0x2e96(%rip)
jmp    12cd <astrcat+0x16d>
mov    0x2e8a(%rip),%rax
mov    -0x8(%rbp),%rcx
cmp    (%rcx),%rax
je     1204 <astrcat+0xa4>
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
call   1040 <strlen@plt>
mov    %rax,0x2e62(%rip)
mov    -0x8(%rbp),%rax
mov    (%rax),%rax
mov    %rax,0x2e5c(%rip)
mov    0x2e4d(%rip),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,%rcx
mov    -0x28(%rbp),%rax
add    %rcx,%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
mov    -0x18(%rbp),%rsi
call   1070 <realloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    125e <astrcat+0xfe>
mov    $0x2,%edi
lea    0xda7(%rip),%rsi
call   1050 <err@plt>
mov    -0x20(%rbp),%rcx
mov    -0x8(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
add    0x2de1(%rip),%rdi
mov    -0x18(%rbp),%rdx
sub    0x2dd6(%rip),%rdx
lea    0xd7f(%rip),%rsi
call   1060 <strlcpy@plt>
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
add    0x2dbc(%rip),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
sub    0x2dad(%rip),%rdx
call   1030 <strlcat@plt>
mov    -0x18(%rbp),%rax
sub    $0x1,%rax
mov    %rax,0x2d99(%rip)
mov    -0x8(%rbp),%rax
mov    (%rax),%rax
mov    %rax,0x2d93(%rip)
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<astrcat>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%r15
mov    (%rdi),%rbx
test   %rbx,%rbx
je     119d <astrcat+0x3d>
cmp    %rbx,0x2ee0(%rip)
je     11d8 <astrcat+0x78>
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    %rax,%r13
mov    %rax,0x2ec4(%rip)
mov    %rbx,0x2ec5(%rip)
jmp    11df <astrcat+0x7f>
mov    %r14,%rdi
call   1080 <strdup@plt>
mov    %rax,%rbx
mov    %rax,(%r15)
test   %rax,%rax
jne    11c4 <astrcat+0x64>
lea    0xe49(%rip),%rsi
mov    $0x2,%edi
call   1050 <err@plt>
mov    (%r15),%rbx
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    %rax,0x2e85(%rip)
jmp    1262 <astrcat+0x102>
mov    0x2e79(%rip),%r13
mov    %r14,%rdi
call   1040 <strlen@plt>
lea    (%rax,%r13,1),%rbp
add    $0x1,%rbp
lea    (%rax,%r13,1),%r12
add    $0x2,%r12
mov    %rbx,%rdi
mov    %r12,%rsi
call   1070 <realloc@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    1222 <astrcat+0xc2>
lea    0xdef(%rip),%rsi
mov    $0x2,%edi
call   1050 <err@plt>
mov    0x2e36(%rip),%r13
mov    %rbx,(%r15)
add    %r13,%rbx
mov    %r12,%rdx
sub    %r13,%rdx
lea    0xdd3(%rip),%rsi
mov    %rbx,%rdi
call   1060 <strlcpy@plt>
mov    0x2e14(%rip),%rax
mov    (%r15),%rdi
add    %rax,%rdi
sub    %rax,%r12
mov    %r14,%rsi
mov    %r12,%rdx
call   1030 <strlcat@plt>
mov    %rbp,0x2df9(%rip)
mov    (%r15),%rbx
mov    %rbx,0x2df7(%rip)
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@astrcat.offset = internal global i64 0
@astrcat.oldstr = internal global i8* null
@.str = [8 x i8] c"astrcat\00"
@.str.1 = [2 x i8] c"\0A\00"
define void @astrcat(i8** %0, i8* %1) {
%3 = alloca i8**
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i8*
store i8** %0, i8*** %3
store i8* %1, i8** %4
%7 = load i8**, i8*** %3
%8 = load i8*, i8** %7
%9 = icmp ne i8* %8, null
br i1 %9, label %23, label %10
10: ; preds = %2
%11 = load i8*, i8** %4
%12 = call i8* @strdup(i8* %11)
%13 = load i8**, i8*** %3
store i8* %12, i8** %13
%14 = icmp ne i8* %12, null
br i1 %14, label %17, label %15
15: ; preds = %10
%16 = call i32 @err(i32 2, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
br label %17
17: ; preds = %15, %10
%18 = load i8**, i8*** %3
%19 = load i8*, i8** %18
%20 = call i64 @strlen(i8* %19)
store i64 %20, i64* @astrcat.offset
%21 = load i8**, i8*** %3
%22 = load i8*, i8** %21
store i8* %22, i8** @astrcat.oldstr
br label %73
23: ; preds = %2
%24 = load i8*, i8** @astrcat.oldstr
%25 = load i8**, i8*** %3
%26 = load i8*, i8** %25
%27 = icmp ne i8* %24, %26
br i1 %27, label %28, label %34
28: ; preds = %23
%29 = load i8**, i8*** %3
%30 = load i8*, i8** %29
%31 = call i64 @strlen(i8* %30)
store i64 %31, i64* @astrcat.offset
%32 = load i8**, i8*** %3
%33 = load i8*, i8** %32
store i8* %33, i8** @astrcat.oldstr
br label %34
34: ; preds = %28, %23
%35 = load i64, i64* @astrcat.offset
%36 = add i64 %35, 1
%37 = load i8*, i8** %4
%38 = call i64 @strlen(i8* %37)
%39 = add i64 %36, %38
%40 = add i64 %39, 1
store i64 %40, i64* %5
%41 = load i8**, i8*** %3
%42 = load i8*, i8** %41
%43 = load i64, i64* %5
%44 = call i8* @realloc(i8* %42, i64 %43)
store i8* %44, i8** %6
%45 = load i8*, i8** %6
%46 = icmp eq i8* %45, null
br i1 %46, label %47, label %49
47: ; preds = %34
%48 = call i32 @err(i32 2, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
br label %49
49: ; preds = %47, %34
%50 = load i8*, i8** %6
%51 = load i8**, i8*** %3
store i8* %50, i8** %51
%52 = load i8**, i8*** %3
%53 = load i8*, i8** %52
%54 = load i64, i64* @astrcat.offset
%55 = getelementptr inbounds i8, i8* %53, i64 %54
%56 = load i64, i64* %5
%57 = load i64, i64* @astrcat.offset
%58 = sub i64 %56, %57
%59 = call i32 @strlcpy(i8* %55, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i64 %58)
%60 = load i8**, i8*** %3
%61 = load i8*, i8** %60
%62 = load i64, i64* @astrcat.offset
%63 = getelementptr inbounds i8, i8* %61, i64 %62
%64 = load i8*, i8** %4
%65 = load i64, i64* %5
%66 = load i64, i64* @astrcat.offset
%67 = sub i64 %65, %66
%68 = call i32 @strlcat(i8* %63, i8* %64, i64 %67)
%69 = load i64, i64* %5
%70 = sub i64 %69, 1
store i64 %70, i64* @astrcat.offset
%71 = load i8**, i8*** %3
%72 = load i8*, i8** %71
store i8* %72, i8** @astrcat.oldstr
br label %73
73: ; preds = %49, %17
ret void
}

LLVM IR (-O2):
@astrcat.offset = internal unnamed_addr global i64 0
@astrcat.oldstr = internal unnamed_addr global i8* null
@.str = [8 x i8] c"astrcat\00"
@.str.1 = [2 x i8] c"\0A\00"
define void @astrcat(i8** nocapture %0, i8* %1) {
%3 = load i8*, i8** %0
%4 = icmp eq i8* %3, null
br i1 %4, label %5, label %14
5: ; preds = %2
%6 = tail call i8* @strdup(i8* %1)
store i8* %6, i8** %0
%7 = icmp eq i8* %6, null
br i1 %7, label %8, label %11
8: ; preds = %5
%9 = tail call i32 @err(i32 2, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%10 = load i8*, i8** %0
br label %11
11: ; preds = %8, %5
%12 = phi i8* [ %10, %8 ], [ %6, %5 ]
%13 = tail call i64 @strlen(i8* dereferenceable(1) %12)
store i64 %13, i64* @astrcat.offset
br label %43
14: ; preds = %2
%15 = load i8*, i8** @astrcat.oldstr
%16 = icmp eq i8* %15, %3
br i1 %16, label %17, label %19
17: ; preds = %14
%18 = load i64, i64* @astrcat.offset
br label %21
19: ; preds = %14
%20 = tail call i64 @strlen(i8* %3)
store i64 %20, i64* @astrcat.offset
store i8* %3, i8** @astrcat.oldstr
br label %21
21: ; preds = %17, %19
%22 = phi i64 [ %18, %17 ], [ %20, %19 ]
%23 = add i64 %22, 1
%24 = tail call i64 @strlen(i8* dereferenceable(1) %1)
%25 = add i64 %23, %24
%26 = add i64 %25, 1
%27 = tail call i8* @realloc(i8* %3, i64 %26)
%28 = icmp eq i8* %27, null
br i1 %28, label %29, label %32
29: ; preds = %21
%30 = tail call i32 @err(i32 2, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%31 = load i64, i64* @astrcat.offset
br label %32
32: ; preds = %29, %21
%33 = phi i64 [ %31, %29 ], [ %22, %21 ]
store i8* %27, i8** %0
%34 = getelementptr inbounds i8, i8* %27, i64 %33
%35 = sub i64 %26, %33
%36 = tail call i32 @strlcpy(i8* %34, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i64 %35)
%37 = load i8*, i8** %0
%38 = load i64, i64* @astrcat.offset
%39 = getelementptr inbounds i8, i8* %37, i64 %38
%40 = sub i64 %26, %38
%41 = tail call i32 @strlcat(i8* %39, i8* %1, i64 %40)
store i64 %25, i64* @astrcat.offset
%42 = load i8*, i8** %0
br label %43
43: ; preds = %32, %11
%44 = phi i8* [ %12, %11 ], [ %42, %32 ]
store i8* %44, i8** @astrcat.oldstr
ret void
}