#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 abs(int);
scalar_t__ is_pow2(int);
int pow2_next(int);
int pow2_prev(int);
int pow2_near(int n) {
  if (is_pow2(n)) {
    return n;
  }
  int nNext = pow2_next(n);
  int nPrev = pow2_prev(n);
  if (nNext - n > abs(n - nPrev)) {
    return nPrev;
  } else {
    return nNext;
  }
}


x86 assembly (clang, -O0):
<pow2_near>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    -0x8(%rbp),%edi
call   1060 <is_pow2@plt>
cmp    $0x0,%rax
je     1168 <pow2_near+0x28>
mov    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11b0 <pow2_near+0x70>
mov    -0x8(%rbp),%edi
call   1040 <pow2_next@plt>
mov    %eax,-0xc(%rbp)
mov    -0x8(%rbp),%edi
call   1050 <pow2_prev@plt>
mov    %eax,-0x10(%rbp)
mov    -0xc(%rbp),%eax
sub    -0x8(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    -0x8(%rbp),%edi
sub    -0x10(%rbp),%edi
call   1030 <abs@plt>
mov    %eax,%ecx
mov    -0x14(%rbp),%eax
cmp    %ecx,%eax
jle    11aa <pow2_near+0x6a>
mov    -0x10(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11b0 <pow2_near+0x70>
mov    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pow2_near>:
push   %rbp
push   %rbx
push   %rax
mov    %edi,%ebx
call   1050 <is_pow2@plt>
test   %rax,%rax
jne    1163 <pow2_near+0x33>
mov    %ebx,%edi
call   1030 <pow2_next@plt>
mov    %eax,%ebp
mov    %ebx,%edi
call   1040 <pow2_prev@plt>
mov    %ebp,%ecx
sub    %ebx,%ecx
sub    %eax,%ebx
mov    %ebx,%edx
neg    %edx
cmovs  %ebx,%edx
cmp    %edx,%ecx
cmovg  %eax,%ebp
mov    %ebp,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
define i32 @pow2_near(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 = call i64 @is_pow2(i32 %6)
%8 = icmp ne i64 %7, 0
br i1 %8, label %9, label %11
9: ; preds = %1
%10 = load i32, i32* %3
store i32 %10, i32* %2
br label %28
11: ; preds = %1
%12 = load i32, i32* %3
%13 = call i32 @pow2_next(i32 %12)
store i32 %13, i32* %4
%14 = load i32, i32* %3
%15 = call i32 @pow2_prev(i32 %14)
store i32 %15, i32* %5
%16 = load i32, i32* %4
%17 = load i32, i32* %3
%18 = sub nsw i32 %16, %17
%19 = load i32, i32* %3
%20 = load i32, i32* %5
%21 = sub nsw i32 %19, %20
%22 = call i32 @abs(i32 %21)
%23 = icmp sgt i32 %18, %22
br i1 %23, label %24, label %26
24: ; preds = %11
%25 = load i32, i32* %5
store i32 %25, i32* %2
br label %28
26: ; preds = %11
%27 = load i32, i32* %4
store i32 %27, i32* %2
br label %28
28: ; preds = %26, %24, %9
%29 = load i32, i32* %2
ret i32 %29
}

LLVM IR (-O2):
define i32 @pow2_near(i32 %0) {
%2 = tail call i64 @is_pow2(i32 %0)
%3 = icmp eq i64 %2, 0
br i1 %3, label %4, label %12
4: ; preds = %1
%5 = tail call i32 @pow2_next(i32 %0)
%6 = tail call i32 @pow2_prev(i32 %0)
%7 = sub nsw i32 %5, %0
%8 = sub nsw i32 %0, %6
%9 = tail call i32 @llvm.abs.i32(i32 %8, i1 true)
%10 = icmp sgt i32 %7, %9
%11 = select i1 %10, i32 %6, i32 %5
br label %12
12: ; preds = %1, %4
%13 = phi i32 [ %11, %4 ], [ %0, %1 ]
ret i32 %13
}