#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 /*<<< orphan*/ dec2bin(int);
int /*<<< orphan*/ printf(char *);
unsigned setbits(unsigned x, int p, int n, int y) {
  int r, rightmost_nbits_y, shift_value, x_bitmask, lhs, rhs;
  r = (1 << n) - 1;
  rightmost_nbits_y = y & r;
  shift_value = (p + 1 - n);
  rhs = rightmost_nbits_y << shift_value;
  x_bitmask = ~(r << shift_value);
  lhs = x & x_bitmask;
  printf("r = ");
  dec2bin(r);
  printf("y = ");
  dec2bin(y);
  printf("rightmost_nbits_y = ");
  dec2bin(rightmost_nbits_y);
  printf("rhs = ");
  dec2bin(rhs);
  printf("x = ");
  dec2bin(x);
  printf("x_bitmask = ");
  dec2bin(x_bitmask);
  printf("lhs = ");
  dec2bin(lhs);
  printf("lhs | rhs = ");
  dec2bin(lhs | rhs);
  return lhs | rhs;
}


x86 assembly (clang, -O0):
<setbits>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %edx,-0xc(%rbp)
mov    %ecx,-0x10(%rbp)
mov    -0xc(%rbp),%ecx
mov    $0x1,%eax
shl    %cl,%eax
sub    $0x1,%eax
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%eax
and    -0x14(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x8(%rbp),%eax
add    $0x1,%eax
sub    -0xc(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%eax
mov    -0x1c(%rbp),%ecx
shl    %cl,%eax
mov    %eax,-0x28(%rbp)
mov    -0x14(%rbp),%eax
mov    -0x1c(%rbp),%ecx
shl    %cl,%eax
xor    $0xffffffff,%eax
mov    %eax,-0x20(%rbp)
mov    -0x4(%rbp),%eax
and    -0x20(%rbp),%eax
mov    %eax,-0x24(%rbp)
lea    0xe7e(%rip),%rdi
call   1030 <printf@plt>
mov    -0x14(%rbp),%edi
call   1040 <dec2bin@plt>
lea    0xe7f(%rip),%rdi
call   1030 <printf@plt>
mov    -0x10(%rbp),%edi
call   1040 <dec2bin@plt>
lea    0xe5b(%rip),%rdi
call   1030 <printf@plt>
mov    -0x18(%rbp),%edi
call   1040 <dec2bin@plt>
lea    0xe7b(%rip),%rdi
call   1030 <printf@plt>
mov    -0x28(%rbp),%edi
call   1040 <dec2bin@plt>
lea    0xe48(%rip),%rdi
call   1030 <printf@plt>
mov    -0x4(%rbp),%edi
call   1040 <dec2bin@plt>
lea    0xe39(%rip),%rdi
call   1030 <printf@plt>
mov    -0x20(%rbp),%edi
call   1040 <dec2bin@plt>
lea    0xe32(%rip),%rdi
call   1030 <printf@plt>
mov    -0x24(%rbp),%edi
call   1040 <dec2bin@plt>
lea    0xe25(%rip),%rdi
call   1030 <printf@plt>
mov    -0x24(%rbp),%edi
or     -0x28(%rbp),%edi
call   1040 <dec2bin@plt>
mov    -0x24(%rbp),%eax
or     -0x28(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<setbits>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %ecx,%r12d
mov    %edx,%ecx
mov    $0xffffffff,%r13d
shl    %cl,%r13d
not    %r13d
mov    %r13d,%r14d
and    %r12d,%r14d
sub    %edx,%esi
lea    0x1(%rsi),%ecx
mov    %r14d,%r15d
shl    %cl,%r15d
mov    %r13d,%ebp
shl    %cl,%ebp
mov    %edi,0x4(%rsp)
not    %ebp
mov    %ebp,%ebx
and    %edi,%ebx
lea    0xe9d(%rip),%rdi
call   1030 <printf@plt>
mov    %r13d,%edi
call   1040 <dec2bin@plt>
lea    0xe9e(%rip),%rdi
call   1030 <printf@plt>
mov    %r12d,%edi
call   1040 <dec2bin@plt>
lea    0xe7a(%rip),%rdi
call   1030 <printf@plt>
mov    %r14d,%edi
call   1040 <dec2bin@plt>
lea    0xe9a(%rip),%rdi
call   1030 <printf@plt>
mov    %r15d,%edi
call   1040 <dec2bin@plt>
lea    0xe67(%rip),%rdi
call   1030 <printf@plt>
mov    0x4(%rsp),%edi
call   1040 <dec2bin@plt>
lea    0xe57(%rip),%rdi
call   1030 <printf@plt>
mov    %ebp,%edi
call   1040 <dec2bin@plt>
lea    0xe51(%rip),%rdi
call   1030 <printf@plt>
mov    %ebx,%edi
call   1040 <dec2bin@plt>
lea    0xe45(%rip),%rdi
call   1030 <printf@plt>
or     %r15d,%ebx
mov    %ebx,%edi
call   1040 <dec2bin@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [5 x i8] c"r = \00"
@.str.1 = [5 x i8] c"y = \00"
@.str.2 = [21 x i8] c"rightmost_nbits_y = \00"
@.str.3 = [7 x i8] c"rhs = \00"
@.str.4 = [5 x i8] c"x = \00"
@.str.5 = [13 x i8] c"x_bitmask = \00"
@.str.6 = [7 x i8] c"lhs = \00"
@.str.7 = [13 x i8] c"lhs | rhs = \00"
define i32 @setbits(i32 %0, i32 %1, i32 %2, i32 %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
store i32 %0, i32* %5
store i32 %1, i32* %6
store i32 %2, i32* %7
store i32 %3, i32* %8
%15 = load i32, i32* %7
%16 = shl i32 1, %15
%17 = sub nsw i32 %16, 1
store i32 %17, i32* %9
%18 = load i32, i32* %8
%19 = load i32, i32* %9
%20 = and i32 %18, %19
store i32 %20, i32* %10
%21 = load i32, i32* %6
%22 = add nsw i32 %21, 1
%23 = load i32, i32* %7
%24 = sub nsw i32 %22, %23
store i32 %24, i32* %11
%25 = load i32, i32* %10
%26 = load i32, i32* %11
%27 = shl i32 %25, %26
store i32 %27, i32* %14
%28 = load i32, i32* %9
%29 = load i32, i32* %11
%30 = shl i32 %28, %29
%31 = xor i32 %30, -1
store i32 %31, i32* %12
%32 = load i32, i32* %5
%33 = load i32, i32* %12
%34 = and i32 %32, %33
store i32 %34, i32* %13
%35 = call i32 @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%36 = load i32, i32* %9
%37 = call i32 @dec2bin(i32 %36)
%38 = call i32 @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%39 = load i32, i32* %8
%40 = call i32 @dec2bin(i32 %39)
%41 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
%42 = load i32, i32* %10
%43 = call i32 @dec2bin(i32 %42)
%44 = call i32 @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
%45 = load i32, i32* %14
%46 = call i32 @dec2bin(i32 %45)
%47 = call i32 @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
%48 = load i32, i32* %5
%49 = call i32 @dec2bin(i32 %48)
%50 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.5, i64 0, i64 0))
%51 = load i32, i32* %12
%52 = call i32 @dec2bin(i32 %51)
%53 = call i32 @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.6, i64 0, i64 0))
%54 = load i32, i32* %13
%55 = call i32 @dec2bin(i32 %54)
%56 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.7, i64 0, i64 0))
%57 = load i32, i32* %13
%58 = load i32, i32* %14
%59 = or i32 %57, %58
%60 = call i32 @dec2bin(i32 %59)
%61 = load i32, i32* %13
%62 = load i32, i32* %14
%63 = or i32 %61, %62
ret i32 %63
}

