#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 int uint64_t;

/* Variables and functions */
int /*<<< orphan*/ printf(char *, int, ...);
uint64_t set_from_shift(uint64_t a, uint64_t b) {
  printf("set_from_shift input - a: %lu, b: %lu\n", a, b);
  uint64_t shift, set_bits;
  uint64_t high_bit = 1 << b - 1;
  shift = 1;
  set_bits = 0;
  while (a) {
    set_bits *= 2;
    if (a & high_bit) {
      set_bits |= 1;
    } else {
      a -= 1;
    }
    shift += 1;
    a %= high_bit;
    high_bit >>= 1;
    printf("end while loop set_bits: %lu\n", set_bits);
    printf("end while loop shift: %lu\n", shift);
  }
  uint64_t result = set_bits + ((1 << (shift - 1)) >> 1);
  printf("set_from_shift result: %lu\n", result);
  return result;
}


x86 assembly (clang, -O0):
<set_from_shift>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    -0x4(%rbp),%esi
mov    -0x8(%rbp),%edx
lea    0xed5(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x8(%rbp),%ecx
sub    $0x1,%ecx
mov    $0x1,%eax
shl    %cl,%eax
mov    %eax,-0x14(%rbp)
movl   $0x1,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
cmpl   $0x0,-0x4(%rbp)
je     11ce <set_from_shift+0xbe>
mov    -0x10(%rbp),%eax
shl    $0x1,%eax
mov    %eax,-0x10(%rbp)
mov    -0x4(%rbp),%eax
and    -0x14(%rbp),%eax
cmp    $0x0,%eax
je     1180 <set_from_shift+0x70>
mov    -0x10(%rbp),%eax
or     $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    1189 <set_from_shift+0x79>
mov    -0x4(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x4(%rbp)
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
mov    -0x14(%rbp),%ecx
mov    -0x4(%rbp),%eax
cltd
idiv   %ecx
mov    %edx,-0x4(%rbp)
mov    -0x14(%rbp),%eax
sar    $0x1,%eax
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%esi
lea    0xe76(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0xc(%rbp),%esi
lea    0xe83(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    1150 <set_from_shift+0x40>
mov    -0x10(%rbp),%eax
mov    -0xc(%rbp),%ecx
sub    $0x1,%ecx
mov    $0x1,%edx
shl    %cl,%edx
mov    %edx,%ecx
sar    $0x1,%ecx
add    %ecx,%eax
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%esi
lea    0xe6e(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x18(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<set_from_shift>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %esi,%ebp
mov    %edi,%ebx
lea    0xeda(%rip),%rdi
xor    %r14d,%r14d
mov    %ebx,%esi
mov    %ebp,%edx
xor    %eax,%eax
call   1030 <printf@plt>
mov    $0x0,%r15d
test   %ebx,%ebx
je     11a7 <set_from_shift+0x97>
add    $0xff,%bpl
mov    $0x1,%r12d
mov    %ebp,%ecx
shl    %cl,%r12d
lea    0xef1(%rip),%r13
xor    %r14d,%r14d
xor    %r15d,%r15d
nopw   0x0(%rax,%rax,1)
xor    %eax,%eax
xor    %ecx,%ecx
test   %r12d,%ebx
sete   %al
setne  %cl
lea    (%rcx,%r15,2),%r15d
sub    %eax,%ebx
lea    0x2(%r14),%ebp
mov    %ebx,%eax
cltd
idiv   %r12d
sar    %r12d
mov    %edx,%ebx
lea    0xe9e(%rip),%rdi
mov    %r15d,%esi
xor    %eax,%eax
call   1030 <printf@plt>
mov    %r13,%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1030 <printf@plt>
add    $0x1,%r14d
test   %ebx,%ebx
jne    1160 <set_from_shift+0x50>
mov    $0x1,%ebx
mov    %r14d,%ecx
shl    %cl,%ebx
sar    %ebx
add    %r15d,%ebx
lea    0xea3(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1030 <printf@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [39 x i8] c"set_from_shift input - a: %lu, b: %lu\0A\00"
@.str.1 = [30 x i8] c"end while loop set_bits: %lu\0A\00"
@.str.2 = [27 x i8] c"end while loop shift: %lu\0A\00"
@.str.3 = [28 x i8] c"set_from_shift result: %lu\0A\00"
define i32 @set_from_shift(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i32 %0, i32* %3
store i32 %1, i32* %4
%9 = load i32, i32* %3
%10 = load i32, i32* %4
%11 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str, i64 0, i64 0), i32 %9, i32 %10)
%12 = load i32, i32* %4
%13 = sub nsw i32 %12, 1
%14 = shl i32 1, %13
store i32 %14, i32* %7
store i32 1, i32* %5
store i32 0, i32* %6
br label %15
15: ; preds = %31, %2
%16 = load i32, i32* %3
%17 = icmp ne i32 %16, 0
br i1 %17, label %18, label %43
18: ; preds = %15
%19 = load i32, i32* %6
%20 = mul nsw i32 %19, 2
store i32 %20, i32* %6
%21 = load i32, i32* %3
%22 = load i32, i32* %7
%23 = and i32 %21, %22
%24 = icmp ne i32 %23, 0
br i1 %24, label %25, label %28
25: ; preds = %18
%26 = load i32, i32* %6
%27 = or i32 %26, 1
store i32 %27, i32* %6
br label %31
28: ; preds = %18
%29 = load i32, i32* %3
%30 = sub nsw i32 %29, 1
store i32 %30, i32* %3
br label %31
31: ; preds = %28, %25
%32 = load i32, i32* %5
%33 = add nsw i32 %32, 1
store i32 %33, i32* %5
%34 = load i32, i32* %7
%35 = load i32, i32* %3
%36 = srem i32 %35, %34
store i32 %36, i32* %3
%37 = load i32, i32* %7
%38 = ashr i32 %37, 1
store i32 %38, i32* %7
%39 = load i32, i32* %6
%40 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0), i32 %39)
%41 = load i32, i32* %5
%42 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0), i32 %41)
br label %15
43: ; preds = %15
%44 = load i32, i32* %6
%45 = load i32, i32* %5
%46 = sub nsw i32 %45, 1
%47 = shl i32 1, %46
%48 = ashr i32 %47, 1
%49 = add nsw i32 %44, %48
store i32 %49, i32* %8
%50 = load i32, i32* %8
%51 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0), i32 %50)
%52 = load i32, i32* %8
ret i32 %52
}

