#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 */
typedef struct TYPE_2__ TYPE_1__;

/* Type definitions */
struct TYPE_2__ {
  unsigned char *s6_addr;
};
struct sockaddr_in6 {
  TYPE_1__ sin6_addr;
};

/* Variables and functions */
int /*<<< orphan*/ memset(unsigned char *, int, unsigned int);
void ipv6netmask(unsigned prefix_length, struct sockaddr_in6 *sa) {
  unsigned char *hb = sa->sin6_addr.s6_addr;
  unsigned onebytes = prefix_length / 8;
  unsigned bits = prefix_length % 8;
  unsigned nullbytes = 16 - onebytes;
  memset(hb, -1, onebytes);
  memset(hb + onebytes, 0, nullbytes);
  if (bits) {
    unsigned char x = -1;
    x <<= 8 - bits;
    hb[onebytes] = x;
  }
}


x86 assembly (clang, -O0):
<ipv6netmask>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x18(%rbp)
mov    -0x4(%rbp),%eax
shr    $0x3,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x4(%rbp),%eax
and    $0x7,%eax
mov    %eax,-0x20(%rbp)
mov    $0x10,%eax
sub    -0x1c(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x1c(%rbp),%edx
mov    $0xffffffff,%esi
call   1030 <memset@plt>
mov    -0x18(%rbp),%rdi
mov    -0x1c(%rbp),%eax
add    %rax,%rdi
mov    -0x24(%rbp),%edx
xor    %esi,%esi
call   1030 <memset@plt>
cmpl   $0x0,-0x20(%rbp)
je     1198 <ipv6netmask+0x88>
movb   $0xff,-0x25(%rbp)
mov    $0x8,%ecx
sub    -0x20(%rbp),%ecx
movzbl -0x25(%rbp),%eax
shl    %cl,%eax
mov    %al,-0x25(%rbp)
mov    -0x25(%rbp),%dl
mov    -0x18(%rbp),%rax
mov    -0x1c(%rbp),%ecx
mov    %dl,(%rax,%rcx,1)
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ipv6netmask>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edi,%r15d
mov    (%rsi),%r14
mov    %edi,%ebx
shr    $0x3,%ebx
mov    $0x10,%ebp
sub    %ebx,%ebp
mov    %r14,%rdi
mov    $0xffffffff,%esi
mov    %ebx,%edx
call   1030 <memset@plt>
add    %r14,%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    %ebp,%edx
call   1030 <memset@plt>
and    $0x7,%r15d
je     115b <ipv6netmask+0x4b>
mov    $0x8,%cl
sub    %r15b,%cl
mov    $0xff,%eax
shl    %cl,%eax
mov    %al,(%rbx)
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.sockaddr_in6 = type { %struct.TYPE_2__ }
%struct.TYPE_2__ = type { i8* }
define void @ipv6netmask(i32 %0, %struct.sockaddr_in6* nocapture %1) {
%3 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %1, i64 0, i32 0, i32 0
%4 = load i8*, i8** %3
%5 = lshr i32 %0, 3
%6 = and i32 %0, 7
%7 = sub nsw i32 16, %5
%8 = tail call i32 @memset(i8* %4, i32 -1, i32 %5)
%9 = zext i32 %5 to i64
%10 = getelementptr inbounds i8, i8* %4, i64 %9
%11 = tail call i32 @memset(i8* %10, i32 0, i32 %7)
%12 = icmp eq i32 %6, 0
br i1 %12, label %17, label %13
13: ; preds = %2
%14 = sub nuw nsw i32 8, %6
%15 = shl nuw nsw i32 255, %14
%16 = trunc i32 %15 to i8
store i8 %16, i8* %10
br label %17
17: ; preds = %13, %2
ret void
}