LLVM IR (-O2):
@.str = [5 x i8] c"r = \00"
@.str.1 = [5 x i8] c"y = \00"
@.str.2 = [21 x i8] c"rightmost_nbits_y = \00"
@.str.3 = [7 x i8] c"rhs = \00"
@.str.4 = [5 x i8] c"x = \00"
@.str.5 = [13 x i8] c"x_bitmask = \00"
@.str.6 = [7 x i8] c"lhs = \00"
@.str.7 = [13 x i8] c"lhs | rhs = \00"
define i32 @setbits(i32 %0, i32 %1, i32 %2, i32 %3) {
%5 = shl nsw i32 -1, %2
%6 = xor i32 %5, -1
%7 = and i32 %6, %3
%8 = add nsw i32 %1, 1
%9 = sub i32 %8, %2
%10 = shl i32 %7, %9
%11 = shl i32 %6, %9
%12 = xor i32 %11, -1
%13 = and i32 %12, %0
%14 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%15 = tail call i32 @dec2bin(i32 %6)
%16 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%17 = tail call i32 @dec2bin(i32 %3)
%18 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
%19 = tail call i32 @dec2bin(i32 %7)
%20 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
%21 = tail call i32 @dec2bin(i32 %10)
%22 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
%23 = tail call i32 @dec2bin(i32 %0)
%24 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.5, i64 0, i64 0))
%25 = tail call i32 @dec2bin(i32 %12)
%26 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @.str.6, i64 0, i64 0))
%27 = tail call i32 @dec2bin(i32 %13)
%28 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.7, i64 0, i64 0))
%29 = or i32 %13, %10
%30 = tail call i32 @dec2bin(i32 %29)
ret i32 %29
}