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

/* Variables and functions */
int AF_INET;
int AF_INET6;
int /*<<< orphan*/ getb(int /*<<< orphan*/ *, int);
int extractbits(int af, u_char *adrspace) {
  unsigned bits;
  int i;
  bits = 0;
  if (af == AF_INET6) {
    for (i = 0; i < 128; i++)
      if (!getb((u_char *)adrspace, i))
        bits++;
    return (128 - bits);
  }
  if (af == AF_INET) {
    for (i = 0; i < 32; i++)
      if (!getb((u_char *)adrspace, i))
        bits++;
    return (32 - bits);
  }
  return (-1);
}


x86 assembly (clang, -O0):
<extractbits>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x8(%rbp),%eax
mov    0x2eb0(%rip),%rcx
cmp    (%rcx),%eax
jne    118d <extractbits+0x7d>
movl   $0x0,-0x18(%rbp)
cmpl   $0x80,-0x18(%rbp)
jge    117d <extractbits+0x6d>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%esi
call   1030 <getb@plt>
cmp    $0x0,%eax
jne    116a <extractbits+0x5a>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    116f <extractbits+0x5f>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    113f <extractbits+0x2f>
mov    $0x80,%eax
sub    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11f8 <extractbits+0xe8>
mov    -0x8(%rbp),%eax
mov    0x2e51(%rip),%rcx
cmp    (%rcx),%eax
jne    11f1 <extractbits+0xe1>
movl   $0x0,-0x18(%rbp)
cmpl   $0x20,-0x18(%rbp)
jge    11e1 <extractbits+0xd1>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%esi
call   1030 <getb@plt>
cmp    $0x0,%eax
jne    11ce <extractbits+0xbe>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    11d3 <extractbits+0xc3>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    11a6 <extractbits+0x96>
mov    $0x20,%eax
sub    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11f8 <extractbits+0xe8>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<extractbits>:
push   %rbp
push   %r14
push   %rbx
mov    %rsi,%r14
mov    0x2ec2(%rip),%rax
cmp    %edi,(%rax)
jne    1152 <extractbits+0x42>
xor    %ebp,%ebp
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
mov    %r14,%rdi
mov    %ebp,%esi
call   1030 <getb@plt>
cmp    $0x1,%eax
adc    $0x0,%ebx
add    $0x1,%ebp
cmp    $0x80,%ebp
jne    1130 <extractbits+0x20>
mov    $0x80,%eax
jmp    118d <extractbits+0x7d>
mov    0x2e8f(%rip),%rcx
mov    $0xffffffff,%eax
cmp    %edi,(%rcx)
jne    118f <extractbits+0x7f>
xor    %ebp,%ebp
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
mov    %r14,%rdi
mov    %ebp,%esi
call   1030 <getb@plt>
cmp    $0x1,%eax
adc    $0x0,%ebx
add    $0x1,%ebp
cmp    $0x20,%ebp
jne    1170 <extractbits+0x60>
mov    $0x20,%eax
sub    %ebx,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@AF_INET6 = global i32 0
@AF_INET = global i32 0
define i32 @extractbits(i32 %0, i32* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i32
%7 = alloca i32
store i32 %0, i32* %4
store i32* %1, i32** %5
store i32 0, i32* %6
%8 = load i32, i32* %4
%9 = load i32, i32* @AF_INET6
%10 = icmp eq i32 %8, %9
br i1 %10, label %11, label %30
11: ; preds = %2
store i32 0, i32* %7
br label %12
12: ; preds = %24, %11
%13 = load i32, i32* %7
%14 = icmp slt i32 %13, 128
br i1 %14, label %15, label %27
15: ; preds = %12
%16 = load i32*, i32** %5
%17 = load i32, i32* %7
%18 = call i32 @getb(i32* %16, i32 %17)
%19 = icmp ne i32 %18, 0
br i1 %19, label %23, label %20
20: ; preds = %15
%21 = load i32, i32* %6
%22 = add i32 %21, 1
store i32 %22, i32* %6
br label %23
23: ; preds = %20, %15
br label %24
24: ; preds = %23
%25 = load i32, i32* %7
%26 = add nsw i32 %25, 1
store i32 %26, i32* %7
br label %12
27: ; preds = %12
%28 = load i32, i32* %6
%29 = sub i32 128, %28
store i32 %29, i32* %3
br label %54
30: ; preds = %2
%31 = load i32, i32* %4
%32 = load i32, i32* @AF_INET
%33 = icmp eq i32 %31, %32
br i1 %33, label %34, label %53
34: ; preds = %30
store i32 0, i32* %7
br label %35
35: ; preds = %47, %34
%36 = load i32, i32* %7
%37 = icmp slt i32 %36, 32
br i1 %37, label %38, label %50
38: ; preds = %35
%39 = load i32*, i32** %5
%40 = load i32, i32* %7
%41 = call i32 @getb(i32* %39, i32 %40)
%42 = icmp ne i32 %41, 0
br i1 %42, label %46, label %43
43: ; preds = %38
%44 = load i32, i32* %6
%45 = add i32 %44, 1
store i32 %45, i32* %6
br label %46
46: ; preds = %43, %38
br label %47
47: ; preds = %46
%48 = load i32, i32* %7
%49 = add nsw i32 %48, 1
store i32 %49, i32* %7
br label %35
50: ; preds = %35
%51 = load i32, i32* %6
%52 = sub i32 32, %51
store i32 %52, i32* %3
br label %54
53: ; preds = %30
store i32 -1, i32* %3
br label %54
54: ; preds = %53, %50, %27
%55 = load i32, i32* %3
ret i32 %55
}

LLVM IR (-O2):
@AF_INET6 = global i32 0
@AF_INET = global i32 0
define i32 @extractbits(i32 %0, i32* %1) {
%3 = load i32, i32* @AF_INET6
%4 = icmp eq i32 %3, %0
br i1 %4, label %5, label %16
5: ; preds = %2, %5
%6 = phi i32 [ %12, %5 ], [ 0, %2 ]
%7 = phi i32 [ %11, %5 ], [ 0, %2 ]
%8 = tail call i32 @getb(i32* %1, i32 %6)
%9 = icmp eq i32 %8, 0
%10 = zext i1 %9 to i32
%11 = add i32 %7, %10
%12 = add nuw nsw i32 %6, 1
%13 = icmp eq i32 %12, 128
br i1 %13, label %14, label %5
14: ; preds = %5
%15 = sub i32 128, %11
br label %30
16: ; preds = %2
%17 = load i32, i32* @AF_INET
%18 = icmp eq i32 %17, %0
br i1 %18, label %19, label %30
19: ; preds = %16, %19
%20 = phi i32 [ %26, %19 ], [ 0, %16 ]
%21 = phi i32 [ %25, %19 ], [ 0, %16 ]
%22 = tail call i32 @getb(i32* %1, i32 %20)
%23 = icmp eq i32 %22, 0
%24 = zext i1 %23 to i32
%25 = add i32 %21, %24
%26 = add nuw nsw i32 %20, 1
%27 = icmp eq i32 %26, 32
br i1 %27, label %28, label %19
28: ; preds = %19
%29 = sub i32 32, %25
br label %30
30: ; preds = %16, %28, %14
%31 = phi i32 [ %15, %14 ], [ %29, %28 ], [ -1, %16 ]
ret i32 %31
}