#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 */
typedef int uint64_t;

/* Variables and functions */
int /*<<< orphan*/ mark_multiples(char *, int, int, int);
int sqrtl(int);
void calc_primes(char *bitmap, uint64_t max) {
  uint64_t a;
  for (a = 2; a <= sqrtl(max); a++) {
    if (bitmap[a / 8] & 1 << (a % 8))
      continue;
    switch (a % 8) {
    case 0:
      mark_multiples(bitmap, max, a, 0);
      break;
    case 1:
      mark_multiples(bitmap, max, a, 1);
      break;
    case 2:
      mark_multiples(bitmap, max, a, 2);
      break;
    case 3:
      mark_multiples(bitmap, max, a, 3);
      break;
    case 4:
      mark_multiples(bitmap, max, a, 4);
      break;
    case 5:
      mark_multiples(bitmap, max, a, 5);
      break;
    case 6:
      mark_multiples(bitmap, max, a, 6);
      break;
    case 7:
      mark_multiples(bitmap, max, a, 7);
      break;
    }
  }
  bitmap[0] = 0x53;
}


x86 assembly (clang, -O0):
<calc_primes>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movl   $0x2,-0x10(%rbp)
mov    -0x10(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    -0xc(%rbp),%edi
call   1040 <sqrtl@plt>
mov    %eax,%ecx
mov    -0x14(%rbp),%eax
cmp    %ecx,%eax
jg     12a7 <calc_primes+0x187>
mov    -0x8(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%eax
mov    $0x8,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
mov    -0x20(%rbp),%rax
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%eax
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%eax
mov    $0x8,%ecx
cltd
idiv   %ecx
mov    -0x18(%rbp),%eax
mov    %edx,%ecx
mov    $0x1,%edx
shl    %cl,%edx
mov    %edx,%ecx
and    %ecx,%eax
cmp    $0x0,%eax
je     119d <calc_primes+0x7d>
jmp    1299 <calc_primes+0x179>
mov    -0x10(%rbp),%eax
mov    %eax,%edx
add    $0x7,%edx
test   %eax,%eax
mov    %eax,%ecx
cmovs  %edx,%ecx
and    $0xfffffff8,%ecx
sub    %ecx,%eax
mov    %eax,%ecx
mov    %rcx,-0x28(%rbp)
sub    $0x7,%eax
ja     1294 <calc_primes+0x174>
mov    -0x28(%rbp),%rax
lea    0xe35(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
mov    -0x10(%rbp),%edx
xor    %ecx,%ecx
call   1030 <mark_multiples@plt>
jmp    1294 <calc_primes+0x174>
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
mov    -0x10(%rbp),%edx
mov    $0x1,%ecx
call   1030 <mark_multiples@plt>
jmp    1294 <calc_primes+0x174>
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
mov    -0x10(%rbp),%edx
mov    $0x2,%ecx
call   1030 <mark_multiples@plt>
jmp    1294 <calc_primes+0x174>
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
mov    -0x10(%rbp),%edx
mov    $0x3,%ecx
call   1030 <mark_multiples@plt>
jmp    1294 <calc_primes+0x174>
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
mov    -0x10(%rbp),%edx
mov    $0x4,%ecx
call   1030 <mark_multiples@plt>
jmp    1294 <calc_primes+0x174>
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
mov    -0x10(%rbp),%edx
mov    $0x5,%ecx
call   1030 <mark_multiples@plt>
jmp    1294 <calc_primes+0x174>
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
mov    -0x10(%rbp),%edx
mov    $0x6,%ecx
call   1030 <mark_multiples@plt>
jmp    1294 <calc_primes+0x174>
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
mov    -0x10(%rbp),%edx
mov    $0x7,%ecx
call   1030 <mark_multiples@plt>
jmp    1299 <calc_primes+0x179>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    1136 <calc_primes+0x16>
mov    -0x8(%rbp),%rax
movb   $0x53,(%rax)
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<calc_primes>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %esi,%r15d
mov    %rdi,%r14
mov    %esi,%edi
call   1040 <sqrtl@plt>
cmp    $0x2,%eax
jge    1148 <calc_primes+0x28>
movb   $0x53,(%r14)
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    $0x2,%ebx
jmp    1161 <calc_primes+0x41>
nop
lea    0x1(%rbx),%ebp
mov    %r15d,%edi
call   1040 <sqrtl@plt>
cmp    %eax,%ebx
mov    %ebp,%ebx
jge    1139 <calc_primes+0x19>
mov    %ebx,%eax
shr    $0x3,%rax
movzbl (%r14,%rax,1),%eax
mov    %ebx,%ecx
and    $0x7,%ecx
bt     %ecx,%eax
jb     1150 <calc_primes+0x30>
mov    %r14,%rdi
mov    %r15d,%esi
mov    %ebx,%edx
call   1030 <mark_multiples@plt>
jmp    1150 <calc_primes+0x30>

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

LLVM IR (-O2):
define void @calc_primes(i8* %0, i32 %1) {
%3 = tail call i32 @sqrtl(i32 %1)
%4 = icmp slt i32 %3, 2
br i1 %4, label %22, label %5
5: ; preds = %2, %18
%6 = phi i32 [ %19, %18 ], [ 2, %2 ]
%7 = lshr i32 %6, 3
%8 = zext i32 %7 to i64
%9 = getelementptr inbounds i8, i8* %0, i64 %8
%10 = load i8, i8* %9
%11 = zext i8 %10 to i32
%12 = and i32 %6, 7
%13 = shl nuw nsw i32 1, %12
%14 = and i32 %13, %11
%15 = icmp eq i32 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %5
%17 = tail call i32 @mark_multiples(i8* %0, i32 %1, i32 %6, i32 %12)
br label %18
18: ; preds = %16, %5
%19 = add nuw nsw i32 %6, 1
%20 = tail call i32 @sqrtl(i32 %1)
%21 = icmp slt i32 %6, %20
br i1 %21, label %5, label %22
22: ; preds = %18, %2
store i8 83, i8* %0
ret void
}