#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 *malloc(int);
int /*<<< orphan*/ printf(char *, int *);
int *realloc(int *, int);
int main(void) {
  int *p = malloc(sizeof(int) * 5);
  int i = 0;
  for (i = 0; i < 5; i++) {
    p[i] = i + 1;
  }
  printf("\np=%p\n", p);
  int *q = realloc(p, 10);
  printf("q=%p\n", q);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x14,%edi
call   1040 <malloc@plt>
mov    %rax,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
movl   $0x0,-0x14(%rbp)
cmpl   $0x5,-0x14(%rbp)
jge    1184 <main+0x54>
mov    -0x14(%rbp),%edx
add    $0x1,%edx
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    115b <main+0x2b>
mov    -0x10(%rbp),%rsi
lea    0xe71(%rip),%rdi
call   1030 <printf@plt>
mov    -0x10(%rbp),%rdi
mov    $0xa,%esi
call   1050 <realloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rsi
lea    0xe56(%rip),%rdi
call   1030 <printf@plt>
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
mov    $0x14,%edi
call   1040 <malloc@plt>
mov    %rax,%rbx
movaps 0xebb(%rip),%xmm0
movups %xmm0,(%rax)
movl   $0x5,0x10(%rax)
lea    0xeba(%rip),%rdi
mov    %rax,%rsi
call   1030 <printf@plt>
mov    %rbx,%rdi
mov    $0xa,%esi
call   1050 <realloc@plt>
lea    0xea5(%rip),%rdi
mov    %rax,%rsi
call   1030 <printf@plt>
xor    %eax,%eax
pop    %rbx
ret

LLVM IR (-O0):
@.str = [7 x i8] c"\0Ap=%p\0A\00"
@.str.1 = [6 x i8] c"q=%p\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca i32
%4 = alloca i32*
store i32 0, i32* %1
%5 = call i32* @malloc(i32 20)
store i32* %5, i32** %2
store i32 0, i32* %3
store i32 0, i32* %3
br label %6
6: ; preds = %16, %0
%7 = load i32, i32* %3
%8 = icmp slt i32 %7, 5
br i1 %8, label %9, label %19
9: ; preds = %6
%10 = load i32, i32* %3
%11 = add nsw i32 %10, 1
%12 = load i32*, i32** %2
%13 = load i32, i32* %3
%14 = sext i32 %13 to i64
%15 = getelementptr inbounds i32, i32* %12, i64 %14
store i32 %11, i32* %15
br label %16
16: ; preds = %9
%17 = load i32, i32* %3
%18 = add nsw i32 %17, 1
store i32 %18, i32* %3
br label %6
19: ; preds = %6
%20 = load i32*, i32** %2
%21 = call i32 @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i32* %20)
%22 = load i32*, i32** %2
%23 = call i32* @realloc(i32* %22, i32 10)
store i32* %23, i32** %4
%24 = load i32*, i32** %4
%25 = call i32 @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i32* %24)
%26 = load i32, i32* %1
ret i32 %26
}

LLVM IR (-O2):
@.str = [7 x i8] c"\0Ap=%p\0A\00"
@.str.1 = [6 x i8] c"q=%p\0A\00"
define i32 @main() {
%1 = tail call i32* @malloc(i32 20)
%2 = bitcast i32* %1 to <4 x i32>*
store <4 x i32> <i32 1, i32 2, i32 3, i32 4>, <4 x i32>* %2
%3 = getelementptr inbounds i32, i32* %1, i64 4
store i32 5, i32* %3
%4 = tail call i32 @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i32* %1)
%5 = tail call i32* @realloc(i32* %1, i32 10)
%6 = tail call i32 @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i32* %5)
ret i32 0
}