#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 /*<<< orphan*/ frac;
typedef unsigned long long __u64;
typedef unsigned long long __u32;

/* Variables and functions */
scalar_t__ hea_get_u64_bits(unsigned long long, unsigned long long,
                            unsigned long long);
__u32 hea_cq_timestamp_ns(__u32 frac) {
  const __u32 log_prec = 10;
  const __u64 ns = 1000000000ULL << log_prec;
  const __u32 bits = sizeof(frac) * 8;
  __u64 ret = 0;
  __u32 i;
  for (i = 0; i < bits; i++) {
    if (hea_get_u64_bits(frac, i, i))
      ret += ns >> (i + 1);
  }
  return (__u32)((ret + (1UL << (log_prec - 1))) >> log_prec);
}


x86 assembly (clang, -O0):
<hea_cq_timestamp_ns>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
movq   $0xa,-0x10(%rbp)
movabs $0xee6b280000,%rax
mov    %rax,-0x18(%rbp)
movq   $0x40,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
cmpq   $0x40,-0x30(%rbp)
jae    11a3 <hea_cq_timestamp_ns+0x93>
mov    -0x8(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    -0x30(%rbp),%rdx
call   1030 <hea_get_u64_bits@plt>
cmp    $0x0,%rax
je     118d <hea_cq_timestamp_ns+0x7d>
mov    -0x30(%rbp),%rcx
add    $0x1,%rcx
movabs $0xee6b280000,%rax
shr    %cl,%rax
add    -0x28(%rbp),%rax
mov    %rax,-0x28(%rbp)
jmp    1192 <hea_cq_timestamp_ns+0x82>
mov    -0x30(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x30(%rbp)
jmp    114a <hea_cq_timestamp_ns+0x3a>
mov    -0x28(%rbp),%rax
add    $0x200,%rax
shr    $0xa,%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<hea_cq_timestamp_ns>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r14
xor    %ebx,%ebx
movabs $0x7735940000,%r12
xor    %r15d,%r15d
nopw   0x0(%rax,%rax,1)
mov    %r14,%rdi
mov    %rbx,%rsi
mov    %rbx,%rdx
call   1030 <hea_get_u64_bits@plt>
lea    0x1(%rbx),%rdx
mov    %r12,%rsi
mov    %ebx,%ecx
shr    %cl,%rsi
test   %rax,%rax
cmove  %rax,%rsi
add    %rsi,%r15
mov    %rdx,%rbx
cmp    $0x40,%rdx
jne    1130 <hea_cq_timestamp_ns+0x20>
add    $0x200,%r15
shr    $0xa,%r15
mov    %r15,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i64 @hea_cq_timestamp_ns(i64 %0) {
%2 = alloca i64
%3 = alloca i64
%4 = alloca i64
%5 = alloca i64
%6 = alloca i64
%7 = alloca i64
store i64 %0, i64* %2
store i64 10, i64* %3
store i64 1024000000000, i64* %4
store i64 64, i64* %5
store i64 0, i64* %6
store i64 0, i64* %7
br label %8
8: ; preds = %24, %1
%9 = load i64, i64* %7
%10 = icmp ult i64 %9, 64
br i1 %10, label %11, label %27
11: ; preds = %8
%12 = load i64, i64* %2
%13 = load i64, i64* %7
%14 = load i64, i64* %7
%15 = call i64 @hea_get_u64_bits(i64 %12, i64 %13, i64 %14)
%16 = icmp ne i64 %15, 0
br i1 %16, label %17, label %23
17: ; preds = %11
%18 = load i64, i64* %7
%19 = add i64 %18, 1
%20 = lshr i64 1024000000000, %19
%21 = load i64, i64* %6
%22 = add i64 %21, %20
store i64 %22, i64* %6
br label %23
23: ; preds = %17, %11
br label %24
24: ; preds = %23
%25 = load i64, i64* %7
%26 = add i64 %25, 1
store i64 %26, i64* %7
br label %8
27: ; preds = %8
%28 = load i64, i64* %6
%29 = add i64 %28, 512
%30 = lshr i64 %29, 10
ret i64 %30
}

LLVM IR (-O2):
define i64 @hea_cq_timestamp_ns(i64 %0) {
br label %2
2: ; preds = %1, %2
%3 = phi i64 [ 0, %1 ], [ %7, %2 ]
%4 = phi i64 [ 0, %1 ], [ %10, %2 ]
%5 = tail call i64 @hea_get_u64_bits(i64 %0, i64 %3, i64 %3)
%6 = icmp eq i64 %5, 0
%7 = add nuw nsw i64 %3, 1
%8 = lshr i64 512000000000, %3
%9 = select i1 %6, i64 0, i64 %8
%10 = add i64 %9, %4
%11 = icmp eq i64 %7, 64
br i1 %11, label %12, label %2
12: ; preds = %2
%13 = add i64 %10, 512
%14 = lshr i64 %13, 10
ret i64 %14
}