LLVM IR (-O2):
@.str = [39 x i8] c"set_from_shift input - a: %lu, b: %lu\0A\00"
@.str.1 = [30 x i8] c"end while loop set_bits: %lu\0A\00"
@.str.2 = [27 x i8] c"end while loop shift: %lu\0A\00"
@.str.3 = [28 x i8] c"set_from_shift result: %lu\0A\00"
define i32 @set_from_shift(i32 %0, i32 %1) {
%3 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str, i64 0, i64 0), i32 %0, i32 %1)
%4 = icmp eq i32 %0, 0
br i1 %4, label %27, label %5
5: ; preds = %2
%6 = add nsw i32 %1, -1
%7 = shl nuw i32 1, %6
br label %8
8: ; preds = %5, %8
%9 = phi i32 [ %22, %8 ], [ %0, %5 ]
%10 = phi i32 [ %23, %8 ], [ %7, %5 ]
%11 = phi i32 [ %18, %8 ], [ 0, %5 ]
%12 = phi i32 [ %21, %8 ], [ 1, %5 ]
%13 = shl nsw i32 %11, 1
%14 = and i32 %9, %10
%15 = icmp eq i32 %14, 0
%16 = xor i1 %15, true
%17 = zext i1 %16 to i32
%18 = or i32 %13, %17
%19 = sext i1 %15 to i32
%20 = add nsw i32 %9, %19
%21 = add nuw nsw i32 %12, 1
%22 = srem i32 %20, %10
%23 = ashr i32 %10, 1
%24 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0), i32 %18)
%25 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0), i32 %21)
%26 = icmp eq i32 %22, 0
br i1 %26, label %27, label %8
27: ; preds = %8, %2
%28 = phi i32 [ 0, %2 ], [ %12, %8 ]
%29 = phi i32 [ 0, %2 ], [ %18, %8 ]
%30 = shl nuw i32 1, %28
%31 = ashr i32 %30, 1
%32 = add nsw i32 %31, %29
%33 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0), i32 %32)
ret i32 %32
}