#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 lg(unsigned int);
int /*<<< orphan*/ postcondition(int);
int /*<<< orphan*/ precondition(int);
unsigned nextPowerOfTwo(unsigned u) {
  precondition(u > 0);
  int lgu = lg(u);
  int lguRoundedUp = lg(u + (1 << lgu) - 1);
  unsigned n = 1 << lguRoundedUp;
  postcondition(n / 2 < u && u <= n);
  return n;
}


x86 assembly (clang, -O0):
<nextPowerOfTwo>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
cmpl   $0x0,-0x4(%rbp)
seta   %al
and    $0x1,%al
movzbl %al,%edi
call   1050 <precondition@plt>
mov    -0x4(%rbp),%edi
call   1030 <lg@plt>
mov    %eax,-0x8(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x8(%rbp),%ecx
mov    $0x1,%eax
shl    %cl,%eax
add    %eax,%edi
sub    $0x1,%edi
call   1030 <lg@plt>
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%ecx
mov    $0x1,%eax
shl    %cl,%eax
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%ecx
shr    $0x1,%ecx
xor    %eax,%eax
cmp    -0x4(%rbp),%ecx
mov    %al,-0x11(%rbp)
jae    119e <nextPowerOfTwo+0x6e>
mov    -0x4(%rbp),%eax
cmp    -0x10(%rbp),%eax
setbe  %al
mov    %al,-0x11(%rbp)
mov    -0x11(%rbp),%al
and    $0x1,%al
movzbl %al,%edi
call   1040 <postcondition@plt>
mov    -0x10(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<nextPowerOfTwo>:
push   %rbp
push   %r14
push   %rbx
mov    %edi,%r14d
xor    %edi,%edi
mov    %r14d,%ebx
add    $0xffffffff,%ebx
setb   %dil
call   1050 <precondition@plt>
mov    %r14d,%edi
call   1030 <lg@plt>
mov    $0x1,%ebp
mov    $0x1,%edi
mov    %eax,%ecx
shl    %cl,%edi
add    %ebx,%edi
call   1030 <lg@plt>
mov    %eax,%ecx
shl    %cl,%ebp
mov    %ebp,%eax
shr    %eax
cmp    %r14d,%eax
setb   %al
cmp    %r14d,%ebp
setae  %cl
and    %al,%cl
movzbl %cl,%edi
call   1040 <postcondition@plt>
mov    %ebp,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
define i32 @nextPowerOfTwo(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
store i32 %0, i32* %2
%6 = load i32, i32* %2
%7 = icmp ugt i32 %6, 0
%8 = zext i1 %7 to i32
%9 = call i32 @precondition(i32 %8)
%10 = load i32, i32* %2
%11 = call i32 @lg(i32 %10)
store i32 %11, i32* %3
%12 = load i32, i32* %2
%13 = load i32, i32* %3
%14 = shl i32 1, %13
%15 = add i32 %12, %14
%16 = sub i32 %15, 1
%17 = call i32 @lg(i32 %16)
store i32 %17, i32* %4
%18 = load i32, i32* %4
%19 = shl i32 1, %18
store i32 %19, i32* %5
%20 = load i32, i32* %5
%21 = udiv i32 %20, 2
%22 = load i32, i32* %2
%23 = icmp ult i32 %21, %22
br i1 %23, label %24, label %28
24: ; preds = %1
%25 = load i32, i32* %2
%26 = load i32, i32* %5
%27 = icmp ule i32 %25, %26
br label %28
28: ; preds = %24, %1
%29 = phi i1 [ false, %1 ], [ %27, %24 ]
%30 = zext i1 %29 to i32
%31 = call i32 @postcondition(i32 %30)
%32 = load i32, i32* %5
ret i32 %32
}

LLVM IR (-O2):
define i32 @nextPowerOfTwo(i32 %0) {
%2 = icmp ne i32 %0, 0
%3 = zext i1 %2 to i32
%4 = tail call i32 @precondition(i32 %3)
%5 = tail call i32 @lg(i32 %0)
%6 = shl nuw i32 1, %5
%7 = add i32 %0, -1
%8 = add i32 %7, %6
%9 = tail call i32 @lg(i32 %8)
%10 = shl nuw i32 1, %9
%11 = lshr i32 %10, 1
%12 = icmp ult i32 %11, %0
%13 = icmp uge i32 %10, %0
%14 = and i1 %13, %12
%15 = zext i1 %14 to i32
%16 = tail call i32 @postcondition(i32 %15)
ret i32 %10
}