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

/* Variables and functions */
int /*<<< orphan*/ memcpy(struct in6_addr *, struct in6_addr *, int);
int /*<<< orphan*/ memset(struct in6_addr *, int, int);
int ipv6_prefix_to_mask(unsigned prefix, struct in6_addr *mask) {
  struct in6_addr in6;
  int i, j;
  if (prefix > 128)
    return -1;
  memset(&in6, 0x0, sizeof(in6));
  for (i = prefix, j = 0; i > 0; i -= 8, j++) {
    if (i >= 8) {
      in6.s6_addr[j] = 0xff;
    } else {
      in6.s6_addr[j] = (unsigned long)(0xffU << (8 - i));
    }
  }
  memcpy(mask, &in6, sizeof(*mask));
  return 0;
}


x86 assembly (clang, -O0):
<ipv6_prefix_to_mask>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x80,-0x8(%rbp)
jbe    1148 <ipv6_prefix_to_mask+0x28>
movl   $0xffffffff,-0x4(%rbp)
jmp    11e0 <ipv6_prefix_to_mask+0xc0>
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1030 <memset@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jle    11c7 <ipv6_prefix_to_mask+0xa7>
cmpl   $0x8,-0x1c(%rbp)
jl     118d <ipv6_prefix_to_mask+0x6d>
mov    -0x18(%rbp),%rax
movslq -0x20(%rbp),%rcx
movl   $0xff,(%rax,%rcx,4)
jmp    11ab <ipv6_prefix_to_mask+0x8b>
mov    $0x8,%ecx
sub    -0x1c(%rbp),%ecx
mov    $0xff,%eax
shl    %cl,%eax
mov    %eax,%eax
mov    %eax,%edx
mov    -0x18(%rbp),%rax
movslq -0x20(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
jmp    11b0 <ipv6_prefix_to_mask+0x90>
mov    -0x1c(%rbp),%eax
sub    $0x8,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    1165 <ipv6_prefix_to_mask+0x45>
mov    -0x10(%rbp),%rdi
lea    -0x18(%rbp),%rsi
mov    $0x8,%edx
call   1040 <memcpy@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ipv6_prefix_to_mask>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    $0xffffffff,%ebx
cmp    $0x80,%edi
ja     1196 <ipv6_prefix_to_mask+0x76>
mov    %rsi,%r14
mov    %edi,%ebp
lea    0x8(%rsp),%rdi
xor    %ebx,%ebx
xor    %esi,%esi
mov    $0x8,%edx
call   1030 <memset@plt>
test   %ebp,%ebp
je     1184 <ipv6_prefix_to_mask+0x64>
mov    0x8(%rsp),%rax
cs nopw 0x0(%rax,%rax,1)
cmp    $0x7,%ebp
jbe    1176 <ipv6_prefix_to_mask+0x56>
movl   $0xff,(%rax)
add    $0x4,%rax
add    $0xfffffff8,%ebp
jne    1160 <ipv6_prefix_to_mask+0x40>
jmp    1184 <ipv6_prefix_to_mask+0x64>
mov    $0x8,%cl
sub    %bpl,%cl
mov    $0xff,%edx
shl    %cl,%edx
mov    %edx,(%rax)
lea    0x8(%rsp),%rsi
mov    %r14,%rdi
mov    $0x8,%edx
call   1040 <memcpy@plt>
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.in6_addr = type { i32* }
define i32 @ipv6_prefix_to_mask(i32 %0, %struct.in6_addr* %1) {
%3 = alloca %struct.in6_addr
%4 = bitcast %struct.in6_addr* %3 to i8*
%5 = icmp ugt i32 %0, 128
br i1 %5, label %27, label %6
6: ; preds = %2
%7 = call i32 @memset(%struct.in6_addr* %3, i32 0, i32 8)
%8 = icmp eq i32 %0, 0
br i1 %8, label %25, label %9
9: ; preds = %6
%10 = getelementptr inbounds %struct.in6_addr, %struct.in6_addr* %3, i64 0, i32 0
%11 = load i32*, i32** %10
br label %12
12: ; preds = %9, %20
%13 = phi i64 [ 0, %9 ], [ %23, %20 ]
%14 = phi i32 [ %0, %9 ], [ %22, %20 ]
%15 = icmp ugt i32 %14, 7
br i1 %15, label %20, label %16
16: ; preds = %12
%17 = sub nuw nsw i32 8, %14
%18 = shl nuw nsw i32 255, %17
%19 = getelementptr inbounds i32, i32* %11, i64 %13
store i32 %18, i32* %19
br label %25
20: ; preds = %12
%21 = getelementptr inbounds i32, i32* %11, i64 %13
store i32 255, i32* %21
%22 = add nsw i32 %14, -8
%23 = add nuw nsw i64 %13, 1
%24 = icmp eq i32 %14, 8
br i1 %24, label %25, label %12
25: ; preds = %20, %16, %6
%26 = call i32 @memcpy(%struct.in6_addr* %1, %struct.in6_addr* %3, i32 8)
br label %27
27: ; preds = %2, %25
%28 = phi i32 [ 0, %25 ], [ -1, %2 ]
ret i32 %28
}