#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 */
struct in6_addr {
  int /*<<< orphan*/ *s6_addr32;
};
typedef int l;
typedef int i;

/* Variables and functions */
int ntohl(int /*<<< orphan*/);
int ipv6_prefix_length(const struct in6_addr *a) {
  int l, i;
  for (l = 0; l < 128; l++) {
    if (((ntohl(a->s6_addr32[(l) / 32]) >> (31 - ((l)&31))) & 1) == 0)
      break;
  }
  for (i = l + 1; i < 128; i++) {
    if (((ntohl(a->s6_addr32[(i) / 32]) >> (31 - ((i)&31))) & 1) == 1)
      return -1;
  }
  return l;
}


x86 assembly (clang, -O0):
<ipv6_prefix_length>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
cmpl   $0x80,-0x14(%rbp)
jge    118a <ipv6_prefix_length+0x7a>
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x20(%rbp)
mov    -0x14(%rbp),%eax
mov    $0x20,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
mov    -0x20(%rbp),%rax
movslq %ecx,%rcx
mov    (%rax,%rcx,4),%edi
call   1030 <ntohl@plt>
mov    -0x14(%rbp),%edx
and    $0x1f,%edx
mov    $0x1f,%ecx
sub    %edx,%ecx
sar    %cl,%eax
and    $0x1,%eax
cmp    $0x0,%eax
jne    1177 <ipv6_prefix_length+0x67>
jmp    118a <ipv6_prefix_length+0x7a>
jmp    117c <ipv6_prefix_length+0x6c>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1123 <ipv6_prefix_length+0x13>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
cmpl   $0x80,-0x18(%rbp)
jge    1201 <ipv6_prefix_length+0xf1>
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%eax
mov    $0x20,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
mov    -0x28(%rbp),%rax
movslq %ecx,%rcx
mov    (%rax,%rcx,4),%edi
call   1030 <ntohl@plt>
mov    -0x18(%rbp),%edx
and    $0x1f,%edx
mov    $0x1f,%ecx
sub    %edx,%ecx
sar    %cl,%eax
and    $0x1,%eax
cmp    $0x1,%eax
jne    11ee <ipv6_prefix_length+0xde>
movl   $0xffffffff,-0x4(%rbp)
jmp    1207 <ipv6_prefix_length+0xf7>
jmp    11f3 <ipv6_prefix_length+0xe3>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    1193 <ipv6_prefix_length+0x83>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ipv6_prefix_length>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    (%rdi),%r14
xor    %r12d,%r12d
xchg   %ax,%ax
mov    %r12d,%eax
shr    $0x5,%eax
mov    (%r14,%rax,4),%edi
call   1030 <ntohl@plt>
mov    %r12d,%ecx
not    %cl
movzbl %cl,%ecx
bt     %ecx,%eax
jae    114f <ipv6_prefix_length+0x3f>
add    $0x1,%r12d
cmp    $0x80,%r12d
jne    1120 <ipv6_prefix_length+0x10>
mov    $0x80,%r12d
cmp    $0x7f,%r12d
mov    $0x7f,%r15d
cmova  %r12d,%r15d
neg    %r15d
lea    0x1(%r12),%ebx
cs nopw 0x0(%rax,%rax,1)
nop
lea    (%r15,%rbx,1),%eax
cmp    $0x1,%eax
je     119c <ipv6_prefix_length+0x8c>
mov    %ebx,%eax
shr    $0x5,%eax
mov    (%r14,%rax,4),%edi
call   1030 <ntohl@plt>
mov    %ebx,%ecx
not    %cl
movzbl %cl,%ecx
add    $0x1,%ebx
bt     %ecx,%eax
jae    1170 <ipv6_prefix_length+0x60>
mov    $0xffffffff,%r12d
mov    %r12d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.in6_addr = type { i32* }
define i32 @ipv6_prefix_length(%struct.in6_addr* %0) {
%2 = alloca i32
%3 = alloca %struct.in6_addr*
%4 = alloca i32
%5 = alloca i32
store %struct.in6_addr* %0, %struct.in6_addr** %3
store i32 0, i32* %4
br label %6
6: ; preds = %27, %1
%7 = load i32, i32* %4
%8 = icmp slt i32 %7, 128
br i1 %8, label %9, label %30
9: ; preds = %6
%10 = load %struct.in6_addr*, %struct.in6_addr** %3
%11 = getelementptr inbounds %struct.in6_addr, %struct.in6_addr* %10, i32 0, i32 0
%12 = load i32*, i32** %11
%13 = load i32, i32* %4
%14 = sdiv i32 %13, 32
%15 = sext i32 %14 to i64
%16 = getelementptr inbounds i32, i32* %12, i64 %15
%17 = load i32, i32* %16
%18 = call i32 @ntohl(i32 %17)
%19 = load i32, i32* %4
%20 = and i32 %19, 31
%21 = sub nsw i32 31, %20
%22 = ashr i32 %18, %21
%23 = and i32 %22, 1
%24 = icmp eq i32 %23, 0
br i1 %24, label %25, label %26
25: ; preds = %9
br label %30
26: ; preds = %9
br label %27
27: ; preds = %26
%28 = load i32, i32* %4
%29 = add nsw i32 %28, 1
store i32 %29, i32* %4
br label %6
30: ; preds = %25, %6
%31 = load i32, i32* %4
%32 = add nsw i32 %31, 1
store i32 %32, i32* %5
br label %33
33: ; preds = %54, %30
%34 = load i32, i32* %5
%35 = icmp slt i32 %34, 128
br i1 %35, label %36, label %57
36: ; preds = %33
%37 = load %struct.in6_addr*, %struct.in6_addr** %3
%38 = getelementptr inbounds %struct.in6_addr, %struct.in6_addr* %37, i32 0, i32 0
%39 = load i32*, i32** %38
%40 = load i32, i32* %5
%41 = sdiv i32 %40, 32
%42 = sext i32 %41 to i64
%43 = getelementptr inbounds i32, i32* %39, i64 %42
%44 = load i32, i32* %43
%45 = call i32 @ntohl(i32 %44)
%46 = load i32, i32* %5
%47 = and i32 %46, 31
%48 = sub nsw i32 31, %47
%49 = ashr i32 %45, %48
%50 = and i32 %49, 1
%51 = icmp eq i32 %50, 1
br i1 %51, label %52, label %53
52: ; preds = %36
store i32 -1, i32* %2
br label %59
53: ; preds = %36
br label %54
54: ; preds = %53
%55 = load i32, i32* %5
%56 = add nsw i32 %55, 1
store i32 %56, i32* %5
br label %33
57: ; preds = %33
%58 = load i32, i32* %4
store i32 %58, i32* %2
br label %59
59: ; preds = %57, %52
%60 = load i32, i32* %2
ret i32 %60
}

