#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 *realloc(int *, int);
int scanf(char *, int *);
void populate_array(int *A, int *num_elements, int size) {
  int num;
  while (scanf("%d", &num) == 1) {
    if (*num_elements > size) {
      size *= 2;
      A = realloc(A, size * sizeof(int));
    }
    A[*num_elements] = num;
    *num_elements += 1;
  }
}


x86 assembly (clang, -O0):
<populate_array>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
lea    0xec6(%rip),%rdi
lea    -0x18(%rbp),%rsi
call   1040 <scanf@plt>
cmp    $0x1,%eax
jne    119c <populate_array+0x7c>
mov    -0x10(%rbp),%rax
mov    (%rax),%eax
cmp    -0x14(%rbp),%eax
jle    117b <populate_array+0x5b>
mov    -0x14(%rbp),%eax
shl    $0x1,%eax
mov    %eax,-0x14(%rbp)
mov    -0x8(%rbp),%rdi
movslq -0x14(%rbp),%rax
shl    $0x2,%rax
mov    %eax,%esi
call   1030 <realloc@plt>
mov    %rax,-0x8(%rbp)
mov    -0x18(%rbp),%edx
mov    -0x8(%rbp),%rax
mov    -0x10(%rbp),%rcx
movslq (%rcx),%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x10(%rbp),%rax
mov    (%rax),%ecx
add    $0x1,%ecx
mov    %ecx,(%rax)
jmp    1133 <populate_array+0x13>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<populate_array>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edx,%ebx
mov    %rsi,%r15
mov    %rdi,%r14
lea    0xec6(%rip),%rdi
lea    0x4(%rsp),%rsi
call   1040 <scanf@plt>
cmp    $0x1,%eax
jne    119f <populate_array+0x7f>
lea    0xeb0(%rip),%r12
lea    0x4(%rsp),%r13
jmp    117e <populate_array+0x5e>
nopw   0x0(%rax,%rax,1)
mov    0x4(%rsp),%ecx
cltq
mov    %ecx,(%r14,%rax,4)
addl   $0x1,(%r15)
mov    %r12,%rdi
mov    %r13,%rsi
call   1040 <scanf@plt>
cmp    $0x1,%eax
jne    119f <populate_array+0x7f>
mov    (%r15),%eax
cmp    %ebx,%eax
jle    1160 <populate_array+0x40>
lea    (%rbx,%rbx,1),%ebp
shl    $0x3,%ebx
mov    %r14,%rdi
mov    %ebx,%esi
call   1030 <realloc@plt>
mov    %rax,%r14
mov    (%r15),%eax
mov    %ebp,%ebx
jmp    1160 <populate_array+0x40>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"%d\00"
define void @populate_array(i32* %0, i32* %1, i32 %2) {
%4 = alloca i32*
%5 = alloca i32*
%6 = alloca i32
%7 = alloca i32
store i32* %0, i32** %4
store i32* %1, i32** %5
store i32 %2, i32* %6
br label %8
8: ; preds = %25, %3
%9 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %7)
%10 = icmp eq i32 %9, 1
br i1 %10, label %11, label %35
11: ; preds = %8
%12 = load i32*, i32** %5
%13 = load i32, i32* %12
%14 = load i32, i32* %6
%15 = icmp sgt i32 %13, %14
br i1 %15, label %16, label %25
16: ; preds = %11
%17 = load i32, i32* %6
%18 = mul nsw i32 %17, 2
store i32 %18, i32* %6
%19 = load i32*, i32** %4
%20 = load i32, i32* %6
%21 = sext i32 %20 to i64
%22 = mul i64 %21, 4
%23 = trunc i64 %22 to i32
%24 = call i32* @realloc(i32* %19, i32 %23)
store i32* %24, i32** %4
br label %25
25: ; preds = %16, %11
%26 = load i32, i32* %7
%27 = load i32*, i32** %4
%28 = load i32*, i32** %5
%29 = load i32, i32* %28
%30 = sext i32 %29 to i64
%31 = getelementptr inbounds i32, i32* %27, i64 %30
store i32 %26, i32* %31
%32 = load i32*, i32** %5
%33 = load i32, i32* %32
%34 = add nsw i32 %33, 1
store i32 %34, i32* %32
br label %8
35: ; preds = %8
ret void
}

LLVM IR (-O2):
@.str = [3 x i8] c"%d\00"
define void @populate_array(i32* %0, i32* nocapture %1, i32 %2) {
%4 = alloca i32
%5 = bitcast i32* %4 to i8*
%6 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %4)
%7 = icmp eq i32 %6, 1
br i1 %7, label %8, label %29
8: ; preds = %3, %18
%9 = phi i32 [ %21, %18 ], [ %2, %3 ]
%10 = phi i32* [ %20, %18 ], [ %0, %3 ]
%11 = load i32, i32* %1
%12 = icmp sgt i32 %11, %9
br i1 %12, label %13, label %18
13: ; preds = %8
%14 = shl nsw i32 %9, 1
%15 = shl i32 %9, 3
%16 = call i32* @realloc(i32* %10, i32 %15)
%17 = load i32, i32* %1
br label %18
18: ; preds = %13, %8
%19 = phi i32 [ %17, %13 ], [ %11, %8 ]
%20 = phi i32* [ %16, %13 ], [ %10, %8 ]
%21 = phi i32 [ %14, %13 ], [ %9, %8 ]
%22 = load i32, i32* %4
%23 = sext i32 %19 to i64
%24 = getelementptr inbounds i32, i32* %20, i64 %23
store i32 %22, i32* %24
%25 = load i32, i32* %1
%26 = add nsw i32 %25, 1
store i32 %26, i32* %1
%27 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %4)
%28 = icmp eq i32 %27, 1
br i1 %28, label %8, label %29
29: ; preds = %18, %3
ret void
}