#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*/ assert(int /*<<< orphan*/);
scalar_t__ malloc(int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ puts(char *);
int *realloc(int *, int);
int scanf(char *, int *);
void ints(void) {
  int *array;
  int value;
  int count = 0;
  int size = 20;
  puts("Enter some numbers, separated by spaces,");
  puts("to store in a dinamically allocated array:");
  array = (int *)malloc(size * sizeof(int));
  assert(array != NULL);
  while (scanf("%d", &value) == 1) {
    count += 1;
    if (count > size) {
      size += 20;
      array = realloc(array, size * sizeof(int));
      assert(array != NULL);
    }
    array[count - 1] = value;
  }
  if (count < size) {
    array = realloc(array, count * sizeof(int));
    assert(array != NULL);
  }
  printf("So you had %d numbers:\n", count);
  value = 0;
  while (value++ < count)
    printf("%d, ", *array++);
  printf("\b\b.\n");
}


x86 assembly (clang, -O0):
<ints>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x10(%rbp)
movl   $0x14,-0x14(%rbp)
lea    0xe83(%rip),%rdi
call   1030 <puts@plt>
lea    0xea0(%rip),%rdi
call   1030 <puts@plt>
movslq -0x14(%rbp),%rax
shl    $0x2,%rax
mov    %eax,%edi
call   1060 <malloc@plt>
mov    %rax,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
lea    0xe9a(%rip),%rdi
lea    -0xc(%rbp),%rsi
call   1080 <scanf@plt>
cmp    $0x1,%eax
jne    122b <ints+0xcb>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%eax
cmp    -0x14(%rbp),%eax
jle    1213 <ints+0xb3>
mov    -0x14(%rbp),%eax
add    $0x14,%eax
mov    %eax,-0x14(%rbp)
mov    -0x8(%rbp),%rdi
movslq -0x14(%rbp),%rax
shl    $0x2,%rax
mov    %eax,%esi
call   1070 <realloc@plt>
mov    %rax,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
mov    -0xc(%rbp),%edx
mov    -0x8(%rbp),%rax
mov    -0x10(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
mov    %edx,(%rax,%rcx,4)
jmp    11b3 <ints+0x53>
mov    -0x10(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    1260 <ints+0x100>
mov    -0x8(%rbp),%rdi
movslq -0x10(%rbp),%rax
shl    $0x2,%rax
mov    %eax,%esi
call   1070 <realloc@plt>
mov    %rax,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
mov    -0x10(%rbp),%esi
lea    0xded(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0xc(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,%ecx
add    $0x1,%ecx
mov    %ecx,-0xc(%rbp)
cmp    -0x10(%rbp),%eax
jge    12b0 <ints+0x150>
mov    -0x8(%rbp),%rax
mov    %rax,%rcx
add    $0x4,%rcx
mov    %rcx,-0x8(%rbp)
mov    (%rax),%esi
lea    0xdcb(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1278 <ints+0x118>
lea    0xdbd(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ints>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
lea    0xe8e(%rip),%rdi
call   1030 <puts@plt>
lea    0xeab(%rip),%rdi
call   1030 <puts@plt>
mov    $0x50,%edi
call   1060 <malloc@plt>
mov    %rax,%rbx
xor    %edi,%edi
test   %rax,%rax
setne  %dil
call   1050 <assert@plt>
lea    0xeaf(%rip),%rdi
lea    0x4(%rsp),%rsi
call   1080 <scanf@plt>
cmp    $0x1,%eax
jne    1220 <ints+0xc0>
mov    $0x14,%r13d
xor    %r14d,%r14d
lea    0xe90(%rip),%r15
lea    0x4(%rsp),%r12
jmp    11ec <ints+0x8c>
nopl   0x0(%rax,%rax,1)
mov    0x4(%rsp),%eax
mov    %eax,(%rbx,%r14,4)
mov    %r15,%rdi
mov    %r12,%rsi
call   1080 <scanf@plt>
add    $0x1,%r14
cmp    $0x1,%eax
jne    1225 <ints+0xc5>
movslq %r13d,%rax
cmp    %rax,%r14
jl     11d0 <ints+0x70>
mov    %r13d,%ebp
add    $0x14,%ebp
lea    0x50(,%r13,4),%esi
mov    %rbx,%rdi
call   1070 <realloc@plt>
mov    %rax,%rbx
xor    %edi,%edi
test   %rax,%rax
setne  %dil
call   1050 <assert@plt>
mov    %ebp,%r13d
jmp    11d0 <ints+0x70>
xor    %r14d,%r14d
jmp    122a <ints+0xca>
cmp    %r14d,%r13d
jle    124b <ints+0xeb>
lea    0x0(,%r14,4),%esi
mov    %rbx,%rdi
call   1070 <realloc@plt>
mov    %rax,%rbx
xor    %edi,%edi
test   %rax,%rax
setne  %dil
call   1050 <assert@plt>
lea    0xe05(%rip),%rdi
mov    %r14d,%esi
xor    %eax,%eax
call   1040 <printf@plt>
movl   $0x1,0x4(%rsp)
test   %r14d,%r14d
jle    1290 <ints+0x130>
lea    0xdff(%rip),%r15
mov    (%rbx),%esi
add    $0x4,%rbx
mov    %r15,%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    0x4(%rsp),%eax
lea    0x1(%rax),%ecx
mov    %ecx,0x4(%rsp)
cmp    %r14d,%eax
jl     1270 <ints+0x110>
lea    0xddd(%rip),%rdi
call   1030 <puts@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [41 x i8] c"Enter some numbers, separated by spaces,\00"
@.str.1 = [43 x i8] c"to store in a dinamically allocated array:\00"
@.str.2 = [3 x i8] c"%d\00"
@.str.3 = [24 x i8] c"So you had %d numbers:\0A\00"
@.str.4 = [5 x i8] c"%d, \00"
@.str.5 = [5 x i8] c"\08\08.\0A\00"
define void @ints() {
%1 = alloca i32*
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
store i32 0, i32* %3
store i32 20, i32* %4
%5 = call i32 @puts(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0))
%6 = call i32 @puts(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.1, i64 0, i64 0))
%7 = load i32, i32* %4
%8 = sext i32 %7 to i64
%9 = mul i64 %8, 4
%10 = trunc i64 %9 to i32
%11 = call i64 @malloc(i32 %10)
%12 = inttoptr i64 %11 to i32*
store i32* %12, i32** %1
%13 = load i32*, i32** %1
%14 = icmp ne i32* %13, null
%15 = zext i1 %14 to i32
%16 = call i32 @assert(i32 %15)
br label %17
17: ; preds = %39, %0
%18 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32* %2)
%19 = icmp eq i32 %18, 1
br i1 %19, label %20, label %46
20: ; preds = %17
%21 = load i32, i32* %3
%22 = add nsw i32 %21, 1
store i32 %22, i32* %3
%23 = load i32, i32* %3
%24 = load i32, i32* %4
%25 = icmp sgt i32 %23, %24
br i1 %25, label %26, label %39
26: ; preds = %20
%27 = load i32, i32* %4
%28 = add nsw i32 %27, 20
store i32 %28, i32* %4
%29 = load i32*, i32** %1
%30 = load i32, i32* %4
%31 = sext i32 %30 to i64
%32 = mul i64 %31, 4
%33 = trunc i64 %32 to i32
%34 = call i32* @realloc(i32* %29, i32 %33)
store i32* %34, i32** %1
%35 = load i32*, i32** %1
%36 = icmp ne i32* %35, null
%37 = zext i1 %36 to i32
%38 = call i32 @assert(i32 %37)
br label %39
39: ; preds = %26, %20
%40 = load i32, i32* %2
%41 = load i32*, i32** %1
%42 = load i32, i32* %3
%43 = sub nsw i32 %42, 1
%44 = sext i32 %43 to i64
%45 = getelementptr inbounds i32, i32* %41, i64 %44
store i32 %40, i32* %45
br label %17
46: ; preds = %17
%47 = load i32, i32* %3
%48 = load i32, i32* %4
%49 = icmp slt i32 %47, %48
br i1 %49, label %50, label %61
50: ; preds = %46
%51 = load i32*, i32** %1
%52 = load i32, i32* %3
%53 = sext i32 %52 to i64
%54 = mul i64 %53, 4
%55 = trunc i64 %54 to i32
%56 = call i32* @realloc(i32* %51, i32 %55)
store i32* %56, i32** %1
%57 = load i32*, i32** %1
%58 = icmp ne i32* %57, null
%59 = zext i1 %58 to i32
%60 = call i32 @assert(i32 %59)
br label %61
61: ; preds = %50, %46
%62 = load i32, i32* %3
%63 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0), i32 %62)
store i32 0, i32* %2
br label %64
64: ; preds = %69, %61
%65 = load i32, i32* %2
%66 = add nsw i32 %65, 1
store i32 %66, i32* %2
%67 = load i32, i32* %3
%68 = icmp slt i32 %65, %67
br i1 %68, label %69, label %74
69: ; preds = %64
%70 = load i32*, i32** %1
%71 = getelementptr inbounds i32, i32* %70, i32 1
store i32* %71, i32** %1
%72 = load i32, i32* %70
%73 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0), i32 %72)
br label %64
74: ; preds = %64
%75 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0))
ret void
}

