#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 unsigned int first_half;

/* Variables and functions */
int /*<<< orphan*/ assert(int);
unsigned next_largest(unsigned n) {
  assert(n != 0);
  assert(n != ~0);
  unsigned ls_bit = n & ~(n - 1);
  unsigned first_half = n + ls_bit;
  assert(first_half != 0);
  unsigned ms_bit = (first_half) & ~(first_half - 1);
  unsigned rest = ((ms_bit / ls_bit) - 1) >> 1;
  return first_half | rest;
}


x86 assembly (clang, -O0):
<next_largest>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
cmpl   $0x0,-0x4(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1030 <assert@plt>
cmpl   $0xffffffff,-0x4(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1030 <assert@plt>
mov    -0x4(%rbp),%eax
mov    -0x4(%rbp),%ecx
sub    $0x1,%ecx
xor    $0xffffffff,%ecx
and    %ecx,%eax
mov    %eax,-0x8(%rbp)
mov    -0x4(%rbp),%eax
add    -0x8(%rbp),%eax
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1030 <assert@plt>
mov    -0xc(%rbp),%eax
mov    -0xc(%rbp),%ecx
sub    $0x1,%ecx
xor    $0xffffffff,%ecx
and    %ecx,%eax
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%eax
xor    %edx,%edx
divl   -0x8(%rbp)
sub    $0x1,%eax
shr    $0x1,%eax
mov    %eax,-0x14(%rbp)
mov    -0xc(%rbp),%eax
or     -0x14(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<next_largest>:
push   %rbp
push   %r14
push   %rbx
mov    %edi,%ebx
xor    %edi,%edi
mov    %ebx,%r14d
neg    %r14d
setb   %dil
call   1030 <assert@plt>
xor    %edi,%edi
cmp    $0xffffffff,%ebx
setne  %dil
call   1030 <assert@plt>
and    %ebx,%r14d
add    %r14d,%ebx
xor    %edi,%edi
mov    %ebx,%ebp
neg    %ebp
setb   %dil
call   1030 <assert@plt>
and    %ebx,%ebp
mov    %ebp,%eax
xor    %edx,%edx
div    %r14d
add    $0xffffffff,%eax
shr    %eax
or     %ebx,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

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* %2
%7 = load i32, i32* %2
%8 = icmp ne i32 %7, 0
%9 = zext i1 %8 to i32
%10 = call i32 @assert(i32 %9)
%11 = load i32, i32* %2
%12 = icmp ne i32 %11, -1
%13 = zext i1 %12 to i32
%14 = call i32 @assert(i32 %13)
%15 = load i32, i32* %2
%16 = load i32, i32* %2
%17 = sub i32 %16, 1
%18 = xor i32 %17, -1
%19 = and i32 %15, %18
store i32 %19, i32* %3
%20 = load i32, i32* %2
%21 = load i32, i32* %3
%22 = add i32 %20, %21
store i32 %22, i32* %4
%23 = load i32, i32* %4
%24 = icmp ne i32 %23, 0
%25 = zext i1 %24 to i32
%26 = call i32 @assert(i32 %25)
%27 = load i32, i32* %4
%28 = load i32, i32* %4
%29 = sub i32 %28, 1
%30 = xor i32 %29, -1
%31 = and i32 %27, %30
store i32 %31, i32* %5
%32 = load i32, i32* %5
%33 = load i32, i32* %3
%34 = udiv i32 %32, %33
%35 = sub i32 %34, 1
%36 = lshr i32 %35, 1
store i32 %36, i32* %6
%37 = load i32, i32* %4
%38 = load i32, i32* %6
%39 = or i32 %37, %38
ret i32 %39
}

LLVM IR (-O2):
define i32 @next_largest(i32 %0) {
%2 = icmp ne i32 %0, 0
%3 = zext i1 %2 to i32
%4 = tail call i32 @assert(i32 %3)
%5 = icmp ne i32 %0, -1
%6 = zext i1 %5 to i32
%7 = tail call i32 @assert(i32 %6)
%8 = sub i32 0, %0
%9 = and i32 %8, %0
%10 = add i32 %9, %0
%11 = icmp ne i32 %10, 0
%12 = zext i1 %11 to i32
%13 = tail call i32 @assert(i32 %12)
%14 = sub i32 0, %10
%15 = and i32 %10, %14
%16 = udiv i32 %15, %9
%17 = add i32 %16, -1
%18 = lshr i32 %17, 1
%19 = or i32 %18, %10
ret i32 %19
}