#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 uint32_t;

/* Variables and functions */
int /*<<< orphan*/ compare();
int /*<<< orphan*/ printf(char *, ...);
double test_branchless(int const, int);
double test_naive(int const, int);
int main() {
  compare();
  const int iterations = (1 << 20);
  uint32_t mask = 0;
  printf("| %8s | %15s | %15s | %10s |\n", "mask", "naive", "branchless",
         "speedup");
  for (int i = 0; i < 33; i++) {
    const double tn = test_naive(iterations, mask);
    const double tb = test_branchless(iterations, mask);
    const double speedup = tn / tb;
    printf("| %08x | %15.4f | %15.4f | %10.2f |\n", mask, tn, tb, speedup);
    mask = 2 * mask + 1;
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x0,%al
call   1060 <compare@plt>
movl   $0x100000,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
lea    0xe95(%rip),%rdi
lea    0xeac(%rip),%rsi
lea    0xeaa(%rip),%rdx
lea    0xea9(%rip),%rcx
lea    0xead(%rip),%r8
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x0,-0x10(%rbp)
cmpl   $0x21,-0x10(%rbp)
jge    120c <main+0xcc>
mov    -0xc(%rbp),%esi
mov    $0x100000,%edi
call   1030 <test_naive@plt>
movsd  %xmm0,-0x18(%rbp)
mov    -0xc(%rbp),%esi
mov    $0x100000,%edi
call   1040 <test_branchless@plt>
movsd  %xmm0,-0x20(%rbp)
movsd  -0x18(%rbp),%xmm0
divsd  -0x20(%rbp),%xmm0
movsd  %xmm0,-0x28(%rbp)
mov    -0xc(%rbp),%esi
movsd  -0x18(%rbp),%xmm0
movsd  -0x20(%rbp),%xmm1
movsd  -0x28(%rbp),%xmm2
lea    0xe51(%rip),%rdi
mov    $0x3,%al
call   1050 <printf@plt>
mov    -0xc(%rbp),%eax
shl    $0x1,%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    1195 <main+0x55>
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
xor    %ebx,%ebx
xor    %eax,%eax
call   1060 <compare@plt>
lea    0xea8(%rip),%rdi
lea    0xebf(%rip),%rsi
lea    0xebd(%rip),%rdx
lea    0xebc(%rip),%rcx
lea    0xec0(%rip),%r8
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x21,%ebp
lea    0xeb5(%rip),%r14
nopw   0x0(%rax,%rax,1)
mov    $0x100000,%edi
mov    %ebx,%esi
call   1030 <test_naive@plt>
movsd  %xmm0,0x8(%rsp)
mov    $0x100000,%edi
mov    %ebx,%esi
call   1040 <test_branchless@plt>
movapd %xmm0,%xmm1
movsd  0x8(%rsp),%xmm0
movapd %xmm0,%xmm2
divsd  %xmm1,%xmm2
mov    %r14,%rdi
mov    %ebx,%esi
mov    $0x3,%al
call   1050 <printf@plt>
add    %ebx,%ebx
add    $0x1,%ebx
add    $0xffffffff,%ebp
jne    1190 <main+0x50>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [30 x i8] c"| %8s | %15s | %15s | %10s |\0A\00"
@.str.1 = [5 x i8] c"mask\00"
@.str.2 = [6 x i8] c"naive\00"
@.str.3 = [11 x i8] c"branchless\00"
@.str.4 = [8 x i8] c"speedup\00"
@.str.5 = [37 x i8] c"| %08x | %15.4f | %15.4f | %10.2f |\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca double
%6 = alloca double
%7 = alloca double
store i32 0, i32* %1
%8 = call i32 (...) @compare()
store i32 1048576, i32* %2
store i32 0, i32* %3
%9 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0))
store i32 0, i32* %4
br label %10
10: ; preds = %29, %0
%11 = load i32, i32* %4
%12 = icmp slt i32 %11, 33
br i1 %12, label %13, label %32
13: ; preds = %10
%14 = load i32, i32* %3
%15 = call double @test_naive(i32 1048576, i32 %14)
store double %15, double* %5
%16 = load i32, i32* %3
%17 = call double @test_branchless(i32 1048576, i32 %16)
store double %17, double* %6
%18 = load double, double* %5
%19 = load double, double* %6
%20 = fdiv double %18, %19
store double %20, double* %7
%21 = load i32, i32* %3
%22 = load double, double* %5
%23 = load double, double* %6
%24 = load double, double* %7
%25 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.5, i64 0, i64 0), i32 %21, double %22, double %23, double %24)
%26 = load i32, i32* %3
%27 = mul nsw i32 2, %26
%28 = add nsw i32 %27, 1
store i32 %28, i32* %3
br label %29
29: ; preds = %13
%30 = load i32, i32* %4
%31 = add nsw i32 %30, 1
store i32 %31, i32* %4
br label %10
32: ; preds = %10
%33 = load i32, i32* %1
ret i32 %33
}

LLVM IR (-O2):
@.str = [30 x i8] c"| %8s | %15s | %15s | %10s |\0A\00"
@.str.1 = [5 x i8] c"mask\00"
@.str.2 = [6 x i8] c"naive\00"
@.str.3 = [11 x i8] c"branchless\00"
@.str.4 = [8 x i8] c"speedup\00"
@.str.5 = [37 x i8] c"| %08x | %15.4f | %15.4f | %10.2f |\0A\00"
define i32 @main() {
%1 = tail call i32 (...) @compare()
%2 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0))
br label %4
3: ; preds = %4
ret i32 0
4: ; preds = %0, %4
%5 = phi i32 [ 0, %0 ], [ %12, %4 ]
%6 = phi i32 [ 0, %0 ], [ %13, %4 ]
%7 = tail call double @test_naive(i32 1048576, i32 %5)
%8 = tail call double @test_branchless(i32 1048576, i32 %5)
%9 = fdiv double %7, %8
%10 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([37 x i8], [37 x i8]* @.str.5, i64 0, i64 0), i32 %5, double %7, double %8, double %9)
%11 = shl nsw i32 %5, 1
%12 = or i32 %11, 1
%13 = add nuw nsw i32 %6, 1
%14 = icmp eq i32 %13, 33
br i1 %14, label %3, label %4
}