#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 */
scalar_t__ get_bit(int, int);
int set_bit(int, int);
int set_bits_less(int, int);
int unset_bits_less(int, int);
int next_largest(int number) {
  int trail_zeros = 0;
  int trail_ones = 0;
  int flip_pos = 0;
  while (!get_bit(number, flip_pos)) {
    flip_pos++;
    trail_zeros++;
    if (flip_pos > 32)
      return number;
  }
  while (get_bit(number, flip_pos)) {
    flip_pos++;
    trail_ones++;
    if (flip_pos > 32)
      return number;
  }
  number = set_bit(number, flip_pos);
  trail_ones--;
  number = unset_bits_less(number, flip_pos);
  number = set_bits_less(number, trail_ones);
  return number;
}


x86 assembly (clang, -O0):
<next_largest>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x14(%rbp),%esi
call   1060 <get_bit@plt>
cmp    $0x0,%rax
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    1181 <next_largest+0x41>
jmp    11ad <next_largest+0x6d>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
cmpl   $0x20,-0x14(%rbp)
jle    11a8 <next_largest+0x68>
mov    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    122c <next_largest+0xec>
jmp    1160 <next_largest+0x20>
jmp    11b2 <next_largest+0x72>
mov    -0x8(%rbp),%edi
mov    -0x14(%rbp),%esi
call   1060 <get_bit@plt>
cmp    $0x0,%rax
je     11f3 <next_largest+0xb3>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
cmpl   $0x20,-0x14(%rbp)
jle    11ee <next_largest+0xae>
mov    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    122c <next_largest+0xec>
jmp    11b2 <next_largest+0x72>
mov    -0x8(%rbp),%edi
mov    -0x14(%rbp),%esi
call   1050 <set_bit@plt>
mov    %eax,-0x8(%rbp)
mov    -0x10(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x10(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x14(%rbp),%esi
call   1040 <unset_bits_less@plt>
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%esi
call   1030 <set_bits_less@plt>
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<next_largest>:
push   %rbp
push   %r14
push   %rbx
mov    %edi,%r14d
xor    %ebx,%ebx
nopl   0x0(%rax)
mov    %r14d,%edi
mov    %ebx,%esi
call   1060 <get_bit@plt>
test   %rax,%rax
jne    1169 <next_largest+0x29>
add    $0x1,%ebx
cmp    $0x21,%ebx
jne    1150 <next_largest+0x10>
jmp    118a <next_largest+0x4a>
mov    $0x1,%ebp
xchg   %ax,%ax
mov    %r14d,%edi
mov    %ebx,%esi
call   1060 <get_bit@plt>
test   %rax,%rax
je     1192 <next_largest+0x52>
add    $0x1,%ebx
add    $0xffffffff,%ebp
cmp    $0x21,%ebx
jne    1170 <next_largest+0x30>
mov    %r14d,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    %r14d,%edi
mov    %ebx,%esi
call   1050 <set_bit@plt>
neg    %ebp
mov    %eax,%edi
mov    %ebx,%esi
call   1040 <unset_bits_less@plt>
mov    %eax,%edi
mov    %ebp,%esi
pop    %rbx
pop    %r14
pop    %rbp
jmp    1030 <set_bits_less@plt>

LLVM IR (-O0):
define i32 @next_largest(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store i32 %0, i32* %3
store i32 0, i32* %4
store i32 0, i32* %5
store i32 0, i32* %6
br label %7
7: ; preds = %22, %1
%8 = load i32, i32* %3
%9 = load i32, i32* %6
%10 = call i64 @get_bit(i32 %8, i32 %9)
%11 = icmp ne i64 %10, 0
%12 = xor i1 %11, true
br i1 %12, label %13, label %23
13: ; preds = %7
%14 = load i32, i32* %6
%15 = add nsw i32 %14, 1
store i32 %15, i32* %6
%16 = load i32, i32* %4
%17 = add nsw i32 %16, 1
store i32 %17, i32* %4
%18 = load i32, i32* %6
%19 = icmp sgt i32 %18, 32
br i1 %19, label %20, label %22
20: ; preds = %13
%21 = load i32, i32* %3
store i32 %21, i32* %2
br label %52
22: ; preds = %13
br label %7
23: ; preds = %7
br label %24
24: ; preds = %38, %23
%25 = load i32, i32* %3
%26 = load i32, i32* %6
%27 = call i64 @get_bit(i32 %25, i32 %26)
%28 = icmp ne i64 %27, 0
br i1 %28, label %29, label %39
29: ; preds = %24
%30 = load i32, i32* %6
%31 = add nsw i32 %30, 1
store i32 %31, i32* %6
%32 = load i32, i32* %5
%33 = add nsw i32 %32, 1
store i32 %33, i32* %5
%34 = load i32, i32* %6
%35 = icmp sgt i32 %34, 32
br i1 %35, label %36, label %38
36: ; preds = %29
%37 = load i32, i32* %3
store i32 %37, i32* %2
br label %52
38: ; preds = %29
br label %24
39: ; preds = %24
%40 = load i32, i32* %3
%41 = load i32, i32* %6
%42 = call i32 @set_bit(i32 %40, i32 %41)
store i32 %42, i32* %3
%43 = load i32, i32* %5
%44 = add nsw i32 %43, -1
store i32 %44, i32* %5
%45 = load i32, i32* %3
%46 = load i32, i32* %6
%47 = call i32 @unset_bits_less(i32 %45, i32 %46)
store i32 %47, i32* %3
%48 = load i32, i32* %3
%49 = load i32, i32* %5
%50 = call i32 @set_bits_less(i32 %48, i32 %49)
store i32 %50, i32* %3
%51 = load i32, i32* %3
store i32 %51, i32* %2
br label %52
52: ; preds = %39, %36, %20
%53 = load i32, i32* %2
ret i32 %53
}

LLVM IR (-O2):
define i32 @next_largest(i32 %0) {
br label %2
2: ; preds = %8, %1
%3 = phi i32 [ 0, %1 ], [ %9, %8 ]
%4 = tail call i64 @get_bit(i32 %0, i32 %3)
%5 = icmp eq i64 %4, 0
br i1 %5, label %8, label %6
6: ; preds = %2
%7 = sub nsw i32 33, %3
br label %11
8: ; preds = %2
%9 = add nuw nsw i32 %3, 1
%10 = icmp eq i32 %9, 33
br i1 %10, label %25, label %2
11: ; preds = %6, %16
%12 = phi i32 [ %18, %16 ], [ 0, %6 ]
%13 = phi i32 [ %17, %16 ], [ %3, %6 ]
%14 = tail call i64 @get_bit(i32 %0, i32 %13)
%15 = icmp eq i64 %14, 0
br i1 %15, label %20, label %16
16: ; preds = %11
%17 = add nuw nsw i32 %13, 1
%18 = add nuw nsw i32 %12, 1
%19 = icmp eq i32 %18, %7
br i1 %19, label %25, label %11
20: ; preds = %11
%21 = tail call i32 @set_bit(i32 %0, i32 %13)
%22 = add nsw i32 %12, -1
%23 = tail call i32 @unset_bits_less(i32 %21, i32 %13)
%24 = tail call i32 @set_bits_less(i32 %23, i32 %22)
br label %25
25: ; preds = %8, %16, %20
%26 = phi i32 [ %24, %20 ], [ %0, %16 ], [ %0, %8 ]
ret i32 %26
}