#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 /*<<< orphan*/ temp;

/* Variables and functions */
unsigned char atoi(char *);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
scalar_t__ pig_newseg(int);
scalar_t__ strcmp(char const *, char *);
unsigned int *to_ipv4_mask(const char *mask) {
  unsigned int *retval = NULL;
  const char *mp = NULL;
  char temp[20];
  unsigned char byte = 0;
  size_t t = 0;
  retval = (unsigned int *)pig_newseg(sizeof(unsigned int));
  if (strcmp(mask, "*") == 0) {
    *retval = 0xffffffff;
    return retval;
  }
  *retval = 0;
  memset(temp, 0, sizeof(temp));
  for (mp = mask; *mp != 0; mp++) {
    if (*mp == '.' || *(mp + 1) == 0) {
      if (*(mp + 1) == 0) {
        temp[t] = *mp;
      }
      byte = 0xff;
      if (strcmp(temp, "*") != 0) {
        byte = atoi(temp);
      }
      t = 0;
      memset(temp, 0, sizeof(temp));
      *retval = ((*retval) << 8) | byte;
    } else {
      temp[t] = *mp;
      t = (t + 1) % sizeof(temp);
    }
  }
  return retval;
}


x86 assembly (clang, -O0):
<to_ipv4_mask>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
movb   $0x0,-0x41(%rbp)
movq   $0x0,-0x50(%rbp)
mov    $0x4,%edi
call   1050 <pig_newseg@plt>
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe7f(%rip),%rsi
call   1040 <strcmp@plt>
cmp    $0x0,%rax
jne    11a7 <to_ipv4_mask+0x67>
mov    -0x18(%rbp),%rax
movl   $0xffffffff,(%rax)
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    12b7 <to_ipv4_mask+0x177>
mov    -0x18(%rbp),%rax
movl   $0x0,(%rax)
lea    -0x40(%rbp),%rdi
xor    %esi,%esi
mov    $0x14,%edx
call   1030 <memset@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     12af <to_ipv4_mask+0x16f>
mov    -0x20(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2e,%eax
je     11fa <to_ipv4_mask+0xba>
mov    -0x20(%rbp),%rax
movsbl 0x1(%rax),%eax
cmp    $0x0,%eax
jne    1275 <to_ipv4_mask+0x135>
mov    -0x20(%rbp),%rax
movsbl 0x1(%rax),%eax
cmp    $0x0,%eax
jne    1219 <to_ipv4_mask+0xd9>
mov    -0x20(%rbp),%rax
mov    (%rax),%cl
mov    -0x50(%rbp),%rax
mov    %cl,-0x40(%rbp,%rax,1)
movb   $0xff,-0x41(%rbp)
lea    -0x40(%rbp),%rdi
lea    0xdd8(%rip),%rsi
call   1040 <strcmp@plt>
cmp    $0x0,%rax
je     1243 <to_ipv4_mask+0x103>
lea    -0x40(%rbp),%rdi
call   1060 <atoi@plt>
mov    %al,-0x41(%rbp)
movq   $0x0,-0x50(%rbp)
lea    -0x40(%rbp),%rdi
xor    %esi,%esi
mov    $0x14,%edx
call   1030 <memset@plt>
mov    -0x18(%rbp),%rax
mov    (%rax),%ecx
shl    $0x8,%ecx
movzbl -0x41(%rbp),%eax
or     %eax,%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
jmp    1299 <to_ipv4_mask+0x159>
mov    -0x20(%rbp),%rax
mov    (%rax),%cl
mov    -0x50(%rbp),%rax
mov    %cl,-0x40(%rbp,%rax,1)
mov    -0x50(%rbp),%rax
add    $0x1,%rax
mov    $0x14,%ecx
xor    %edx,%edx
div    %rcx
mov    %rdx,-0x50(%rbp)
jmp    129e <to_ipv4_mask+0x15e>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    11c9 <to_ipv4_mask+0x89>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<to_ipv4_mask>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdi,%r13
mov    $0x4,%edi
call   1050 <pig_newseg@plt>
mov    %rax,%r14
lea    0xe9b(%rip),%rsi
mov    %r13,%rdi
call   1040 <strcmp@plt>
test   %rax,%rax
je     1245 <to_ipv4_mask+0x105>
movl   $0x0,(%r14)
mov    %rsp,%r15
xor    %ebx,%ebx
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x14,%edx
call   1030 <memset@plt>
add    $0x1,%r13
lea    0xe64(%rip),%r12
movabs $0xcccccccccccccccd,%rbp
cc cc cc
jmp    11dd <to_ipv4_mask+0x9d>
nopl   0x0(%rax,%rax,1)
mov    %r15,%rdi
call   1060 <atoi@plt>
mov    %eax,%ebx
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x14,%edx
call   1030 <memset@plt>
mov    (%r14),%eax
shl    $0x8,%eax
movzbl %bl,%ecx
or     %eax,%ecx
mov    %ecx,(%r14)
add    $0x1,%r13
xor    %ebx,%ebx
movzbl -0x1(%r13),%eax
cmp    $0x2e,%al
je     1220 <to_ipv4_mask+0xe0>
test   %al,%al
je     124c <to_ipv4_mask+0x10c>
cmpb   $0x0,0x0(%r13)
je     1227 <to_ipv4_mask+0xe7>
mov    %al,(%rsp,%rbx,1)
lea    0x1(%rbx),%rax
mul    %rbp
shr    $0x2,%rdx
and    $0xfffffffffffffffc,%rdx
lea    (%rdx,%rdx,4),%rax
neg    %rax
add    %rax,%rbx
add    $0x1,%rbx
add    $0x1,%r13
jmp    11dd <to_ipv4_mask+0x9d>
nopw   0x0(%rax,%rax,1)
cmpb   $0x0,0x0(%r13)
jne    122a <to_ipv4_mask+0xea>
mov    %al,(%rsp,%rbx,1)
mov    %r15,%rdi
mov    %r12,%rsi
call   1040 <strcmp@plt>
test   %rax,%rax
jne    11b0 <to_ipv4_mask+0x70>
mov    $0xff,%bl
jmp    11ba <to_ipv4_mask+0x7a>
movl   $0xffffffff,(%r14)
mov    %r14,%rax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c"*\00"
define i32* @to_ipv4_mask(i8* %0) {
%2 = alloca i32*
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca i8*
%6 = alloca [20 x i8]
%7 = alloca i8
%8 = alloca i64
store i8* %0, i8** %3
store i32* null, i32** %4
store i8* null, i8** %5
store i8 0, i8* %7
store i64 0, i64* %8
%9 = call i64 @pig_newseg(i32 4)
%10 = inttoptr i64 %9 to i32*
store i32* %10, i32** %4
%11 = load i8*, i8** %3
%12 = call i64 @strcmp(i8* %11, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%13 = icmp eq i64 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %1
%15 = load i32*, i32** %4
store i32 -1, i32* %15
%16 = load i32*, i32** %4
store i32* %16, i32** %2
br label %80
17: ; preds = %1
%18 = load i32*, i32** %4
store i32 0, i32* %18
%19 = getelementptr inbounds [20 x i8], [20 x i8]* %6, i64 0, i64 0
%20 = call i32 @memset(i8* %19, i32 0, i32 20)
%21 = load i8*, i8** %3
store i8* %21, i8** %5
br label %22
22: ; preds = %75, %17
%23 = load i8*, i8** %5
%24 = load i8, i8* %23
%25 = sext i8 %24 to i32
%26 = icmp ne i32 %25, 0
br i1 %26, label %27, label %78
27: ; preds = %22
%28 = load i8*, i8** %5
%29 = load i8, i8* %28
%30 = sext i8 %29 to i32
%31 = icmp eq i32 %30, 46
br i1 %31, label %38, label %32
32: ; preds = %27
%33 = load i8*, i8** %5
%34 = getelementptr inbounds i8, i8* %33, i64 1
%35 = load i8, i8* %34
%36 = sext i8 %35 to i32
%37 = icmp eq i32 %36, 0
br i1 %37, label %38, label %66
38: ; preds = %32, %27
%39 = load i8*, i8** %5
%40 = getelementptr inbounds i8, i8* %39, i64 1
%41 = load i8, i8* %40
%42 = sext i8 %41 to i32
%43 = icmp eq i32 %42, 0
br i1 %43, label %44, label %49
44: ; preds = %38
%45 = load i8*, i8** %5
%46 = load i8, i8* %45
%47 = load i64, i64* %8
%48 = getelementptr inbounds [20 x i8], [20 x i8]* %6, i64 0, i64 %47
store i8 %46, i8* %48
br label %49
49: ; preds = %44, %38
store i8 -1, i8* %7
%50 = getelementptr inbounds [20 x i8], [20 x i8]* %6, i64 0, i64 0
%51 = call i64 @strcmp(i8* %50, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%52 = icmp ne i64 %51, 0
br i1 %52, label %53, label %56
53: ; preds = %49
%54 = getelementptr inbounds [20 x i8], [20 x i8]* %6, i64 0, i64 0
%55 = call zeroext i8 @atoi(i8* %54)
store i8 %55, i8* %7
br label %56
56: ; preds = %53, %49
store i64 0, i64* %8
%57 = getelementptr inbounds [20 x i8], [20 x i8]* %6, i64 0, i64 0
%58 = call i32 @memset(i8* %57, i32 0, i32 20)
%59 = load i32*, i32** %4
%60 = load i32, i32* %59
%61 = shl i32 %60, 8
%62 = load i8, i8* %7
%63 = zext i8 %62 to i32
%64 = or i32 %61, %63
%65 = load i32*, i32** %4
store i32 %64, i32* %65
br label %74
66: ; preds = %32
%67 = load i8*, i8** %5
%68 = load i8, i8* %67
%69 = load i64, i64* %8
%70 = getelementptr inbounds [20 x i8], [20 x i8]* %6, i64 0, i64 %69
store i8 %68, i8* %70
%71 = load i64, i64* %8
%72 = add i64 %71, 1
%73 = urem i64 %72, 20
store i64 %73, i64* %8
br label %74
74: ; preds = %66, %56
br label %75
75: ; preds = %74
%76 = load i8*, i8** %5
%77 = getelementptr inbounds i8, i8* %76, i32 1
store i8* %77, i8** %5
br label %22
78: ; preds = %22
%79 = load i32*, i32** %4
store i32* %79, i32** %2
br label %80
80: ; preds = %78, %14
%81 = load i32*, i32** %2
ret i32* %81
}

LLVM IR (-O2):
@.str = [2 x i8] c"*\00"
define i32* @to_ipv4_mask(i8* %0) {
%2 = alloca [20 x i8]
%3 = getelementptr inbounds [20 x i8], [20 x i8]* %2, i64 0, i64 0
%4 = tail call i64 @pig_newseg(i32 4)
%5 = inttoptr i64 %4 to i32*
%6 = tail call i64 @strcmp(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%7 = icmp eq i64 %6, 0
br i1 %7, label %8, label %9
8: ; preds = %1
store i32 -1, i32* %5
br label %44
9: ; preds = %1
store i32 0, i32* %5
%10 = call i32 @memset(i8* %3, i32 0, i32 20)
br label %11
11: ; preds = %41, %9
%12 = phi i8* [ %0, %9 ], [ %43, %41 ]
%13 = phi i64 [ 0, %9 ], [ %42, %41 ]
%14 = load i8, i8* %12
switch i8 %14, label %15 [
i8 0, label %44
i8 46, label %19
]
15: ; preds = %11
%16 = getelementptr inbounds i8, i8* %12, i64 1
%17 = load i8, i8* %16
%18 = icmp eq i8 %17, 0
br i1 %18, label %23, label %37
19: ; preds = %11
%20 = getelementptr inbounds i8, i8* %12, i64 1
%21 = load i8, i8* %20
%22 = icmp eq i8 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %15, %19
%24 = getelementptr inbounds [20 x i8], [20 x i8]* %2, i64 0, i64 %13
store i8 %14, i8* %24
br label %25
25: ; preds = %23, %19
%26 = call i64 @strcmp(i8* %3, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%27 = icmp eq i64 %26, 0
br i1 %27, label %30, label %28
28: ; preds = %25
%29 = call zeroext i8 @atoi(i8* %3)
br label %30
30: ; preds = %28, %25
%31 = phi i8 [ %29, %28 ], [ -1, %25 ]
%32 = call i32 @memset(i8* %3, i32 0, i32 20)
%33 = load i32, i32* %5
%34 = shl i32 %33, 8
%35 = zext i8 %31 to i32
%36 = or i32 %34, %35
store i32 %36, i32* %5
br label %41
37: ; preds = %15
%38 = getelementptr inbounds [20 x i8], [20 x i8]* %2, i64 0, i64 %13
store i8 %14, i8* %38
%39 = add nsw i64 %13, 1
%40 = urem i64 %39, 20
br label %41
41: ; preds = %30, %37
%42 = phi i64 [ 0, %30 ], [ %40, %37 ]
%43 = getelementptr inbounds i8, i8* %12, i64 1
br label %11
44: ; preds = %11, %8
ret i32* %5
}