#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 uint8_t;

/* Variables and functions */
uint8_t setbits(uint8_t x, uint8_t p, uint8_t n, uint8_t y) {
  uint8_t y_bits;
  uint8_t result;
  uint8_t x_mask_left;
  uint8_t x_mask_right;
  uint8_t x_mask;
  y_bits = 0;
  result = 0;
  x_mask_left = 0xff;
  x_mask_right = 0xff;
  x_mask = 0;
  x_mask_left = x_mask_left >> p;
  x_mask_left = ~x_mask_left;
  x_mask_right = x_mask_right >> (8 - p);
  x_mask = x_mask_left | x_mask_right;
  y_bits = y << (8 - n);
  y_bits = y_bits >> p;
  result = x_mask & x;
  result = result | y_bits;
  return result;
}


x86 assembly (clang, -O0):
<setbits>:
push   %rbp
mov    %rsp,%rbp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %edx,-0xc(%rbp)
mov    %ecx,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
movl   $0xff,-0x1c(%rbp)
movl   $0xff,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x1c(%rbp),%eax
mov    -0x8(%rbp),%ecx
sar    %cl,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
xor    $0xffffffff,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x20(%rbp),%eax
mov    $0x8,%ecx
sub    -0x8(%rbp),%ecx
sar    %cl,%eax
mov    %eax,-0x20(%rbp)
mov    -0x1c(%rbp),%eax
or     -0x20(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x10(%rbp),%eax
mov    $0x8,%ecx
sub    -0xc(%rbp),%ecx
shl    %cl,%eax
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    -0x8(%rbp),%ecx
sar    %cl,%eax
mov    %eax,-0x14(%rbp)
mov    -0x24(%rbp),%eax
and    -0x4(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%eax
or     -0x14(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%eax
pop    %rbp
ret

x86 assembly (clang, -O2):
<setbits>:
mov    %ecx,%r9d
mov    %esi,%r10d
mov    $0xffffff00,%esi
mov    %r10d,%ecx
sar    %cl,%esi
mov    $0x8,%r8b
mov    $0x8,%cl
sub    %r10b,%cl
mov    $0xff,%eax
shr    %cl,%eax
sub    %dl,%r8b
mov    %r8d,%ecx
shl    %cl,%r9d
mov    %r10d,%ecx
sar    %cl,%r9d
or     %esi,%eax
and    %edi,%eax
or     %r9d,%eax
ret

LLVM IR (-O0):
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
store i32 %0, i32* %5
store i32 %1, i32* %6
store i32 %2, i32* %7
store i32 %3, i32* %8
store i32 0, i32* %9
store i32 0, i32* %10
store i32 255, i32* %11
store i32 255, i32* %12
store i32 0, i32* %13
%14 = load i32, i32* %11
%15 = load i32, i32* %6
%16 = ashr i32 %14, %15
store i32 %16, i32* %11
%17 = load i32, i32* %11
%18 = xor i32 %17, -1
store i32 %18, i32* %11
%19 = load i32, i32* %12
%20 = load i32, i32* %6
%21 = sub nsw i32 8, %20
%22 = ashr i32 %19, %21
store i32 %22, i32* %12
%23 = load i32, i32* %11
%24 = load i32, i32* %12
%25 = or i32 %23, %24
store i32 %25, i32* %13
%26 = load i32, i32* %8
%27 = load i32, i32* %7
%28 = sub nsw i32 8, %27
%29 = shl i32 %26, %28
store i32 %29, i32* %9
%30 = load i32, i32* %9
%31 = load i32, i32* %6
%32 = ashr i32 %30, %31
store i32 %32, i32* %9
%33 = load i32, i32* %13
%34 = load i32, i32* %5
%35 = and i32 %33, %34
store i32 %35, i32* %10
%36 = load i32, i32* %10
%37 = load i32, i32* %9
%38 = or i32 %36, %37
store i32 %38, i32* %10
%39 = load i32, i32* %10
ret i32 %39
}

LLVM IR (-O2):
define i32 @setbits(i32 %0, i32 %1, i32 %2, i32 %3) {
%5 = ashr i32 -256, %1
%6 = sub nsw i32 8, %1
%7 = lshr i32 255, %6
%8 = or i32 %7, %5
%9 = sub nsw i32 8, %2
%10 = shl i32 %3, %9
%11 = ashr i32 %10, %1
%12 = and i32 %8, %0
%13 = or i32 %12, %11
ret i32 %13
}