#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*/ exit(int /*<<< orphan*/);
char *malloc(int);
int /*<<< orphan*/ printf(char *);
char *realloc(char *, size_t);
int /*<<< orphan*/ strcat(char *, char const *);
int strlen(char const *);
char *strcatdyn(char *d, const char *s) {
  size_t numblocks;
  if (!d) {
    numblocks = 1;
    *(d = malloc(256 + 1)) = '\0';
  } else
    numblocks = strlen(d) / 256 + 1;
  if (!d) {
    printf("Error during dynamic memory reallocation: OUT OF MEMORY\n");
    exit(0);
  }
  while ((strlen(d) + strlen(s) + 1) > (numblocks * 256)) {
    d = realloc(d, ++numblocks * 256 + 1);
  }
  strcat(d, s);
  if (!d)
    return d;
  return d;
}


x86 assembly (clang, -O0):
<strcatdyn>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1199 <strcatdyn+0x39>
movq   $0x1,-0x20(%rbp)
mov    $0x101,%edi
call   1050 <malloc@plt>
mov    %rax,-0x10(%rbp)
movb   $0x0,(%rax)
jmp    11b3 <strcatdyn+0x53>
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    $0x100,%ecx
cltd
idiv   %ecx
add    $0x1,%eax
cltq
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11d1 <strcatdyn+0x71>
lea    0xe3b(%rip),%rdi
call   1040 <printf@plt>
xor    %edi,%edi
call   1080 <exit@plt>
jmp    11d6 <strcatdyn+0x76>
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x24(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x24(%rbp),%eax
add    %ecx,%eax
add    $0x1,%eax
cltq
mov    -0x20(%rbp),%rcx
shl    $0x8,%rcx
cmp    %rcx,%rax
jbe    122e <strcatdyn+0xce>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
add    $0x1,%rsi
mov    %rsi,-0x20(%rbp)
shl    $0x8,%rsi
add    $0x1,%rsi
call   1060 <realloc@plt>
mov    %rax,-0x10(%rbp)
jmp    11d6 <strcatdyn+0x76>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1070 <strcat@plt>
cmpq   $0x0,-0x10(%rbp)
jne    1253 <strcatdyn+0xf3>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    125b <strcatdyn+0xfb>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<strcatdyn>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
test   %rdi,%rdi
je     116e <strcatdyn+0x2e>
mov    %rdi,%r12
call   1030 <strlen@plt>
lea    0xff(%rax),%ecx
test   %eax,%eax
cmovns %eax,%ecx
sar    $0x8,%ecx
add    $0x1,%ecx
movslq %ecx,%rbx
jmp    1183 <strcatdyn+0x43>
mov    $0x101,%edi
call   1040 <malloc@plt>
mov    %rax,%r12
movb   $0x0,(%rax)
mov    $0x1,%ebx
mov    %r12,%rdi
call   1030 <strlen@plt>
mov    %eax,%r15d
mov    %r14,%rdi
call   1030 <strlen@plt>
add    %r15d,%eax
add    $0x1,%eax
cltq
shl    $0x8,%rbx
cmp    %rax,%rbx
jae    11e9 <strcatdyn+0xa9>
nopw   0x0(%rax,%rax,1)
lea    0x101(%rbx),%rsi
mov    %r12,%rdi
call   1050 <realloc@plt>
mov    %rax,%r12
mov    %rax,%rdi
call   1030 <strlen@plt>
mov    %eax,%r15d
mov    %r14,%rdi
call   1030 <strlen@plt>
add    %r15d,%eax
add    $0x1,%eax
cltq
add    $0x100,%rbx
cmp    %rax,%rbx
jb     11b0 <strcatdyn+0x70>
mov    %r12,%rdi
mov    %r14,%rsi
call   1060 <strcat@plt>
mov    %r12,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [57 x i8] c"Error during dynamic memory reallocation: OUT OF MEMORY\0A\00"
define i8* @strcatdyn(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i64
store i8* %0, i8** %4
store i8* %1, i8** %5
%7 = load i8*, i8** %4
%8 = icmp ne i8* %7, null
br i1 %8, label %11, label %9
9: ; preds = %2
store i64 1, i64* %6
%10 = call i8* @malloc(i32 257)
store i8* %10, i8** %4
store i8 0, i8* %10
br label %17
11: ; preds = %2
%12 = load i8*, i8** %4
%13 = call i32 @strlen(i8* %12)
%14 = sdiv i32 %13, 256
%15 = add nsw i32 %14, 1
%16 = sext i32 %15 to i64
store i64 %16, i64* %6
br label %17
17: ; preds = %11, %9
%18 = load i8*, i8** %4
%19 = icmp ne i8* %18, null
br i1 %19, label %23, label %20
20: ; preds = %17
%21 = call i32 @printf(i8* getelementptr inbounds ([57 x i8], [57 x i8]* @.str, i64 0, i64 0))
%22 = call i32 @exit(i32 0)
unreachable
23: ; preds = %17
br label %24
24: ; preds = %35, %23
%25 = load i8*, i8** %4
%26 = call i32 @strlen(i8* %25)
%27 = load i8*, i8** %5
%28 = call i32 @strlen(i8* %27)
%29 = add nsw i32 %26, %28
%30 = add nsw i32 %29, 1
%31 = sext i32 %30 to i64
%32 = load i64, i64* %6
%33 = mul i64 %32, 256
%34 = icmp ugt i64 %31, %33
br i1 %34, label %35, label %42
35: ; preds = %24
%36 = load i8*, i8** %4
%37 = load i64, i64* %6
%38 = add i64 %37, 1
store i64 %38, i64* %6
%39 = mul i64 %38, 256
%40 = add i64 %39, 1
%41 = call i8* @realloc(i8* %36, i64 %40)
store i8* %41, i8** %4
br label %24
42: ; preds = %24
%43 = load i8*, i8** %4
%44 = load i8*, i8** %5
%45 = call i32 @strcat(i8* %43, i8* %44)
%46 = load i8*, i8** %4
%47 = icmp ne i8* %46, null
br i1 %47, label %50, label %48
48: ; preds = %42
%49 = load i8*, i8** %4
store i8* %49, i8** %3
br label %52
50: ; preds = %42
%51 = load i8*, i8** %4
store i8* %51, i8** %3
br label %52
52: ; preds = %50, %48
%53 = load i8*, i8** %3
ret i8* %53
}

LLVM IR (-O2):
define i8* @strcatdyn(i8* %0, i8* %1) {
%3 = icmp eq i8* %0, null
br i1 %3, label %4, label %6
4: ; preds = %2
%5 = tail call dereferenceable_or_null(257) i8* @malloc(i32 257)
store i8 0, i8* %5
br label %11
6: ; preds = %2
%7 = tail call i32 @strlen(i8* %0)
%8 = sdiv i32 %7, 256
%9 = add nsw i32 %8, 1
%10 = sext i32 %9 to i64
br label %11
11: ; preds = %6, %4
%12 = phi i8* [ %0, %6 ], [ %5, %4 ]
%13 = phi i64 [ %10, %6 ], [ 1, %4 ]
%14 = tail call i32 @strlen(i8* %12)
%15 = tail call i32 @strlen(i8* %1)
%16 = add i32 %14, 1
%17 = add i32 %16, %15
%18 = sext i32 %17 to i64
%19 = shl nsw i64 %13, 8
%20 = icmp ult i64 %19, %18
br i1 %20, label %21, label %34
21: ; preds = %11, %21
%22 = phi i64 [ %24, %21 ], [ %13, %11 ]
%23 = phi i8* [ %27, %21 ], [ %12, %11 ]
%24 = add i64 %22, 1
%25 = shl i64 %24, 8
%26 = or i64 %25, 1
%27 = tail call i8* @realloc(i8* %23, i64 %26)
%28 = tail call i32 @strlen(i8* %27)
%29 = tail call i32 @strlen(i8* %1)
%30 = add i32 %28, 1
%31 = add i32 %30, %29
%32 = sext i32 %31 to i64
%33 = icmp ult i64 %25, %32
br i1 %33, label %21, label %34
34: ; preds = %21, %11
%35 = phi i8* [ %12, %11 ], [ %27, %21 ]
%36 = tail call i32 @strcat(i8* %35, i8* %1)
ret i8* %35
}