#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__ realloc(char *, size_t);
int /*<<< orphan*/ strcat(char *, char *);
char *strchr(char *, char);
int strlen(char *);
char *dynamic_cat(char s[], size_t *size, char in[]) {
  while (&s[*size - 1] - strchr(s, '\0') < strlen(in)) {
    char *tmp = (char *)realloc(s, *size *= 2);
    if (!tmp)
      return NULL;
    else
      s = tmp;
  }
  strcat(s, in);
  return s;
}


x86 assembly (clang, -O0):
<dynamic_cat>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%rcx
mov    (%rcx),%rcx
sub    $0x1,%rcx
add    %rcx,%rax
mov    %rax,-0x38(%rbp)
mov    -0x10(%rbp),%rdi
xor    %esi,%esi
call   1040 <strchr@plt>
mov    %rax,%rcx
mov    -0x38(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x30(%rbp)
mov    -0x20(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x30(%rbp),%rax
movslq %ecx,%rcx
cmp    %rcx,%rax
jge    11de <dynamic_cat+0x9e>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rax
mov    (%rax),%rsi
shl    $0x1,%rsi
mov    %rsi,(%rax)
call   1050 <realloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11d1 <dynamic_cat+0x91>
movq   $0x0,-0x8(%rbp)
jmp    11f3 <dynamic_cat+0xb3>
mov    -0x28(%rbp),%rax
mov    %rax,-0x10(%rbp)
jmp    1154 <dynamic_cat+0x14>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1060 <strcat@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dynamic_cat>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%r12
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    (%r15),%r13
mov    %r12,%rdi
call   1030 <strlen@plt>
mov    %rax,%rbx
not    %rbx
add    %r13,%rbx
mov    %r14,%rdi
call   1030 <strlen@plt>
cltq
cmp    %rax,%rbx
jge    1191 <dynamic_cat+0x61>
mov    (%r15),%rsi
add    %rsi,%rsi
mov    %rsi,(%r15)
mov    %r12,%rdi
call   1040 <realloc@plt>
mov    %rax,%r12
test   %rax,%rax
jne    1150 <dynamic_cat+0x20>
xor    %r12d,%r12d
jmp    119c <dynamic_cat+0x6c>
mov    %r12,%rdi
mov    %r14,%rsi
call   1050 <strcat@plt>
mov    %r12,%rax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i8* @dynamic_cat(i8* %0, i64* %1, i8* %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i64*
%7 = alloca i8*
%8 = alloca i8*
store i8* %0, i8** %5
store i64* %1, i64** %6
store i8* %2, i8** %7
br label %9
9: ; preds = %36, %3
%10 = load i8*, i8** %5
%11 = load i64*, i64** %6
%12 = load i64, i64* %11
%13 = sub i64 %12, 1
%14 = getelementptr inbounds i8, i8* %10, i64 %13
%15 = load i8*, i8** %5
%16 = call i8* @strchr(i8* %15, i8 signext 0)
%17 = ptrtoint i8* %14 to i64
%18 = ptrtoint i8* %16 to i64
%19 = sub i64 %17, %18
%20 = load i8*, i8** %7
%21 = call i32 @strlen(i8* %20)
%22 = sext i32 %21 to i64
%23 = icmp slt i64 %19, %22
br i1 %23, label %24, label %37
24: ; preds = %9
%25 = load i8*, i8** %5
%26 = load i64*, i64** %6
%27 = load i64, i64* %26
%28 = mul i64 %27, 2
store i64 %28, i64* %26
%29 = call i64 @realloc(i8* %25, i64 %28)
%30 = inttoptr i64 %29 to i8*
store i8* %30, i8** %8
%31 = load i8*, i8** %8
%32 = icmp ne i8* %31, null
br i1 %32, label %34, label %33
33: ; preds = %24
store i8* null, i8** %4
br label %42
34: ; preds = %24
%35 = load i8*, i8** %8
store i8* %35, i8** %5
br label %36
36: ; preds = %34
br label %9
37: ; preds = %9
%38 = load i8*, i8** %5
%39 = load i8*, i8** %7
%40 = call i32 @strcat(i8* %38, i8* %39)
%41 = load i8*, i8** %5
store i8* %41, i8** %4
br label %42
42: ; preds = %37, %33
%43 = load i8*, i8** %4
ret i8* %43
}

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