LLVM IR (-O2):
@.str = [41 x i8] c"Enter some numbers, separated by spaces,\00"
@.str.1 = [43 x i8] c"to store in a dinamically allocated array:\00"
@.str.2 = [3 x i8] c"%d\00"
@.str.3 = [24 x i8] c"So you had %d numbers:\0A\00"
@.str.4 = [5 x i8] c"%d, \00"
@str = [4 x i8] c"\08\08.\00"
define void @ints() {
%1 = alloca i32
%2 = bitcast i32* %1 to i8*
%3 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0))
%4 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([43 x i8], [43 x i8]* @.str.1, i64 0, i64 0))
%5 = tail call i64 @malloc(i32 80)
%6 = inttoptr i64 %5 to i32*
%7 = icmp ne i64 %5, 0
%8 = zext i1 %7 to i32
%9 = tail call i32 @assert(i32 %8)
%10 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32* %1)
%11 = icmp eq i32 %10, 1
br i1 %11, label %12, label %36
12: ; preds = %0, %26
%13 = phi i64 [ %16, %26 ], [ 0, %0 ]
%14 = phi i32 [ %28, %26 ], [ 20, %0 ]
%15 = phi i32* [ %27, %26 ], [ %6, %0 ]
%16 = add nuw i64 %13, 1
%17 = sext i32 %14 to i64
%18 = icmp slt i64 %13, %17
br i1 %18, label %26, label %19
19: ; preds = %12
%20 = add nsw i32 %14, 20
%21 = shl i32 %20, 2
%22 = call i32* @realloc(i32* %15, i32 %21)
%23 = icmp ne i32* %22, null
%24 = zext i1 %23 to i32
%25 = call i32 @assert(i32 %24)
br label %26
26: ; preds = %19, %12
%27 = phi i32* [ %22, %19 ], [ %15, %12 ]
%28 = phi i32 [ %20, %19 ], [ %14, %12 ]
%29 = load i32, i32* %1
%30 = getelementptr inbounds i32, i32* %27, i64 %13
store i32 %29, i32* %30
%31 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32* %1)
%32 = icmp eq i32 %31, 1
br i1 %32, label %12, label %33
33: ; preds = %26
%34 = trunc i64 %16 to i32
%35 = icmp sgt i32 %28, %34
br i1 %35, label %36, label %44
36: ; preds = %0, %33
%37 = phi i32 [ %34, %33 ], [ 0, %0 ]
%38 = phi i32* [ %27, %33 ], [ %6, %0 ]
%39 = shl i32 %37, 2
%40 = call i32* @realloc(i32* %38, i32 %39)
%41 = icmp ne i32* %40, null
%42 = zext i1 %41 to i32
%43 = call i32 @assert(i32 %42)
br label %44
44: ; preds = %36, %33
%45 = phi i32 [ %37, %36 ], [ %34, %33 ]
%46 = phi i32* [ %40, %36 ], [ %27, %33 ]
%47 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0), i32 %45)
store i32 1, i32* %1
%48 = icmp sgt i32 %45, 0
br i1 %48, label %49, label %57
49: ; preds = %44, %49
%50 = phi i32* [ %51, %49 ], [ %46, %44 ]
%51 = getelementptr inbounds i32, i32* %50, i64 1
%52 = load i32, i32* %50
%53 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0), i32 %52)
%54 = load i32, i32* %1
%55 = add nsw i32 %54, 1
store i32 %55, i32* %1
%56 = icmp slt i32 %54, %45
br i1 %56, label %49, label %57
57: ; preds = %49, %44
%58 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @str, i64 0, i64 0))
ret void
}