#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 uint32_t;
struct sockaddr_in6 {
  int /*<<< orphan*/ sin6_addr;
};
struct sockaddr_in {
  int /*<<< orphan*/ sin_addr;
};
struct sockaddr {
  int sa_family;
};
typedef int sa_family_t;

/* Variables and functions */
#define AF_INET 129
#define AF_INET6 128
int /*<<< orphan*/ memset(int /*<<< orphan*/ *, int /*<<< orphan*/, int);
struct sockaddr *ifa_make_sockaddr_mask(sa_family_t family, struct sockaddr *sa,
                                        uint32_t prefixlen) {
  int i;
  char *p = NULL, c;
  uint32_t max_prefixlen = 0;
  if (sa == NULL)
    return NULL;
  switch (family) {
  case AF_INET:
    memset(&((struct sockaddr_in *)sa)->sin_addr, 0,
           sizeof(((struct sockaddr_in *)sa)->sin_addr));
    p = (char *)&((struct sockaddr_in *)sa)->sin_addr;
    max_prefixlen = 32;
    break;
  case AF_INET6:
    memset(&((struct sockaddr_in6 *)sa)->sin6_addr, 0,
           sizeof(((struct sockaddr_in6 *)sa)->sin6_addr));
    p = (char *)&((struct sockaddr_in6 *)sa)->sin6_addr;
    max_prefixlen = 128;
    break;
  default:
    return NULL;
  }
  sa->sa_family = family;
  if (p) {
    if (prefixlen > max_prefixlen)
      prefixlen = max_prefixlen;
    for (i = 0; i < (prefixlen / 8); i++)
      *p++ = 0xff;
    c = 0xff;
    c <<= (8 - (prefixlen % 8));
    *p = c;
  }
  return sa;
}


