#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 */

/* Variables and functions */
double ceil(int /*<<< orphan*/);
long long floor(double);
int /*<<< orphan*/ log2(long long);
long long int next_power(long long int x) {
  long long int res, i, j, k;
  double next_num = ceil(log2(x));
  res = 1;
  i = floor(next_num);
  if (x & (x - 1))
    k = i;
  else
    k = i + 1;
  for (j = 1; j <= k; j++) {
    res *= 2;
  }
  return res;
}


x86 assembly (clang, -O0):
<next_power>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <log2@plt>
mov    %eax,%edi
call   1040 <ceil@plt>
movsd  %xmm0,-0x30(%rbp)
movq   $0x1,-0x10(%rbp)
movsd  -0x30(%rbp),%xmm0
call   1050 <floor@plt>
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%rax
mov    -0x8(%rbp),%rcx
sub    $0x1,%rcx
and    %rcx,%rax
cmp    $0x0,%rax
je     118d <next_power+0x5d>
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
jmp    1199 <next_power+0x69>
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
movq   $0x1,-0x20(%rbp)
mov    -0x20(%rbp),%rax
cmp    -0x28(%rbp),%rax
jg     11cc <next_power+0x9c>
mov    -0x10(%rbp),%rax
shl    $0x1,%rax
mov    %rax,-0x10(%rbp)
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    11a1 <next_power+0x71>
mov    -0x10(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<next_power>:
push   %rbx
mov    %rdi,%rbx
call   1030 <log2@plt>
mov    %eax,%edi
call   1040 <ceil@plt>
call   1050 <floor@plt>
lea    -0x1(%rbx),%rdx
xor    %ecx,%ecx
test   %rdx,%rbx
sete   %cl
lea    (%rax,%rcx,1),%rdx
test   %rdx,%rdx
jle    1173 <next_power+0x43>
add    %rcx,%rax
add    $0xffffffffffffffff,%rax
mov    %edx,%ecx
and    $0x7,%ecx
cmp    $0x7,%rax
jae    117a <next_power+0x4a>
mov    $0x1,%eax
jmp    119a <next_power+0x6a>
mov    $0x1,%eax
pop    %rbx
ret
and    $0xfffffffffffffff8,%rdx
mov    $0x1,%eax
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
shl    $0x8,%rax
add    $0xfffffffffffffff8,%rdx
jne    1190 <next_power+0x60>
test   %rcx,%rcx
je     11a9 <next_power+0x79>
nop
add    %rax,%rax
add    $0xffffffffffffffff,%rcx
jne    11a0 <next_power+0x70>
pop    %rbx
ret

LLVM IR (-O0):
define i64 @next_power(i64 %0) {
%2 = alloca i64
%3 = alloca i64
%4 = alloca i64
%5 = alloca i64
%6 = alloca i64
%7 = alloca double
store i64 %0, i64* %2
%8 = load i64, i64* %2
%9 = call i32 @log2(i64 %8)
%10 = call double @ceil(i32 %9)
store double %10, double* %7
store i64 1, i64* %3
%11 = load double, double* %7
%12 = call i64 @floor(double %11)
store i64 %12, i64* %4
%13 = load i64, i64* %2
%14 = load i64, i64* %2
%15 = sub nsw i64 %14, 1
%16 = and i64 %13, %15
%17 = icmp ne i64 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %1
%19 = load i64, i64* %4
store i64 %19, i64* %6
br label %23
20: ; preds = %1
%21 = load i64, i64* %4
%22 = add nsw i64 %21, 1
store i64 %22, i64* %6
br label %23
23: ; preds = %20, %18
store i64 1, i64* %5
br label %24
24: ; preds = %31, %23
%25 = load i64, i64* %5
%26 = load i64, i64* %6
%27 = icmp sle i64 %25, %26
br i1 %27, label %28, label %34
28: ; preds = %24
%29 = load i64, i64* %3
%30 = mul nsw i64 %29, 2
store i64 %30, i64* %3
br label %31
31: ; preds = %28
%32 = load i64, i64* %5
%33 = add nsw i64 %32, 1
store i64 %33, i64* %5
br label %24
34: ; preds = %24
%35 = load i64, i64* %3
ret i64 %35
}

LLVM IR (-O2):
define i64 @next_power(i64 %0) {
%2 = tail call i32 @log2(i64 %0)
%3 = tail call double @ceil(i32 %2)
%4 = tail call i64 @floor(double %3)
%5 = tail call i64 @llvm.ctpop.i64(i64 %0)
%6 = icmp ult i64 %5, 2
%7 = zext i1 %6 to i64
%8 = add i64 %4, %7
%9 = icmp slt i64 %8, 1
br i1 %9, label %33, label %10
10: ; preds = %1
%11 = add i64 %4, %7
%12 = add i64 %11, -1
%13 = and i64 %8, 7
%14 = icmp ult i64 %12, 7
br i1 %14, label %23, label %15
15: ; preds = %10
%16 = and i64 %8, -8
br label %17
17: ; preds = %17, %15
%18 = phi i64 [ 1, %15 ], [ %20, %17 ]
%19 = phi i64 [ 0, %15 ], [ %21, %17 ]
%20 = shl i64 %18, 8
%21 = add i64 %19, 8
%22 = icmp eq i64 %21, %16
br i1 %22, label %23, label %17
23: ; preds = %17, %10
%24 = phi i64 [ undef, %10 ], [ %20, %17 ]
%25 = phi i64 [ 1, %10 ], [ %20, %17 ]
%26 = icmp eq i64 %13, 0
br i1 %26, label %33, label %27
27: ; preds = %23, %27
%28 = phi i64 [ %30, %27 ], [ %25, %23 ]
%29 = phi i64 [ %31, %27 ], [ 0, %23 ]
%30 = shl nsw i64 %28, 1
%31 = add i64 %29, 1
%32 = icmp eq i64 %31, %13
br i1 %32, label %33, label %27
33: ; preds = %23, %27, %1
%34 = phi i64 [ 1, %1 ], [ %24, %23 ], [ %30, %27 ]
ret i64 %34
}