#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 ip6_rthdr0 {
  int ip6r0_len;
  struct in6_addr *ip6r0_addr;
};
struct ip6_rthdr {
  int ip6r_type;
};
struct in6_addr {
  int dummy;
};

/* Variables and functions */
#define IPV6_RTHDR_TYPE_0 128
struct in6_addr *inet6_rth_getaddr(const void *bp, int index) {
  struct ip6_rthdr *rthdr = (struct ip6_rthdr *)bp;
  switch (rthdr->ip6r_type) {
    struct ip6_rthdr0 *rthdr0;
  case IPV6_RTHDR_TYPE_0:
    rthdr0 = (struct ip6_rthdr0 *)rthdr;
    if (index >= rthdr0->ip6r0_len * 8 / sizeof(struct in6_addr))
      break;
    return &rthdr0->ip6r0_addr[index];
  }
  return NULL;
}


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)
mov    -0x20(%rbp),%rax
mov    (%rax),%eax
sub    $0x80,%eax
jne    116f <inet6_rth_getaddr+0x6f>
jmp    1129 <inet6_rth_getaddr+0x29>
mov    -0x20(%rbp),%rax
mov    %rax,-0x28(%rbp)
movslq -0x14(%rbp),%rax
mov    -0x28(%rbp),%rcx
mov    (%rcx),%ecx
shl    $0x3,%ecx
movslq %ecx,%rcx
shr    $0x2,%rcx
cmp    %rcx,%rax
jb     1153 <inet6_rth_getaddr+0x53>
jmp    116f <inet6_rth_getaddr+0x6f>
mov    -0x28(%rbp),%rax
mov    0x8(%rax),%rax
movslq -0x14(%rbp),%rcx
shl    $0x2,%rcx
add    %rcx,%rax
mov    %rax,-0x8(%rbp)
jmp    1177 <inet6_rth_getaddr+0x77>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
pop    %rbp
ret

x86 assembly (clang, -O2):
<inet6_rth_getaddr>:
xor    %eax,%eax
cmpl   $0x80,(%rdi)
jne    111c <inet6_rth_getaddr+0x1c>
cmp    $0xff,%esi
ja     111c <inet6_rth_getaddr+0x1c>
mov    %esi,%eax
shl    $0x2,%rax
add    0x8(%rdi),%rax
ret

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

LLVM IR (-O2):
%struct.in6_addr = type { i32 }
define %struct.in6_addr* @inet6_rth_getaddr(i8* nocapture %0, i32 %1) {
%3 = bitcast i8* %0 to i32*
%4 = load i32, i32* %3
%5 = icmp eq i32 %4, 128
%6 = icmp ult i32 %1, 256
%7 = and i1 %5, %6
br i1 %7, label %8, label %14
8: ; preds = %2
%9 = zext i32 %1 to i64
%10 = getelementptr inbounds i8, i8* %0, i64 8
%11 = bitcast i8* %10 to %struct.in6_addr**
%12 = load %struct.in6_addr*, %struct.in6_addr** %11
%13 = getelementptr inbounds %struct.in6_addr, %struct.in6_addr* %12, i64 %9
br label %14
14: ; preds = %2, %8
%15 = phi %struct.in6_addr* [ %13, %8 ], [ null, %2 ]
ret %struct.in6_addr* %15
}