int main(int argc, char **argv) {
  int i;
  char *nonprime;
  printf("Allocating space\n");
  nonprime = (char *)calloc(1, 1000000);
  printf("Counting\n");
  nonprime[0] = 1;
  nonprime[1] = 1;
  do {
    __asm__ __volatile__("xchg %bx,%bx");
  } while (0);
  for (i = 2; i < 1000000; i++)
    if (!nonprime[i])
      mark(nonprime, 2 * i, i);
  printf("Primes less than %d: %d\n", 1000000,
         1000000 - sum(nonprime, 1000000));
  printf("Done\n");
  free(nonprime);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xe93(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x1,%edi
mov    $0xf4240,%esi
call   1050 <calloc@plt>
mov    %rax,-0x20(%rbp)
lea    0xe84(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x20(%rbp),%rax
movb   $0x1,(%rax)
mov    -0x20(%rbp),%rax
movb   $0x1,0x1(%rax)
xchg   %bx,%bx
movl   $0x2,-0x14(%rbp)
cmpl   $0xf4240,-0x14(%rbp)
jge    11f4 <main+0xa4>
mov    -0x20(%rbp),%rax
movslq -0x14(%rbp),%rcx
cmpb   $0x0,(%rax,%rcx,1)
jne    11e1 <main+0x91>
mov    -0x20(%rbp),%rdi
mov    -0x14(%rbp),%esi
shl    $0x1,%esi
mov    -0x14(%rbp),%edx
mov    $0x0,%al
call   1070 <mark@plt>
jmp    11e6 <main+0x96>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    11ae <main+0x5e>
mov    -0x20(%rbp),%rdi
mov    $0xf4240,%esi
mov    $0x0,%al
call   1060 <sum@plt>
mov    $0xf4240,%edx
sub    %eax,%edx
lea    0xe0a(%rip),%rdi
mov    $0xf4240,%esi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xe10(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
lea    0xeae(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%edi
mov    $0xf4240,%esi
call   1060 <calloc@plt>
mov    %rax,%r14
lea    0xea1(%rip),%rdi
call   1040 <puts@plt>
movw   $0x101,(%r14)
xchg   %bx,%bx
mov    $0x2,%ebx
mov    $0x4,%ebp
jmp    11c0 <main+0x60>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
add    $0x1,%rbx
add    $0x2,%ebp
cmp    $0xf4240,%rbx
je     11d7 <main+0x77>
cmpb   $0x0,(%r14,%rbx,1)
jne    11b0 <main+0x50>
mov    %r14,%rdi
mov    %ebp,%esi
mov    %ebx,%edx
xor    %eax,%eax
call   1080 <mark@plt>
jmp    11b0 <main+0x50>
mov    $0xf4240,%ebx
mov    %r14,%rdi
mov    $0xf4240,%esi
xor    %eax,%eax
call   1070 <sum@plt>
sub    %eax,%ebx
lea    0xe0c(%rip),%rdi
mov    $0xf4240,%esi
mov    %ebx,%edx
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xe2a(%rip),%rdi
call   1040 <puts@plt>
mov    %r14,%rdi
call   1030 <free@plt>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [18 x i8] c"Allocating space\0A\00"
@.str.1 = [10 x i8] c"Counting\0A\00"
@.str.2 = [25 x i8] c"Primes less than %d: %d\0A\00"
@.str.3 = [6 x i8] c"Done\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%8 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
%9 = call i8* @calloc(i64 1, i64 1000000)
store i8* %9, i8** %7
%10 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
%11 = load i8*, i8** %7
%12 = getelementptr inbounds i8, i8* %11, i64 0
store i8 1, i8* %12
%13 = load i8*, i8** %7
%14 = getelementptr inbounds i8, i8* %13, i64 1
store i8 1, i8* %14
br label %15
15: ; preds = %2
br label %16
16: ; preds = %15
store i32 2, i32* %6
br label %17
17: ; preds = %34, %16
%18 = load i32, i32* %6
%19 = icmp slt i32 %18, 1000000
br i1 %19, label %20, label %37
20: ; preds = %17
%21 = load i8*, i8** %7
%22 = load i32, i32* %6
%23 = sext i32 %22 to i64
%24 = getelementptr inbounds i8, i8* %21, i64 %23
%25 = load i8, i8* %24
%26 = icmp ne i8 %25, 0
br i1 %26, label %33, label %27
27: ; preds = %20
%28 = load i8*, i8** %7
%29 = load i32, i32* %6
%30 = mul nsw i32 2, %29
%31 = load i32, i32* %6
%32 = call i32 (i8*, i32, i32, ...) bitcast (i32 (...)* @mark to i32 (i8*, i32, i32, ...)*)(i8* %28, i32 %30, i32 %31)
br label %33
33: ; preds = %27, %20
br label %34
34: ; preds = %33
%35 = load i32, i32* %6
%36 = add nsw i32 %35, 1
store i32 %36, i32* %6
br label %17
37: ; preds = %17
%38 = load i8*, i8** %7
%39 = call i32 (i8*, i32, ...) bitcast (i32 (...)* @sum to i32 (i8*, i32, ...)*)(i8* %38, i32 1000000)
%40 = sub nsw i32 1000000, %39
%41 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0), i32 1000000, i32 %40)
%42 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
%43 = load i8*, i8** %7
ret i32 0
}

LLVM IR (-O2):
@.str.2 = [25 x i8] c"Primes less than %d: %d\0A\00"
@str = [17 x i8] c"Allocating space\00"
@str.4 = [9 x i8] c"Counting\00"
@str.5 = [5 x i8] c"Done\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @str, i64 0, i64 0))
%4 = tail call dereferenceable_or_null(1000000) i8* @calloc(i64 1, i64 1000000)
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @str.4, i64 0, i64 0))
store i8 1, i8* %4
%6 = getelementptr inbounds i8, i8* %4, i64 1
store i8 1, i8* %6
tail call void asm sideeffect "xchg %bx,%bx", "~{dirflag},~{fpsr},~{flags}"()
br label %7
7: ; preds = %2, %16
%8 = phi i64 [ 2, %2 ], [ %17, %16 ]
%9 = trunc i64 %8 to i32
%10 = getelementptr inbounds i8, i8* %4, i64 %8
%11 = load i8, i8* %10
%12 = icmp eq i8 %11, 0
br i1 %12, label %13, label %16
13: ; preds = %7
%14 = shl nuw nsw i32 %9, 1
%15 = tail call i32 (i8*, i32, i32, ...) bitcast (i32 (...)* @mark to i32 (i8*, i32, i32, ...)*)(i8* %4, i32 %14, i32 %9)
br label %16
16: ; preds = %7, %13
%17 = add nuw nsw i64 %8, 1
%18 = icmp eq i64 %17, 1000000
br i1 %18, label %19, label %7
19: ; preds = %16
%20 = tail call i32 (i8*, i32, ...) bitcast (i32 (...)* @sum to i32 (i8*, i32, ...)*)(i8* %4, i32 1000000)
%21 = sub nsw i32 1000000, %20
%22 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0), i32 1000000, i32 %21)
%23 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @str.5, i64 0, i64 0))
tail call void @free(i8* %4)
ret i32 0
}