#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 */
unsigned int inet_addr(char const *);
unsigned int ntohl(unsigned int);
int get_prefix_len_by_mask(const char *mask) {
  int i;
  int len = 0;
  unsigned int n = inet_addr(mask);
  n = ntohl(n);
  for (i = 0; i < 32; i++) {
    if (n & (1 << (31 - i))) {
      len++;
    } else {
      break;
    }
  }
  return len;
}


x86 assembly (clang, -O0):
<get_prefix_len_by_mask>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x0,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <inet_addr@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%edi
call   1040 <ntohl@plt>
mov    %eax,-0x14(%rbp)
movl   $0x0,-0xc(%rbp)
cmpl   $0x20,-0xc(%rbp)
jge    11a0 <get_prefix_len_by_mask+0x80>
mov    -0x14(%rbp),%eax
mov    $0x1f,%ecx
sub    -0xc(%rbp),%ecx
mov    $0x1,%edx
shl    %cl,%edx
mov    %edx,%ecx
and    %ecx,%eax
cmp    $0x0,%eax
je     1188 <get_prefix_len_by_mask+0x68>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    118d <get_prefix_len_by_mask+0x6d>
jmp    11a0 <get_prefix_len_by_mask+0x80>
jmp    1192 <get_prefix_len_by_mask+0x72>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    1151 <get_prefix_len_by_mask+0x31>
mov    -0x10(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_prefix_len_by_mask>:
push   %rax
call   1030 <inet_addr@plt>
mov    %eax,%edi
call   1040 <ntohl@plt>
xor    %edx,%edx
nop
mov    %eax,%esi
mov    %edx,%ecx
shl    %cl,%esi
test   $0x80000000,%esi
je     1187 <get_prefix_len_by_mask+0x67>
lea    0x1(%rdx),%ecx
mov    %eax,%esi
shl    %cl,%esi
test   $0x80000000,%esi
je     117a <get_prefix_len_by_mask+0x5a>
lea    0x2(%rdx),%ecx
mov    %eax,%esi
shl    %cl,%esi
test   $0x80000000,%esi
je     117f <get_prefix_len_by_mask+0x5f>
lea    0x3(%rdx),%ecx
mov    %eax,%esi
shl    %cl,%esi
test   $0x80000000,%esi
je     1184 <get_prefix_len_by_mask+0x64>
add    $0x4,%edx
cmp    $0x20,%edx
jne    1130 <get_prefix_len_by_mask+0x10>
mov    $0x20,%edx
jmp    1187 <get_prefix_len_by_mask+0x67>
add    $0x1,%edx
jmp    1187 <get_prefix_len_by_mask+0x67>
add    $0x2,%edx
jmp    1187 <get_prefix_len_by_mask+0x67>
add    $0x3,%edx
mov    %edx,%eax
pop    %rcx
ret

LLVM IR (-O0):
define i32 @get_prefix_len_by_mask(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
store i8* %0, i8** %2
store i32 0, i32* %4
%6 = load i8*, i8** %2
%7 = call i32 @inet_addr(i8* %6)
store i32 %7, i32* %5
%8 = load i32, i32* %5
%9 = call i32 @ntohl(i32 %8)
store i32 %9, i32* %5
store i32 0, i32* %3
br label %10
10: ; preds = %25, %1
%11 = load i32, i32* %3
%12 = icmp slt i32 %11, 32
br i1 %12, label %13, label %28
13: ; preds = %10
%14 = load i32, i32* %5
%15 = load i32, i32* %3
%16 = sub nsw i32 31, %15
%17 = shl i32 1, %16
%18 = and i32 %14, %17
%19 = icmp ne i32 %18, 0
br i1 %19, label %20, label %23
20: ; preds = %13
%21 = load i32, i32* %4
%22 = add nsw i32 %21, 1
store i32 %22, i32* %4
br label %24
23: ; preds = %13
br label %28
24: ; preds = %20
br label %25
25: ; preds = %24
%26 = load i32, i32* %3
%27 = add nsw i32 %26, 1
store i32 %27, i32* %3
br label %10
28: ; preds = %23, %10
%29 = load i32, i32* %4
ret i32 %29
}

LLVM IR (-O2):
define i32 @get_prefix_len_by_mask(i8* %0) {
%2 = tail call i32 @inet_addr(i8* %0)
%3 = tail call i32 @ntohl(i32 %2)
br label %4
4: ; preds = %24, %1
%5 = phi i32 [ 0, %1 ], [ %25, %24 ]
%6 = lshr i32 -2147483648, %5
%7 = and i32 %6, %3
%8 = icmp eq i32 %7, 0
br i1 %8, label %27, label %9
9: ; preds = %4
%10 = or i32 %5, 1
%11 = lshr i32 -2147483648, %10
%12 = and i32 %11, %3
%13 = icmp eq i32 %12, 0
br i1 %13, label %27, label %14
14: ; preds = %9
%15 = or i32 %5, 2
%16 = lshr i32 -2147483648, %15
%17 = and i32 %16, %3
%18 = icmp eq i32 %17, 0
br i1 %18, label %27, label %19
19: ; preds = %14
%20 = or i32 %5, 3
%21 = lshr i32 -2147483648, %20
%22 = and i32 %21, %3
%23 = icmp eq i32 %22, 0
br i1 %23, label %27, label %24
24: ; preds = %19
%25 = add nuw nsw i32 %5, 4
%26 = icmp eq i32 %25, 32
br i1 %26, label %27, label %4
27: ; preds = %24, %19, %14, %9, %4
%28 = phi i32 [ %5, %4 ], [ %10, %9 ], [ %15, %14 ], [ %20, %19 ], [ 32, %24 ]
ret i32 %28
}