#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*/ ln();
scalar_t__ malloc(int);
int /*<<< orphan*/ printf(char *, char, ...);
char *realloc(char *, int);
int /*<<< orphan*/ strcat(char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
size_t strlen(char *);
void realloc2() {
  char *str = (char *)malloc(6);
  ;
  strcpy(str, "hello");
  str = realloc(str, sizeof(char) * 10);
  printf("length:%zd, %s\n", strlen(str), str);
  strcat(str, " world");
  printf("length:%zd, %s\n", strlen(str), str);
  int index = 0;
  char *tmp = str;
  while (*tmp) {
    printf("%c\t", *tmp);
    tmp++;
    index++;
  }
  ln();
  printf("%d\n", index);
  str[strlen(str)] = 'A';
  strcat(str, "Hello Clang");
  printf("length:%zd, %s\n", strlen(str), str);
  tmp = str;
  while (*tmp) {
    printf("%c\t", *tmp);
    tmp++;
    index++;
  }
  ln();
}


x86 assembly (clang, -O0):
<realloc2>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    $0x6,%edi
call   1060 <malloc@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe6f(%rip),%rsi
call   1030 <strcpy@plt>
mov    -0x8(%rbp),%rdi
mov    $0xa,%esi
call   1070 <realloc@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x8(%rbp),%rdx
lea    0xe4a(%rip),%rdi
movsbl %al,%esi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x8(%rbp),%rdi
lea    0xe45(%rip),%rsi
call   1080 <strcat@plt>
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x8(%rbp),%rdx
lea    0xe1c(%rip),%rdi
movsbl %al,%esi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x0,-0xc(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
cmpb   $0x0,(%rax)
je     123f <realloc2+0xcf>
mov    -0x18(%rbp),%rax
lea    0xe02(%rip),%rdi
movsbl (%rax),%esi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    1203 <realloc2+0x93>
mov    $0x0,%al
call   1090 <ln@plt>
mov    -0xc(%rbp),%eax
lea    0xdd1(%rip),%rdi
movsbl %al,%esi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x8(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,%rcx
mov    -0x20(%rbp),%rax
movb   $0x41,(%rax,%rcx,1)
mov    -0x8(%rbp),%rdi
lea    0xda4(%rip),%rsi
call   1080 <strcat@plt>
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x8(%rbp),%rdx
lea    0xd6c(%rip),%rdi
movsbl %al,%esi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x8(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
cmpb   $0x0,(%rax)
je     12e8 <realloc2+0x178>
mov    -0x18(%rbp),%rax
lea    0xd59(%rip),%rdi
movsbl (%rax),%esi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    12ac <realloc2+0x13c>
mov    $0x0,%al
call   1090 <ln@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<realloc2>:
push   %r15
push   %r14
push   %rbx
mov    $0x6,%edi
call   1060 <malloc@plt>
mov    %rax,%rbx
lea    0xe77(%rip),%rsi
mov    %rax,%rdi
call   1030 <strcpy@plt>
mov    %rbx,%rdi
mov    $0xa,%esi
call   1070 <realloc@plt>
mov    %rax,%r15
mov    %rax,%rdi
call   1040 <strlen@plt>
movsbl %al,%esi
lea    0xe53(%rip),%r14
xor    %ebx,%ebx
mov    %r14,%rdi
mov    %r15,%rdx
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xe4d(%rip),%rsi
mov    %r15,%rdi
call   1080 <strcat@plt>
mov    %r15,%rdi
call   1040 <strlen@plt>
movsbl %al,%esi
mov    %r14,%rdi
mov    %r15,%rdx
xor    %eax,%eax
call   1050 <printf@plt>
mov    (%r15),%al
test   %al,%al
je     121b <realloc2+0xab>
lea    0xe26(%rip),%r14
xor    %ebx,%ebx
nopl   0x0(%rax)
movsbl %al,%esi
mov    %r14,%rdi
xor    %eax,%eax
call   1050 <printf@plt>
movzbl 0x1(%r15,%rbx,1),%eax
add    $0x1,%rbx
test   %al,%al
jne    1200 <realloc2+0x90>
xor    %eax,%eax
call   1090 <ln@plt>
movsbl %bl,%esi
lea    0xdf5(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %r15,%rdi
call   1040 <strlen@plt>
movb   $0x41,(%r15,%rax,1)
lea    0xdde(%rip),%rsi
mov    %r15,%rdi
call   1080 <strcat@plt>
mov    %r15,%rdi
call   1040 <strlen@plt>
movsbl %al,%esi
lea    0xda5(%rip),%rdi
mov    %r15,%rdx
xor    %eax,%eax
call   1050 <printf@plt>
mov    (%r15),%al
test   %al,%al
je     1299 <realloc2+0x129>
add    $0x1,%r15
lea    0xda0(%rip),%r14
nopl   (%rax)
movsbl %al,%esi
mov    %r14,%rdi
xor    %eax,%eax
call   1050 <printf@plt>
movzbl (%r15),%eax
add    $0x1,%r15
test   %al,%al
jne    1280 <realloc2+0x110>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %r15
jmp    1090 <ln@plt>

LLVM IR (-O0):
@.str = [6 x i8] c"hello\00"
@.str.1 = [16 x i8] c"length:%zd, %s\0A\00"
@.str.2 = [7 x i8] c" world\00"
@.str.3 = [4 x i8] c"%c\09\00"
@.str.4 = [4 x i8] c"%d\0A\00"
@.str.5 = [12 x i8] c"Hello Clang\00"
define void @realloc2() {
%1 = alloca i8*
%2 = alloca i32
%3 = alloca i8*
%4 = call i64 @malloc(i32 6)
%5 = inttoptr i64 %4 to i8*
store i8* %5, i8** %1
%6 = load i8*, i8** %1
%7 = call i32 @strcpy(i8* %6, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%8 = load i8*, i8** %1
%9 = call i8* @realloc(i8* %8, i32 10)
store i8* %9, i8** %1
%10 = load i8*, i8** %1
%11 = call i64 @strlen(i8* %10)
%12 = trunc i64 %11 to i8
%13 = load i8*, i8** %1
%14 = call i32 (i8*, i8, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i8 signext %12, i8* %13)
%15 = load i8*, i8** %1
%16 = call i32 @strcat(i8* %15, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%17 = load i8*, i8** %1
%18 = call i64 @strlen(i8* %17)
%19 = trunc i64 %18 to i8
%20 = load i8*, i8** %1
%21 = call i32 (i8*, i8, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i8 signext %19, i8* %20)
store i32 0, i32* %2
%22 = load i8*, i8** %1
store i8* %22, i8** %3
br label %23
23: ; preds = %27, %0
%24 = load i8*, i8** %3
%25 = load i8, i8* %24
%26 = icmp ne i8 %25, 0
br i1 %26, label %27, label %35
27: ; preds = %23
%28 = load i8*, i8** %3
%29 = load i8, i8* %28
%30 = call i32 (i8*, i8, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i8 signext %29)
%31 = load i8*, i8** %3
%32 = getelementptr inbounds i8, i8* %31, i32 1
store i8* %32, i8** %3
%33 = load i32, i32* %2
%34 = add nsw i32 %33, 1
store i32 %34, i32* %2
br label %23
35: ; preds = %23
%36 = call i32 (...) @ln()
%37 = load i32, i32* %2
%38 = trunc i32 %37 to i8
%39 = call i32 (i8*, i8, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i8 signext %38)
%40 = load i8*, i8** %1
%41 = load i8*, i8** %1
%42 = call i64 @strlen(i8* %41)
%43 = getelementptr inbounds i8, i8* %40, i64 %42
store i8 65, i8* %43
%44 = load i8*, i8** %1
%45 = call i32 @strcat(i8* %44, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.5, i64 0, i64 0))
%46 = load i8*, i8** %1
%47 = call i64 @strlen(i8* %46)
%48 = trunc i64 %47 to i8
%49 = load i8*, i8** %1
%50 = call i32 (i8*, i8, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i8 signext %48, i8* %49)
%51 = load i8*, i8** %1
store i8* %51, i8** %3
br label %52
52: ; preds = %56, %35
%53 = load i8*, i8** %3
%54 = load i8, i8* %53
%55 = icmp ne i8 %54, 0
br i1 %55, label %56, label %64
56: ; preds = %52
%57 = load i8*, i8** %3
%58 = load i8, i8* %57
%59 = call i32 (i8*, i8, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i8 signext %58)
%60 = load i8*, i8** %3
%61 = getelementptr inbounds i8, i8* %60, i32 1
store i8* %61, i8** %3
%62 = load i32, i32* %2
%63 = add nsw i32 %62, 1
store i32 %63, i32* %2
br label %52
64: ; preds = %52
%65 = call i32 (...) @ln()
ret void
}

LLVM IR (-O2):
@.str = [6 x i8] c"hello\00"
@.str.1 = [16 x i8] c"length:%zd, %s\0A\00"
@.str.2 = [7 x i8] c" world\00"
@.str.3 = [4 x i8] c"%c\09\00"
@.str.4 = [4 x i8] c"%d\0A\00"
@.str.5 = [12 x i8] c"Hello Clang\00"
define void @realloc2() {
%1 = tail call i64 @malloc(i32 6)
%2 = inttoptr i64 %1 to i8*
%3 = tail call i32 @strcpy(i8* %2, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%4 = tail call i8* @realloc(i8* %2, i32 10)
%5 = tail call i64 @strlen(i8* dereferenceable(1) %4)
%6 = trunc i64 %5 to i8
%7 = tail call i32 (i8*, i8, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i8 signext %6, i8* %4)
%8 = tail call i32 @strcat(i8* %4, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%9 = tail call i64 @strlen(i8* dereferenceable(1) %4)
%10 = trunc i64 %9 to i8
%11 = tail call i32 (i8*, i8, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i8 signext %10, i8* %4)
%12 = load i8, i8* %4
%13 = icmp eq i8 %12, 0
br i1 %13, label %25, label %14
14: ; preds = %0, %14
%15 = phi i8 [ %21, %14 ], [ %12, %0 ]
%16 = phi i8* [ %19, %14 ], [ %4, %0 ]
%17 = phi i32 [ %20, %14 ], [ 0, %0 ]
%18 = tail call i32 (i8*, i8, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i8 signext %15)
%19 = getelementptr inbounds i8, i8* %16, i64 1
%20 = add nuw nsw i32 %17, 1
%21 = load i8, i8* %19
%22 = icmp eq i8 %21, 0
br i1 %22, label %23, label %14
23: ; preds = %14
%24 = trunc i32 %20 to i8
br label %25
25: ; preds = %23, %0
%26 = phi i8 [ 0, %0 ], [ %24, %23 ]
%27 = tail call i32 (...) @ln()
%28 = tail call i32 (i8*, i8, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i8 signext %26)
%29 = tail call i64 @strlen(i8* dereferenceable(1) %4)
%30 = getelementptr inbounds i8, i8* %4, i64 %29
store i8 65, i8* %30
%31 = tail call i32 @strcat(i8* %4, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.5, i64 0, i64 0))
%32 = tail call i64 @strlen(i8* dereferenceable(1) %4)
%33 = trunc i64 %32 to i8
%34 = tail call i32 (i8*, i8, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i8 signext %33, i8* %4)
%35 = load i8, i8* %4
%36 = icmp eq i8 %35, 0
br i1 %36, label %44, label %37
37: ; preds = %25, %37
%38 = phi i8 [ %42, %37 ], [ %35, %25 ]
%39 = phi i8* [ %41, %37 ], [ %4, %25 ]
%40 = tail call i32 (i8*, i8, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i8 signext %38)
%41 = getelementptr inbounds i8, i8* %39, i64 1
%42 = load i8, i8* %41
%43 = icmp eq i8 %42, 0
br i1 %43, label %44, label %37
44: ; preds = %37, %25
%45 = tail call i32 (...) @ln()
ret void
}