#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 */
uint32_t round_up_pow2(uint32_t v) {
  int pos;
  if (v == 0)
    return 1;
  else if ((v & (v - 1)) == 0)
    return v;
  if (v & 0xFFFF0000UL)
    pos = (v & 0xFF000000UL) ? 3 : 2;
  else
    pos = (v & 0x0000FF00UL) ? 1 : 0;
  unsigned int byte = v >> (pos << 3);
  byte |= byte >> 1;
  byte |= byte >> 2;
  byte |= byte >> 4;
  v = (byte + 1) << (pos << 3);
  return v;
}


x86 assembly (clang, -O0):
<round_up_pow2>:
push   %rbp
mov    %rsp,%rbp
mov    %edi,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jne    111d <round_up_pow2+0x1d>
movl   $0x1,-0x4(%rbp)
jmp    11eb <round_up_pow2+0xeb>
mov    -0x8(%rbp),%eax
mov    -0x8(%rbp),%ecx
sub    $0x1,%ecx
and    %ecx,%eax
cmp    $0x0,%eax
jne    113c <round_up_pow2+0x3c>
mov    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11eb <round_up_pow2+0xeb>
jmp    1141 <round_up_pow2+0x41>
movslq -0x8(%rbp),%rax
movabs $0xffff0000,%rcx
and    %rcx,%rax
cmp    $0x0,%rax
je     1186 <round_up_pow2+0x86>
movslq -0x8(%rbp),%rdx
movabs $0xff000000,%rax
and    %rax,%rdx
mov    $0x2,%eax
mov    $0x3,%ecx
cmp    $0x0,%rdx
cmovne %ecx,%eax
mov    %eax,-0xc(%rbp)
jmp    11a2 <round_up_pow2+0xa2>
movslq -0x8(%rbp),%rdx
and    $0xff00,%rdx
xor    %eax,%eax
mov    $0x1,%ecx
cmp    $0x0,%rdx
cmovne %ecx,%eax
mov    %eax,-0xc(%rbp)
mov    -0x8(%rbp),%eax
mov    -0xc(%rbp),%ecx
shl    $0x3,%ecx
sar    %cl,%eax
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%eax
shr    $0x1,%eax
or     -0x10(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%eax
shr    $0x2,%eax
or     -0x10(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%eax
shr    $0x4,%eax
or     -0x10(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    -0xc(%rbp),%ecx
shl    $0x3,%ecx
shl    %cl,%eax
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
pop    %rbp
ret

x86 assembly (clang, -O2):
<round_up_pow2>:
test   %edi,%edi
je     112b <round_up_pow2+0x2b>
mov    %edi,%eax
lea    -0x1(%rax),%ecx
test   %ecx,%edi
je     115a <round_up_pow2+0x5a>
mov    %eax,%edx
test   $0xffff0000,%edx
je     1131 <round_up_pow2+0x31>
and    $0xff000000,%edx
cmp    $0x1,%rdx
mov    $0x3,%ecx
sbb    $0x0,%ecx
jmp    113c <round_up_pow2+0x3c>
mov    $0x1,%eax
ret
xor    %ecx,%ecx
test   $0xff00,%edx
setne  %cl
shl    $0x3,%ecx
sar    %cl,%eax
mov    %eax,%edx
shr    %edx
or     %eax,%edx
mov    %edx,%esi
shr    $0x2,%esi
or     %edx,%esi
mov    %esi,%eax
shr    $0x4,%eax
or     %esi,%eax
add    $0x1,%eax
shl    %cl,%eax
ret

LLVM IR (-O0):
define i32 @round_up_pow2(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
store i32 %0, i32* %3
%6 = load i32, i32* %3
%7 = icmp eq i32 %6, 0
br i1 %7, label %8, label %9
8: ; preds = %1
store i32 1, i32* %2
br label %60
9: ; preds = %1
%10 = load i32, i32* %3
%11 = load i32, i32* %3
%12 = sub nsw i32 %11, 1
%13 = and i32 %10, %12
%14 = icmp eq i32 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %9
%16 = load i32, i32* %3
store i32 %16, i32* %2
br label %60
17: ; preds = %9
br label %18
18: ; preds = %17
%19 = load i32, i32* %3
%20 = sext i32 %19 to i64
%21 = and i64 %20, 4294901760
%22 = icmp ne i64 %21, 0
br i1 %22, label %23, label %30
23: ; preds = %18
%24 = load i32, i32* %3
%25 = sext i32 %24 to i64
%26 = and i64 %25, 4278190080
%27 = icmp ne i64 %26, 0
%28 = zext i1 %27 to i64
%29 = select i1 %27, i32 3, i32 2
store i32 %29, i32* %4
br label %37
30: ; preds = %18
%31 = load i32, i32* %3
%32 = sext i32 %31 to i64
%33 = and i64 %32, 65280
%34 = icmp ne i64 %33, 0
%35 = zext i1 %34 to i64
%36 = select i1 %34, i32 1, i32 0
store i32 %36, i32* %4
br label %37
37: ; preds = %30, %23
%38 = load i32, i32* %3
%39 = load i32, i32* %4
%40 = shl i32 %39, 3
%41 = ashr i32 %38, %40
store i32 %41, i32* %5
%42 = load i32, i32* %5
%43 = lshr i32 %42, 1
%44 = load i32, i32* %5
%45 = or i32 %44, %43
store i32 %45, i32* %5
%46 = load i32, i32* %5
%47 = lshr i32 %46, 2
%48 = load i32, i32* %5
%49 = or i32 %48, %47
store i32 %49, i32* %5
%50 = load i32, i32* %5
%51 = lshr i32 %50, 4
%52 = load i32, i32* %5
%53 = or i32 %52, %51
store i32 %53, i32* %5
%54 = load i32, i32* %5
%55 = add i32 %54, 1
%56 = load i32, i32* %4
%57 = shl i32 %56, 3
%58 = shl i32 %55, %57
store i32 %58, i32* %3
%59 = load i32, i32* %3
store i32 %59, i32* %2
br label %60
60: ; preds = %37, %15, %8
%61 = load i32, i32* %2
ret i32 %61
}

LLVM IR (-O2):
define i32 @round_up_pow2(i32 %0) {
%2 = icmp eq i32 %0, 0
br i1 %2, label %30, label %3
3: ; preds = %1
%4 = tail call i32 @llvm.ctpop.i32(i32 %0)
%5 = icmp ult i32 %4, 2
br i1 %5, label %30, label %6
6: ; preds = %3
%7 = zext i32 %0 to i64
%8 = and i64 %7, 4294901760
%9 = icmp eq i64 %8, 0
br i1 %9, label %14, label %10
10: ; preds = %6
%11 = and i64 %7, 4278190080
%12 = icmp eq i64 %11, 0
%13 = select i1 %12, i32 2, i32 3
br label %18
14: ; preds = %6
%15 = and i64 %7, 65280
%16 = icmp ne i64 %15, 0
%17 = zext i1 %16 to i32
br label %18
18: ; preds = %14, %10
%19 = phi i32 [ %13, %10 ], [ %17, %14 ]
%20 = shl nuw nsw i32 %19, 3
%21 = ashr i32 %0, %20
%22 = lshr i32 %21, 1
%23 = or i32 %22, %21
%24 = lshr i32 %23, 2
%25 = or i32 %24, %23
%26 = lshr i32 %25, 4
%27 = or i32 %26, %25
%28 = add i32 %27, 1
%29 = shl i32 %28, %20
br label %30
30: ; preds = %3, %1, %18
%31 = phi i32 [ %29, %18 ], [ 1, %1 ], [ %0, %3 ]
ret i32 %31
}