#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 */
char *malloc(unsigned int);
int pow(int, int);
int sqrt(unsigned int);
char *EratosthenesSieve(unsigned int num) {
  int i = 0, j = 0, k = 0;
  char *sieve = malloc((num - 2) * sizeof *sieve);
  for (i = 0; i < (num - 2); i++) {
    sieve[i] = 1;
  }
  for (i = 2; i < sqrt(num); i++) {
    if (sieve[i - 2] == 1) {
      k = 0;
      j = (pow(i, 2) + k * i);
      while (j < num) {
        sieve[j - 2] = 0;
        k++;
        j = (pow(i, 2) + k * i);
      }
    }
  }
  return sieve;
}


x86 assembly (clang, -O0):
<EratosthenesSieve>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
movl   $0x0,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
mov    -0x4(%rbp),%eax
sub    $0x2,%eax
mov    %eax,%eax
shl    $0x0,%rax
mov    %eax,%edi
call   1030 <malloc@plt>
mov    %rax,-0x18(%rbp)
movl   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%eax
mov    -0x4(%rbp),%ecx
sub    $0x2,%ecx
cmp    %ecx,%eax
jae    1199 <EratosthenesSieve+0x69>
mov    -0x18(%rbp),%rax
movslq -0x8(%rbp),%rcx
movb   $0x1,(%rax,%rcx,1)
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    116e <EratosthenesSieve+0x3e>
movl   $0x2,-0x8(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x4(%rbp),%edi
call   1050 <sqrt@plt>
mov    %eax,%ecx
mov    -0x1c(%rbp),%eax
cmp    %ecx,%eax
jge    1251 <EratosthenesSieve+0x121>
mov    -0x18(%rbp),%rax
mov    -0x8(%rbp),%ecx
sub    $0x2,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x1,%eax
jne    123e <EratosthenesSieve+0x10e>
movl   $0x0,-0x10(%rbp)
mov    -0x8(%rbp),%edi
mov    $0x2,%esi
call   1040 <pow@plt>
mov    -0x10(%rbp),%ecx
imul   -0x8(%rbp),%ecx
add    %ecx,%eax
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%eax
cmp    -0x4(%rbp),%eax
jae    1239 <EratosthenesSieve+0x109>
mov    -0x18(%rbp),%rax
mov    -0xc(%rbp),%ecx
sub    $0x2,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
mov    -0x8(%rbp),%edi
mov    $0x2,%esi
call   1040 <pow@plt>
mov    -0x10(%rbp),%ecx
imul   -0x8(%rbp),%ecx
add    %ecx,%eax
mov    %eax,-0xc(%rbp)
jmp    11f5 <EratosthenesSieve+0xc5>
jmp    123e <EratosthenesSieve+0x10e>
jmp    1243 <EratosthenesSieve+0x113>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    11a0 <EratosthenesSieve+0x70>
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<EratosthenesSieve>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edi,%r15d
lea    -0x2(%r15),%edi
call   1040 <malloc@plt>
mov    %rax,%r14
mov    %r15d,%eax
add    $0xfffffffe,%eax
je     116d <EratosthenesSieve+0x2d>
mov    %eax,%edx
mov    %r14,%rdi
mov    $0x1,%esi
call   1030 <memset@plt>
mov    %r15d,%edi
call   1060 <sqrt@plt>
cmp    $0x3,%eax
jl     11e6 <EratosthenesSieve+0xa6>
mov    $0x2,%ebx
jmp    11a3 <EratosthenesSieve+0x63>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
add    $0x1,%rbx
mov    %r15d,%edi
call   1060 <sqrt@plt>
cltq
cmp    %rax,%rbx
jge    11e6 <EratosthenesSieve+0xa6>
cmpb   $0x1,-0x2(%rbx,%r14,1)
jne    1190 <EratosthenesSieve+0x50>
mov    %ebx,%edi
mov    $0x2,%esi
call   1050 <pow@plt>
cmp    %r15d,%eax
jae    1190 <EratosthenesSieve+0x50>
xor    %ebp,%ebp
xchg   %ax,%ax
cltq
movb   $0x0,-0x2(%rax,%r14,1)
mov    %ebx,%edi
mov    $0x2,%esi
call   1050 <pow@plt>
mov    %eax,%ecx
lea    (%rbx,%rcx,1),%eax
add    %ebp,%eax
add    %ebx,%ebp
add    %ebp,%ecx
cmp    %r15d,%ecx
jb     11c0 <EratosthenesSieve+0x80>
jmp    1190 <EratosthenesSieve+0x50>
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i8* @EratosthenesSieve(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
store i32 %0, i32* %2
store i32 0, i32* %3
store i32 0, i32* %4
store i32 0, i32* %5
%7 = load i32, i32* %2
%8 = sub i32 %7, 2
%9 = zext i32 %8 to i64
%10 = mul i64 %9, 1
%11 = trunc i64 %10 to i32
%12 = call i8* @malloc(i32 %11)
store i8* %12, i8** %6
store i32 0, i32* %3
br label %13
13: ; preds = %23, %1
%14 = load i32, i32* %3
%15 = load i32, i32* %2
%16 = sub i32 %15, 2
%17 = icmp ult i32 %14, %16
br i1 %17, label %18, label %26
18: ; preds = %13
%19 = load i8*, i8** %6
%20 = load i32, i32* %3
%21 = sext i32 %20 to i64
%22 = getelementptr inbounds i8, i8* %19, i64 %21
store i8 1, i8* %22
br label %23
23: ; preds = %18
%24 = load i32, i32* %3
%25 = add nsw i32 %24, 1
store i32 %25, i32* %3
br label %13
26: ; preds = %13
store i32 2, i32* %3
br label %27
27: ; preds = %68, %26
%28 = load i32, i32* %3
%29 = load i32, i32* %2
%30 = call i32 @sqrt(i32 %29)
%31 = icmp slt i32 %28, %30
br i1 %31, label %32, label %71
32: ; preds = %27
%33 = load i8*, i8** %6
%34 = load i32, i32* %3
%35 = sub nsw i32 %34, 2
%36 = sext i32 %35 to i64
%37 = getelementptr inbounds i8, i8* %33, i64 %36
%38 = load i8, i8* %37
%39 = sext i8 %38 to i32
%40 = icmp eq i32 %39, 1
br i1 %40, label %41, label %67
41: ; preds = %32
store i32 0, i32* %5
%42 = load i32, i32* %3
%43 = call i32 @pow(i32 %42, i32 2)
%44 = load i32, i32* %5
%45 = load i32, i32* %3
%46 = mul nsw i32 %44, %45
%47 = add nsw i32 %43, %46
store i32 %47, i32* %4
br label %48
48: ; preds = %52, %41
%49 = load i32, i32* %4
%50 = load i32, i32* %2
%51 = icmp ult i32 %49, %50
br i1 %51, label %52, label %66
52: ; preds = %48
%53 = load i8*, i8** %6
%54 = load i32, i32* %4
%55 = sub nsw i32 %54, 2
%56 = sext i32 %55 to i64
%57 = getelementptr inbounds i8, i8* %53, i64 %56
store i8 0, i8* %57
%58 = load i32, i32* %5
%59 = add nsw i32 %58, 1
store i32 %59, i32* %5
%60 = load i32, i32* %3
%61 = call i32 @pow(i32 %60, i32 2)
%62 = load i32, i32* %5
%63 = load i32, i32* %3
%64 = mul nsw i32 %62, %63
%65 = add nsw i32 %61, %64
store i32 %65, i32* %4
br label %48
66: ; preds = %48
br label %67
67: ; preds = %66, %32
br label %68
68: ; preds = %67
%69 = load i32, i32* %3
%70 = add nsw i32 %69, 1
store i32 %70, i32* %3
br label %27
71: ; preds = %27
%72 = load i8*, i8** %6
ret i8* %72
}

LLVM IR (-O2):
define noalias i8* @EratosthenesSieve(i32 %0) {
%2 = add i32 %0, -2
%3 = tail call i8* @malloc(i32 %2)
%4 = icmp eq i32 %2, 0
br i1 %4, label %7, label %5
5: ; preds = %1
%6 = zext i32 %2 to i64
br label %7
7: ; preds = %5, %1
%8 = tail call i32 @sqrt(i32 %0)
%9 = icmp sgt i32 %8, 2
br i1 %9, label %10, label %36
10: ; preds = %7, %31
%11 = phi i64 [ %32, %31 ], [ 2, %7 ]
%12 = add nsw i64 %11, -2
%13 = getelementptr inbounds i8, i8* %3, i64 %12
%14 = load i8, i8* %13
%15 = icmp eq i8 %14, 1
br i1 %15, label %16, label %31
16: ; preds = %10
%17 = trunc i64 %11 to i32
%18 = tail call i32 @pow(i32 %17, i32 2)
%19 = icmp ult i32 %18, %0
br i1 %19, label %20, label %31
20: ; preds = %16, %20
%21 = phi i32 [ %26, %20 ], [ 0, %16 ]
%22 = phi i32 [ %29, %20 ], [ %18, %16 ]
%23 = add nsw i32 %22, -2
%24 = sext i32 %23 to i64
%25 = getelementptr inbounds i8, i8* %3, i64 %24
store i8 0, i8* %25
%26 = add nuw nsw i32 %21, 1
%27 = tail call i32 @pow(i32 %17, i32 2)
%28 = mul nsw i32 %26, %17
%29 = add nsw i32 %27, %28
%30 = icmp ult i32 %29, %0
br i1 %30, label %20, label %31
31: ; preds = %20, %16, %10
%32 = add nuw nsw i64 %11, 1
%33 = tail call i32 @sqrt(i32 %0)
%34 = sext i32 %33 to i64
%35 = icmp slt i64 %32, %34
br i1 %35, label %10, label %36
36: ; preds = %31, %7
ret i8* %3
}