#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 */

/* Variables and functions */
scalar_t__ is_valid_ip(unsigned char *);
int /*<<< orphan*/ sscanf(unsigned char *, char *, unsigned int *,
                          unsigned int *, unsigned int *, unsigned int *);
int is_valid_netmask(unsigned char netmask[16]) {
  if (is_valid_ip(netmask) > 0) {
    unsigned int b = 0, i, n[4];
    sscanf(netmask, "%u.%u.%u.%u", &n[3], &n[2], &n[1], &n[0]);
    for (i = 0; i < 4; ++i)
      b += n[i] << (i * 8);
    b = ~b + 1;
    if ((b & (b - 1)) == 0)
      return 1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<is_valid_netmask>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <is_valid_ip@plt>
cmp    $0x0,%rax
jle    11d7 <is_valid_netmask+0xb7>
movl   $0x0,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x30(%rbp),%rdx
add    $0xc,%rdx
lea    -0x30(%rbp),%rcx
add    $0x8,%rcx
lea    -0x30(%rbp),%r8
add    $0x4,%r8
lea    -0x30(%rbp),%r9
lea    0xe93(%rip),%rsi
call   1030 <sscanf@plt>
movl   $0x0,-0x18(%rbp)
cmpl   $0x4,-0x18(%rbp)
jae    11a6 <is_valid_netmask+0x86>
mov    -0x18(%rbp),%eax
mov    -0x30(%rbp,%rax,4),%eax
mov    -0x18(%rbp),%ecx
shl    $0x3,%ecx
shl    %cl,%eax
add    -0x14(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    1179 <is_valid_netmask+0x59>
mov    -0x14(%rbp),%eax
xor    $0xffffffff,%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    -0x14(%rbp),%ecx
sub    $0x1,%ecx
and    %ecx,%eax
cmp    $0x0,%eax
jne    11d2 <is_valid_netmask+0xb2>
movl   $0x1,-0x4(%rbp)
jmp    11de <is_valid_netmask+0xbe>
jmp    11d7 <is_valid_netmask+0xb7>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<is_valid_netmask>:
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%rbx
call   1040 <is_valid_ip@plt>
test   %rax,%rax
jle    1179 <is_valid_netmask+0x59>
lea    0xc(%rsp),%rdx
lea    0x8(%rsp),%rcx
lea    0x4(%rsp),%r8
lea    0xeb8(%rip),%rsi
mov    %rsp,%r9
mov    %rbx,%rdi
call   1030 <sscanf@plt>
mov    0x4(%rsp),%eax
mov    0x8(%rsp),%ecx
shl    $0x8,%eax
add    (%rsp),%eax
shl    $0x10,%ecx
add    %eax,%ecx
mov    0xc(%rsp),%eax
shl    $0x18,%eax
add    %ecx,%eax
mov    %eax,%ecx
not    %ecx
neg    %eax
test   %ecx,%eax
je     117d <is_valid_netmask+0x5d>
xor    %eax,%eax
jmp    1182 <is_valid_netmask+0x62>
mov    $0x1,%eax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@.str = [12 x i8] c"%u.%u.%u.%u\00"
define i32 @is_valid_netmask(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca [4 x i32]
store i8* %0, i8** %3
%7 = load i8*, i8** %3
%8 = call i64 @is_valid_ip(i8* %7)
%9 = icmp sgt i64 %8, 0
br i1 %9, label %10, label %44
10: ; preds = %1
store i32 0, i32* %4
%11 = load i8*, i8** %3
%12 = getelementptr inbounds [4 x i32], [4 x i32]* %6, i64 0, i64 3
%13 = getelementptr inbounds [4 x i32], [4 x i32]* %6, i64 0, i64 2
%14 = getelementptr inbounds [4 x i32], [4 x i32]* %6, i64 0, i64 1
%15 = getelementptr inbounds [4 x i32], [4 x i32]* %6, i64 0, i64 0
%16 = call i32 @sscanf(i8* %11, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32* %12, i32* %13, i32* %14, i32* %15)
store i32 0, i32* %5
br label %17
17: ; preds = %30, %10
%18 = load i32, i32* %5
%19 = icmp ult i32 %18, 4
br i1 %19, label %20, label %33
20: ; preds = %17
%21 = load i32, i32* %5
%22 = zext i32 %21 to i64
%23 = getelementptr inbounds [4 x i32], [4 x i32]* %6, i64 0, i64 %22
%24 = load i32, i32* %23
%25 = load i32, i32* %5
%26 = mul i32 %25, 8
%27 = shl i32 %24, %26
%28 = load i32, i32* %4
%29 = add i32 %28, %27
store i32 %29, i32* %4
br label %30
30: ; preds = %20
%31 = load i32, i32* %5
%32 = add i32 %31, 1
store i32 %32, i32* %5
br label %17
33: ; preds = %17
%34 = load i32, i32* %4
%35 = xor i32 %34, -1
%36 = add i32 %35, 1
store i32 %36, i32* %4
%37 = load i32, i32* %4
%38 = load i32, i32* %4
%39 = sub i32 %38, 1
%40 = and i32 %37, %39
%41 = icmp eq i32 %40, 0
br i1 %41, label %42, label %43
42: ; preds = %33
store i32 1, i32* %2
br label %45
43: ; preds = %33
br label %44
44: ; preds = %43, %1
store i32 0, i32* %2
br label %45
45: ; preds = %44, %42
%46 = load i32, i32* %2
ret i32 %46
}

LLVM IR (-O2):
@.str = [12 x i8] c"%u.%u.%u.%u\00"
define i32 @is_valid_netmask(i8* %0) {
%2 = alloca [4 x i32]
%3 = tail call i64 @is_valid_ip(i8* %0)
%4 = icmp sgt i64 %3, 0
br i1 %4, label %5, label %26
5: ; preds = %1
%6 = bitcast [4 x i32]* %2 to i8*
%7 = getelementptr inbounds [4 x i32], [4 x i32]* %2, i64 0, i64 3
%8 = getelementptr inbounds [4 x i32], [4 x i32]* %2, i64 0, i64 2
%9 = getelementptr inbounds [4 x i32], [4 x i32]* %2, i64 0, i64 1
%10 = getelementptr inbounds [4 x i32], [4 x i32]* %2, i64 0, i64 0
%11 = call i32 @sscanf(i8* %0, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32* %7, i32* %8, i32* %9, i32* %10)
%12 = load i32, i32* %10
%13 = load i32, i32* %9
%14 = shl i32 %13, 8
%15 = add i32 %14, %12
%16 = load i32, i32* %8
%17 = shl i32 %16, 16
%18 = add i32 %17, %15
%19 = load i32, i32* %7
%20 = shl i32 %19, 24
%21 = add i32 %20, %18
%22 = xor i32 %21, -1
%23 = sub i32 0, %21
%24 = and i32 %23, %22
%25 = icmp eq i32 %24, 0
br i1 %25, label %27, label %26
26: ; preds = %5, %1
br label %27
27: ; preds = %5, %26
%28 = phi i32 [ 1, %5 ], [ 0, %26 ]
ret i32 %28
}