x86 assembly (clang, -O0):
<ifa_make_sockaddr_mask>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0xc(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
movq   $0x0,-0x28(%rbp)
movl   $0x0,-0x30(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1149 <ifa_make_sockaddr_mask+0x39>
movq   $0x0,-0x8(%rbp)
jmp    1262 <ifa_make_sockaddr_mask+0x152>
mov    -0xc(%rbp),%eax
mov    %eax,-0x34(%rbp)
sub    $0x80,%eax
je     1196 <ifa_make_sockaddr_mask+0x86>
jmp    115f <ifa_make_sockaddr_mask+0x4f>
mov    -0x34(%rbp),%eax
sub    $0x81,%eax
jne    11ba <ifa_make_sockaddr_mask+0xaa>
jmp    1172 <ifa_make_sockaddr_mask+0x62>
mov    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1030 <memset@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
movl   $0x20,-0x30(%rbp)
jmp    11c7 <ifa_make_sockaddr_mask+0xb7>
mov    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1030 <memset@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
movl   $0x80,-0x30(%rbp)
jmp    11c7 <ifa_make_sockaddr_mask+0xb7>
movq   $0x0,-0x8(%rbp)
jmp    1262 <ifa_make_sockaddr_mask+0x152>
mov    -0xc(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
cmpq   $0x0,-0x28(%rbp)
je     125a <ifa_make_sockaddr_mask+0x14a>
mov    -0x1c(%rbp),%eax
cmp    -0x30(%rbp),%eax
jle    11ed <ifa_make_sockaddr_mask+0xdd>
mov    -0x30(%rbp),%eax
mov    %eax,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%eax
mov    %eax,-0x38(%rbp)
mov    -0x1c(%rbp),%eax
mov    $0x8,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
mov    -0x38(%rbp),%eax
cmp    %ecx,%eax
jge    1232 <ifa_make_sockaddr_mask+0x122>
mov    -0x28(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x28(%rbp)
movb   $0xff,(%rax)
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    11f4 <ifa_make_sockaddr_mask+0xe4>
movb   $0xff,-0x29(%rbp)
mov    -0x1c(%rbp),%eax
mov    $0x8,%ecx
cltd
idiv   %ecx
mov    $0x8,%ecx
sub    %edx,%ecx
movsbl -0x29(%rbp),%eax
shl    %cl,%eax
mov    %al,-0x29(%rbp)
mov    -0x29(%rbp),%cl
mov    -0x28(%rbp),%rax
mov    %cl,(%rax)
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ifa_make_sockaddr_mask>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
test   %rsi,%rsi
je     113b <ifa_make_sockaddr_mask+0x2b>
mov    %edx,%ebx
mov    %rsi,%r14
mov    %edi,%ebp
cmp    $0x81,%edi
je     1140 <ifa_make_sockaddr_mask+0x30>
cmp    $0x80,%ebp
jne    113b <ifa_make_sockaddr_mask+0x2b>
mov    $0x80,%r15d
jmp    1146 <ifa_make_sockaddr_mask+0x36>
xor    %r14d,%r14d
jmp    11a8 <ifa_make_sockaddr_mask+0x98>
mov    $0x20,%r15d
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1030 <memset@plt>
mov    %ebp,(%r14)
cmp    %ebx,%r15d
cmovl  %r15d,%ebx
mov    %r14,%rax
cmp    $0x8,%ebx
jl     118b <ifa_make_sockaddr_mask+0x7b>
mov    %ebx,%ebp
shr    $0x3,%ebp
and    $0x1f,%ebp
add    $0xffffffff,%ebp
lea    0x1(%rbp),%rdx
mov    %r14,%rdi
mov    $0xff,%esi
call   1030 <memset@plt>
lea    (%r14,%rbp,1),%rax
add    $0x1,%rax
lea    0x7(%rbx),%ecx
test   %ebx,%ebx
cmovns %ebx,%ecx
and    $0xf8,%ecx
sub    %ecx,%ebx
mov    $0x8,%cl
sub    %bl,%cl
mov    $0xffffffff,%edx
shl    %cl,%edx
mov    %dl,(%rax)
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr = type { i32 }
%struct.sockaddr_in = type { i32 }
%struct.sockaddr_in6 = type { i32 }
define %struct.sockaddr* @ifa_make_sockaddr_mask(i32 %0, %struct.sockaddr* %1, i32 %2) {
%4 = alloca %struct.sockaddr*
%5 = alloca i32
%6 = alloca %struct.sockaddr*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i8
%11 = alloca i32
store i32 %0, i32* %5
store %struct.sockaddr* %1, %struct.sockaddr** %6
store i32 %2, i32* %7
store i8* null, i8** %9
store i32 0, i32* %11
%12 = load %struct.sockaddr*, %struct.sockaddr** %6
%13 = icmp eq %struct.sockaddr* %12, null
br i1 %13, label %14, label %15
14: ; preds = %3
store %struct.sockaddr* null, %struct.sockaddr** %4
br label %72
15: ; preds = %3
%16 = load i32, i32* %5
switch i32 %16, label %35 [
i32 129, label %17
i32 128, label %26
]
17: ; preds = %15
%18 = load %struct.sockaddr*, %struct.sockaddr** %6
%19 = bitcast %struct.sockaddr* %18 to %struct.sockaddr_in*
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %19, i32 0, i32 0
%21 = call i32 @memset(i32* %20, i32 0, i32 4)
%22 = load %struct.sockaddr*, %struct.sockaddr** %6
%23 = bitcast %struct.sockaddr* %22 to %struct.sockaddr_in*
%24 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %23, i32 0, i32 0
%25 = bitcast i32* %24 to i8*
store i8* %25, i8** %9
store i32 32, i32* %11
br label %36
26: ; preds = %15
%27 = load %struct.sockaddr*, %struct.sockaddr** %6
%28 = bitcast %struct.sockaddr* %27 to %struct.sockaddr_in6*
%29 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %28, i32 0, i32 0
%30 = call i32 @memset(i32* %29, i32 0, i32 4)
%31 = load %struct.sockaddr*, %struct.sockaddr** %6
%32 = bitcast %struct.sockaddr* %31 to %struct.sockaddr_in6*
%33 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %32, i32 0, i32 0
%34 = bitcast i32* %33 to i8*
store i8* %34, i8** %9
store i32 128, i32* %11
br label %36
35: ; preds = %15
store %struct.sockaddr* null, %struct.sockaddr** %4
br label %72
36: ; preds = %26, %17
%37 = load i32, i32* %5
%38 = load %struct.sockaddr*, %struct.sockaddr** %6
%39 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %38, i32 0, i32 0
store i32 %37, i32* %39
%40 = load i8*, i8** %9
%41 = icmp ne i8* %40, null
br i1 %41, label %42, label %70
42: ; preds = %36
%43 = load i32, i32* %7
%44 = load i32, i32* %11
%45 = icmp sgt i32 %43, %44
br i1 %45, label %46, label %48
46: ; preds = %42
%47 = load i32, i32* %11
store i32 %47, i32* %7
br label %48
48: ; preds = %46, %42
store i32 0, i32* %8
br label %49
49: ; preds = %57, %48
%50 = load i32, i32* %8
%51 = load i32, i32* %7
%52 = sdiv i32 %51, 8
%53 = icmp slt i32 %50, %52
br i1 %53, label %54, label %60
54: ; preds = %49
%55 = load i8*, i8** %9
%56 = getelementptr inbounds i8, i8* %55, i32 1
store i8* %56, i8** %9
store i8 -1, i8* %55
br label %57
57: ; preds = %54
%58 = load i32, i32* %8
%59 = add nsw i32 %58, 1
store i32 %59, i32* %8
br label %49
60: ; preds = %49
store i8 -1, i8* %10
%61 = load i32, i32* %7
%62 = srem i32 %61, 8
%63 = sub nsw i32 8, %62
%64 = load i8, i8* %10
%65 = sext i8 %64 to i32
%66 = shl i32 %65, %63
%67 = trunc i32 %66 to i8
store i8 %67, i8* %10
%68 = load i8, i8* %10
%69 = load i8*, i8** %9
store i8 %68, i8* %69
br label %70
70: ; preds = %60, %36
%71 = load %struct.sockaddr*, %struct.sockaddr** %6
store %struct.sockaddr* %71, %struct.sockaddr** %4
br label %72
72: ; preds = %70, %35, %14
%73 = load %struct.sockaddr*, %struct.sockaddr** %4
ret %struct.sockaddr* %73
}

LLVM IR (-O2):
%struct.sockaddr = type { i32 }
define %struct.sockaddr* @ifa_make_sockaddr_mask(i32 %0, %struct.sockaddr* %1, i32 %2) {
%4 = bitcast %struct.sockaddr* %1 to i8*
%5 = icmp eq %struct.sockaddr* %1, null
br i1 %5, label %29, label %6
6: ; preds = %3
switch i32 %0, label %29 [
i32 129, label %8
i32 128, label %7
]
7: ; preds = %6
br label %8
8: ; preds = %6, %7
%9 = phi i32 [ 128, %7 ], [ 32, %6 ]
%10 = getelementptr %struct.sockaddr, %struct.sockaddr* %1, i64 0, i32 0
%11 = tail call i32 @memset(i32* %10, i32 0, i32 4)
%12 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %1, i64 0, i32 0
store i32 %0, i32* %12
%13 = icmp slt i32 %9, %2
%14 = select i1 %13, i32 %9, i32 %2
%15 = icmp sgt i32 %14, 7
br i1 %15, label %16, label %23
16: ; preds = %8
%17 = lshr i32 %14, 3
%18 = and i32 %17, 31
%19 = add nsw i32 %18, -1
%20 = zext i32 %19 to i64
%21 = add nuw nsw i64 %20, 1
%22 = getelementptr i8, i8* %4, i64 %21
br label %23
23: ; preds = %16, %8
%24 = phi i8* [ %4, %8 ], [ %22, %16 ]
%25 = srem i32 %14, 8
%26 = sub nsw i32 8, %25
%27 = shl nsw i32 -1, %26
%28 = trunc i32 %27 to i8
store i8 %28, i8* %24
br label %29
29: ; preds = %6, %3, %23
%30 = phi %struct.sockaddr* [ %1, %23 ], [ null, %3 ], [ null, %6 ]
ret %struct.sockaddr* %30
}