#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 uint8_t;
struct in6_addr {
  int dummy;
};

/* Variables and functions */
struct in6_addr *inet6_rth_getaddr(const void *bp, int index) {
  uint8_t *rthp = (uint8_t *)bp;
  struct in6_addr *addr = NULL;
  if (rthp[1] & 1)
    return NULL;
  if (index < 0 || index > rthp[3])
    return NULL;
  addr = (struct in6_addr *)(rthp + 8 + index * sizeof(struct in6_addr));
  return addr;
}


x86 assembly (clang, -O0):
<inet6_rth_getaddr>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x20(%rbp),%rax
mov    0x4(%rax),%eax
and    $0x1,%eax
cmp    $0x0,%eax
je     113b <inet6_rth_getaddr+0x3b>
movq   $0x0,-0x8(%rbp)
jmp    1185 <inet6_rth_getaddr+0x85>
cmpl   $0x0,-0x14(%rbp)
jl     1155 <inet6_rth_getaddr+0x55>
mov    -0x14(%rbp),%eax
mov    -0x20(%rbp),%rcx
cmp    0xc(%rcx),%eax
jle    1162 <inet6_rth_getaddr+0x62>
movq   $0x0,-0x8(%rbp)
jmp    1185 <inet6_rth_getaddr+0x85>
mov    -0x20(%rbp),%rax
add    $0x20,%rax
movslq -0x14(%rbp),%rcx
shl    $0x2,%rcx
shl    $0x2,%rcx
add    %rcx,%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
pop    %rbp
ret

x86 assembly (clang, -O2):
<inet6_rth_getaddr>:
xor    %eax,%eax
test   %esi,%esi
js     1123 <inet6_rth_getaddr+0x23>
mov    0x4(%rdi),%ecx
and    $0x1,%ecx
jne    1123 <inet6_rth_getaddr+0x23>
cmp    %esi,0xc(%rdi)
jge    1116 <inet6_rth_getaddr+0x16>
xor    %eax,%eax
ret
mov    %esi,%eax
shl    $0x4,%rax
add    %rdi,%rax
add    $0x20,%rax
ret

LLVM IR (-O0):
%struct.in6_addr = type { i32 }
define %struct.in6_addr* @inet6_rth_getaddr(i8* %0, i32 %1) {
%3 = alloca %struct.in6_addr*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32*
%7 = alloca %struct.in6_addr*
store i8* %0, i8** %4
store i32 %1, i32* %5
%8 = load i8*, i8** %4
%9 = bitcast i8* %8 to i32*
store i32* %9, i32** %6
store %struct.in6_addr* null, %struct.in6_addr** %7
%10 = load i32*, i32** %6
%11 = getelementptr inbounds i32, i32* %10, i64 1
%12 = load i32, i32* %11
%13 = and i32 %12, 1
%14 = icmp ne i32 %13, 0
br i1 %14, label %15, label %16
15: ; preds = %2
store %struct.in6_addr* null, %struct.in6_addr** %3
br label %35
16: ; preds = %2
%17 = load i32, i32* %5
%18 = icmp slt i32 %17, 0
br i1 %18, label %25, label %19
19: ; preds = %16
%20 = load i32, i32* %5
%21 = load i32*, i32** %6
%22 = getelementptr inbounds i32, i32* %21, i64 3
%23 = load i32, i32* %22
%24 = icmp sgt i32 %20, %23
br i1 %24, label %25, label %26
25: ; preds = %19, %16
store %struct.in6_addr* null, %struct.in6_addr** %3
br label %35
26: ; preds = %19
%27 = load i32*, i32** %6
%28 = getelementptr inbounds i32, i32* %27, i64 8
%29 = load i32, i32* %5
%30 = sext i32 %29 to i64
%31 = mul i64 %30, 4
%32 = getelementptr inbounds i32, i32* %28, i64 %31
%33 = bitcast i32* %32 to %struct.in6_addr*
store %struct.in6_addr* %33, %struct.in6_addr** %7
%34 = load %struct.in6_addr*, %struct.in6_addr** %7
store %struct.in6_addr* %34, %struct.in6_addr** %3
br label %35
35: ; preds = %26, %25, %15
%36 = load %struct.in6_addr*, %struct.in6_addr** %3
ret %struct.in6_addr* %36
}

LLVM IR (-O2):
%struct.in6_addr = type { i32 }
define %struct.in6_addr* @inet6_rth_getaddr(i8* %0, i32 %1) {
%3 = getelementptr inbounds i8, i8* %0, i64 4
%4 = bitcast i8* %3 to i32*
%5 = load i32, i32* %4
%6 = and i32 %5, 1
%7 = icmp ne i32 %6, 0
%8 = icmp slt i32 %1, 0
%9 = or i1 %8, %7
br i1 %9, label %22, label %10
10: ; preds = %2
%11 = getelementptr inbounds i8, i8* %0, i64 12
%12 = bitcast i8* %11 to i32*
%13 = load i32, i32* %12
%14 = icmp slt i32 %13, %1
br i1 %14, label %22, label %15
15: ; preds = %10
%16 = getelementptr inbounds i8, i8* %0, i64 32
%17 = bitcast i8* %16 to i32*
%18 = zext i32 %1 to i64
%19 = shl nuw nsw i64 %18, 2
%20 = getelementptr inbounds i32, i32* %17, i64 %19
%21 = bitcast i32* %20 to %struct.in6_addr*
br label %22
22: ; preds = %10, %2, %15
%23 = phi %struct.in6_addr* [ %21, %15 ], [ null, %2 ], [ null, %10 ]
ret %struct.in6_addr* %23
}