LLVM IR (-O2):
%struct.in6_addr = type { i32* }
define i32 @ipv6_prefix_length(%struct.in6_addr* nocapture %0) {
%2 = getelementptr inbounds %struct.in6_addr, %struct.in6_addr* %0, i64 0, i32 0
%3 = load i32*, i32** %2
br label %4
4: ; preds = %1, %16
%5 = phi i32 [ 0, %1 ], [ %17, %16 ]
%6 = lshr i32 %5, 5
%7 = zext i32 %6 to i64
%8 = getelementptr inbounds i32, i32* %3, i64 %7
%9 = load i32, i32* %8
%10 = tail call i32 @ntohl(i32 %9)
%11 = and i32 %5, 31
%12 = xor i32 %11, 31
%13 = shl nuw i32 1, %12
%14 = and i32 %10, %13
%15 = icmp eq i32 %14, 0
br i1 %15, label %19, label %16
16: ; preds = %4
%17 = add nuw nsw i32 %5, 1
%18 = icmp eq i32 %17, 128
br i1 %18, label %19, label %4
19: ; preds = %4, %16
%20 = phi i32 [ %5, %4 ], [ 128, %16 ]
%21 = call i32 @llvm.umax.i32(i32 %20, i32 127)
br label %22
22: ; preds = %26, %19
%23 = phi i32 [ %20, %19 ], [ %24, %26 ]
%24 = add nuw nsw i32 %23, 1
%25 = icmp eq i32 %23, %21
br i1 %25, label %37, label %26
26: ; preds = %22
%27 = lshr i32 %24, 5
%28 = zext i32 %27 to i64
%29 = getelementptr inbounds i32, i32* %3, i64 %28
%30 = load i32, i32* %29
%31 = tail call i32 @ntohl(i32 %30)
%32 = and i32 %24, 31
%33 = xor i32 %32, 31
%34 = shl nuw i32 1, %33
%35 = and i32 %31, %34
%36 = icmp eq i32 %35, 0
br i1 %36, label %22, label %37
37: ; preds = %22, %26
%38 = phi i32 [ -1, %26 ], [ %20, %22 ]
ret i32 %38
}