#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 *calloc(int, int);
int /*<<< orphan*/ memset(int *, int, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
void sieve(int n) {
  int prime;
  int sieve_size;
  bool *s, *p;
  sieve_size = (n - 2) / 2;
  s = calloc(sieve_size, sizeof(bool));
  if (!s) {
    perror("malloc");
    return;
  }
  memset(s, false, sieve_size);
  printf("1\n");
  printf("2\n");
  for (int i = 0; i < sieve_size;) {
    prime = i * 2 + 3;
    printf("%d\n", prime);
    for (int j = i; j < sieve_size; j += prime) {
      s[j] = true;
    }
    while (s[i] == true)
      i++;
  }
}


x86 assembly (clang, -O0):
<sieve>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    -0x4(%rbp),%eax
sub    $0x2,%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%edi
mov    $0x4,%esi
call   1050 <calloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1189 <sieve+0x49>
lea    0xe81(%rip),%rdi
call   1060 <perror@plt>
jmp    123c <sieve+0xfc>
mov    -0x18(%rbp),%rdi
mov    -0xc(%rbp),%edx
xor    %esi,%esi
call   1040 <memset@plt>
lea    0xe69(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xe5e(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    123c <sieve+0xfc>
mov    -0x24(%rbp),%eax
shl    $0x1,%eax
add    $0x3,%eax
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%esi
lea    0xe31(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x24(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    -0x28(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    1212 <sieve+0xd2>
mov    -0x18(%rbp),%rax
movslq -0x28(%rbp),%rcx
movl   $0x1,(%rax,%rcx,4)
mov    -0x8(%rbp),%eax
add    -0x28(%rbp),%eax
mov    %eax,-0x28(%rbp)
jmp    11e9 <sieve+0xa9>
jmp    1217 <sieve+0xd7>
mov    -0x18(%rbp),%rax
movslq -0x24(%rbp),%rcx
cmpl   $0x1,(%rax,%rcx,4)
jne    1237 <sieve+0xf7>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    1217 <sieve+0xd7>
jmp    11ba <sieve+0x7a>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sieve>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %edi,%r15d
lea    -0x2(%r15),%eax
shr    $0x1f,%eax
lea    (%r15,%rax,1),%r12d
add    $0xfffffffe,%r12d
sar    %r12d
mov    %r12d,%edi
mov    $0x4,%esi
call   1060 <calloc@plt>
test   %rax,%rax
je     122a <sieve+0xda>
mov    %rax,%r14
mov    %rax,%rdi
xor    %esi,%esi
mov    %r12d,%edx
call   1050 <memset@plt>
lea    0xe71(%rip),%rdi
call   1030 <puts@plt>
lea    0xe67(%rip),%rdi
call   1030 <puts@plt>
cmp    $0x4,%r15d
jl     1221 <sieve+0xd1>
movslq %r12d,%rbx
lea    0xe4c(%rip),%r15
xor    %ebp,%ebp
nopl   (%rax)
lea    0x3(%rbp,%rbp,1),%esi
mov    %r15,%rdi
xor    %eax,%eax
call   1040 <printf@plt>
movslq %ebp,%rax
cmp    %r12d,%ebp
jge    1201 <sieve+0xb1>
lea    (%rax,%rax,1),%ecx
movslq %ecx,%rcx
lea    (%r14,%rax,4),%rdx
lea    0xc(,%rcx,4),%rsi
add    $0x3,%rcx
mov    %rax,%rdi
nop
add    %rcx,%rdi
movl   $0x1,(%rdx)
add    %rsi,%rdx
cmp    %rbx,%rdi
jl     11f0 <sieve+0xa0>
lea    -0x1(%rax),%ebp
lea    (%r14,%rax,4),%rax
nopl   0x0(%rax,%rax,1)
add    $0x1,%ebp
cmpl   $0x1,(%rax)
lea    0x4(%rax),%rax
je     1210 <sieve+0xc0>
cmp    %ebp,%r12d
jg     11c0 <sieve+0x70>
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdcf(%rip),%rdi
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1070 <perror@plt>

LLVM IR (-O0):
@.str = [7 x i8] c"malloc\00"
@.str.1 = [3 x i8] c"1\0A\00"
@.str.2 = [3 x i8] c"2\0A\00"
@.str.3 = [4 x i8] c"%d\0A\00"
define void @sieve(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i32*
%7 = alloca i32
%8 = alloca i32
store i32 %0, i32* %2
%9 = load i32, i32* %2
%10 = sub nsw i32 %9, 2
%11 = sdiv i32 %10, 2
store i32 %11, i32* %4
%12 = load i32, i32* %4
%13 = call i32* @calloc(i32 %12, i32 4)
store i32* %13, i32** %5
%14 = load i32*, i32** %5
%15 = icmp ne i32* %14, null
br i1 %15, label %18, label %16
16: ; preds = %1
%17 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %60
18: ; preds = %1
%19 = load i32*, i32** %5
%20 = load i32, i32* %4
%21 = call i32 @memset(i32* %19, i32 0, i32 %20)
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%23 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
store i32 0, i32* %7
br label %24
24: ; preds = %59, %18
%25 = load i32, i32* %7
%26 = load i32, i32* %4
%27 = icmp slt i32 %25, %26
br i1 %27, label %28, label %60
28: ; preds = %24
%29 = load i32, i32* %7
%30 = mul nsw i32 %29, 2
%31 = add nsw i32 %30, 3
store i32 %31, i32* %3
%32 = load i32, i32* %3
%33 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i32 %32)
%34 = load i32, i32* %7
store i32 %34, i32* %8
br label %35
35: ; preds = %44, %28
%36 = load i32, i32* %8
%37 = load i32, i32* %4
%38 = icmp slt i32 %36, %37
br i1 %38, label %39, label %48
39: ; preds = %35
%40 = load i32*, i32** %5
%41 = load i32, i32* %8
%42 = sext i32 %41 to i64
%43 = getelementptr inbounds i32, i32* %40, i64 %42
store i32 1, i32* %43
br label %44
44: ; preds = %39
%45 = load i32, i32* %3
%46 = load i32, i32* %8
%47 = add nsw i32 %46, %45
store i32 %47, i32* %8
br label %35
48: ; preds = %35
br label %49
49: ; preds = %56, %48
%50 = load i32*, i32** %5
%51 = load i32, i32* %7
%52 = sext i32 %51 to i64
%53 = getelementptr inbounds i32, i32* %50, i64 %52
%54 = load i32, i32* %53
%55 = icmp eq i32 %54, 1
br i1 %55, label %56, label %59
56: ; preds = %49
%57 = load i32, i32* %7
%58 = add nsw i32 %57, 1
store i32 %58, i32* %7
br label %49
59: ; preds = %49
br label %24
60: ; preds = %16, %24
ret void
}

LLVM IR (-O2):
@.str = [7 x i8] c"malloc\00"
@.str.3 = [4 x i8] c"%d\0A\00"
@str = [2 x i8] c"1\00"
@str.4 = [2 x i8] c"2\00"
define void @sieve(i32 %0) {
%2 = add nsw i32 %0, -2
%3 = sdiv i32 %2, 2
%4 = tail call i32* @calloc(i32 %3, i32 4)
%5 = icmp eq i32* %4, null
br i1 %5, label %6, label %8
6: ; preds = %1
%7 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %40
8: ; preds = %1
%9 = tail call i32 @memset(i32* %4, i32 0, i32 %3)
%10 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([2 x i8], [2 x i8]* @str, i64 0, i64 0))
%11 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([2 x i8], [2 x i8]* @str.4, i64 0, i64 0))
%12 = icmp sgt i32 %0, 3
br i1 %12, label %13, label %40
13: ; preds = %8
%14 = sext i32 %3 to i64
br label %18
15: ; preds = %34
%16 = trunc i64 %35 to i32
%17 = icmp sgt i32 %3, %16
br i1 %17, label %18, label %40
18: ; preds = %13, %15
%19 = phi i32 [ %16, %15 ], [ 0, %13 ]
%20 = shl nsw i32 %19, 1
%21 = add nsw i32 %20, 3
%22 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i32 %21)
%23 = icmp slt i32 %19, %3
%24 = sext i32 %19 to i64
br i1 %23, label %25, label %33
25: ; preds = %18
%26 = sext i32 %20 to i64
%27 = add nsw i64 %26, 3
br label %28
28: ; preds = %25, %28
%29 = phi i64 [ %24, %25 ], [ %31, %28 ]
%30 = getelementptr inbounds i32, i32* %4, i64 %29
store i32 1, i32* %30
%31 = add i64 %29, %27
%32 = icmp slt i64 %31, %14
br i1 %32, label %28, label %33
33: ; preds = %28, %18
br label %34
34: ; preds = %33, %34
%35 = phi i64 [ %39, %34 ], [ %24, %33 ]
%36 = getelementptr inbounds i32, i32* %4, i64 %35
%37 = load i32, i32* %36
%38 = icmp eq i32 %37, 1
%39 = add i64 %35, 1
br i1 %38, label %34, label %15
40: ; preds = %15, %8, %6
ret void
}