#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 /*<<< orphan*/ uint64_t;

/* Variables and functions */
int /*<<< orphan*/ get_byte_and_offset(size_t, int /*<<< orphan*/, size_t *,
                                       unsigned char *);
void set_bit(unsigned char *bytes, size_t bytes_len, uint64_t index,
             unsigned val) {
  size_t byte;
  unsigned char offset;
  get_byte_and_offset(bytes_len, index, &byte, &offset);
  ;
  if (val) {
    bytes[byte] |= (1U << offset);
  } else {
    bytes[byte] &= ~(1U << offset);
  }
}


x86 assembly (clang, -O0):
<set_bit>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
lea    -0x20(%rbp),%rdx
lea    -0x21(%rbp),%rcx
call   1030 <get_byte_and_offset@plt>
cmpl   $0x0,-0x18(%rbp)
je     1165 <set_bit+0x55>
movzbl -0x21(%rbp),%ecx
mov    $0x1,%esi
shl    %cl,%esi
mov    -0x8(%rbp),%rax
mov    -0x20(%rbp),%rcx
movzbl (%rax,%rcx,1),%edx
or     %esi,%edx
mov    %dl,(%rax,%rcx,1)
jmp    1184 <set_bit+0x74>
movzbl -0x21(%rbp),%ecx
mov    $0x1,%esi
shl    %cl,%esi
xor    $0xffffffff,%esi
mov    -0x8(%rbp),%rax
mov    -0x20(%rbp),%rcx
movzbl (%rax,%rcx,1),%edx
and    %esi,%edx
mov    %dl,(%rax,%rcx,1)
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<set_bit>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %ecx,%r14d
mov    %rdi,%rbp
lea    0x8(%rsp),%rax
lea    0x7(%rsp),%rcx
mov    %rsi,%rdi
mov    %edx,%esi
mov    %rax,%rdx
call   1030 <get_byte_and_offset@plt>
mov    0x7(%rsp),%cl
mov    $0x1,%eax
shl    %cl,%eax
mov    0x8(%rsp),%rcx
mov    0x0(%rbp,%rcx,1),%dl
mov    %eax,%ebx
not    %bl
and    %dl,%bl
or     %al,%dl
test   %r14d,%r14d
movzbl %dl,%eax
movzbl %bl,%edx
cmovne %eax,%edx
mov    %dl,0x0(%rbp,%rcx,1)
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
define void @set_bit(i8* %0, i64 %1, i32 %2, i32 %3) {
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i32
%8 = alloca i32
%9 = alloca i64
%10 = alloca i8
store i8* %0, i8** %5
store i64 %1, i64* %6
store i32 %2, i32* %7
store i32 %3, i32* %8
%11 = load i64, i64* %6
%12 = load i32, i32* %7
%13 = call i32 @get_byte_and_offset(i64 %11, i32 %12, i64* %9, i8* %10)
%14 = load i32, i32* %8
%15 = icmp ne i32 %14, 0
br i1 %15, label %16, label %27
16: ; preds = %4
%17 = load i8, i8* %10
%18 = zext i8 %17 to i32
%19 = shl i32 1, %18
%20 = load i8*, i8** %5
%21 = load i64, i64* %9
%22 = getelementptr inbounds i8, i8* %20, i64 %21
%23 = load i8, i8* %22
%24 = zext i8 %23 to i32
%25 = or i32 %24, %19
%26 = trunc i32 %25 to i8
store i8 %26, i8* %22
br label %39
27: ; preds = %4
%28 = load i8, i8* %10
%29 = zext i8 %28 to i32
%30 = shl i32 1, %29
%31 = xor i32 %30, -1
%32 = load i8*, i8** %5
%33 = load i64, i64* %9
%34 = getelementptr inbounds i8, i8* %32, i64 %33
%35 = load i8, i8* %34
%36 = zext i8 %35 to i32
%37 = and i32 %36, %31
%38 = trunc i32 %37 to i8
store i8 %38, i8* %34
br label %39
39: ; preds = %27, %16
ret void
}

LLVM IR (-O2):
define void @set_bit(i8* nocapture %0, i64 %1, i32 %2, i32 %3) {
%5 = alloca i64
%6 = alloca i8
%7 = bitcast i64* %5 to i8*
%8 = call i32 @get_byte_and_offset(i64 %1, i32 %2, i64* %5, i8* %6)
%9 = icmp eq i32 %3, 0
%10 = load i8, i8* %6
%11 = zext i8 %10 to i32
%12 = shl nuw i32 1, %11
%13 = load i64, i64* %5
%14 = getelementptr inbounds i8, i8* %0, i64 %13
%15 = load i8, i8* %14
%16 = trunc i32 %12 to i8
%17 = xor i8 %16, -1
%18 = and i8 %15, %17
%19 = or i8 %15, %16
%20 = select i1 %9, i8 %18, i8 %19
store i8 %20, i8* %14
ret void
}