void init_mask(char *basemask, const unsigned int bitmap, const int bits) {
  int i;
  for (i = 0; i < bits; i++) {
    basemask[i] = (char)!!(bitmap & (1 << i));
  }
}


x86 assembly (clang, -O0):
<init_mask>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x10(%rbp),%eax
jge    115c <init_mask+0x5c>
mov    -0xc(%rbp),%eax
mov    -0x14(%rbp),%ecx
mov    $0x1,%edx
shl    %cl,%edx
mov    %edx,%ecx
and    %ecx,%eax
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
xor    $0xff,%al
and    $0x1,%al
movzbl %al,%eax
mov    %al,%dl
mov    -0x8(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1115 <init_mask+0x15>
pop    %rbp
ret

x86 assembly (clang, -O2):
<init_mask>:
test   %edx,%edx
jle    1520 <init_mask+0x420>
mov    %edx,%r9d
cmp    $0x10,%edx
jae    1117 <init_mask+0x17>
xor    %ecx,%ecx
jmp    1510 <init_mask+0x410>
mov    %r9d,%ecx
and    $0xfffffff0,%ecx
movd   %esi,%xmm0
pshufd $0x0,%xmm0,%xmm14
lea    -0x10(%rcx),%rax
mov    %rax,%r8
shr    $0x4,%r8
add    $0x1,%r8
test   %rax,%rax
je     1521 <init_mask+0x421>
mov    %r8,%rax
and    $0xfffffffffffffffe,%rax
movdqa 0xeb1(%rip),%xmm15
movdqa 0xeb8(%rip),%xmm10
movdqa 0xebf(%rip),%xmm11
movdqa 0xec7(%rip),%xmm4
xor    %edx,%edx
movdqa 0xecc(%rip),%xmm8
movdqa 0xed4(%rip),%xmm6
pxor   %xmm12,%xmm12
pcmpeqd %xmm13,%xmm13
cs nopw 0x0(%rax,%rax,1)
movdqa %xmm4,%xmm0
pslld  $0x17,%xmm0
paddd  %xmm8,%xmm0
cvttps2dq %xmm0,%xmm0
pshufd $0xf5,%xmm0,%xmm1
pmuludq %xmm6,%xmm0
pshufd $0xe8,%xmm0,%xmm7
pmuludq %xmm6,%xmm1
pshufd $0xe8,%xmm1,%xmm0
punpckldq %xmm0,%xmm7
movdqa %xmm11,%xmm0
pslld  $0x17,%xmm0
paddd  %xmm8,%xmm0
cvttps2dq %xmm0,%xmm0
pshufd $0xf5,%xmm0,%xmm5
pmuludq %xmm6,%xmm0
pshufd $0xe8,%xmm0,%xmm1
pmuludq %xmm6,%xmm5
pshufd $0xe8,%xmm5,%xmm0
punpckldq %xmm0,%xmm1
movdqa %xmm10,%xmm0
pslld  $0x17,%xmm0
paddd  %xmm8,%xmm0
cvttps2dq %xmm0,%xmm0
pshufd $0xf5,%xmm0,%xmm5
pmuludq %xmm6,%xmm0
pshufd $0xe8,%xmm0,%xmm0
pmuludq %xmm6,%xmm5
pshufd $0xe8,%xmm5,%xmm5
punpckldq %xmm5,%xmm0
movdqa %xmm15,%xmm5
pslld  $0x17,%xmm5
paddd  %xmm8,%xmm5
cvttps2dq %xmm5,%xmm5
pshufd $0xf5,%xmm5,%xmm9
pmuludq %xmm6,%xmm5
pshufd $0xe8,%xmm5,%xmm5
pmuludq %xmm6,%xmm9
pshufd $0xe8,%xmm9,%xmm2
punpckldq %xmm2,%xmm5
pand   %xmm14,%xmm5
pand   %xmm14,%xmm0
pand   %xmm14,%xmm1
pand   %xmm14,%xmm7
pcmpeqd %xmm12,%xmm7
pxor   %xmm13,%xmm7
pcmpeqd %xmm12,%xmm1
pxor   %xmm13,%xmm1
packssdw %xmm1,%xmm7
pcmpeqd %xmm12,%xmm0
pxor   %xmm13,%xmm0
pcmpeqd %xmm12,%xmm5
pxor   %xmm13,%xmm5
packssdw %xmm5,%xmm0
packsswb %xmm0,%xmm7
movdqa 0xdc5(%rip),%xmm9
pand   %xmm9,%xmm7
movdqu %xmm7,(%rdi,%rdx,1)
movdqa %xmm15,%xmm1
movdqa 0xdbe(%rip),%xmm3
paddd  %xmm3,%xmm1
movdqa %xmm10,%xmm5
paddd  %xmm3,%xmm5
movdqa %xmm11,%xmm0
paddd  %xmm3,%xmm0
movdqa %xmm4,%xmm2
paddd  %xmm3,%xmm2
pslld  $0x17,%xmm2
paddd  %xmm8,%xmm2
cvttps2dq %xmm2,%xmm2
pshufd $0xf5,%xmm2,%xmm3
pmuludq %xmm6,%xmm2
pshufd $0xe8,%xmm2,%xmm7
pmuludq %xmm6,%xmm3
pshufd $0xe8,%xmm3,%xmm2
punpckldq %xmm2,%xmm7
pslld  $0x17,%xmm0
paddd  %xmm8,%xmm0
cvttps2dq %xmm0,%xmm0
pshufd $0xf5,%xmm0,%xmm2
pmuludq %xmm6,%xmm0
pshufd $0xe8,%xmm0,%xmm0
pmuludq %xmm6,%xmm2
pshufd $0xe8,%xmm2,%xmm2
punpckldq %xmm2,%xmm0
pslld  $0x17,%xmm5
paddd  %xmm8,%xmm5
cvttps2dq %xmm5,%xmm2
pshufd $0xf5,%xmm2,%xmm3
pmuludq %xmm6,%xmm2
pshufd $0xe8,%xmm2,%xmm5
pmuludq %xmm6,%xmm3
pshufd $0xe8,%xmm3,%xmm2
punpckldq %xmm2,%xmm5
pslld  $0x17,%xmm1
paddd  %xmm8,%xmm1
cvttps2dq %xmm1,%xmm1
pshufd $0xf5,%xmm1,%xmm2
pmuludq %xmm6,%xmm1
pshufd $0xe8,%xmm1,%xmm1
pmuludq %xmm6,%xmm2
pshufd $0xe8,%xmm2,%xmm2
punpckldq %xmm2,%xmm1
pand   %xmm14,%xmm0
pand   %xmm14,%xmm7
pcmpeqd %xmm12,%xmm7
pxor   %xmm13,%xmm7
pcmpeqd %xmm12,%xmm0
pxor   %xmm13,%xmm0
packssdw %xmm0,%xmm7
pand   %xmm14,%xmm1
pand   %xmm14,%xmm5
pcmpeqd %xmm12,%xmm5
pxor   %xmm13,%xmm5
pcmpeqd %xmm12,%xmm1
pxor   %xmm13,%xmm1
packssdw %xmm1,%xmm5
packsswb %xmm5,%xmm7
pand   %xmm9,%xmm7
movdqu %xmm7,0x10(%rdi,%rdx,1)
add    $0x20,%rdx
movdqa 0xcad(%rip),%xmm0
paddd  %xmm0,%xmm4
paddd  %xmm0,%xmm11
paddd  %xmm0,%xmm10
paddd  %xmm0,%xmm15
add    $0xfffffffffffffffe,%rax
jne    1190 <init_mask+0x90>
test   $0x1,%r8b
je     1508 <init_mask+0x408>
pslld  $0x17,%xmm4
movdqa 0xc39(%rip),%xmm6
paddd  %xmm6,%xmm4
cvttps2dq %xmm4,%xmm0
movdqa 0xc39(%rip),%xmm5
pshufd $0xf5,%xmm0,%xmm1
pmuludq %xmm5,%xmm0
pshufd $0xe8,%xmm0,%xmm4
pmuludq %xmm5,%xmm1
pshufd $0xe8,%xmm1,%xmm0
punpckldq %xmm0,%xmm4
pslld  $0x17,%xmm11
paddd  %xmm6,%xmm11
cvttps2dq %xmm11,%xmm0
pshufd $0xf5,%xmm0,%xmm2
pmuludq %xmm5,%xmm0
pshufd $0xe8,%xmm0,%xmm1
pmuludq %xmm5,%xmm2
pshufd $0xe8,%xmm2,%xmm0
punpckldq %xmm0,%xmm1
pslld  $0x17,%xmm10
paddd  %xmm6,%xmm10
cvttps2dq %xmm10,%xmm0
pshufd $0xf5,%xmm0,%xmm2
pmuludq %xmm5,%xmm0
pshufd $0xe8,%xmm0,%xmm0
pmuludq %xmm5,%xmm2
pshufd $0xe8,%xmm2,%xmm2
punpckldq %xmm2,%xmm0
pslld  $0x17,%xmm15
paddd  %xmm6,%xmm15
cvttps2dq %xmm15,%xmm2
pshufd $0xf5,%xmm2,%xmm3
pmuludq %xmm5,%xmm2
pshufd $0xe8,%xmm2,%xmm2
pmuludq %xmm5,%xmm3
pshufd $0xe8,%xmm3,%xmm3
punpckldq %xmm3,%xmm2
pand   %xmm14,%xmm2
pand   %xmm14,%xmm0
pand   %xmm14,%xmm1
pand   %xmm14,%xmm4
pxor   %xmm3,%xmm3
pcmpeqd %xmm3,%xmm4
pcmpeqd %xmm5,%xmm5
pxor   %xmm5,%xmm4
pcmpeqd %xmm3,%xmm1
pxor   %xmm5,%xmm1
packssdw %xmm1,%xmm4
pcmpeqd %xmm3,%xmm0
pxor   %xmm5,%xmm0
pcmpeqd %xmm3,%xmm2
pxor   %xmm5,%xmm2
packssdw %xmm2,%xmm0
packsswb %xmm0,%xmm4
pand   0xb5d(%rip),%xmm4
movdqu %xmm4,(%rdi,%rdx,1)
cmp    %r9,%rcx
je     1520 <init_mask+0x420>
nopl   (%rax)
bt     %ecx,%esi
setb   (%rdi,%rcx,1)
add    $0x1,%rcx
cmp    %rcx,%r9
jne    1510 <init_mask+0x410>
ret
movdqa 0xad6(%rip),%xmm15
movdqa 0xadd(%rip),%xmm10
movdqa 0xae4(%rip),%xmm11
movdqa 0xaec(%rip),%xmm4
xor    %edx,%edx
test   $0x1,%r8b
jne    13fa <init_mask+0x2fa>
jmp    1508 <init_mask+0x408>

LLVM IR (-O0):
define void @init_mask(i8* %0, i32 %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
store i32 %2, i32* %6
store i32 0, i32* %7
br label %8
8: ; preds = %26, %3
%9 = load i32, i32* %7
%10 = load i32, i32* %6
%11 = icmp slt i32 %9, %10
br i1 %11, label %12, label %29
12: ; preds = %8
%13 = load i32, i32* %5
%14 = load i32, i32* %7
%15 = shl i32 1, %14
%16 = and i32 %13, %15
%17 = icmp ne i32 %16, 0
%18 = xor i1 %17, true
%19 = xor i1 %18, true
%20 = zext i1 %19 to i32
%21 = trunc i32 %20 to i8
%22 = load i8*, i8** %4
%23 = load i32, i32* %7
%24 = sext i32 %23 to i64
%25 = getelementptr inbounds i8, i8* %22, i64 %24
store i8 %21, i8* %25
br label %26
26: ; preds = %12
%27 = load i32, i32* %7
%28 = add nsw i32 %27, 1
store i32 %28, i32* %7
br label %8
29: ; preds = %8
ret void
}

LLVM IR (-O2):
define void @init_mask(i8* nocapture writeonly %0, i32 %1, i32 %2) {
%4 = icmp sgt i32 %2, 0
br i1 %4, label %5, label %66
5: ; preds = %3
%6 = zext i32 %2 to i64
%7 = icmp ult i32 %2, 16
br i1 %7, label %54, label %8
8: ; preds = %5
%9 = and i64 %6, 4294967280
%10 = insertelement <16 x i32> poison, i32 %1, i64 0
%11 = shufflevector <16 x i32> %10, <16 x i32> poison, <16 x i32> zeroinitializer
%12 = add nsw i64 %9, -16
%13 = lshr exact i64 %12, 4
%14 = add nuw nsw i64 %13, 1
%15 = and i64 %14, 1
%16 = icmp eq i64 %12, 0
br i1 %16, label %41, label %17
17: ; preds = %8
%18 = and i64 %14, 2305843009213693950
br label %19
19: ; preds = %19, %17
%20 = phi i64 [ 0, %17 ], [ %37, %19 ]
%21 = phi <16 x i32> [ <i32 0, i32 1, i32 2, i32 3, i32 4, i32 5, i32 6, i32 7, i32 8, i32 9, i32 10, i32 11, i32 12, i32 13, i32 14, i32 15>, %17 ], [ %38, %19 ]
%22 = phi i64 [ 0, %17 ], [ %39, %19 ]
%23 = shl nuw <16 x i32> <i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1>, %21
%24 = and <16 x i32> %23, %11
%25 = icmp ne <16 x i32> %24, zeroinitializer
%26 = zext <16 x i1> %25 to <16 x i8>
%27 = getelementptr inbounds i8, i8* %0, i64 %20
%28 = bitcast i8* %27 to <16 x i8>*
store <16 x i8> %26, <16 x i8>* %28
%29 = or i64 %20, 16
%30 = add <16 x i32> %21, <i32 16, i32 16, i32 16, i32 16, i32 16, i32 16, i32 16, i32 16, i32 16, i32 16, i32 16, i32 16, i32 16, i32 16, i32 16, i32 16>
%31 = shl nuw <16 x i32> <i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1>, %30
%32 = and <16 x i32> %31, %11
%33 = icmp ne <16 x i32> %32, zeroinitializer
%34 = zext <16 x i1> %33 to <16 x i8>
%35 = getelementptr inbounds i8, i8* %0, i64 %29
%36 = bitcast i8* %35 to <16 x i8>*
store <16 x i8> %34, <16 x i8>* %36
%37 = add nuw i64 %20, 32
%38 = add <16 x i32> %21, <i32 32, i32 32, i32 32, i32 32, i32 32, i32 32, i32 32, i32 32, i32 32, i32 32, i32 32, i32 32, i32 32, i32 32, i32 32, i32 32>
%39 = add i64 %22, 2
%40 = icmp eq i64 %39, %18
br i1 %40, label %41, label %19
41: ; preds = %19, %8
%42 = phi i64 [ 0, %8 ], [ %37, %19 ]
%43 = phi <16 x i32> [ <i32 0, i32 1, i32 2, i32 3, i32 4, i32 5, i32 6, i32 7, i32 8, i32 9, i32 10, i32 11, i32 12, i32 13, i32 14, i32 15>, %8 ], [ %38, %19 ]
%44 = icmp eq i64 %15, 0
br i1 %44, label %52, label %45
45: ; preds = %41
%46 = shl nuw <16 x i32> <i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1>, %43
%47 = and <16 x i32> %46, %11
%48 = icmp ne <16 x i32> %47, zeroinitializer
%49 = zext <16 x i1> %48 to <16 x i8>
%50 = getelementptr inbounds i8, i8* %0, i64 %42
%51 = bitcast i8* %50 to <16 x i8>*
store <16 x i8> %49, <16 x i8>* %51
br label %52
52: ; preds = %41, %45
%53 = icmp eq i64 %9, %6
br i1 %53, label %66, label %54
54: ; preds = %5, %52
%55 = phi i64 [ 0, %5 ], [ %9, %52 ]
br label %56
56: ; preds = %54, %56
%57 = phi i64 [ %64, %56 ], [ %55, %54 ]
%58 = trunc i64 %57 to i32
%59 = shl nuw i32 1, %58
%60 = and i32 %59, %1
%61 = icmp ne i32 %60, 0
%62 = zext i1 %61 to i8
%63 = getelementptr inbounds i8, i8* %0, i64 %57
store i8 %62, i8* %63
%64 = add nuw nsw i64 %57, 1
%65 = icmp eq i64 %64, %6
br i1 %65, label %66, label %56
66: ; preds = %56, %52, %3
ret void
}