#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 scalar_t__ uint32_t;

/* Variables and functions */
scalar_t__ UINT_MAX;
scalar_t__ parse_ipv4_string(char *);
int compare_subnet(int prefix_length, char *candidate, char *destination) {
  uint32_t net_mask = UINT_MAX;
  uint32_t shift_amnt = 32 - prefix_length;
  net_mask = net_mask >> shift_amnt;
  net_mask = net_mask << shift_amnt;
  uint32_t destination_ip = parse_ipv4_string(destination);
  uint32_t candidate_ip = parse_ipv4_string(candidate);
  uint32_t subnet = candidate_ip & net_mask;
  uint32_t masked_destination = destination_ip & net_mask;
  if (masked_destination == subnet) {
    return 0;
  }
  return -1;
}


x86 assembly (clang, -O0):
<compare_subnet>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    0x2ebe(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x20(%rbp)
mov    $0x20,%eax
sub    -0x8(%rbp),%eax
cltq
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rax
mov    -0x28(%rbp),%rcx
sar    %cl,%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    -0x28(%rbp),%rcx
shl    %cl,%rax
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <parse_ipv4_string@plt>
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <parse_ipv4_string@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
and    -0x20(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x30(%rbp),%rax
and    -0x20(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x48(%rbp),%rax
cmp    -0x40(%rbp),%rax
jne    11a9 <compare_subnet+0x99>
movl   $0x0,-0x4(%rbp)
jmp    11b0 <compare_subnet+0xa0>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<compare_subnet>:
push   %r15
push   %r14
push   %rbx
mov    0x2ecc(%rip),%rax
mov    (%rax),%rbx
mov    $0x20,%cl
sub    %dil,%cl
shr    %cl,%rbx
mov    %rdx,%rdi
mov    %rsi,%r14
shl    %cl,%rbx
call   1030 <parse_ipv4_string@plt>
mov    %rax,%r15
mov    %r14,%rdi
call   1030 <parse_ipv4_string@plt>
xor    %r15,%rax
and    %rbx,%rax
neg    %rax
sbb    %eax,%eax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@UINT_MAX = global i64 0
define i32 @compare_subnet(i32 %0, i8* %1, i8* %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i64
%10 = alloca i64
%11 = alloca i64
%12 = alloca i64
%13 = alloca i64
store i32 %0, i32* %5
store i8* %1, i8** %6
store i8* %2, i8** %7
%14 = load i64, i64* @UINT_MAX
store i64 %14, i64* %8
%15 = load i32, i32* %5
%16 = sub nsw i32 32, %15
%17 = sext i32 %16 to i64
store i64 %17, i64* %9
%18 = load i64, i64* %8
%19 = load i64, i64* %9
%20 = ashr i64 %18, %19
store i64 %20, i64* %8
%21 = load i64, i64* %8
%22 = load i64, i64* %9
%23 = shl i64 %21, %22
store i64 %23, i64* %8
%24 = load i8*, i8** %7
%25 = call i64 @parse_ipv4_string(i8* %24)
store i64 %25, i64* %10
%26 = load i8*, i8** %6
%27 = call i64 @parse_ipv4_string(i8* %26)
store i64 %27, i64* %11
%28 = load i64, i64* %11
%29 = load i64, i64* %8
%30 = and i64 %28, %29
store i64 %30, i64* %12
%31 = load i64, i64* %10
%32 = load i64, i64* %8
%33 = and i64 %31, %32
store i64 %33, i64* %13
%34 = load i64, i64* %13
%35 = load i64, i64* %12
%36 = icmp eq i64 %34, %35
br i1 %36, label %37, label %38
37: ; preds = %3
store i32 0, i32* %4
br label %39
38: ; preds = %3
store i32 -1, i32* %4
br label %39
39: ; preds = %38, %37
%40 = load i32, i32* %4
ret i32 %40
}

LLVM IR (-O2):
@UINT_MAX = global i64 0
define i32 @compare_subnet(i32 %0, i8* %1, i8* %2) {
%4 = load i64, i64* @UINT_MAX
%5 = sub nsw i32 32, %0
%6 = zext i32 %5 to i64
%7 = shl nsw i64 -1, %6
%8 = and i64 %4, %7
%9 = tail call i64 @parse_ipv4_string(i8* %2)
%10 = tail call i64 @parse_ipv4_string(i8* %1)
%11 = xor i64 %10, %9
%12 = and i64 %8, %11
%13 = icmp ne i64 %12, 0
%14 = sext i1 %13 to i32
ret i32 %14
}