#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 alea(int);
int count_set_bits(int);
int random_code(int ref) {
  int nb = 6 - count_set_bits(ref);
  int b = alea(1 << nb);
  int iter = 1;
  int result = 0;
  while (nb) {
    if (!(iter & ref)) {
      if (b & 1)
        result |= iter;
      b >>= 1;
      nb--;
    }
    iter <<= 1;
  }
  return result;
}


x86 assembly (clang, -O0):
<random_code>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    -0x4(%rbp),%edi
call   1040 <count_set_bits@plt>
mov    %eax,%ecx
mov    $0x6,%eax
sub    %ecx,%eax
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%ecx
mov    $0x1,%edi
shl    %cl,%edi
call   1030 <alea@plt>
mov    %eax,-0xc(%rbp)
movl   $0x1,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
cmpl   $0x0,-0x8(%rbp)
je     11b0 <random_code+0x90>
mov    -0x10(%rbp),%eax
and    -0x4(%rbp),%eax
cmp    $0x0,%eax
jne    11a2 <random_code+0x82>
mov    -0xc(%rbp),%eax
and    $0x1,%eax
cmp    $0x0,%eax
je     1190 <random_code+0x70>
mov    -0x10(%rbp),%eax
or     -0x14(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    -0xc(%rbp),%eax
sar    $0x1,%eax
mov    %eax,-0xc(%rbp)
mov    -0x8(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x8(%rbp)
mov    -0x10(%rbp),%eax
shl    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    115f <random_code+0x3f>
mov    -0x14(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<random_code>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edi,%r15d
call   1040 <count_set_bits@plt>
mov    %eax,%r14d
lea    -0x6(%r14),%ebp
neg    %ebp
mov    $0x1,%ebx
mov    $0x1,%edi
mov    %ebp,%ecx
shl    %cl,%edi
call   1030 <alea@plt>
xor    %ecx,%ecx
cmp    $0x6,%r14d
jne    1160 <random_code+0x40>
mov    %ecx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
xor    %ecx,%ecx
jmp    1176 <random_code+0x56>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
add    %ebx,%ebx
test   %ebp,%ebp
je     1153 <random_code+0x33>
test   %r15d,%ebx
jne    1170 <random_code+0x50>
mov    %eax,%edx
and    $0x1,%edx
neg    %edx
and    %ebx,%edx
or     %edx,%ecx
sar    %eax
add    $0xffffffff,%ebp
jmp    1170 <random_code+0x50>

LLVM IR (-O0):
define i32 @random_code(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 = call i32 @count_set_bits(i32 %7)
%9 = sub nsw i32 6, %8
store i32 %9, i32* %3
%10 = load i32, i32* %3
%11 = shl i32 1, %10
%12 = call i32 @alea(i32 %11)
store i32 %12, i32* %4
store i32 1, i32* %5
store i32 0, i32* %6
br label %13
13: ; preds = %34, %1
%14 = load i32, i32* %3
%15 = icmp ne i32 %14, 0
br i1 %15, label %16, label %37
16: ; preds = %13
%17 = load i32, i32* %5
%18 = load i32, i32* %2
%19 = and i32 %17, %18
%20 = icmp ne i32 %19, 0
br i1 %20, label %34, label %21
21: ; preds = %16
%22 = load i32, i32* %4
%23 = and i32 %22, 1
%24 = icmp ne i32 %23, 0
br i1 %24, label %25, label %29
25: ; preds = %21
%26 = load i32, i32* %5
%27 = load i32, i32* %6
%28 = or i32 %27, %26
store i32 %28, i32* %6
br label %29
29: ; preds = %25, %21
%30 = load i32, i32* %4
%31 = ashr i32 %30, 1
store i32 %31, i32* %4
%32 = load i32, i32* %3
%33 = add nsw i32 %32, -1
store i32 %33, i32* %3
br label %34
34: ; preds = %29, %16
%35 = load i32, i32* %5
%36 = shl i32 %35, 1
store i32 %36, i32* %5
br label %13
37: ; preds = %13
%38 = load i32, i32* %6
ret i32 %38
}

LLVM IR (-O2):
define i32 @random_code(i32 %0) {
%2 = tail call i32 @count_set_bits(i32 %0)
%3 = sub nsw i32 6, %2
%4 = shl nuw i32 1, %3
%5 = tail call i32 @alea(i32 %4)
%6 = icmp eq i32 %2, 6
br i1 %6, label %27, label %7
7: ; preds = %1, %21
%8 = phi i32 [ %24, %21 ], [ 0, %1 ]
%9 = phi i32 [ %25, %21 ], [ 1, %1 ]
%10 = phi i32 [ %23, %21 ], [ %5, %1 ]
%11 = phi i32 [ %22, %21 ], [ %3, %1 ]
%12 = and i32 %9, %0
%13 = icmp eq i32 %12, 0
br i1 %13, label %14, label %21
14: ; preds = %7
%15 = and i32 %10, 1
%16 = icmp eq i32 %15, 0
%17 = select i1 %16, i32 0, i32 %9
%18 = or i32 %17, %8
%19 = ashr i32 %10, 1
%20 = add nsw i32 %11, -1
br label %21
21: ; preds = %14, %7
%22 = phi i32 [ %11, %7 ], [ %20, %14 ]
%23 = phi i32 [ %10, %7 ], [ %19, %14 ]
%24 = phi i32 [ %8, %7 ], [ %18, %14 ]
%25 = shl i32 %9, 1
%26 = icmp eq i32 %22, 0
br i1 %26, label %27, label %7
27: ; preds = %21, %1
%28 = phi i32 [ 0, %1 ], [ %24, %21 ]
ret i32 %28
}