#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) {
  unsigned 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
jae    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    11b7 <ifa_make_sockaddr_mask+0xa7>
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
lea    0x7(%rbx),%ebp
mov    %r14,%rax
cmp    $0xf,%ebp
jb     119d <ifa_make_sockaddr_mask+0x8d>
test   %ebx,%ebx
mov    %ebx,%eax
cmovs  %ebp,%eax
sar    $0x3,%eax
cmp    $0x2,%eax
mov    $0x1,%r15d
cmovae %eax,%r15d
mov    %r14,%rdi
mov    $0xff,%esi
mov    %r15,%rdx
call   1030 <memset@plt>
add    $0xffffffff,%r15d
lea    (%r15,%r14,1),%rax
add    $0x1,%rax
test   %ebx,%ebx
cmovns %ebx,%ebp
and    $0xf8,%ebp
sub    %ebp,%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 ult 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 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 %31, label %6
6: ; preds = %3
switch i32 %0, label %31 [
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 = add nsw i32 %14, 7
%16 = icmp ult i32 %15, 15
br i1 %16, label %25, label %17
17: ; preds = %8
%18 = sdiv i32 %14, 8
%19 = call i32 @llvm.umax.i32(i32 %18, i32 1)
%20 = zext i32 %19 to i64
%21 = add i32 %19, -1
%22 = zext i32 %21 to i64
%23 = add nuw nsw i64 %22, 1
%24 = getelementptr i8, i8* %4, i64 %23
br label %25
25: ; preds = %17, %8
%26 = phi i8* [ %4, %8 ], [ %24, %17 ]
%27 = srem i32 %14, 8
%28 = sub nsw i32 8, %27
%29 = shl nsw i32 -1, %28
%30 = trunc i32 %29 to i8
store i8 %30, i8* %26
br label %31
31: ; preds = %6, %3, %25
%32 = phi %struct.sockaddr* [ %1, %25 ], [ null, %3 ], [ null, %6 ]
ret %struct.sockaddr* %32
}