#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 */
int CHAR_BIT;
size_t round_size_to_next_power_of_2(size_t x) {
  if ((x == 0)) {
    return 1;
  }
  x--;

#pragma GCC unroll(8)

  for (size_t i = 1, n = sizeof(size_t) * CHAR_BIT; i < n; i <<= 1) {
    x |= x >> i;
  }
  return x + 1;
}


x86 assembly (clang, -O0):
<round_size_to_next_power_of_2>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1120 <round_size_to_next_power_of_2+0x20>
movq   $0x1,-0x8(%rbp)
jmp    1184 <round_size_to_next_power_of_2+0x84>
mov    -0x10(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x10(%rbp)
movq   $0x1,-0x18(%rbp)
mov    0x2ea5(%rip),%rax
movslq (%rax),%rax
shl    $0x3,%rax
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rax
cmp    -0x20(%rbp),%rax
jae    1178 <round_size_to_next_power_of_2+0x78>
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%rcx
shr    %cl,%rax
or     -0x10(%rbp),%rax
mov    %rax,-0x10(%rbp)
mov    -0x18(%rbp),%rax
shl    $0x1,%rax
mov    %rax,-0x18(%rbp)
jmp    1146 <round_size_to_next_power_of_2+0x46>
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
pop    %rbp
ret

x86 assembly (clang, -O2):
<round_size_to_next_power_of_2>:
test   %rdi,%rdi
je     11d1 <round_size_to_next_power_of_2+0xd1>
mov    %rdi,%rax
add    $0xffffffffffffffff,%rax
mov    0x2ec9(%rip),%rcx
movslq (%rcx),%rsi
test   %rsi,%rsi
je     11cc <round_size_to_next_power_of_2+0xcc>
shl    $0x3,%rsi
mov    $0x1,%edx
nopl   0x0(%rax)
mov    %rax,%rdi
mov    %edx,%ecx
shr    %cl,%rdi
or     %rdi,%rax
lea    (%rdx,%rdx,1),%rcx
cmp    %rsi,%rcx
jae    11cc <round_size_to_next_power_of_2+0xcc>
mov    %rax,%rdi
shr    %cl,%rdi
or     %rdi,%rax
lea    0x0(,%rdx,4),%rcx
cmp    %rsi,%rcx
jae    11cc <round_size_to_next_power_of_2+0xcc>
mov    %rax,%rdi
shr    %cl,%rdi
or     %rdi,%rax
lea    0x0(,%rdx,8),%rcx
cmp    %rsi,%rcx
jae    11cc <round_size_to_next_power_of_2+0xcc>
mov    %rax,%rdi
shr    %cl,%rdi
or     %rdi,%rax
mov    %rdx,%rcx
shl    $0x4,%rcx
cmp    %rsi,%rcx
jae    11cc <round_size_to_next_power_of_2+0xcc>
mov    %rax,%rdi
shr    %cl,%rdi
or     %rax,%rdi
mov    %rdx,%rcx
shl    $0x5,%rcx
cmp    %rsi,%rcx
jae    11d7 <round_size_to_next_power_of_2+0xd7>
mov    %rdi,%rax
shr    %cl,%rax
or     %rdi,%rax
mov    %rdx,%rcx
shl    $0x6,%rcx
cmp    %rsi,%rcx
jae    11cc <round_size_to_next_power_of_2+0xcc>
mov    %rdx,%rcx
shl    $0x7,%rcx
cmp    %rsi,%rcx
jae    11cc <round_size_to_next_power_of_2+0xcc>
shl    $0x8,%rdx
cmp    %rsi,%rdx
jb     1130 <round_size_to_next_power_of_2+0x30>
add    $0x1,%rax
ret
mov    $0x1,%eax
ret
mov    %rdi,%rax
add    $0x1,%rax
ret

LLVM IR (-O0):
@CHAR_BIT = global i32 0
define i64 @round_size_to_next_power_of_2(i64 %0) {
%2 = alloca i64
%3 = alloca i64
%4 = alloca i64
%5 = alloca i64
store i64 %0, i64* %3
%6 = load i64, i64* %3
%7 = icmp eq i64 %6, 0
br i1 %7, label %8, label %9
8: ; preds = %1
store i64 1, i64* %2
br label %31
9: ; preds = %1
%10 = load i64, i64* %3
%11 = add i64 %10, -1
store i64 %11, i64* %3
store i64 1, i64* %4
%12 = load i32, i32* @CHAR_BIT
%13 = sext i32 %12 to i64
%14 = mul i64 8, %13
store i64 %14, i64* %5
br label %15
15: ; preds = %25, %9
%16 = load i64, i64* %4
%17 = load i64, i64* %5
%18 = icmp ult i64 %16, %17
br i1 %18, label %19, label %28
19: ; preds = %15
%20 = load i64, i64* %3
%21 = load i64, i64* %4
%22 = lshr i64 %20, %21
%23 = load i64, i64* %3
%24 = or i64 %23, %22
store i64 %24, i64* %3
br label %25
25: ; preds = %19
%26 = load i64, i64* %4
%27 = shl i64 %26, 1
store i64 %27, i64* %4
br label %15
28: ; preds = %15
%29 = load i64, i64* %3
%30 = add i64 %29, 1
store i64 %30, i64* %2
br label %31
31: ; preds = %28, %8
%32 = load i64, i64* %2
ret i64 %32
}

LLVM IR (-O2):
@CHAR_BIT = global i32 0
define i64 @round_size_to_next_power_of_2(i64 %0) {
%2 = icmp eq i64 %0, 0
br i1 %2, label %50, label %3
3: ; preds = %1
%4 = add i64 %0, -1
%5 = load i32, i32* @CHAR_BIT
%6 = sext i32 %5 to i64
%7 = shl nsw i64 %6, 3
%8 = icmp eq i32 %5, 0
br i1 %8, label %9, label %12
9: ; preds = %12, %19, %24, %29, %34, %39, %3
%10 = phi i64 [ %4, %3 ], [ %41, %39 ], [ %36, %34 ], [ %31, %29 ], [ %26, %24 ], [ %21, %19 ], [ %16, %12 ]
%11 = add i64 %10, 1
br label %50
12: ; preds = %3, %39
%13 = phi i64 [ %47, %39 ], [ 1, %3 ]
%14 = phi i64 [ %41, %39 ], [ %4, %3 ]
%15 = lshr i64 %14, %13
%16 = or i64 %15, %14
%17 = shl i64 %13, 1
%18 = icmp ult i64 %17, %7
br i1 %18, label %19, label %9
19: ; preds = %12
%20 = lshr i64 %16, %17
%21 = or i64 %20, %16
%22 = shl i64 %13, 2
%23 = icmp ult i64 %22, %7
br i1 %23, label %24, label %9
24: ; preds = %19
%25 = lshr i64 %21, %22
%26 = or i64 %25, %21
%27 = shl i64 %13, 3
%28 = icmp ult i64 %27, %7
br i1 %28, label %29, label %9
29: ; preds = %24
%30 = lshr i64 %26, %27
%31 = or i64 %30, %26
%32 = shl i64 %13, 4
%33 = icmp ult i64 %32, %7
br i1 %33, label %34, label %9
34: ; preds = %29
%35 = lshr i64 %31, %32
%36 = or i64 %35, %31
%37 = shl i64 %13, 5
%38 = icmp ult i64 %37, %7
br i1 %38, label %39, label %9
39: ; preds = %34
%40 = lshr i64 %36, %37
%41 = or i64 %40, %36
%42 = shl i64 %13, 6
%43 = icmp ult i64 %42, %7
%44 = shl i64 %13, 7
%45 = icmp ult i64 %44, %7
%46 = and i1 %43, %45
%47 = shl i64 %13, 8
%48 = icmp ult i64 %47, %7
%49 = select i1 %46, i1 %48, i1 false
br i1 %49, label %12, label %9
50: ; preds = %1, %9
%51 = phi i64 [ %11, %9 ], [ 1, %1 ]
ret i64 %51
}