#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*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ free(char *);
scalar_t__ malloc(size_t);
char *mempcpy(char *, char const *, size_t);
char *memset(char *, char, int);
scalar_t__ realloc(char *, size_t);
int /*<<< orphan*/ stderr;
size_t strlen(char const *);
int concat_str(char **str, const char *cat) {
  char *newp;
  char *wp;
  size_t str_len;
  size_t cat_len = strlen(cat);
  if (0) {
    fprintf(stderr, "++ in concat_str(), cat is \"%s\", cat_len is %zd\n", cat,
            cat_len);
  }
  if (*str == NULL) {
    if (0) {
      fprintf(stderr, "++ in concat_str(), *str is NULL\n");
    }
    str_len = 0;
    newp = (char *)malloc(cat_len + 1);
    if (newp == NULL) {
      if (0) {
        fprintf(stderr, "++ in concat_str(), newp is NULL\n");
      }
      return -1;
    }
  } else {
    if (0) {
      fprintf(stderr, "++ in concat_str(), *str is \"%s\"\n", *str);
    }
    str_len = strlen(*str);
    newp = (char *)realloc(*str, str_len + cat_len + 1);
    if (newp == NULL) {
      if (0) {
        fprintf(stderr, "++ in concat_str(), newp is NULL\n");
      }
      free(*str);
      *str = NULL;
      return -1;
    }
  }
  if (0) {
    fprintf(stderr, "++ in concat_str(), newp is %p\n", newp);
  }
  wp = newp + str_len;
  wp = mempcpy(wp, cat, cat_len);
  wp = memset(wp, '\0', 1);
  for (char *p = newp; p < newp + 5; p++) {
    if (0) {
      fprintf(stderr, "++ in concat_str(), newp[%p] is 0x%02X or \'%c\'\n", p,
              *p, *p);
    }
  }
  *str = newp;
  if (0) {
    fprintf(stderr, "++ in concat_str(), *str is %p\n", *str);
  }
  return 0;
}


