#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 const *);
int IPV6_RTHDR_LOOSE;
#define IPV6_RTHDR_TYPE_0 128
int inet6_rthdr_getflags(const 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 (-1);
    naddr = (rt0->ip6r0_len * 8) / sizeof(struct in6_addr);
    if (idx < 0 || naddr < idx)
      return (-1);
    return IPV6_RTHDR_LOOSE;
  }
  default:
    return (-1);
  }
}


x86 assembly (clang, -O0):
<inet6_rthdr_getflags>:
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    11c3 <inet6_rthdr_getflags+0xb3>
jmp    1142 <inet6_rthdr_getflags+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_getflags+0x62>
mov    -0x28(%rbp),%rcx
mov    $0x2e,%eax
cmp    (%rcx),%eax
jge    117e <inet6_rthdr_getflags+0x6e>
movl   $0xffffffff,-0x4(%rbp)
jmp    11ca <inet6_rthdr_getflags+0xba>
mov    -0x28(%rbp),%rax
mov    (%rax),%eax
shl    $0x3,%eax
cltq
shr    $0x2,%rax
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x14(%rbp)
jl     11a6 <inet6_rthdr_getflags+0x96>
mov    -0x2c(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    11b2 <inet6_rthdr_getflags+0xa2>
movl   $0xffffffff,-0x4(%rbp)
jmp    11ca <inet6_rthdr_getflags+0xba>
mov    0x2e2f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    11ca <inet6_rthdr_getflags+0xba>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<inet6_rthdr_getflags>:
push   %rbx
mov    %esi,%ebx
call   1030 <CMSG_DATA@plt>
mov    %rax,%rcx
mov    $0xffffffff,%eax
cmpl   $0x80,(%rcx)
jne    1154 <inet6_rthdr_getflags+0x44>
mov    (%rcx),%ecx
cmp    $0x2e,%ecx
jg     1154 <inet6_rthdr_getflags+0x44>
mov    %ecx,%edx
and    $0x1,%edx
jne    1154 <inet6_rthdr_getflags+0x44>
add    %ecx,%ecx
mov    $0xffffffff,%edx
mov    $0xffffffff,%eax
cmp    %ebx,%ecx
jl     114f <inet6_rthdr_getflags+0x3f>
mov    0x2e9b(%rip),%rax
mov    (%rax),%eax
test   %ebx,%ebx
cmovs  %edx,%eax
pop    %rbx
ret

LLVM IR (-O0):
%struct.cmsghdr = type { i32 }
%struct.ip6_rthdr = type { i32 }
%struct.ip6_rthdr0 = type { i32 }
@IPV6_RTHDR_LOOSE = global i32 0
define i32 @inet6_rthdr_getflags(%struct.cmsghdr* %0, i32 %1) {
%3 = alloca i32
%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 %46 [
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 i32 -1, i32* %3
br label %47
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 slt 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 i32 -1, i32* %3
br label %47
44: ; preds = %39
%45 = load i32, i32* @IPV6_RTHDR_LOOSE
store i32 %45, i32* %3
br label %47
46: ; preds = %2
store i32 -1, i32* %3
br label %47
47: ; preds = %46, %44, %43, %28
%48 = load i32, i32* %3
ret i32 %48
}

LLVM IR (-O2):
%struct.cmsghdr = type { i32 }
%struct.ip6_rthdr = type { i32 }
%struct.ip6_rthdr0 = type { i32 }
@IPV6_RTHDR_LOOSE = global i32 0
define i32 @inet6_rthdr_getflags(%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 %23
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 %23, label %16
16: ; preds = %8
%17 = icmp slt i32 %1, 0
%18 = shl nsw i32 %11, 1
%19 = icmp slt i32 %18, %1
%20 = select i1 %17, i1 true, i1 %19
%21 = load i32, i32* @IPV6_RTHDR_LOOSE
%22 = select i1 %20, i32 -1, i32 %21
br label %23
23: ; preds = %16, %2, %8
%24 = phi i32 [ -1, %8 ], [ -1, %2 ], [ %22, %16 ]
ret i32 %24
}