#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 */
int /*<<< orphan*/ sscanf(char const *, char *, unsigned int *, unsigned int *,
                          unsigned int *, unsigned int *);
int submask_masktoprefix(const char *ip_str) {
  unsigned int ip_num = 0;
  unsigned int c1, c2, c3, c4;
  int cnt = 0;
  int i = 0;
  sscanf(ip_str, "%u.%u.%u.%u", &c1, &c2, &c3, &c4);
  ip_num = c1 << 24 | c2 << 16 | c3 << 8 | c4;
  if (ip_num == 0xffffffff)
    return 32;
  if (ip_num == 0xffffff00)
    return 24;
  if (ip_num == 0xffff0000)
    return 16;
  if (ip_num == 0xff000000)
    return 6;
  for (i = 0; i < 32; i++) {
    if ((ip_num << i) & 0x80000000)
      cnt++;
    else
      break;
  }
  return cnt;
}


x86 assembly (clang, -O0):
<submask_masktoprefix>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
movl   $0x0,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xec4(%rip),%rsi
lea    -0x18(%rbp),%rdx
lea    -0x1c(%rbp),%rcx
lea    -0x20(%rbp),%r8
lea    -0x24(%rbp),%r9
call   1030 <sscanf@plt>
mov    -0x18(%rbp),%eax
shl    $0x18,%eax
mov    -0x1c(%rbp),%ecx
shl    $0x10,%ecx
or     %ecx,%eax
mov    -0x20(%rbp),%ecx
shl    $0x8,%ecx
or     %ecx,%eax
or     -0x24(%rbp),%eax
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    1183 <submask_masktoprefix+0x73>
movl   $0x20,-0x4(%rbp)
jmp    1221 <submask_masktoprefix+0x111>
cmpl   $0xffffff00,-0x14(%rbp)
jne    119c <submask_masktoprefix+0x8c>
movl   $0x18,-0x4(%rbp)
jmp    1221 <submask_masktoprefix+0x111>
cmpl   $0xffff0000,-0x14(%rbp)
jne    11b5 <submask_masktoprefix+0xa5>
movl   $0x10,-0x4(%rbp)
jmp    1221 <submask_masktoprefix+0x111>
cmpl   $0xff000000,-0x14(%rbp)
jne    11ce <submask_masktoprefix+0xbe>
movl   $0x6,-0x4(%rbp)
jmp    1221 <submask_masktoprefix+0x111>
movl   $0x0,-0x2c(%rbp)
cmpl   $0x20,-0x2c(%rbp)
jge    121b <submask_masktoprefix+0x10b>
mov    -0x14(%rbp),%eax
mov    -0x2c(%rbp),%ecx
shl    %cl,%eax
and    $0x80000000,%eax
cmp    $0x0,%eax
je     1203 <submask_masktoprefix+0xf3>
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
jmp    1208 <submask_masktoprefix+0xf8>
jmp    121b <submask_masktoprefix+0x10b>
jmp    120d <submask_masktoprefix+0xfd>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    11d5 <submask_masktoprefix+0xc5>
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<submask_masktoprefix>:
sub    $0x18,%rsp
lea    0xee5(%rip),%rsi
lea    0x14(%rsp),%rdx
lea    0x10(%rsp),%rcx
lea    0xc(%rsp),%r8
lea    0x8(%rsp),%r9
call   1030 <sscanf@plt>
mov    0x14(%rsp),%eax
shl    $0x18,%eax
mov    0x10(%rsp),%ecx
shl    $0x10,%ecx
or     %eax,%ecx
mov    0xc(%rsp),%eax
shl    $0x8,%eax
or     %ecx,%eax
or     0x8(%rsp),%eax
cmp    $0xfffffeff,%eax
jg     116d <submask_masktoprefix+0x5d>
cmp    $0xff000000,%eax
je     11a4 <submask_masktoprefix+0x94>
cmp    $0xffff0000,%eax
jne    117e <submask_masktoprefix+0x6e>
mov    $0x10,%ecx
jmp    11b0 <submask_masktoprefix+0xa0>
cmp    $0xffffff00,%eax
je     11ab <submask_masktoprefix+0x9b>
mov    $0x20,%ecx
cmp    $0xffffffff,%eax
je     11b0 <submask_masktoprefix+0xa0>
xor    %ecx,%ecx
test   %eax,%eax
jns    11b0 <submask_masktoprefix+0xa0>
xor    %ecx,%ecx
cs nopw 0x0(%rax,%rax,1)
mov    %ecx,%esi
lea    0x1(%rsi),%ecx
mov    %eax,%edx
shl    %cl,%edx
cmp    $0x1e,%esi
ja     11b0 <submask_masktoprefix+0xa0>
test   %edx,%edx
js     1190 <submask_masktoprefix+0x80>
jmp    11b0 <submask_masktoprefix+0xa0>
mov    $0x6,%ecx
jmp    11b0 <submask_masktoprefix+0xa0>
mov    $0x18,%ecx
mov    %ecx,%eax
add    $0x18,%rsp
ret

