#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 */
typedef int uint32_t;
struct TYPE_2__ {
  int /*<<< orphan*/ s_addr;
};
struct sockaddr_in {
  TYPE_1__ sin_addr;
};
typedef int /*<<< orphan*/ ones;

/* Variables and functions */
int /*<<< orphan*/ htonl(int);
int /*<<< orphan*/ memset(int *, int, int);
void addr_mask_ipv4(struct sockaddr_in *addr, size_t n) {
  if (n > 32) {
    return;
  }
  if (n == 0) {
    addr->sin_addr.s_addr = 0;
    return;
  }
  uint32_t ones;
  memset(&ones, 255, sizeof(ones));
  ones <<= (32 - n);
  addr->sin_addr.s_addr &= htonl(ones);
}


x86 assembly (clang, -O0):
<addr_mask_ipv4>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpq   $0x20,-0x10(%rbp)
jbe    1140 <addr_mask_ipv4+0x20>
jmp    1190 <addr_mask_ipv4+0x70>
cmpq   $0x0,-0x10(%rbp)
jne    115a <addr_mask_ipv4+0x3a>
mov    -0x8(%rbp),%rax
movl   $0x0,(%rax)
jmp    1190 <addr_mask_ipv4+0x70>
lea    -0x14(%rbp),%rdi
mov    $0xff,%esi
mov    $0x4,%edx
call   1040 <memset@plt>
mov    $0x20,%ecx
sub    -0x10(%rbp),%rcx
mov    -0x14(%rbp),%eax
shl    %cl,%eax
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%edi
call   1030 <htonl@plt>
mov    %eax,%ecx
mov    -0x8(%rbp),%rax
and    (%rax),%ecx
mov    %ecx,(%rax)
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<addr_mask_ipv4>:
push   %r14
push   %rbx
push   %rax
cmp    $0x20,%rsi
ja     1164 <addr_mask_ipv4+0x44>
mov    %rsi,%rbx
mov    %rdi,%r14
test   %rsi,%rsi
je     115d <addr_mask_ipv4+0x3d>
lea    0x4(%rsp),%rdi
mov    $0xff,%esi
mov    $0x4,%edx
call   1040 <memset@plt>
mov    0x4(%rsp),%edi
neg    %bl
mov    %ebx,%ecx
shl    %cl,%edi
call   1030 <htonl@plt>
and    %eax,(%r14)
jmp    1164 <addr_mask_ipv4+0x44>
movl   $0x0,(%r14)
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { %struct.TYPE_2__ }
%struct.TYPE_2__ = type { i32 }
define void @addr_mask_ipv4(%struct.sockaddr_in* %0, i64 %1) {
%3 = alloca %struct.sockaddr_in*
%4 = alloca i64
%5 = alloca i32
store %struct.sockaddr_in* %0, %struct.sockaddr_in** %3
store i64 %1, i64* %4
%6 = load i64, i64* %4
%7 = icmp ugt i64 %6, 32
br i1 %7, label %8, label %9
8: ; preds = %2
br label %30
9: ; preds = %2
%10 = load i64, i64* %4
%11 = icmp eq i64 %10, 0
br i1 %11, label %12, label %16
12: ; preds = %9
%13 = load %struct.sockaddr_in*, %struct.sockaddr_in** %3
%14 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %13, i32 0, i32 0
%15 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %14, i32 0, i32 0
store i32 0, i32* %15
br label %30
16: ; preds = %9
%17 = call i32 @memset(i32* %5, i32 255, i32 4)
%18 = load i64, i64* %4
%19 = sub i64 32, %18
%20 = load i32, i32* %5
%21 = trunc i64 %19 to i32
%22 = shl i32 %20, %21
store i32 %22, i32* %5
%23 = load i32, i32* %5
%24 = call i32 @htonl(i32 %23)
%25 = load %struct.sockaddr_in*, %struct.sockaddr_in** %3
%26 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %25, i32 0, i32 0
%27 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %26, i32 0, i32 0
%28 = load i32, i32* %27
%29 = and i32 %28, %24
store i32 %29, i32* %27
br label %30
30: ; preds = %16, %12, %8
ret void
}

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