#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;
  int ip6r0_segleft;
};
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 idx) {
  struct ip6_rthdr *rh = (struct ip6_rthdr *)bp;
  struct ip6_rthdr0 *rh0;
  int addrs;
  switch (rh->ip6r_type) {
  case IPV6_RTHDR_TYPE_0:
    rh0 = (struct ip6_rthdr0 *)bp;
    if ((rh0->ip6r0_len % 2) != 0 ||
        (addrs = (rh0->ip6r0_len >> 1)) < rh0->ip6r0_segleft)
      return (NULL);
    if (idx < 0 || addrs <= idx)
      return (NULL);
    return (((struct in6_addr *)(rh0 + 1)) + idx);
  default:
    return (NULL);
    break;
  }
}


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    11ad <inet6_rth_getaddr+0xad>
jmp    1129 <inet6_rth_getaddr+0x29>
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    (%rax),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
cmp    $0x0,%edx
jne    1161 <inet6_rth_getaddr+0x61>
mov    -0x28(%rbp),%rax
mov    (%rax),%eax
sar    $0x1,%eax
mov    %eax,-0x2c(%rbp)
mov    -0x28(%rbp),%rcx
cmp    0x4(%rcx),%eax
jge    116e <inet6_rth_getaddr+0x6e>
movq   $0x0,-0x8(%rbp)
jmp    11b5 <inet6_rth_getaddr+0xb5>
cmpl   $0x0,-0x14(%rbp)
jl     1184 <inet6_rth_getaddr+0x84>
mov    -0x2c(%rbp),%eax
cmp    -0x14(%rbp),%eax
jg     1191 <inet6_rth_getaddr+0x91>
movq   $0x0,-0x8(%rbp)
jmp    11b5 <inet6_rth_getaddr+0xb5>
mov    -0x28(%rbp),%rax
add    $0x8,%rax
movslq -0x14(%rbp),%rcx
shl    $0x2,%rcx
add    %rcx,%rax
mov    %rax,-0x8(%rbp)
jmp    11b5 <inet6_rth_getaddr+0xb5>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
pop    %rbp
ret

x86 assembly (clang, -O2):
<inet6_rth_getaddr>:
cmpl   $0x80,(%rdi)
jne    1120 <inet6_rth_getaddr+0x20>
xor    %eax,%eax
cmpl   $0x40,0x4(%rdi)
jg     111f <inet6_rth_getaddr+0x1f>
cmp    $0x3f,%esi
ja     111f <inet6_rth_getaddr+0x1f>
mov    %esi,%eax
lea    (%rdi,%rax,4),%rax
add    $0x8,%rax
ret
xor    %eax,%eax
ret

LLVM IR (-O0):
%struct.in6_addr = type { i32 }
%struct.ip6_rthdr = type { i32 }
%struct.ip6_rthdr0 = type { i32, 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 %struct.ip6_rthdr*
%7 = alloca %struct.ip6_rthdr0*
%8 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
%9 = load i8*, i8** %4
%10 = bitcast i8* %9 to %struct.ip6_rthdr*
store %struct.ip6_rthdr* %10, %struct.ip6_rthdr** %6
%11 = load %struct.ip6_rthdr*, %struct.ip6_rthdr** %6
%12 = getelementptr inbounds %struct.ip6_rthdr, %struct.ip6_rthdr* %11, i32 0, i32 0
%13 = load i32, i32* %12
switch i32 %13, label %47 [
i32 128, label %14
]
14: ; preds = %2
%15 = load i8*, i8** %4
%16 = bitcast i8* %15 to %struct.ip6_rthdr0*
store %struct.ip6_rthdr0* %16, %struct.ip6_rthdr0** %7
%17 = load %struct.ip6_rthdr0*, %struct.ip6_rthdr0** %7
%18 = getelementptr inbounds %struct.ip6_rthdr0, %struct.ip6_rthdr0* %17, i32 0, i32 0
%19 = load i32, i32* %18
%20 = srem i32 %19, 2
%21 = icmp ne i32 %20, 0
br i1 %21, label %31, label %22
22: ; preds = %14
%23 = load %struct.ip6_rthdr0*, %struct.ip6_rthdr0** %7
%24 = getelementptr inbounds %struct.ip6_rthdr0, %struct.ip6_rthdr0* %23, i32 0, i32 0
%25 = load i32, i32* %24
%26 = ashr i32 %25, 1
store i32 %26, i32* %8
%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 i32, i32* %28
%30 = icmp slt i32 %26, %29
br i1 %30, label %31, label %32
31: ; preds = %22, %14
store %struct.in6_addr* null, %struct.in6_addr** %3
br label %48
32: ; preds = %22
%33 = load i32, i32* %5
%34 = icmp slt i32 %33, 0
br i1 %34, label %39, label %35
35: ; preds = %32
%36 = load i32, i32* %8
%37 = load i32, i32* %5
%38 = icmp sle i32 %36, %37
br i1 %38, label %39, label %40
39: ; preds = %35, %32
store %struct.in6_addr* null, %struct.in6_addr** %3
br label %48
40: ; preds = %35
%41 = load %struct.ip6_rthdr0*, %struct.ip6_rthdr0** %7
%42 = getelementptr inbounds %struct.ip6_rthdr0, %struct.ip6_rthdr0* %41, i64 1
%43 = bitcast %struct.ip6_rthdr0* %42 to %struct.in6_addr*
%44 = load i32, i32* %5
%45 = sext i32 %44 to i64
%46 = getelementptr inbounds %struct.in6_addr, %struct.in6_addr* %43, i64 %45
store %struct.in6_addr* %46, %struct.in6_addr** %3
br label %48
47: ; preds = %2
store %struct.in6_addr* null, %struct.in6_addr** %3
br label %48
48: ; preds = %47, %40, %39, %31
%49 = load %struct.in6_addr*, %struct.in6_addr** %3
ret %struct.in6_addr* %49
}

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