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

/* Variables and functions */
int htonl(int);
int /*<<< orphan*/ memcpy(unsigned char *, int *, int);
long strtol(char *, char **, int);
scalar_t__ undot_ip(char *, unsigned char *);
int ip_mask(char *mask, unsigned char *mask2) {
  char *end;
  long int bits;
  uint32_t mask22;
  if (undot_ip(mask, mask2)) {
    bits = strtol(mask, &end, 10);
    if (*end != '\0' || bits > 32 || bits < 0)
      return -1;
    if (bits != 0) {
      mask22 = htonl(0xFFFFFFFF << (32 - bits));
      memcpy(mask2, &mask22, 4);
    } else {
      mask22 = 0xFFFFFFFF;
      memcpy(mask2, &mask22, 4);
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<ip_mask>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1060 <undot_ip@plt>
cmp    $0x0,%rax
je     1209 <ip_mask+0xc9>
mov    -0x10(%rbp),%rdi
lea    -0x20(%rbp),%rsi
mov    $0xa,%edx
call   1040 <strtol@plt>
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
jne    11a3 <ip_mask+0x63>
cmpq   $0x20,-0x28(%rbp)
jg     11a3 <ip_mask+0x63>
cmpq   $0x0,-0x28(%rbp)
jge    11af <ip_mask+0x6f>
movl   $0xffffffff,-0x4(%rbp)
jmp    1210 <ip_mask+0xd0>
cmpq   $0x0,-0x28(%rbp)
je     11eb <ip_mask+0xab>
mov    $0x20,%eax
sub    -0x28(%rbp),%rax
mov    %eax,%ecx
mov    $0xffffffff,%edi
shl    %cl,%edi
call   1030 <htonl@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x18(%rbp),%rdi
lea    -0x2c(%rbp),%rsi
mov    $0x4,%edx
call   1050 <memcpy@plt>
jmp    1204 <ip_mask+0xc4>
movl   $0xffffffff,-0x2c(%rbp)
mov    -0x18(%rbp),%rdi
lea    -0x2c(%rbp),%rsi
mov    $0x4,%edx
call   1050 <memcpy@plt>
jmp    1209 <ip_mask+0xc9>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ip_mask>:
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%r14
mov    %rdi,%rbx
call   1060 <undot_ip@plt>
mov    %rax,%rcx
xor    %eax,%eax
test   %rcx,%rcx
je     11ba <ip_mask+0x7a>
lea    0x10(%rsp),%rsi
mov    %rbx,%rdi
mov    $0xa,%edx
call   1040 <strtol@plt>
mov    %rax,%rcx
mov    0x10(%rsp),%rax
cmpb   $0x0,(%rax)
mov    $0xffffffff,%eax
jne    11ba <ip_mask+0x7a>
cmp    $0x20,%rcx
jg     11ba <ip_mask+0x7a>
test   %rcx,%rcx
js     11ba <ip_mask+0x7a>
je     119d <ip_mask+0x5d>
neg    %cl
mov    $0xffffffff,%edi
shl    %cl,%edi
call   1030 <htonl@plt>
jmp    11a2 <ip_mask+0x62>
mov    $0xffffffff,%eax
mov    %eax,0xc(%rsp)
lea    0xc(%rsp),%rsi
mov    %r14,%rdi
mov    $0x4,%edx
call   1050 <memcpy@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
define i32 @ip_mask(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
%9 = load i8*, i8** %4
%10 = load i8*, i8** %5
%11 = call i64 @undot_ip(i8* %9, i8* %10)
%12 = icmp ne i64 %11, 0
br i1 %12, label %13, label %42
13: ; preds = %2
%14 = load i8*, i8** %4
%15 = call i64 @strtol(i8* %14, i8** %6, i32 10)
store i64 %15, i64* %7
%16 = load i8*, i8** %6
%17 = load i8, i8* %16
%18 = sext i8 %17 to i32
%19 = icmp ne i32 %18, 0
br i1 %19, label %26, label %20
20: ; preds = %13
%21 = load i64, i64* %7
%22 = icmp sgt i64 %21, 32
br i1 %22, label %26, label %23
23: ; preds = %20
%24 = load i64, i64* %7
%25 = icmp slt i64 %24, 0
br i1 %25, label %26, label %27
26: ; preds = %23, %20, %13
store i32 -1, i32* %3
br label %43
27: ; preds = %23
%28 = load i64, i64* %7
%29 = icmp ne i64 %28, 0
br i1 %29, label %30, label %38
30: ; preds = %27
%31 = load i64, i64* %7
%32 = sub nsw i64 32, %31
%33 = trunc i64 %32 to i32
%34 = shl i32 -1, %33
%35 = call i32 @htonl(i32 %34)
store i32 %35, i32* %8
%36 = load i8*, i8** %5
%37 = call i32 @memcpy(i8* %36, i32* %8, i32 4)
br label %41
38: ; preds = %27
store i32 -1, i32* %8
%39 = load i8*, i8** %5
%40 = call i32 @memcpy(i8* %39, i32* %8, i32 4)
br label %41
41: ; preds = %38, %30
br label %42
42: ; preds = %41, %2
store i32 0, i32* %3
br label %43
43: ; preds = %42, %26
%44 = load i32, i32* %3
ret i32 %44
}

LLVM IR (-O2):
define i32 @ip_mask(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = bitcast i8** %3 to i8*
%6 = bitcast i32* %4 to i8*
%7 = tail call i64 @undot_ip(i8* %0, i8* %1)
%8 = icmp eq i64 %7, 0
br i1 %8, label %28, label %9
9: ; preds = %2
%10 = call i64 @strtol(i8* %0, i8** %3, i32 10)
%11 = load i8*, i8** %3
%12 = load i8, i8* %11
%13 = icmp ne i8 %12, 0
%14 = icmp sgt i64 %10, 32
%15 = select i1 %13, i1 true, i1 %14
%16 = icmp slt i64 %10, 0
%17 = select i1 %15, i1 true, i1 %16
br i1 %17, label %28, label %18
18: ; preds = %9
%19 = icmp eq i64 %10, 0
br i1 %19, label %25, label %20
20: ; preds = %18
%21 = trunc i64 %10 to i32
%22 = sub nuw nsw i32 32, %21
%23 = shl nsw i32 -1, %22
%24 = tail call i32 @htonl(i32 %23)
br label %25
25: ; preds = %18, %20
%26 = phi i32 [ %24, %20 ], [ -1, %18 ]
store i32 %26, i32* %4
%27 = call i32 @memcpy(i8* %1, i32* %4, i32 4)
br label %28
28: ; preds = %25, %2, %9
%29 = phi i32 [ -1, %9 ], [ 0, %2 ], [ 0, %25 ]
ret i32 %29
}