x86 assembly (clang, -O0):
<concat_str>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x38(%rbp)
mov    -0x10(%rbp),%rax
cmpq   $0x0,(%rax)
jne    11c0 <concat_str+0x60>
movq   $0x0,-0x30(%rbp)
mov    -0x38(%rbp),%rdi
add    $0x1,%rdi
call   1070 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11bb <concat_str+0x5b>
movl   $0xffffffff,-0x4(%rbp)
jmp    12ac <concat_str+0x14c>
jmp    121f <concat_str+0xbf>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    -0x30(%rbp),%rsi
add    -0x38(%rbp),%rsi
add    $0x1,%rsi
call   1080 <realloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    121a <concat_str+0xba>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1030 <free@plt>
mov    -0x10(%rbp),%rax
movq   $0x0,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    12ac <concat_str+0x14c>
jmp    121f <concat_str+0xbf>
mov    -0x20(%rbp),%rax
add    -0x30(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    %rdi,-0x48(%rbp)
mov    -0x18(%rbp),%rsi
mov    -0x38(%rbp),%rdx
mov    %rdx,-0x50(%rbp)
call   1060 <memcpy@plt>
mov    -0x50(%rbp),%rcx
mov    -0x48(%rbp),%rax
add    %rcx,%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
xor    %esi,%esi
mov    $0x1,%edx
call   1050 <memset@plt>
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rax
mov    -0x20(%rbp),%rcx
add    $0x5,%rcx
cmp    %rcx,%rax
jae    129a <concat_str+0x13a>
jmp    1289 <concat_str+0x129>
mov    -0x40(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x40(%rbp)
jmp    126f <concat_str+0x10f>
mov    -0x20(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<concat_str>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r15
mov    %rdi,%r14
mov    %rsi,%rdi
call   1040 <strlen@plt>
mov    %rax,%r12
mov    (%r14),%rbp
test   %rbp,%rbp
je     11a9 <concat_str+0x49>
mov    %rbp,%rdi
call   1040 <strlen@plt>
mov    %rax,%rbx
lea    (%r12,%rax,1),%rsi
add    $0x1,%rsi
mov    %rbp,%rdi
call   1080 <realloc@plt>
test   %rax,%rax
je     11e4 <concat_str+0x84>
mov    %rax,%r13
jmp    11bd <concat_str+0x5d>
lea    0x1(%r12),%rdi
call   1070 <malloc@plt>
test   %rax,%rax
je     1208 <concat_str+0xa8>
mov    %rax,%r13
xor    %ebx,%ebx
add    %r13,%rbx
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %r12,%rdx
call   1060 <memcpy@plt>
add    %r12,%rbx
xor    %ebp,%ebp
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x1,%edx
call   1050 <memset@plt>
jmp    11f4 <concat_str+0x94>
mov    (%r14),%rdi
call   1030 <free@plt>
mov    $0xffffffff,%ebp
xor    %r13d,%r13d
mov    %r13,(%r14)
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    $0xffffffff,%ebp
jmp    11f7 <concat_str+0x97>

LLVM IR (-O0):
@stderr = global i32 0
define i32 @concat_str(i8** %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8**
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i64
%10 = alloca i8*
store i8** %0, i8*** %4
store i8* %1, i8** %5
%11 = load i8*, i8** %5
%12 = call i64 @strlen(i8* %11)
store i64 %12, i64* %9
%13 = load i8**, i8*** %4
%14 = load i8*, i8** %13
%15 = icmp eq i8* %14, null
br i1 %15, label %16, label %25
16: ; preds = %2
store i64 0, i64* %8
%17 = load i64, i64* %9
%18 = add i64 %17, 1
%19 = call i64 @malloc(i64 %18)
%20 = inttoptr i64 %19 to i8*
store i8* %20, i8** %6
%21 = load i8*, i8** %6
%22 = icmp eq i8* %21, null
br i1 %22, label %23, label %24
23: ; preds = %16
store i32 -1, i32* %3
br label %68
24: ; preds = %16
br label %45
25: ; preds = %2
%26 = load i8**, i8*** %4
%27 = load i8*, i8** %26
%28 = call i64 @strlen(i8* %27)
store i64 %28, i64* %8
%29 = load i8**, i8*** %4
%30 = load i8*, i8** %29
%31 = load i64, i64* %8
%32 = load i64, i64* %9
%33 = add i64 %31, %32
%34 = add i64 %33, 1
%35 = call i64 @realloc(i8* %30, i64 %34)
%36 = inttoptr i64 %35 to i8*
store i8* %36, i8** %6
%37 = load i8*, i8** %6
%38 = icmp eq i8* %37, null
br i1 %38, label %39, label %44
39: ; preds = %25
%40 = load i8**, i8*** %4
%41 = load i8*, i8** %40
%42 = call i32 @free(i8* %41)
%43 = load i8**, i8*** %4
store i8* null, i8** %43
store i32 -1, i32* %3
br label %68
44: ; preds = %25
br label %45
45: ; preds = %44, %24
%46 = load i8*, i8** %6
%47 = load i64, i64* %8
%48 = getelementptr inbounds i8, i8* %46, i64 %47
store i8* %48, i8** %7
%49 = load i8*, i8** %7
%50 = load i8*, i8** %5
%51 = load i64, i64* %9
%52 = call i8* @mempcpy(i8* %49, i8* %50, i64 %51)
store i8* %52, i8** %7
%53 = load i8*, i8** %7
%54 = call i8* @memset(i8* %53, i8 signext 0, i32 1)
store i8* %54, i8** %7
%55 = load i8*, i8** %6
store i8* %55, i8** %10
br label %56
56: ; preds = %62, %45
%57 = load i8*, i8** %10
%58 = load i8*, i8** %6
%59 = getelementptr inbounds i8, i8* %58, i64 5
%60 = icmp ult i8* %57, %59
br i1 %60, label %61, label %65
61: ; preds = %56
br label %62
62: ; preds = %61
%63 = load i8*, i8** %10
%64 = getelementptr inbounds i8, i8* %63, i32 1
store i8* %64, i8** %10
br label %56
65: ; preds = %56
%66 = load i8*, i8** %6
%67 = load i8**, i8*** %4
store i8* %66, i8** %67
store i32 0, i32* %3
br label %68
68: ; preds = %65, %39, %23
%69 = load i32, i32* %3
ret i32 %69
}

LLVM IR (-O2):
@stderr = global i32 0
define i32 @concat_str(i8** nocapture %0, i8* nocapture %1) {
%3 = tail call i64 @strlen(i8* dereferenceable(1) %1)
%4 = load i8*, i8** %0
%5 = icmp eq i8* %4, null
br i1 %5, label %6, label %10
6: ; preds = %2
%7 = add i64 %3, 1
%8 = tail call i64 @malloc(i64 %7)
%9 = icmp eq i64 %8, 0
br i1 %9, label %29, label %19
10: ; preds = %2
%11 = tail call i64 @strlen(i8* %4)
%12 = add i64 %3, 1
%13 = add i64 %12, %11
%14 = tail call i64 @realloc(i8* %4, i64 %13)
%15 = icmp eq i64 %14, 0
br i1 %15, label %16, label %19
16: ; preds = %10
%17 = load i8*, i8** %0
%18 = tail call i32 @free(i8* %17)
br label %26
19: ; preds = %10, %6
%20 = phi i64 [ %8, %6 ], [ %14, %10 ]
%21 = phi i64 [ 0, %6 ], [ %11, %10 ]
%22 = inttoptr i64 %20 to i8*
%23 = getelementptr inbounds i8, i8* %22, i64 %21
tail call void @llvm.memcpy.p0i8.p0i8.i64(i8* align 1 %23, i8* align 1 %1, i64 %3, i1 false)
%24 = getelementptr inbounds i8, i8* %23, i64 %3
%25 = tail call i8* @memset(i8* %24, i8 signext 0, i32 1)
br label %26
26: ; preds = %16, %19
%27 = phi i8* [ %22, %19 ], [ null, %16 ]
%28 = phi i32 [ 0, %19 ], [ -1, %16 ]
store i8* %27, i8** %0
br label %29
29: ; preds = %26, %6
%30 = phi i32 [ -1, %6 ], [ %28, %26 ]
ret i32 %30
}