#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 */
void set_bits_from_bytes(uint8_t *dest, int address, int nb_bits,
                         const uint8_t tab_byte[]) {
  int i;
  int shift = 0;
  for (i = address; i < address + nb_bits; i++) {
    dest[i] = tab_byte[(i - address) / 8] & (1 << shift) ? 1 : 0;
    shift++;
    shift %= 8;
  }
}


x86 assembly (clang, -O0):
<set_bits_from_bytes>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    %rcx,-0x18(%rbp)
movl   $0x0,-0x20(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    -0xc(%rbp),%ecx
add    -0x10(%rbp),%ecx
cmp    %ecx,%eax
jge    11a1 <set_bits_from_bytes+0xa1>
mov    -0x18(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x1c(%rbp),%eax
sub    -0xc(%rbp),%eax
mov    $0x8,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
mov    -0x30(%rbp),%rax
movslq %ecx,%rcx
mov    (%rax,%rcx,4),%eax
mov    %eax,-0x24(%rbp)
mov    -0x20(%rbp),%ecx
mov    $0x1,%eax
shl    %cl,%eax
mov    -0x24(%rbp),%ecx
and    %eax,%ecx
xor    %edx,%edx
mov    $0x1,%eax
cmp    $0x0,%ecx
cmovne %eax,%edx
mov    -0x8(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%eax
mov    $0x8,%ecx
cltd
idiv   %ecx
mov    %edx,-0x20(%rbp)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    111f <set_bits_from_bytes+0x1f>
pop    %rbp
ret

x86 assembly (clang, -O2):
<set_bits_from_bytes>:
test   %edx,%edx
jle    1161 <set_bits_from_bytes+0x61>
mov    %rcx,%r8
add    %esi,%edx
movslq %esi,%rsi
movslq %edx,%r9
lea    (%rdi,%rsi,4),%r10
xor    %eax,%eax
xor    %ecx,%ecx
nopw   0x0(%rax,%rax,1)
lea    0x7(%rax),%edx
test   %eax,%eax
cmovns %eax,%edx
sar    $0x3,%edx
movslq %edx,%rdx
mov    (%r8,%rdx,4),%edx
shr    %cl,%edx
and    $0x1,%edx
mov    %edx,(%r10,%rax,4)
lea    0x1(%rcx),%edx
lea    0x8(%rcx),%edi
test   %edx,%edx
cmovns %edx,%edi
and    $0xfffffff8,%edi
neg    %edi
add    %edi,%ecx
add    $0x1,%ecx
lea    (%rsi,%rax,1),%rdx
add    $0x1,%rdx
add    $0x1,%rax
cmp    %r9,%rdx
jl     1120 <set_bits_from_bytes+0x20>
ret

LLVM IR (-O0):
define void @set_bits_from_bytes(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
store i32* %0, i32** %5
store i32 %1, i32* %6
store i32 %2, i32* %7
store i32* %3, i32** %8
store i32 0, i32* %10
%11 = load i32, i32* %6
store i32 %11, i32* %9
br label %12
12: ; preds = %41, %4
%13 = load i32, i32* %9
%14 = load i32, i32* %6
%15 = load i32, i32* %7
%16 = add nsw i32 %14, %15
%17 = icmp slt i32 %13, %16
br i1 %17, label %18, label %44
18: ; preds = %12
%19 = load i32*, i32** %8
%20 = load i32, i32* %9
%21 = load i32, i32* %6
%22 = sub nsw i32 %20, %21
%23 = sdiv i32 %22, 8
%24 = sext i32 %23 to i64
%25 = getelementptr inbounds i32, i32* %19, i64 %24
%26 = load i32, i32* %25
%27 = load i32, i32* %10
%28 = shl i32 1, %27
%29 = and i32 %26, %28
%30 = icmp ne i32 %29, 0
%31 = zext i1 %30 to i64
%32 = select i1 %30, i32 1, i32 0
%33 = load i32*, i32** %5
%34 = load i32, i32* %9
%35 = sext i32 %34 to i64
%36 = getelementptr inbounds i32, i32* %33, i64 %35
store i32 %32, i32* %36
%37 = load i32, i32* %10
%38 = add nsw i32 %37, 1
store i32 %38, i32* %10
%39 = load i32, i32* %10
%40 = srem i32 %39, 8
store i32 %40, i32* %10
br label %41
41: ; preds = %18
%42 = load i32, i32* %9
%43 = add nsw i32 %42, 1
store i32 %43, i32* %9
br label %12
44: ; preds = %12
ret void
}

LLVM IR (-O2):
define void @set_bits_from_bytes(i32* nocapture writeonly %0, i32 %1, i32 %2, i32* nocapture %3) {
%5 = icmp sgt i32 %2, 0
br i1 %5, label %6, label %26
6: ; preds = %4
%7 = add nsw i32 %2, %1
%8 = sext i32 %1 to i64
%9 = sext i32 %7 to i64
br label %10
10: ; preds = %6, %10
%11 = phi i64 [ %8, %6 ], [ %24, %10 ]
%12 = phi i32 [ 0, %6 ], [ %23, %10 ]
%13 = trunc i64 %11 to i32
%14 = sub i32 %13, %1
%15 = sdiv i32 %14, 8
%16 = sext i32 %15 to i64
%17 = getelementptr inbounds i32, i32* %3, i64 %16
%18 = load i32, i32* %17
%19 = lshr i32 %18, %12
%20 = and i32 %19, 1
%21 = getelementptr inbounds i32, i32* %0, i64 %11
store i32 %20, i32* %21
%22 = add nsw i32 %12, 1
%23 = srem i32 %22, 8
%24 = add nsw i64 %11, 1
%25 = icmp slt i64 %24, %9
br i1 %25, label %10, label %26
26: ; preds = %10, %4
ret void
}