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


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   1040 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
movl   $0x0,(%rax)
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    -0x4(%rbp),%ecx
add    $0x1,%ecx
cmp    %ecx,%eax
jge    1197 <countBits+0x77>
mov    -0x1c(%rbp),%edi
call   1030 <count@plt>
mov    %eax,%edx
mov    -0x18(%rbp),%rax
mov    -0x10(%rbp),%rsi
mov    (%rsi),%ecx
mov    %ecx,%edi
add    $0x1,%edi
mov    %edi,(%rsi)
movslq %ecx,%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1157 <countBits+0x37>
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<countBits>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %edi,%r14d
lea    0x4(,%r14,4),%edi
call   1040 <malloc@plt>
mov    %rax,%r15
movl   $0x0,(%rbx)
test   %r14d,%r14d
js     116b <countBits+0x4b>
add    $0x1,%r14d
xor    %ebp,%ebp
xchg   %ax,%ax
mov    %ebp,%edi
call   1030 <count@plt>
movslq (%rbx),%rcx
lea    0x1(%rcx),%edx
mov    %edx,(%rbx)
mov    %eax,(%r15,%rcx,4)
add    $0x1,%ebp
cmp    %ebp,%r14d
jne    1150 <countBits+0x30>
mov    %r15,%rax
add    $0x8,%rsp
pop    %rbx
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** %5
%14 = load i32*, i32** %4
store i32 0, i32* %14
store i32 0, i32* %6
br label %15
15: ; preds = %29, %2
%16 = load i32, i32* %6
%17 = load i32, i32* %3
%18 = add nsw i32 %17, 1
%19 = icmp slt i32 %16, %18
br i1 %19, label %20, label %32
20: ; preds = %15
%21 = load i32, i32* %6
%22 = call i32 @count(i32 %21)
%23 = load i32*, i32** %5
%24 = load i32*, i32** %4
%25 = load i32, i32* %24
%26 = add nsw i32 %25, 1
store i32 %26, i32* %24
%27 = sext i32 %25 to i64
%28 = getelementptr inbounds i32, i32* %23, i64 %27
store i32 %22, i32* %28
br label %29
29: ; preds = %20
%30 = load i32, i32* %6
%31 = add nsw i32 %30, 1
store i32 %31, i32* %6
br label %15
32: ; preds = %15
%33 = load i32*, i32** %5
ret i32* %33
}

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