#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 *);
#define IPV6_RTHDR_TYPE_0 128
int inet6_rthdr_segments(const struct cmsghdr *cmsg) {
  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;
    if (rt0->ip6r0_len % 2 || 46 < rt0->ip6r0_len)
      return (-1);
    return (rt0->ip6r0_len * 8) / sizeof(struct in6_addr);
  }
  default:
    return (-1);
  }
}


x86 assembly (clang, -O0):
<inet6_rthdr_segments>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <CMSG_DATA@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    (%rax),%eax
sub    $0x80,%eax
jne    1192 <inet6_rthdr_segments+0x82>
jmp    113f <inet6_rthdr_segments+0x2f>
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    (%rax),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
cmp    $0x0,%edx
jne    116f <inet6_rthdr_segments+0x5f>
mov    -0x20(%rbp),%rcx
mov    $0x2e,%eax
cmp    (%rcx),%eax
jge    117b <inet6_rthdr_segments+0x6b>
movl   $0xffffffff,-0x4(%rbp)
jmp    1199 <inet6_rthdr_segments+0x89>
mov    -0x20(%rbp),%rax
mov    (%rax),%eax
shl    $0x3,%eax
cltq
shr    $0x2,%rax
mov    %eax,-0x4(%rbp)
jmp    1199 <inet6_rthdr_segments+0x89>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<inet6_rthdr_segments>:
push   %rax
call   1030 <CMSG_DATA@plt>
mov    %rax,%rcx
mov    $0xffffffff,%eax
cmpl   $0x80,(%rcx)
jne    113c <inet6_rthdr_segments+0x2c>
mov    (%rcx),%ecx
lea    (%rcx,%rcx,1),%eax
test   $0x1,%cl
mov    $0xffffffff,%edx
cmovne %edx,%eax
cmp    $0x2f,%ecx
cmovge %edx,%eax
pop    %rcx
ret

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

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