#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*/);
int /*<<< orphan*/ is_prime(int);
scalar_t__ malloc(int);
int *calculate_primes(int n) {
  int *primes = (int *)malloc(n * sizeof(int));
  assert(primes != NULL);
  int p = 1;
  for (int i = 0; i < n; i++) {
    while (!is_prime(p))
      p++;
    primes[i] = p;
    p++;
  }
  return primes;
}


x86 assembly (clang, -O0):
<calculate_primes>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
movslq -0x4(%rbp),%rax
shl    $0x2,%rax
mov    %eax,%edi
call   1050 <malloc@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1030 <assert@plt>
movl   $0x1,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x4(%rbp),%eax
jge    11cf <calculate_primes+0x9f>
jmp    117f <calculate_primes+0x4f>
mov    -0x14(%rbp),%edi
call   1040 <is_prime@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    119c <calculate_primes+0x6c>
jmp    11aa <calculate_primes+0x7a>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    117f <calculate_primes+0x4f>
mov    -0x14(%rbp),%edx
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    116e <calculate_primes+0x3e>
mov    -0x10(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<calculate_primes>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %edi,%ebx
lea    0x0(,%rbx,4),%edi
call   1050 <malloc@plt>
mov    %rax,%r14
xor    %edi,%edi
test   %rax,%rax
setne  %dil
call   1030 <assert@plt>
test   %ebx,%ebx
jle    118e <calculate_primes+0x5e>
mov    %ebx,%r15d
mov    $0x1,%ebx
xor    %r12d,%r12d
cs nopw 0x0(%rax,%rax,1)
mov    %ebx,%edi
call   1040 <is_prime@plt>
add    $0x1,%ebx
test   %eax,%eax
je     1170 <calculate_primes+0x40>
lea    -0x1(%rbx),%eax
mov    %eax,(%r14,%r12,4)
add    $0x1,%r12
cmp    %r15,%r12
jne    1170 <calculate_primes+0x40>
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i32* @calculate_primes(i32 %0) {
%2 = alloca i32
%3 = alloca i32*
%4 = alloca i32
%5 = alloca i32
store i32 %0, i32* %2
%6 = load i32, i32* %2
%7 = sext i32 %6 to i64
%8 = mul i64 %7, 4
%9 = trunc i64 %8 to i32
%10 = call i64 @malloc(i32 %9)
%11 = inttoptr i64 %10 to i32*
store i32* %11, i32** %3
%12 = load i32*, i32** %3
%13 = icmp ne i32* %12, null
%14 = zext i1 %13 to i32
%15 = call i32 @assert(i32 %14)
store i32 1, i32* %4
store i32 0, i32* %5
br label %16
16: ; preds = %37, %1
%17 = load i32, i32* %5
%18 = load i32, i32* %2
%19 = icmp slt i32 %17, %18
br i1 %19, label %20, label %40
20: ; preds = %16
br label %21
21: ; preds = %26, %20
%22 = load i32, i32* %4
%23 = call i32 @is_prime(i32 %22)
%24 = icmp ne i32 %23, 0
%25 = xor i1 %24, true
br i1 %25, label %26, label %29
26: ; preds = %21
%27 = load i32, i32* %4
%28 = add nsw i32 %27, 1
store i32 %28, i32* %4
br label %21
29: ; preds = %21
%30 = load i32, i32* %4
%31 = load i32*, i32** %3
%32 = load i32, i32* %5
%33 = sext i32 %32 to i64
%34 = getelementptr inbounds i32, i32* %31, i64 %33
store i32 %30, i32* %34
%35 = load i32, i32* %4
%36 = add nsw i32 %35, 1
store i32 %36, i32* %4
br label %37
37: ; preds = %29
%38 = load i32, i32* %5
%39 = add nsw i32 %38, 1
store i32 %39, i32* %5
br label %16
40: ; preds = %16
%41 = load i32*, i32** %3
ret i32* %41
}

LLVM IR (-O2):
define i32* @calculate_primes(i32 %0) {
%2 = shl i32 %0, 2
%3 = tail call i64 @malloc(i32 %2)
%4 = inttoptr i64 %3 to i32*
%5 = icmp ne i64 %3, 0
%6 = zext i1 %5 to i32
%7 = tail call i32 @assert(i32 %6)
%8 = icmp sgt i32 %0, 0
br i1 %8, label %9, label %14
9: ; preds = %1
%10 = zext i32 %0 to i64
br label %11
11: ; preds = %9, %20
%12 = phi i64 [ 0, %9 ], [ %22, %20 ]
%13 = phi i32 [ 1, %9 ], [ %19, %20 ]
br label %15
14: ; preds = %20, %1
ret i32* %4
15: ; preds = %15, %11
%16 = phi i32 [ %19, %15 ], [ %13, %11 ]
%17 = tail call i32 @is_prime(i32 %16)
%18 = icmp eq i32 %17, 0
%19 = add nsw i32 %16, 1
br i1 %18, label %15, label %20
20: ; preds = %15
%21 = getelementptr inbounds i32, i32* %4, i64 %12
store i32 %16, i32* %21
%22 = add nuw nsw i64 %12, 1
%23 = icmp eq i64 %22, %10
br i1 %23, label %14, label %11
}