#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 */
typedef struct TYPE_4__ TYPE_2__;
typedef struct TYPE_3__ TYPE_1__;

/* Type definitions */
typedef int uint8_t;
typedef int uint32_t;
struct sockaddr_storage {
  scalar_t__ ss_family;
};
struct TYPE_3__ {
  int *s6_addr;
};
struct sockaddr_in6 {
  TYPE_1__ sin6_addr;
};
struct TYPE_4__ {
  int s_addr;
};
struct sockaddr_in {
  TYPE_2__ sin_addr;
};

/* Variables and functions */
scalar_t__ AF_INET;
scalar_t__ AF_INET6;
scalar_t__ pow(int, int);
bool test_v_bit(struct sockaddr_storage *addr, uint8_t bit) {
  if (addr->ss_family == AF_INET6) {
    struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)addr;
    uint8_t byte_idx = bit / 8;
    uint8_t subbyte_idx = bit % 8;
    uint8_t subbyte = addr6->sin6_addr.s6_addr[byte_idx];
    uint8_t v_bit = subbyte & ((uint8_t)pow(2, subbyte_idx));
    return v_bit;
  } else if (addr->ss_family == AF_INET) {
    struct sockaddr_in *addr4 = (struct sockaddr_in *)addr;
    uint32_t v_bit = addr4->sin_addr.s_addr & ((uint32_t)pow(2, bit));
    return v_bit;
  }
  return 0;
}


x86 assembly (clang, -O0):
<test_v_bit>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    0x2eb3(%rip),%rcx
cmp    (%rcx),%rax
jne    1194 <test_v_bit+0x84>
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x14(%rbp),%eax
mov    $0x8,%ecx
cltd
idiv   %ecx
mov    %eax,-0x24(%rbp)
mov    -0x14(%rbp),%eax
mov    $0x8,%ecx
cltd
idiv   %ecx
mov    %edx,-0x28(%rbp)
mov    -0x20(%rbp),%rax
mov    (%rax),%rax
movslq -0x24(%rbp),%rcx
mov    (%rax,%rcx,4),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
mov    %eax,-0x40(%rbp)
mov    -0x28(%rbp),%esi
mov    $0x2,%edi
call   1030 <pow@plt>
mov    %rax,%rcx
mov    -0x40(%rbp),%eax
and    %ecx,%eax
mov    %eax,-0x30(%rbp)
mov    -0x30(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11eb <test_v_bit+0xdb>
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    0x2e46(%rip),%rcx
cmp    (%rcx),%rax
jne    11df <test_v_bit+0xcf>
mov    -0x10(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x44(%rbp)
mov    -0x14(%rbp),%esi
mov    $0x2,%edi
call   1030 <pow@plt>
mov    %rax,%rcx
mov    -0x44(%rbp),%eax
and    %ecx,%eax
mov    %eax,-0x3c(%rbp)
mov    -0x3c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11eb <test_v_bit+0xdb>
jmp    11e4 <test_v_bit+0xd4>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_v_bit>:
push   %rbx
mov    (%rdi),%rbx
mov    0x2ec5(%rip),%rax
cmp    (%rax),%rbx
jne    1145 <test_v_bit+0x35>
lea    0x7(%rsi),%eax
test   %esi,%esi
cmovns %esi,%eax
mov    %eax,%ecx
and    $0xfffffff8,%ecx
sub    %ecx,%esi
sar    $0x3,%eax
cltq
mov    (%rbx,%rax,4),%ebx
mov    $0x2,%edi
call   1030 <pow@plt>
and    %ebx,%eax
pop    %rbx
ret
mov    0x2e9c(%rip),%rcx
xor    %eax,%eax
cmp    (%rcx),%rbx
jne    1143 <test_v_bit+0x33>
mov    $0x2,%edi
call   1030 <pow@plt>
and    %eax,%ebx
mov    %ebx,%eax
pop    %rbx
ret

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

LLVM IR (-O2):
%struct.sockaddr_storage = type { i64 }
@AF_INET6 = global i64 0
@AF_INET = global i64 0
define i32 @test_v_bit(%struct.sockaddr_storage* nocapture %0, i32 %1) {
%3 = getelementptr inbounds %struct.sockaddr_storage, %struct.sockaddr_storage* %0, i64 0, i32 0
%4 = load i64, i64* %3
%5 = load i64, i64* @AF_INET6
%6 = icmp eq i64 %4, %5
br i1 %6, label %7, label %17
7: ; preds = %2
%8 = inttoptr i64 %4 to i32*
%9 = sdiv i32 %1, 8
%10 = srem i32 %1, 8
%11 = sext i32 %9 to i64
%12 = getelementptr inbounds i32, i32* %8, i64 %11
%13 = load i32, i32* %12
%14 = tail call i64 @pow(i32 2, i32 %10)
%15 = trunc i64 %14 to i32
%16 = and i32 %13, %15
br label %24
17: ; preds = %2
%18 = load i64, i64* @AF_INET
%19 = icmp eq i64 %4, %18
br i1 %19, label %20, label %24
20: ; preds = %17
%21 = tail call i64 @pow(i32 2, i32 %1)
%22 = and i64 %4, %21
%23 = trunc i64 %22 to i32
br label %24
24: ; preds = %17, %20, %7
%25 = phi i32 [ %16, %7 ], [ %23, %20 ], [ 0, %17 ]
ret i32 %25
}