#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 unset_bit(int, int);
int unset_bits_less(int, int);
int next_smallest(int number) {
  int trail_zeros = 0;
  int trail_ones = 0;
  int flip_pos = 0;
  while (get_bit(number, flip_pos)) {
    flip_pos++;
    trail_ones++;
    if (flip_pos > 32)
      return number;
  }
  while (!get_bit(number, flip_pos)) {
    flip_pos++;
    trail_zeros++;
    if (flip_pos > 32)
      return number;
  }
  number = unset_bit(number, flip_pos);
  trail_ones++;
  trail_zeros--;
  number = unset_bits_less(number, flip_pos);
  return number | (((1 << trail_ones) - 1) << trail_zeros);
}


x86 assembly (clang, -O0):
<next_smallest>:
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   1040 <get_bit@plt>
cmp    $0x0,%rax
je     1191 <next_smallest+0x61>
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    118c <next_smallest+0x5c>
mov    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    122d <next_smallest+0xfd>
jmp    1150 <next_smallest+0x20>
jmp    1196 <next_smallest+0x66>
mov    -0x8(%rbp),%edi
mov    -0x14(%rbp),%esi
call   1040 <get_bit@plt>
cmp    $0x0,%rax
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    11b7 <next_smallest+0x87>
jmp    11e3 <next_smallest+0xb3>
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    11de <next_smallest+0xae>
mov    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    122d <next_smallest+0xfd>
jmp    1196 <next_smallest+0x66>
mov    -0x8(%rbp),%edi
mov    -0x14(%rbp),%esi
call   1050 <unset_bit@plt>
mov    %eax,-0x8(%rbp)
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
mov    -0xc(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0xc(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x14(%rbp),%esi
call   1030 <unset_bits_less@plt>
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%eax
mov    -0x10(%rbp),%ecx
mov    $0x1,%edx
shl    %cl,%edx
sub    $0x1,%edx
mov    -0xc(%rbp),%ecx
shl    %cl,%edx
mov    %edx,%ecx
or     %ecx,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<next_smallest>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edi,%r14d
xor    %ebp,%ebp
nopl   0x0(%rax)
mov    %r14d,%edi
mov    %ebp,%esi
call   1040 <get_bit@plt>
test   %rax,%rax
je     1159 <next_smallest+0x29>
add    $0x1,%ebp
cmp    $0x21,%ebp
jne    1140 <next_smallest+0x10>
jmp    11bb <next_smallest+0x8b>
mov    $0x1,%r15d
mov    %ebp,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %r14d,%edi
mov    %ebx,%esi
call   1040 <get_bit@plt>
test   %rax,%rax
jne    118d <next_smallest+0x5d>
add    $0x1,%ebx
add    $0xffffffff,%r15d
cmp    $0x21,%ebx
jne    1170 <next_smallest+0x40>
jmp    11bb <next_smallest+0x8b>
mov    %r14d,%edi
mov    %ebx,%esi
call   1050 <unset_bit@plt>
mov    %eax,%edi
mov    %ebx,%esi
call   1030 <unset_bits_less@plt>
mov    $0x2,%r14d
mov    %ebp,%ecx
shl    %cl,%r14d
add    $0xffffffff,%r14d
neg    %r15b
mov    %r15d,%ecx
shl    %cl,%r14d
or     %eax,%r14d
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @next_smallest(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 = %21, %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
br i1 %11, label %12, label %22
12: ; preds = %7
%13 = load i32, i32* %6
%14 = add nsw i32 %13, 1
store i32 %14, i32* %6
%15 = load i32, i32* %5
%16 = add nsw i32 %15, 1
store i32 %16, i32* %5
%17 = load i32, i32* %6
%18 = icmp sgt i32 %17, 32
br i1 %18, label %19, label %21
19: ; preds = %12
%20 = load i32, i32* %3
store i32 %20, i32* %2
br label %57
21: ; preds = %12
br label %7
22: ; preds = %7
br label %23
23: ; preds = %38, %22
%24 = load i32, i32* %3
%25 = load i32, i32* %6
%26 = call i64 @get_bit(i32 %24, i32 %25)
%27 = icmp ne i64 %26, 0
%28 = xor i1 %27, true
br i1 %28, label %29, label %39
29: ; preds = %23
%30 = load i32, i32* %6
%31 = add nsw i32 %30, 1
store i32 %31, i32* %6
%32 = load i32, i32* %4
%33 = add nsw i32 %32, 1
store i32 %33, i32* %4
%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 %57
38: ; preds = %29
br label %23
39: ; preds = %23
%40 = load i32, i32* %3
%41 = load i32, i32* %6
%42 = call i32 @unset_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* %4
%46 = add nsw i32 %45, -1
store i32 %46, i32* %4
%47 = load i32, i32* %3
%48 = load i32, i32* %6
%49 = call i32 @unset_bits_less(i32 %47, i32 %48)
store i32 %49, i32* %3
%50 = load i32, i32* %3
%51 = load i32, i32* %5
%52 = shl i32 1, %51
%53 = sub nsw i32 %52, 1
%54 = load i32, i32* %4
%55 = shl i32 %53, %54
%56 = or i32 %50, %55
store i32 %56, i32* %2
br label %57
57: ; preds = %39, %36, %19
%58 = load i32, i32* %2
ret i32 %58
}

LLVM IR (-O2):
define i32 @next_smallest(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 %6, label %8
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 %28, 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 %16, label %20
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 %28, label %11
20: ; preds = %11
%21 = tail call i32 @unset_bit(i32 %0, i32 %13)
%22 = add nsw i32 %12, -1
%23 = tail call i32 @unset_bits_less(i32 %21, i32 %13)
%24 = shl i32 2, %3
%25 = add nsw i32 %24, -1
%26 = shl i32 %25, %22
%27 = or i32 %23, %26
br label %28
28: ; preds = %8, %16, %20
%29 = phi i32 [ %27, %20 ], [ %0, %16 ], [ %0, %8 ]
ret i32 %29
}