#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;
struct in6_addr {
  int /*<<< orphan*/ *s6_addr32;
};

/* Variables and functions */
int /*<<< orphan*/ assert(int);
int /*<<< orphan*/ htonl(int);
void MaskIP6(struct in6_addr *addr, uint8_t bits) {
  register uint8_t i;
  assert(addr != NULL);
  assert(bits <= 128);
  i = bits / 32;
  if (bits % 32)
    addr->s6_addr32[i++] &= htonl(0xffffffff << (32 - (bits % 32)));
  for (; i < (sizeof addr->s6_addr32) / 4; i++)
    addr->s6_addr32[i] = 0;
}


x86 assembly (clang, -O0):
<MaskIP6>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
cmpq   $0x0,-0x8(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
cmpl   $0x80,-0xc(%rbp)
setle  %al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
mov    -0xc(%rbp),%eax
mov    $0x20,%ecx
cltd
idiv   %ecx
mov    %eax,-0x10(%rbp)
mov    -0xc(%rbp),%eax
mov    $0x20,%ecx
cltd
idiv   %ecx
cmp    $0x0,%edx
je     11b2 <MaskIP6+0x92>
mov    -0xc(%rbp),%eax
mov    $0x20,%ecx
cltd
idiv   %ecx
mov    $0x20,%ecx
sub    %edx,%ecx
mov    $0xffffffff,%edi
shl    %cl,%edi
call   1030 <htonl@plt>
mov    %eax,%edx
mov    -0x8(%rbp),%rax
mov    (%rax),%rax
mov    -0x10(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0x10(%rbp)
movslq %ecx,%rcx
and    (%rax,%rcx,4),%edx
mov    %edx,(%rax,%rcx,4)
jmp    11b7 <MaskIP6+0x97>
movslq -0x10(%rbp),%rax
cmp    $0x2,%rax
jae    11e5 <MaskIP6+0xc5>
mov    -0x8(%rbp),%rax
mov    (%rax),%rax
movslq -0x10(%rbp),%rcx
movl   $0x0,(%rax,%rcx,4)
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    11b7 <MaskIP6+0x97>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<MaskIP6>:
push   %rbp
push   %r14
push   %rbx
mov    %esi,%ebx
mov    %rdi,%r14
xor    %edi,%edi
test   %r14,%r14
setne  %dil
call   1040 <assert@plt>
xor    %edi,%edi
cmp    $0x81,%ebx
setl   %dil
call   1040 <assert@plt>
lea    0x1f(%rbx),%eax
test   %ebx,%ebx
cmovns %ebx,%eax
mov    %eax,%ebp
sar    $0x5,%ebp
and    $0xffffffe0,%eax
sub    %eax,%ebx
je     1178 <MaskIP6+0x58>
neg    %bl
mov    $0xffffffff,%edi
mov    %ebx,%ecx
shl    %cl,%edi
call   1030 <htonl@plt>
mov    (%r14),%rcx
movslq %ebp,%rdx
add    $0x1,%ebp
and    %eax,(%rcx,%rdx,4)
cmp    $0x1,%ebp
ja     1194 <MaskIP6+0x74>
mov    (%r14),%rax
mov    %ebp,%ecx
movl   $0x0,(%rax,%rcx,4)
test   %ebp,%ebp
jne    1194 <MaskIP6+0x74>
movl   $0x0,0x4(%rax)
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.in6_addr = type { i32* }
define void @MaskIP6(%struct.in6_addr* %0, i32 %1) {
%3 = alloca %struct.in6_addr*
%4 = alloca i32
%5 = alloca i32
store %struct.in6_addr* %0, %struct.in6_addr** %3
store i32 %1, i32* %4
%6 = load %struct.in6_addr*, %struct.in6_addr** %3
%7 = icmp ne %struct.in6_addr* %6, null
%8 = zext i1 %7 to i32
%9 = call i32 @assert(i32 %8)
%10 = load i32, i32* %4
%11 = icmp sle i32 %10, 128
%12 = zext i1 %11 to i32
%13 = call i32 @assert(i32 %12)
%14 = load i32, i32* %4
%15 = sdiv i32 %14, 32
store i32 %15, i32* %5
%16 = load i32, i32* %4
%17 = srem i32 %16, 32
%18 = icmp ne i32 %17, 0
br i1 %18, label %19, label %34
19: ; preds = %2
%20 = load i32, i32* %4
%21 = srem i32 %20, 32
%22 = sub nsw i32 32, %21
%23 = shl i32 -1, %22
%24 = call i32 @htonl(i32 %23)
%25 = load %struct.in6_addr*, %struct.in6_addr** %3
%26 = getelementptr inbounds %struct.in6_addr, %struct.in6_addr* %25, i32 0, i32 0
%27 = load i32*, i32** %26
%28 = load i32, i32* %5
%29 = add nsw i32 %28, 1
store i32 %29, i32* %5
%30 = sext i32 %28 to i64
%31 = getelementptr inbounds i32, i32* %27, i64 %30
%32 = load i32, i32* %31
%33 = and i32 %32, %24
store i32 %33, i32* %31
br label %34
34: ; preds = %19, %2
br label %35
35: ; preds = %46, %34
%36 = load i32, i32* %5
%37 = sext i32 %36 to i64
%38 = icmp ult i64 %37, 2
br i1 %38, label %39, label %49
39: ; preds = %35
%40 = load %struct.in6_addr*, %struct.in6_addr** %3
%41 = getelementptr inbounds %struct.in6_addr, %struct.in6_addr* %40, i32 0, i32 0
%42 = load i32*, i32** %41
%43 = load i32, i32* %5
%44 = sext i32 %43 to i64
%45 = getelementptr inbounds i32, i32* %42, i64 %44
store i32 0, i32* %45
br label %46
46: ; preds = %39
%47 = load i32, i32* %5
%48 = add nsw i32 %47, 1
store i32 %48, i32* %5
br label %35
49: ; preds = %35
ret void
}

LLVM IR (-O2):
%struct.in6_addr = type { i32* }
define void @MaskIP6(%struct.in6_addr* %0, i32 %1) {
%3 = icmp ne %struct.in6_addr* %0, null
%4 = zext i1 %3 to i32
%5 = tail call i32 @assert(i32 %4)
%6 = icmp slt i32 %1, 129
%7 = zext i1 %6 to i32
%8 = tail call i32 @assert(i32 %7)
%9 = sdiv i32 %1, 32
%10 = srem i32 %1, 32
%11 = icmp eq i32 %10, 0
br i1 %11, label %23, label %12
12: ; preds = %2
%13 = sub nsw i32 32, %10
%14 = shl nsw i32 -1, %13
%15 = tail call i32 @htonl(i32 %14)
%16 = getelementptr inbounds %struct.in6_addr, %struct.in6_addr* %0, i64 0, i32 0
%17 = load i32*, i32** %16
%18 = add nsw i32 %9, 1
%19 = sext i32 %9 to i64
%20 = getelementptr inbounds i32, i32* %17, i64 %19
%21 = load i32, i32* %20
%22 = and i32 %21, %15
store i32 %22, i32* %20
br label %23
23: ; preds = %12, %2
%24 = phi i32 [ %18, %12 ], [ %9, %2 ]
%25 = icmp ult i32 %24, 2
br i1 %25, label %26, label %34
26: ; preds = %23
%27 = getelementptr inbounds %struct.in6_addr, %struct.in6_addr* %0, i64 0, i32 0
%28 = load i32*, i32** %27
%29 = zext i32 %24 to i64
%30 = getelementptr inbounds i32, i32* %28, i64 %29
store i32 0, i32* %30
%31 = icmp eq i32 %24, 0
br i1 %31, label %32, label %34
32: ; preds = %26
%33 = getelementptr inbounds i32, i32* %28, i64 1
store i32 0, i32* %33
br label %34
34: ; preds = %26, %32, %23
ret void
}