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

/* Variables and functions */
scalar_t__ CMSG_DATA(struct cmsghdr *);
#define IPV6_RTHDR_TYPE_0 128
struct in6_addr *inet6_rthdr_getaddr(struct cmsghdr *cmsg, int idx) {
  struct ip6_rthdr *rthdr;
  rthdr = (struct ip6_rthdr *)CMSG_DATA(cmsg);
  switch (rthdr->ip6r_type) {
  case IPV6_RTHDR_TYPE_0: {
    struct ip6_rthdr0 *rt0 = (struct ip6_rthdr0 *)rthdr;
    int naddr;
    if (rt0->ip6r0_len % 2 || 46 < rt0->ip6r0_len)
      return NULL;
    naddr = (rt0->ip6r0_len * 8) / sizeof(struct in6_addr);
    if (idx <= 0 || naddr < idx)
      return NULL;
    return (((struct in6_addr *)(rt0 + 1)) + idx);
  }
  default:
    return NULL;
  }
}


x86 assembly (clang, -O0):
<inet6_rthdr_getaddr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <CMSG_DATA@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    (%rax),%eax
sub    $0x80,%eax
jne    11d0 <inet6_rthdr_getaddr+0xc0>
jmp    1142 <inet6_rthdr_getaddr+0x32>
mov    -0x20(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    (%rax),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
cmp    $0x0,%edx
jne    1172 <inet6_rthdr_getaddr+0x62>
mov    -0x28(%rbp),%rcx
mov    $0x2e,%eax
cmp    (%rcx),%eax
jge    117f <inet6_rthdr_getaddr+0x6f>
movq   $0x0,-0x8(%rbp)
jmp    11d8 <inet6_rthdr_getaddr+0xc8>
mov    -0x28(%rbp),%rax
mov    (%rax),%eax
shl    $0x3,%eax
cltq
shr    $0x2,%rax
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x14(%rbp)
jle    11a7 <inet6_rthdr_getaddr+0x97>
mov    -0x2c(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    11b4 <inet6_rthdr_getaddr+0xa4>
movq   $0x0,-0x8(%rbp)
jmp    11d8 <inet6_rthdr_getaddr+0xc8>
mov    -0x28(%rbp),%rax
add    $0x4,%rax
movslq -0x14(%rbp),%rcx
shl    $0x2,%rcx
add    %rcx,%rax
mov    %rax,-0x8(%rbp)
jmp    11d8 <inet6_rthdr_getaddr+0xc8>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<inet6_rthdr_getaddr>:
push   %rbx
mov    %esi,%ebx
call   1030 <CMSG_DATA@plt>
cmpl   $0x80,(%rax)
jne    114b <inet6_rthdr_getaddr+0x3b>
mov    %rax,%rcx
mov    (%rax),%edx
xor    %eax,%eax
cmp    $0x2e,%edx
jg     1149 <inet6_rthdr_getaddr+0x39>
mov    %edx,%esi
and    $0x1,%esi
jne    1149 <inet6_rthdr_getaddr+0x39>
xor    %eax,%eax
test   %ebx,%ebx
jle    1149 <inet6_rthdr_getaddr+0x39>
add    %edx,%edx
cmp    %ebx,%edx
jl     1149 <inet6_rthdr_getaddr+0x39>
mov    %ebx,%eax
lea    (%rcx,%rax,4),%rax
add    $0x4,%rax
pop    %rbx
ret
xor    %eax,%eax
pop    %rbx
ret

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

LLVM IR (-O2):
%struct.in6_addr = type { i32 }
%struct.cmsghdr = type { i32 }
%struct.ip6_rthdr = type { i32 }
%struct.ip6_rthdr0 = type { i32 }
define %struct.in6_addr* @inet6_rthdr_getaddr(%struct.cmsghdr* %0, i32 %1) {
%3 = tail call i64 @CMSG_DATA(%struct.cmsghdr* %0)
%4 = inttoptr i64 %3 to %struct.ip6_rthdr*
%5 = getelementptr inbounds %struct.ip6_rthdr, %struct.ip6_rthdr* %4, i64 0, i32 0
%6 = load i32, i32* %5
%7 = icmp eq i32 %6, 128
br i1 %7, label %8, label %26
8: ; preds = %2
%9 = inttoptr i64 %3 to %struct.ip6_rthdr0*
%10 = getelementptr inbounds %struct.ip6_rthdr0, %struct.ip6_rthdr0* %9, i64 0, i32 0
%11 = load i32, i32* %10
%12 = and i32 %11, 1
%13 = icmp ne i32 %12, 0
%14 = icmp sgt i32 %11, 46
%15 = or i1 %14, %13
br i1 %15, label %26, label %16
16: ; preds = %8
%17 = icmp slt i32 %1, 1
%18 = shl nsw i32 %11, 1
%19 = icmp slt i32 %18, %1
%20 = select i1 %17, i1 true, i1 %19
br i1 %20, label %26, label %21
21: ; preds = %16
%22 = getelementptr inbounds %struct.ip6_rthdr0, %struct.ip6_rthdr0* %9, i64 1
%23 = zext i32 %1 to i64
%24 = getelementptr inbounds %struct.ip6_rthdr0, %struct.ip6_rthdr0* %22, i64 %23
%25 = bitcast %struct.ip6_rthdr0* %24 to %struct.in6_addr*
br label %26
26: ; preds = %2, %21, %8, %16
%27 = phi %struct.in6_addr* [ %25, %21 ], [ null, %8 ], [ null, %16 ], [ null, %2 ]
ret %struct.in6_addr* %27
}