#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(void *, int);
int /*<<< orphan*/ strcat(char *, char *);
scalar_t__ strlen(char *);
void ExtendString(char **ToExtend, char *ByThis, int *AllocatedLength) {
  while ((int)strlen(*ToExtend) + (int)strlen(ByThis) + 1 > *AllocatedLength) {
    *ToExtend = (char *)Realloc((void *)*ToExtend, *AllocatedLength + 1024);
    *AllocatedLength += 1024;
  }
  strcat(*ToExtend, ByThis);
}


x86 assembly (clang, -O0):
<ExtendString>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,%rcx
mov    -0x1c(%rbp),%eax
add    %ecx,%eax
add    $0x1,%eax
mov    -0x18(%rbp),%rcx
cmp    (%rcx),%eax
jle    11a8 <ExtendString+0x78>
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
mov    -0x18(%rbp),%rax
mov    (%rax),%esi
add    $0x400,%esi
call   1030 <Realloc@plt>
mov    %rax,%rcx
mov    -0x8(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x18(%rbp),%rax
mov    (%rax),%ecx
add    $0x400,%ecx
mov    %ecx,(%rax)
jmp    1144 <ExtendString+0x14>
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
mov    -0x10(%rbp),%rsi
call   1050 <strcat@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ExtendString>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r15
mov    %rsi,%r14
mov    %rdi,%r12
mov    (%rdi),%rbx
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    %rax,%rbp
mov    %r14,%rdi
call   1040 <strlen@plt>
add    %ebp,%eax
mov    (%r15),%ebp
cmp    %ebp,%eax
jl     11ae <ExtendString+0x7e>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
add    $0x400,%ebp
mov    %rbx,%rdi
mov    %ebp,%esi
call   1030 <Realloc@plt>
mov    %rax,%rbx
mov    %rax,(%r12)
mov    (%r15),%ebp
mov    $0x400,%eax
add    %eax,%ebp
mov    %ebp,(%r15)
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    %rax,%r13
mov    %r14,%rdi
call   1040 <strlen@plt>
add    %r13d,%eax
cmp    %ebp,%eax
jge    1170 <ExtendString+0x40>
mov    %rbx,%rdi
mov    %r14,%rsi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1050 <strcat@plt>

LLVM IR (-O0):
define void @ExtendString(i8** %0, i8* %1, i32* %2) {
%4 = alloca i8**
%5 = alloca i8*
%6 = alloca i32*
store i8** %0, i8*** %4
store i8* %1, i8** %5
store i32* %2, i32** %6
br label %7
7: ; preds = %20, %3
%8 = load i8**, i8*** %4
%9 = load i8*, i8** %8
%10 = call i64 @strlen(i8* %9)
%11 = trunc i64 %10 to i32
%12 = load i8*, i8** %5
%13 = call i64 @strlen(i8* %12)
%14 = trunc i64 %13 to i32
%15 = add nsw i32 %11, %14
%16 = add nsw i32 %15, 1
%17 = load i32*, i32** %6
%18 = load i32, i32* %17
%19 = icmp sgt i32 %16, %18
br i1 %19, label %20, label %32
20: ; preds = %7
%21 = load i8**, i8*** %4
%22 = load i8*, i8** %21
%23 = load i32*, i32** %6
%24 = load i32, i32* %23
%25 = add nsw i32 %24, 1024
%26 = call i64 @Realloc(i8* %22, i32 %25)
%27 = inttoptr i64 %26 to i8*
%28 = load i8**, i8*** %4
store i8* %27, i8** %28
%29 = load i32*, i32** %6
%30 = load i32, i32* %29
%31 = add nsw i32 %30, 1024
store i32 %31, i32* %29
br label %7
32: ; preds = %7
%33 = load i8**, i8*** %4
%34 = load i8*, i8** %33
%35 = load i8*, i8** %5
%36 = call i32 @strcat(i8* %34, i8* %35)
ret void
}

LLVM IR (-O2):
define void @ExtendString(i8** nocapture %0, i8* %1, i32* nocapture %2) {
%4 = load i8*, i8** %0
%5 = tail call i64 @strlen(i8* dereferenceable(1) %4)
%6 = trunc i64 %5 to i32
%7 = tail call i64 @strlen(i8* dereferenceable(1) %1)
%8 = trunc i64 %7 to i32
%9 = add nsw i32 %8, %6
%10 = load i32, i32* %2
%11 = icmp slt i32 %9, %10
br i1 %11, label %28, label %12
12: ; preds = %3, %12
%13 = phi i32 [ %19, %12 ], [ %10, %3 ]
%14 = phi i8* [ %17, %12 ], [ %4, %3 ]
%15 = add nsw i32 %13, 1024
%16 = tail call i64 @Realloc(i8* %14, i32 %15)
%17 = inttoptr i64 %16 to i8*
store i8* %17, i8** %0
%18 = load i32, i32* %2
%19 = add nsw i32 %18, 1024
store i32 %19, i32* %2
%20 = tail call i64 @strlen(i8* dereferenceable(1) %17)
%21 = trunc i64 %20 to i32
%22 = tail call i64 @strlen(i8* dereferenceable(1) %1)
%23 = trunc i64 %22 to i32
%24 = add nsw i32 %23, %21
%25 = icmp slt i32 %24, %19
br i1 %25, label %26, label %12
26: ; preds = %12
%27 = inttoptr i64 %16 to i8*
br label %28
28: ; preds = %26, %3
%29 = phi i8* [ %4, %3 ], [ %27, %26 ]
%30 = tail call i32 @strcat(i8* %29, i8* %1)
ret void
}