#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 union sockany {
  int dummy;
} sockany;
typedef int uint8_t;
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ addr;

/* Variables and functions */
int /*<<< orphan*/ copy_addr(struct sockaddr **, int, union sockany *, int *,
                             int, int /*<<< orphan*/);
int /*<<< orphan*/ memset(int *, int, int);
void gen_netmask(struct sockaddr **r, int af, union sockany *sa,
                 int prefixlen) {
  uint8_t addr[16] = {0};
  int i;
  if (prefixlen > 8 * sizeof(addr))
    prefixlen = 8 * sizeof(addr);
  i = prefixlen / 8;
  memset(addr, 0xff, i);
  if (i < sizeof(addr))
    addr[i++] = 0xff << (8 - (prefixlen % 8));
  copy_addr(r, af, sa, addr, sizeof(addr), 0);
}


x86 assembly (clang, -O0):
<gen_netmask>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
lea    -0x60(%rbp),%rdi
xor    %esi,%esi
mov    $0x40,%edx
call   1030 <memset@plt>
movslq -0x1c(%rbp),%rax
cmp    $0x200,%rax
jbe    115d <gen_netmask+0x3d>
movl   $0x200,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    $0x8,%ecx
cltd
idiv   %ecx
mov    %eax,-0x64(%rbp)
lea    -0x60(%rbp),%rdi
mov    -0x64(%rbp),%edx
mov    $0xff,%esi
call   1030 <memset@plt>
movslq -0x64(%rbp),%rax
cmp    $0x40,%rax
jae    11b6 <gen_netmask+0x96>
mov    -0x1c(%rbp),%eax
mov    $0x8,%ecx
cltd
idiv   %ecx
mov    $0x8,%ecx
sub    %edx,%ecx
mov    $0xff,%eax
shl    %cl,%eax
mov    %eax,%ecx
mov    -0x64(%rbp),%eax
mov    %eax,%edx
add    $0x1,%edx
mov    %edx,-0x64(%rbp)
cltq
mov    %ecx,-0x60(%rbp,%rax,4)
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
mov    -0x18(%rbp),%rdx
lea    -0x60(%rbp),%rcx
mov    $0x40,%r8d
xor    %r9d,%r9d
call   1040 <copy_addr@plt>
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gen_netmask>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x58,%rsp
mov    %ecx,%r13d
mov    %rdx,0x8(%rsp)
mov    %esi,%r15d
mov    %rdi,%r12
xorps  %xmm0,%xmm0
movaps %xmm0,0x40(%rsp)
movaps %xmm0,0x30(%rsp)
movaps %xmm0,0x20(%rsp)
movaps %xmm0,0x10(%rsp)
cmp    $0x200,%ecx
mov    $0x200,%ebx
cmovb  %ecx,%ebx
mov    %ebx,%ebp
shr    $0x3,%ebp
lea    0x10(%rsp),%rdi
mov    $0xff,%r14d
mov    $0xff,%esi
mov    %ebp,%edx
call   1030 <memset@plt>
cmp    $0x1ff,%r13d
ja     1197 <gen_netmask+0x77>
mov    %ebp,%eax
and    $0x7,%bl
mov    $0x8,%cl
sub    %bl,%cl
shl    %cl,%r14d
mov    %r14d,0x10(%rsp,%rax,4)
lea    0x10(%rsp),%rcx
mov    %r12,%rdi
mov    %r15d,%esi
mov    0x8(%rsp),%rdx
mov    $0x40,%r8d
xor    %r9d,%r9d
call   1040 <copy_addr@plt>
add    $0x58,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr = type { i32 }
%union.sockany = type { i32 }
define void @gen_netmask(%struct.sockaddr** %0, i32 %1, %union.sockany* %2, i32 %3) {
%5 = alloca %struct.sockaddr**
%6 = alloca i32
%7 = alloca %union.sockany*
%8 = alloca i32
%9 = alloca [16 x i32]
%10 = alloca i32
store %struct.sockaddr** %0, %struct.sockaddr*** %5
store i32 %1, i32* %6
store %union.sockany* %2, %union.sockany** %7
store i32 %3, i32* %8
%11 = bitcast [16 x i32]* %9 to i8*
%12 = load i32, i32* %8
%13 = sext i32 %12 to i64
%14 = icmp ugt i64 %13, 512
br i1 %14, label %15, label %16
15: ; preds = %4
store i32 512, i32* %8
br label %16
16: ; preds = %15, %4
%17 = load i32, i32* %8
%18 = sdiv i32 %17, 8
store i32 %18, i32* %10
%19 = getelementptr inbounds [16 x i32], [16 x i32]* %9, i64 0, i64 0
%20 = load i32, i32* %10
%21 = call i32 @memset(i32* %19, i32 255, i32 %20)
%22 = load i32, i32* %10
%23 = sext i32 %22 to i64
%24 = icmp ult i64 %23, 64
br i1 %24, label %25, label %34
25: ; preds = %16
%26 = load i32, i32* %8
%27 = srem i32 %26, 8
%28 = sub nsw i32 8, %27
%29 = shl i32 255, %28
%30 = load i32, i32* %10
%31 = add nsw i32 %30, 1
store i32 %31, i32* %10
%32 = sext i32 %30 to i64
%33 = getelementptr inbounds [16 x i32], [16 x i32]* %9, i64 0, i64 %32
store i32 %29, i32* %33
br label %34
34: ; preds = %25, %16
%35 = load %struct.sockaddr**, %struct.sockaddr*** %5
%36 = load i32, i32* %6
%37 = load %union.sockany*, %union.sockany** %7
%38 = getelementptr inbounds [16 x i32], [16 x i32]* %9, i64 0, i64 0
%39 = call i32 @copy_addr(%struct.sockaddr** %35, i32 %36, %union.sockany* %37, i32* %38, i32 64, i32 0)
ret void
}

LLVM IR (-O2):
%struct.sockaddr = type { i32 }
%union.sockany = type { i32 }
define void @gen_netmask(%struct.sockaddr** %0, i32 %1, %union.sockany* %2, i32 %3) {
%5 = alloca [16 x i32]
%6 = bitcast [16 x i32]* %5 to i8*
%7 = icmp ult i32 %3, 512
%8 = select i1 %7, i32 %3, i32 512
%9 = lshr i32 %8, 3
%10 = getelementptr inbounds [16 x i32], [16 x i32]* %5, i64 0, i64 0
%11 = call i32 @memset(i32* %10, i32 255, i32 %9)
br i1 %7, label %12, label %18
12: ; preds = %4
%13 = zext i32 %9 to i64
%14 = and i32 %8, 7
%15 = sub nuw nsw i32 8, %14
%16 = shl nuw nsw i32 255, %15
%17 = getelementptr inbounds [16 x i32], [16 x i32]* %5, i64 0, i64 %13
store i32 %16, i32* %17
br label %18
18: ; preds = %12, %4
%19 = call i32 @copy_addr(%struct.sockaddr** %0, i32 %1, %union.sockany* %2, i32* %10, i32 64, i32 0)
ret void
}