#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*/ free(int *);
int *malloc(int);
int *ParseEratosthenesSieve(int *ptr_sieve, int amount_primes) {
  int count = 0, i = 0;
  int *ptr_primes = malloc(amount_primes * sizeof *ptr_primes);
  for (i = 0; count <= amount_primes; i++) {
    if (ptr_sieve[i] == 1) {
      ptr_primes[count++] = i + 2;
    }
  }
  free(ptr_sieve);
  ptr_sieve = NULL;
  return ptr_primes;
}


x86 assembly (clang, -O0):
<ParseEratosthenesSieve>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
movslq -0xc(%rbp),%rax
shl    $0x2,%rax
mov    %eax,%edi
call   1040 <malloc@plt>
mov    %rax,-0x20(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x10(%rbp),%eax
cmp    -0xc(%rbp),%eax
jg     11a3 <ParseEratosthenesSieve+0x83>
mov    -0x8(%rbp),%rax
movslq -0x14(%rbp),%rcx
cmpl   $0x1,(%rax,%rcx,4)
jne    1190 <ParseEratosthenesSieve+0x70>
mov    -0x14(%rbp),%edx
add    $0x2,%edx
mov    -0x20(%rbp),%rax
mov    -0x10(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0x10(%rbp)
movslq %ecx,%rcx
mov    %edx,(%rax,%rcx,4)
jmp    1195 <ParseEratosthenesSieve+0x75>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1157 <ParseEratosthenesSieve+0x37>
mov    -0x8(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
mov    -0x20(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ParseEratosthenesSieve>:
push   %r15
push   %r14
push   %rbx
mov    %esi,%ebx
mov    %rdi,%r14
lea    0x0(,%rbx,4),%edi
call   1040 <malloc@plt>
mov    %rax,%r15
test   %ebx,%ebx
js     116e <ParseEratosthenesSieve+0x4e>
xor    %eax,%eax
xor    %ecx,%ecx
jmp    1158 <ParseEratosthenesSieve+0x38>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
add    $0x1,%rax
cmp    %ebx,%ecx
jg     116e <ParseEratosthenesSieve+0x4e>
cmpl   $0x1,(%r14,%rax,4)
jne    1150 <ParseEratosthenesSieve+0x30>
movslq %ecx,%rdx
add    $0x1,%ecx
lea    0x2(%rax),%esi
mov    %esi,(%r15,%rdx,4)
jmp    1150 <ParseEratosthenesSieve+0x30>
mov    %r14,%rdi
call   1030 <free@plt>
mov    %r15,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

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

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