#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 */
int /*<<< orphan*/ assert(int);
int /*<<< orphan*/ print_binary(int);
int /*<<< orphan*/ print_binary_digits(int, int);
int /*<<< orphan*/ printf(char *, ...);
void test_bit_set_multiple() {
  printf("\n===== test_bit_set_multiple\n");
  uint8_t value = 0b11110000;
  uint8_t bp = 2;
  uint8_t new_bits = 0b1010;
  uint8_t result = (value & ~(0b1111 << bp)) | (new_bits << bp);
  printf("Initial value               : ");
  print_binary(value);
  printf("Bits to set at position %d:%d : ", bp + 3, bp);
  print_binary_digits(new_bits, 4);
  printf("Result                      : ");
  print_binary(result);
  assert(result == 0b11101000);
}


x86 assembly (clang, -O0):
<test_bit_set_multiple>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
lea    0xeb1(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0xf0,-0x4(%rbp)
movl   $0x2,-0x8(%rbp)
movl   $0xa,-0xc(%rbp)
mov    -0x4(%rbp),%eax
mov    -0x8(%rbp),%ecx
mov    $0xf,%edx
shl    %cl,%edx
mov    %edx,%ecx
xor    $0xffffffff,%ecx
and    %ecx,%eax
mov    -0xc(%rbp),%edx
mov    -0x8(%rbp),%ecx
shl    %cl,%edx
mov    %edx,%ecx
or     %ecx,%eax
mov    %eax,-0x10(%rbp)
lea    0xe89(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x4(%rbp),%edi
call   1050 <print_binary@plt>
mov    -0x8(%rbp),%esi
add    $0x3,%esi
mov    -0x8(%rbp),%edx
lea    0xe89(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0xc(%rbp),%edi
mov    $0x4,%esi
call   1060 <print_binary_digits@plt>
lea    0xe8f(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x10(%rbp),%edi
call   1050 <print_binary@plt>
cmpl   $0xe8,-0x10(%rbp)
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_bit_set_multiple>:
push   %rax
lea    0xf07(%rip),%rdi
call   1030 <puts@plt>
lea    0xe9c(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0xf0,%edi
call   1060 <print_binary@plt>
lea    0xea3(%rip),%rdi
mov    $0x5,%esi
mov    $0x2,%edx
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0xa,%edi
mov    $0x4,%esi
call   1070 <print_binary_digits@plt>
lea    0xe9d(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0xe8,%edi
call   1060 <print_binary@plt>
mov    $0x1,%edi
pop    %rax
jmp    1050 <assert@plt>

LLVM IR (-O0):
@.str = [30 x i8] c"\0A===== test_bit_set_multiple\0A\00"
@.str.1 = [31 x i8] c"Initial value : \00"
@.str.2 = [33 x i8] c"Bits to set at position %d:%d : \00"
@.str.3 = [31 x i8] c"Result : \00"
define void @test_bit_set_multiple() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0))
store i32 240, i32* %1
store i32 2, i32* %2
store i32 10, i32* %3
%6 = load i32, i32* %1
%7 = load i32, i32* %2
%8 = shl i32 15, %7
%9 = xor i32 %8, -1
%10 = and i32 %6, %9
%11 = load i32, i32* %3
%12 = load i32, i32* %2
%13 = shl i32 %11, %12
%14 = or i32 %10, %13
store i32 %14, i32* %4
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0))
%16 = load i32, i32* %1
%17 = call i32 @print_binary(i32 %16)
%18 = load i32, i32* %2
%19 = add nsw i32 %18, 3
%20 = load i32, i32* %2
%21 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0), i32 %19, i32 %20)
%22 = load i32, i32* %3
%23 = call i32 @print_binary_digits(i32 %22, i32 4)
%24 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.3, i64 0, i64 0))
%25 = load i32, i32* %4
%26 = call i32 @print_binary(i32 %25)
%27 = load i32, i32* %4
%28 = icmp eq i32 %27, 232
%29 = zext i1 %28 to i32
%30 = call i32 @assert(i32 %29)
ret void
}

LLVM IR (-O2):
@.str.1 = [31 x i8] c"Initial value : \00"
@.str.2 = [33 x i8] c"Bits to set at position %d:%d : \00"
@.str.3 = [31 x i8] c"Result : \00"
@str = [29 x i8] c"\0A===== test_bit_set_multiple\00"
define void @test_bit_set_multiple() {
%1 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @str, i64 0, i64 0))
%2 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0))
%3 = tail call i32 @print_binary(i32 240)
%4 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0), i32 5, i32 2)
%5 = tail call i32 @print_binary_digits(i32 10, i32 4)
%6 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @.str.3, i64 0, i64 0))
%7 = tail call i32 @print_binary(i32 232)
%8 = tail call i32 @assert(i32 1)
ret void
}