int main() {
  int *ptr, i, n1, n2;
  printf("Enter size of array: ");
  scanf("%d", &n1);
  ptr = (int *)malloc(n1 * sizeof(int));
  printf("Address of previously allocated memory: ");
  for (i = 0; i < n1; ++i)
    printf("%u\t", ptr + i);
  printf("\nEnter new size of array: ");
  scanf("%d", &n2);
  ptr = realloc(ptr, n2);
  for (i = 0; i < n2; ++i)
    printf("%u\t", ptr + i);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xeaa(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xeb2(%rip),%rdi
lea    -0x18(%rbp),%rsi
mov    $0x0,%al
call   1060 <scanf@plt>
movslq -0x18(%rbp),%rdi
shl    $0x2,%rdi
call   1040 <malloc@plt>
mov    %rax,-0x10(%rbp)
lea    0xe92(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0x14(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x18(%rbp),%eax
jge    11cc <main+0x8c>
mov    -0x10(%rbp),%rsi
movslq -0x14(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rsi
lea    0xe8b(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1195 <main+0x55>
lea    0xe73(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xe35(%rip),%rdi
lea    -0x1c(%rbp),%rsi
mov    $0x0,%al
call   1060 <scanf@plt>
mov    -0x10(%rbp),%rdi
movslq -0x1c(%rbp),%rsi
call   1050 <realloc@plt>
mov    %rax,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    123b <main+0xfb>
mov    -0x10(%rbp),%rsi
movslq -0x14(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rsi
lea    0xe1c(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1204 <main+0xc4>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
lea    0xeb1(%rip),%rdi
xor    %eax,%eax
call   1030 <printf@plt>
lea    0xeb9(%rip),%rdi
mov    %rsp,%rsi
xor    %eax,%eax
call   1060 <scanf@plt>
movslq (%rsp),%rdi
shl    $0x2,%rdi
call   1040 <malloc@plt>
mov    %rax,%r14
lea    0xe9b(%rip),%rdi
xor    %eax,%eax
call   1030 <printf@plt>
cmpl   $0x0,(%rsp)
jle    11be <main+0x7e>
lea    0xeb0(%rip),%r15
mov    %r14,%r12
xor    %ebx,%ebx
nopw   0x0(%rax,%rax,1)
mov    %r15,%rdi
mov    %r12,%rsi
xor    %eax,%eax
call   1030 <printf@plt>
add    $0x1,%rbx
movslq (%rsp),%rax
add    $0x4,%r12
cmp    %rax,%rbx
jl     11a0 <main+0x60>
lea    0xe81(%rip),%rdi
xor    %eax,%eax
call   1030 <printf@plt>
lea    0xe43(%rip),%rdi
lea    0x4(%rsp),%rsi
xor    %eax,%eax
call   1060 <scanf@plt>
movslq 0x4(%rsp),%r15
mov    %r14,%rdi
mov    %r15,%rsi
call   1050 <realloc@plt>
test   %r15,%r15
jle    121f <main+0xdf>
mov    %rax,%r12
lea    0xe44(%rip),%r14
xor    %ebx,%ebx
mov    %r14,%rdi
mov    %r12,%rsi
xor    %eax,%eax
call   1030 <printf@plt>
add    $0x1,%rbx
movslq 0x4(%rsp),%rax
add    $0x4,%r12
cmp    %rax,%rbx
jl     1200 <main+0xc0>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [22 x i8] c"Enter size of array: \00"
@.str.1 = [3 x i8] c"%d\00"
@.str.2 = [41 x i8] c"Address of previously allocated memory: \00"
@.str.3 = [4 x i8] c"%u\09\00"
@.str.4 = [27 x i8] c"\0AEnter new size of array: \00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
store i32 0, i32* %1
%6 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
%7 = call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %4)
%8 = load i32, i32* %4
%9 = sext i32 %8 to i64
%10 = mul i64 %9, 4
%11 = call i8* @malloc(i64 %10)
%12 = bitcast i8* %11 to i32*
store i32* %12, i32** %2
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.2, i64 0, i64 0))
store i32 0, i32* %3
br label %14
14: ; preds = %24, %0
%15 = load i32, i32* %3
%16 = load i32, i32* %4
%17 = icmp slt i32 %15, %16
br i1 %17, label %18, label %27
18: ; preds = %14
%19 = load i32*, i32** %2
%20 = load i32, i32* %3
%21 = sext i32 %20 to i64
%22 = getelementptr inbounds i32, i32* %19, i64 %21
%23 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i32* %22)
br label %24
24: ; preds = %18
%25 = load i32, i32* %3
%26 = add nsw i32 %25, 1
store i32 %26, i32* %3
br label %14
27: ; preds = %14
%28 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.4, i64 0, i64 0))
%29 = call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %5)
%30 = load i32*, i32** %2
%31 = bitcast i32* %30 to i8*
%32 = load i32, i32* %5
%33 = sext i32 %32 to i64
%34 = call i8* @realloc(i8* %31, i64 %33)
%35 = bitcast i8* %34 to i32*
store i32* %35, i32** %2
store i32 0, i32* %3
br label %36
36: ; preds = %46, %27
%37 = load i32, i32* %3
%38 = load i32, i32* %5
%39 = icmp slt i32 %37, %38
br i1 %39, label %40, label %49
40: ; preds = %36
%41 = load i32*, i32** %2
%42 = load i32, i32* %3
%43 = sext i32 %42 to i64
%44 = getelementptr inbounds i32, i32* %41, i64 %43
%45 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i32* %44)
br label %46
46: ; preds = %40
%47 = load i32, i32* %3
%48 = add nsw i32 %47, 1
store i32 %48, i32* %3
br label %36
49: ; preds = %36
ret i32 0
}