LLVM IR (-O0):
@.str = [12 x i8] c"%u.%u.%u.%u\00"
define i32 @submask_masktoprefix(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i8* %0, i8** %3
store i32 0, i32* %4
store i32 0, i32* %9
store i32 0, i32* %10
%11 = load i8*, i8** %3
%12 = call i32 @sscanf(i8* %11, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32* %5, i32* %6, i32* %7, i32* %8)
%13 = load i32, i32* %5
%14 = shl i32 %13, 24
%15 = load i32, i32* %6
%16 = shl i32 %15, 16
%17 = or i32 %14, %16
%18 = load i32, i32* %7
%19 = shl i32 %18, 8
%20 = or i32 %17, %19
%21 = load i32, i32* %8
%22 = or i32 %20, %21
store i32 %22, i32* %4
%23 = load i32, i32* %4
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %26
25: ; preds = %1
store i32 32, i32* %2
br label %58
26: ; preds = %1
%27 = load i32, i32* %4
%28 = icmp eq i32 %27, -256
br i1 %28, label %29, label %30
29: ; preds = %26
store i32 24, i32* %2
br label %58
30: ; preds = %26
%31 = load i32, i32* %4
%32 = icmp eq i32 %31, -65536
br i1 %32, label %33, label %34
33: ; preds = %30
store i32 16, i32* %2
br label %58
34: ; preds = %30
%35 = load i32, i32* %4
%36 = icmp eq i32 %35, -16777216
br i1 %36, label %37, label %38
37: ; preds = %34
store i32 6, i32* %2
br label %58
38: ; preds = %34
store i32 0, i32* %10
br label %39
39: ; preds = %53, %38
%40 = load i32, i32* %10
%41 = icmp slt i32 %40, 32
br i1 %41, label %42, label %56
42: ; preds = %39
%43 = load i32, i32* %4
%44 = load i32, i32* %10
%45 = shl i32 %43, %44
%46 = and i32 %45, -2147483648
%47 = icmp ne i32 %46, 0
br i1 %47, label %48, label %51
48: ; preds = %42
%49 = load i32, i32* %9
%50 = add nsw i32 %49, 1
store i32 %50, i32* %9
br label %52
51: ; preds = %42
br label %56
52: ; preds = %48
br label %53
53: ; preds = %52
%54 = load i32, i32* %10
%55 = add nsw i32 %54, 1
store i32 %55, i32* %10
br label %39
56: ; preds = %51, %39
%57 = load i32, i32* %9
store i32 %57, i32* %2
br label %58
58: ; preds = %56, %37, %33, %29, %25
%59 = load i32, i32* %2
ret i32 %59
}

LLVM IR (-O2):
@.str = [12 x i8] c"%u.%u.%u.%u\00"
define i32 @submask_masktoprefix(i8* nocapture %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = bitcast i32* %2 to i8*
%7 = bitcast i32* %3 to i8*
%8 = bitcast i32* %4 to i8*
%9 = bitcast i32* %5 to i8*
%10 = call i32 @sscanf(i8* %0, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32* %2, i32* %3, i32* %4, i32* %5)
%11 = load i32, i32* %2
%12 = shl i32 %11, 24
%13 = load i32, i32* %3
%14 = shl i32 %13, 16
%15 = or i32 %14, %12
%16 = load i32, i32* %4
%17 = shl i32 %16, 8
%18 = or i32 %15, %17
%19 = load i32, i32* %5
%20 = or i32 %18, %19
switch i32 %20, label %21 [
i32 -1, label %33
i32 -256, label %23
i32 -65536, label %24
i32 -16777216, label %25
]
21: ; preds = %1
%22 = icmp sgt i32 %20, -1
br i1 %22, label %33, label %26
23: ; preds = %1
br label %33
24: ; preds = %1
br label %33
25: ; preds = %1
br label %33
26: ; preds = %21, %26
%27 = phi i32 [ %28, %26 ], [ 0, %21 ]
%28 = add nuw nsw i32 %27, 1
%29 = icmp ugt i32 %27, 30
%30 = shl i32 %20, %28
%31 = icmp sgt i32 %30, -1
%32 = select i1 %29, i1 true, i1 %31
br i1 %32, label %33, label %26
33: ; preds = %26, %21, %1, %25, %24, %23
%34 = phi i32 [ 24, %23 ], [ 16, %24 ], [ 6, %25 ], [ 32, %1 ], [ 0, %21 ], [ %28, %26 ]
ret i32 %34
}