#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 helper(int);
scalar_t__ malloc(int);
int *countBits(int num, int *returnSize) {
  int i, *res = (int *)malloc(sizeof(int) * (num + 1));
  for (i = 0; i <= num; i++) {
    res[i] = helper(i);
  }
  *returnSize = num + 1;
  return res;
}


x86 assembly (clang, -O0):
<countBits>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x4(%rbp),%eax
add    $0x1,%eax
cltq
shl    $0x2,%rax
mov    %eax,%edi
call   1030 <malloc@plt>
mov    %rax,-0x20(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x4(%rbp),%eax
jg     117c <countBits+0x5c>
mov    -0x14(%rbp),%edi
call   1040 <helper@plt>
mov    %eax,%edx
mov    -0x20(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    114d <countBits+0x2d>
mov    -0x4(%rbp),%ecx
add    $0x1,%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x20(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<countBits>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rsi,%r14
mov    %edi,%ebx
lea    0x1(%rbx),%r12d
lea    0x4(,%rbx,4),%edi
call   1030 <malloc@plt>
mov    %rax,%r15
test   %ebx,%ebx
js     1164 <countBits+0x44>
mov    %r12d,%ebp
xor    %ebx,%ebx
nopl   0x0(%rax)
mov    %ebx,%edi
call   1040 <helper@plt>
mov    %eax,(%r15,%rbx,4)
add    $0x1,%rbx
cmp    %rbx,%rbp
jne    1150 <countBits+0x30>
mov    %r12d,(%r14)
mov    %r15,%rax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32* @countBits(i32 %0, i32* %1) {
%3 = alloca i32
%4 = alloca i32*
%5 = alloca i32
%6 = alloca i32*
store i32 %0, i32* %3
store i32* %1, i32** %4
%7 = load i32, i32* %3
%8 = add nsw i32 %7, 1
%9 = sext i32 %8 to i64
%10 = mul i64 4, %9
%11 = trunc i64 %10 to i32
%12 = call i64 @malloc(i32 %11)
%13 = inttoptr i64 %12 to i32*
store i32* %13, i32** %6
store i32 0, i32* %5
br label %14
14: ; preds = %25, %2
%15 = load i32, i32* %5
%16 = load i32, i32* %3
%17 = icmp sle i32 %15, %16
br i1 %17, label %18, label %28
18: ; preds = %14
%19 = load i32, i32* %5
%20 = call i32 @helper(i32 %19)
%21 = load i32*, i32** %6
%22 = load i32, i32* %5
%23 = sext i32 %22 to i64
%24 = getelementptr inbounds i32, i32* %21, i64 %23
store i32 %20, i32* %24
br label %25
25: ; preds = %18
%26 = load i32, i32* %5
%27 = add nsw i32 %26, 1
store i32 %27, i32* %5
br label %14
28: ; preds = %14
%29 = load i32, i32* %3
%30 = add nsw i32 %29, 1
%31 = load i32*, i32** %4
store i32 %30, i32* %31
%32 = load i32*, i32** %6
ret i32* %32
}

LLVM IR (-O2):
define i32* @countBits(i32 %0, i32* nocapture writeonly %1) {
%3 = add i32 %0, 1
%4 = shl i32 %3, 2
%5 = tail call i64 @malloc(i32 %4)
%6 = inttoptr i64 %5 to i32*
%7 = icmp slt i32 %0, 0
br i1 %7, label %17, label %8
8: ; preds = %2
%9 = zext i32 %3 to i64
br label %10
10: ; preds = %8, %10
%11 = phi i64 [ 0, %8 ], [ %15, %10 ]
%12 = trunc i64 %11 to i32
%13 = tail call i32 @helper(i32 %12)
%14 = getelementptr inbounds i32, i32* %6, i64 %11
store i32 %13, i32* %14
%15 = add nuw nsw i64 %11, 1
%16 = icmp eq i64 %15, %9
br i1 %16, label %17, label %10
17: ; preds = %10, %2
store i32 %3, i32* %1
ret i32* %6
}