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

/* Variables and functions */
scalar_t__ branchless(int, double const);
double get_time();
double test_branchless(int iterations, const uint32_t mask) {
  volatile uint32_t result = 0;
  const uint32_t t1 = get_time();
  for (int i = 0; i < iterations; i++) {
    result += branchless(i, mask);
  }
  const uint32_t t2 = get_time();
  return (t2 - t1) / 1000000.0;
}


x86 assembly (clang, -O0):
<test_branchless>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
movsd  %xmm0,-0x10(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x18(%rbp)
mov    $0x0,%al
call   1030 <get_time@plt>
movsd  %xmm0,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x4(%rbp),%eax
jge    1185 <test_branchless+0x65>
mov    -0x24(%rbp),%edi
movsd  -0x10(%rbp),%xmm0
call   1040 <branchless@plt>
cvtsi2sd %rax,%xmm1
movsd  -0x18(%rbp),%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x18(%rbp)
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    114b <test_branchless+0x2b>
mov    $0x0,%al
call   1030 <get_time@plt>
movsd  %xmm0,-0x30(%rbp)
movsd  -0x30(%rbp),%xmm0
subsd  -0x20(%rbp),%xmm0
movsd  0xe5d(%rip),%xmm1
divsd  %xmm1,%xmm0
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_branchless>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
movsd  %xmm0,0x10(%rsp)
mov    %edi,%ebx
movq   $0x0,(%rsp)
xor    %ebp,%ebp
xor    %eax,%eax
call   1030 <get_time@plt>
movsd  %xmm0,0x8(%rsp)
test   %ebx,%ebx
jle    1176 <test_branchless+0x56>
nopl   0x0(%rax)
mov    %ebp,%edi
movsd  0x10(%rsp),%xmm0
call   1040 <branchless@plt>
xorps  %xmm0,%xmm0
cvtsi2sd %rax,%xmm0
addsd  (%rsp),%xmm0
movsd  %xmm0,(%rsp)
add    $0x1,%ebp
cmp    %ebp,%ebx
jne    1150 <test_branchless+0x30>
xor    %eax,%eax
call   1030 <get_time@plt>
subsd  0x8(%rsp),%xmm0
divsd  0xe75(%rip),%xmm0
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
define double @test_branchless(i32 %0, double %1) {
%3 = alloca i32
%4 = alloca double
%5 = alloca double
%6 = alloca double
%7 = alloca i32
%8 = alloca double
store i32 %0, i32* %3
store double %1, double* %4
store volatile double 0.000000e+00, double* %5
%9 = call double (...) @get_time()
store double %9, double* %6
store i32 0, i32* %7
br label %10
10: ; preds = %21, %2
%11 = load i32, i32* %7
%12 = load i32, i32* %3
%13 = icmp slt i32 %11, %12
br i1 %13, label %14, label %24
14: ; preds = %10
%15 = load i32, i32* %7
%16 = load double, double* %4
%17 = call i64 @branchless(i32 %15, double %16)
%18 = sitofp i64 %17 to double
%19 = load volatile double, double* %5
%20 = fadd double %19, %18
store volatile double %20, double* %5
br label %21
21: ; preds = %14
%22 = load i32, i32* %7
%23 = add nsw i32 %22, 1
store i32 %23, i32* %7
br label %10
24: ; preds = %10
%25 = call double (...) @get_time()
store double %25, double* %8
%26 = load double, double* %8
%27 = load double, double* %6
%28 = fsub double %26, %27
%29 = fdiv double %28, 1.000000e+06
ret double %29
}

LLVM IR (-O2):
define double @test_branchless(i32 %0, double %1) {
%3 = alloca double
%4 = bitcast double* %3 to i8*
store volatile double 0.000000e+00, double* %3
%5 = tail call double (...) @get_time()
%6 = icmp sgt i32 %0, 0
br i1 %6, label %11, label %7
7: ; preds = %11, %2
%8 = tail call double (...) @get_time()
%9 = fsub double %8, %5
%10 = fdiv double %9, 1.000000e+06
ret double %10
11: ; preds = %2, %11
%12 = phi i32 [ %17, %11 ], [ 0, %2 ]
%13 = tail call i64 @branchless(i32 %12, double %1)
%14 = sitofp i64 %13 to double
%15 = load volatile double, double* %3
%16 = fadd double %15, %14
store volatile double %16, double* %3
%17 = add nuw nsw i32 %12, 1
%18 = icmp eq i32 %17, %0
br i1 %18, label %7, label %11
}