LLVM IR (-O2):
@.str = [22 x i8] c"Enter size of array: \00"
@.str.1 = [3 x i8] c"%d\00"
@.str.2 = [41 x i8] c"Address of previously allocated memory: \00"
@.str.3 = [4 x i8] c"%u\09\00"
@.str.4 = [27 x i8] c"\0AEnter new size of array: \00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = bitcast i32* %1 to i8*
%4 = bitcast i32* %2 to i8*
%5 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
%6 = call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %1)
%7 = load i32, i32* %1
%8 = sext i32 %7 to i64
%9 = shl nsw i64 %8, 2
%10 = call i8* @malloc(i64 %9)
%11 = bitcast i8* %10 to i32*
%12 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([41 x i8], [41 x i8]* @.str.2, i64 0, i64 0))
%13 = load i32, i32* %1
%14 = icmp sgt i32 %13, 0
br i1 %14, label %15, label %23
15: ; preds = %0, %15
%16 = phi i64 [ %19, %15 ], [ 0, %0 ]
%17 = getelementptr inbounds i32, i32* %11, i64 %16
%18 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i32* %17)
%19 = add nuw nsw i64 %16, 1
%20 = load i32, i32* %1
%21 = sext i32 %20 to i64
%22 = icmp slt i64 %19, %21
br i1 %22, label %15, label %23
23: ; preds = %15, %0
%24 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @.str.4, i64 0, i64 0))
%25 = call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %2)
%26 = load i32, i32* %2
%27 = sext i32 %26 to i64
%28 = call i8* @realloc(i8* %10, i64 %27)
%29 = bitcast i8* %28 to i32*
%30 = icmp sgt i32 %26, 0
br i1 %30, label %31, label %39
31: ; preds = %23, %31
%32 = phi i64 [ %35, %31 ], [ 0, %23 ]
%33 = getelementptr inbounds i32, i32* %29, i64 %32
%34 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i32* %33)
%35 = add nuw nsw i64 %32, 1
%36 = load i32, i32* %2
%37 = sext i32 %36 to i64
%38 = icmp slt i64 %35, %37
br i1 %38, label %31, label %39
39: ; preds = %31, %23
ret i32 0
}