#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 */
scalar_t__ malloc(size_t);
scalar_t__ realloc(char *, size_t);
int /*<<< orphan*/ strcat(char *, char const *);
int /*<<< orphan*/ strcpy(char *, char *);
size_t strlen(char const *);
size_t _clog_append_str(char **dst, char *orig_buf, const char *src,
                        size_t cur_size) {
  size_t new_size = cur_size;
  while (strlen(*dst) + strlen(src) >= new_size) {
    new_size *= 2;
  }
  if (new_size != cur_size) {
    if (*dst == orig_buf) {
      *dst = (char *)malloc(new_size);
      strcpy(*dst, orig_buf);
    } else {
      *dst = (char *)realloc(*dst, new_size);
    }
  }
  strcat(*dst, src);
  return new_size;
}


x86 assembly (clang, -O0):
<_clog_append_str>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,%rcx
mov    -0x30(%rbp),%rax
add    %rcx,%rax
cmp    -0x28(%rbp),%rax
jb     11ae <_clog_append_str+0x5e>
mov    -0x28(%rbp),%rax
shl    $0x1,%rax
mov    %rax,-0x28(%rbp)
jmp    1170 <_clog_append_str+0x20>
mov    -0x28(%rbp),%rax
cmp    -0x20(%rbp),%rax
je     1214 <_clog_append_str+0xc4>
mov    -0x8(%rbp),%rax
mov    (%rax),%rax
cmp    -0x10(%rbp),%rax
jne    11f5 <_clog_append_str+0xa5>
mov    -0x28(%rbp),%rdi
call   1050 <malloc@plt>
mov    %rax,%rcx
mov    -0x8(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
mov    -0x10(%rbp),%rsi
call   1030 <strcpy@plt>
jmp    120f <_clog_append_str+0xbf>
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
mov    -0x28(%rbp),%rsi
call   1060 <realloc@plt>
mov    %rax,%rcx
mov    -0x8(%rbp),%rax
mov    %rcx,(%rax)
jmp    1214 <_clog_append_str+0xc4>
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
mov    -0x18(%rbp),%rsi
call   1070 <strcat@plt>
mov    -0x28(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_clog_append_str>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rcx,%rbp
mov    %rdx,%r14
mov    %rsi,%r13
mov    %rdi,%r15
mov    (%rdi),%r12
mov    %r12,%rdi
call   1040 <strlen@plt>
mov    %rax,%rbx
mov    %r14,%rdi
call   1040 <strlen@plt>
add    %rbx,%rax
mov    %rbp,%rcx
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %rcx,%rbx
add    %rcx,%rcx
cmp    %rbx,%rax
jae    1190 <_clog_append_str+0x40>
cmp    %rbp,%rbx
je     11d1 <_clog_append_str+0x81>
cmp    %r13,%r12
je     11b8 <_clog_append_str+0x68>
mov    %r12,%rdi
mov    %rbx,%rsi
call   1060 <realloc@plt>
mov    %rax,%r12
mov    %rax,(%r15)
jmp    11d1 <_clog_append_str+0x81>
mov    %rbx,%rdi
call   1050 <malloc@plt>
mov    %rax,(%r15)
mov    %rax,%rdi
mov    %r13,%rsi
call   1030 <strcpy@plt>
mov    (%r15),%r12
mov    %r12,%rdi
mov    %r14,%rsi
call   1070 <strcat@plt>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i64 @_clog_append_str(i8** %0, i8* %1, i8* %2, i64 %3) {
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i64
store i8** %0, i8*** %5
store i8* %1, i8** %6
store i8* %2, i8** %7
store i64 %3, i64* %8
%10 = load i64, i64* %8
store i64 %10, i64* %9
br label %11
11: ; preds = %20, %4
%12 = load i8**, i8*** %5
%13 = load i8*, i8** %12
%14 = call i64 @strlen(i8* %13)
%15 = load i8*, i8** %7
%16 = call i64 @strlen(i8* %15)
%17 = add i64 %14, %16
%18 = load i64, i64* %9
%19 = icmp uge i64 %17, %18
br i1 %19, label %20, label %23
20: ; preds = %11
%21 = load i64, i64* %9
%22 = mul i64 %21, 2
store i64 %22, i64* %9
br label %11
23: ; preds = %11
%24 = load i64, i64* %9
%25 = load i64, i64* %8
%26 = icmp ne i64 %24, %25
br i1 %26, label %27, label %49
27: ; preds = %23
%28 = load i8**, i8*** %5
%29 = load i8*, i8** %28
%30 = load i8*, i8** %6
%31 = icmp eq i8* %29, %30
br i1 %31, label %32, label %41
32: ; preds = %27
%33 = load i64, i64* %9
%34 = call i64 @malloc(i64 %33)
%35 = inttoptr i64 %34 to i8*
%36 = load i8**, i8*** %5
store i8* %35, i8** %36
%37 = load i8**, i8*** %5
%38 = load i8*, i8** %37
%39 = load i8*, i8** %6
%40 = call i32 @strcpy(i8* %38, i8* %39)
br label %48
41: ; preds = %27
%42 = load i8**, i8*** %5
%43 = load i8*, i8** %42
%44 = load i64, i64* %9
%45 = call i64 @realloc(i8* %43, i64 %44)
%46 = inttoptr i64 %45 to i8*
%47 = load i8**, i8*** %5
store i8* %46, i8** %47
br label %48
48: ; preds = %41, %32
br label %49
49: ; preds = %48, %23
%50 = load i8**, i8*** %5
%51 = load i8*, i8** %50
%52 = load i8*, i8** %7
%53 = call i32 @strcat(i8* %51, i8* %52)
%54 = load i64, i64* %9
ret i64 %54
}

LLVM IR (-O2):
define i64 @_clog_append_str(i8** nocapture %0, i8* %1, i8* %2, i64 %3) {
%5 = load i8*, i8** %0
%6 = tail call i64 @strlen(i8* dereferenceable(1) %5)
%7 = tail call i64 @strlen(i8* dereferenceable(1) %2)
%8 = add i64 %7, %6
br label %9
9: ; preds = %9, %4
%10 = phi i64 [ %3, %4 ], [ %12, %9 ]
%11 = icmp ult i64 %8, %10
%12 = shl i64 %10, 1
br i1 %11, label %13, label %9
13: ; preds = %9
%14 = icmp eq i64 %10, %3
br i1 %14, label %25, label %15
15: ; preds = %13
%16 = icmp eq i8* %5, %1
br i1 %16, label %17, label %22
17: ; preds = %15
%18 = tail call i64 @malloc(i64 %10)
%19 = inttoptr i64 %18 to i8*
store i8* %19, i8** %0
%20 = tail call i32 @strcpy(i8* %19, i8* %1)
%21 = load i8*, i8** %0
br label %25
22: ; preds = %15
%23 = tail call i64 @realloc(i8* %5, i64 %10)
%24 = inttoptr i64 %23 to i8*
store i8* %24, i8** %0
br label %25
25: ; preds = %17, %22, %13
%26 = phi i8* [ %21, %17 ], [ %24, %22 ], [ %5, %13 ]
%27 = tail call i32 @strcat(i8* %26, i8* %2)
ret i64 %10
}